mirror of
https://bitbucket.org/anguist/ntpa
synced 2026-09-26 10:54:04 +00:00
119 lines
4.0 KiB
C#
119 lines
4.0 KiB
C#
//
|
|
// FileSystemDestination.cs
|
|
//
|
|
// Author:
|
|
// Carsten Sonne Larsen <cs@innolan.dk>
|
|
//
|
|
// Copyright (c) 2016 Carsten Sonne Larsen
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
using System;
|
|
using System.IO;
|
|
using Ntp.Analyzer.Localize;
|
|
using Ntp.Analyzer.Log;
|
|
using Ntp.Interop;
|
|
|
|
namespace Ntp.Analyzer.Export
|
|
{
|
|
public abstract class FileSystemDestination : StreamDestination
|
|
{
|
|
protected FileSystemDestination ()
|
|
{
|
|
}
|
|
|
|
public static uint? FileUserId;
|
|
public static uint? FileGroupId;
|
|
public static uint FileMask;
|
|
|
|
protected void TestFile (string file, string content)
|
|
{
|
|
if (File.Exists (file))
|
|
File.Delete (file);
|
|
|
|
File.WriteAllText (file, content);
|
|
File.Delete (file);
|
|
}
|
|
|
|
protected void PrepareFile (string file, LogBase log)
|
|
{
|
|
string created = null;
|
|
try {
|
|
created = DirectoryCommand.CreateRecursiveFromFile (file);
|
|
} catch (Exception e) {
|
|
log.WriteLine (String.Format (
|
|
LogMessage.DestinationPathError, file),
|
|
Severity.Error);
|
|
log.WriteLine (e, Severity.Trace);
|
|
}
|
|
|
|
if (created != null) {
|
|
log.WriteLine (String.Format (
|
|
LogMessage.DestinationNewDirectory, created),
|
|
Severity.Notice);
|
|
}
|
|
|
|
if (File.Exists (file))
|
|
File.Delete (file);
|
|
}
|
|
|
|
protected void WriteFile (Stream stream, string file, LogBase log)
|
|
{
|
|
FileStream fileStream = null;
|
|
try {
|
|
fileStream = new FileStream (file, FileMode.CreateNew);
|
|
stream.CopyTo (fileStream);
|
|
} catch (Exception e) {
|
|
log.WriteLine (String.Format (
|
|
LogMessage.DestinationFileError, file),
|
|
Severity.Error);
|
|
log.WriteLine (e, Severity.Trace);
|
|
} finally {
|
|
// Release memory
|
|
if (fileStream != null) {
|
|
fileStream.Close ();
|
|
fileStream = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected void ApplyPermissions (string file, LogBase log)
|
|
{
|
|
if (FileUserId != null) {
|
|
try {
|
|
Permission.ChangeFileOwner (file, FileUserId.Value, FileGroupId);
|
|
} catch (Exception e) {
|
|
log.WriteLine (String.Format (
|
|
LogMessage.DestinationOwnerError, file),
|
|
Severity.Error);
|
|
log.WriteLine (e, Severity.Trace);
|
|
}
|
|
}
|
|
|
|
try {
|
|
Permission.ChangeFileMode (file, FileMask);
|
|
} catch (Exception e) {
|
|
log.WriteLine (String.Format (
|
|
LogMessage.DestinationPermissonError, file),
|
|
Severity.Error);
|
|
log.WriteLine (e, Severity.Trace);
|
|
}
|
|
}
|
|
}
|
|
} |