1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00

New: Setting file permissions on import (Linux)

This commit is contained in:
Mark McDowall 2014-01-14 11:21:20 -08:00
parent 37231d1ef0
commit 5459b5fed4
11 changed files with 125 additions and 13 deletions

View File

@ -27,6 +27,9 @@ Function Build()
AddJsonNet
Write-Host "Removing Mono.Posix.dll"
Remove-Item "$outputFolder\Mono.Posix.dll"
Write-Host "##teamcity[progressFinish 'Build']"
}

View File

@ -1,12 +1,10 @@
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using NLog;
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Instrumentation;
namespace NzbDrone.Common.Disk
@ -23,7 +21,7 @@ enum TransferAction
public abstract long? GetAvailableSpace(string path);
public abstract void InheritFolderPermissions(string filename);
public abstract void SetFilePermissions(string path, string mask);
public abstract void SetPermissions(string path, string mask);
public abstract long? GetTotalSize(string path);
public DateTime GetLastFolderWrite(string path)

View File

@ -13,6 +13,11 @@ namespace NzbDrone.Common.Disk
{
public interface IDiskProvider
{
long? GetAvailableSpace(string path);
void InheritFolderPermissions(string filename);
void SetPermissions(string path, string mask);
long? GetTotalSize(string path);
DateTime GetLastFolderWrite(string path);
DateTime GetLastFileWrite(string path);
void EnsureFolder(string path);
@ -29,8 +34,6 @@ public interface IDiskProvider
void DeleteFile(string path);
void MoveFile(string source, string destination);
void DeleteFolder(string path, bool recursive);
void InheritFolderPermissions(string filename);
long? GetAvailableSpace(string path);
string ReadAllText(string filePath);
void WriteAllText(string filename, string contents);
void FileSetLastWriteTimeUtc(string path, DateTime dateTime);
@ -44,7 +47,6 @@ public interface IDiskProvider
FileAttributes GetFileAttributes(string path);
void EmptyFolder(string path);
string[] GetFixedDrives();
long? GetTotalSize(string path);
string GetVolumeLabel(string path);
}
}

View File

@ -278,12 +278,26 @@ public Boolean CreateEmptySeriesFolders
set { SetValue("CreateEmptySeriesFolders", value); }
}
public string DownloadClientWorkingFolders
public String DownloadClientWorkingFolders
{
get { return GetValue("DownloadClientWorkingFolders", "_UNPACK_|_FAILED_"); }
set { SetValue("DownloadClientWorkingFolders", value); }
}
public String FileChmod
{
get { return GetValue("FileChmod", "0755"); }
set { SetValue("FileChmod", value); }
}
public String FolderChmod
{
get { return GetValue("FolderChmod", "0755"); }
set { SetValue("FolderChmod", value); }
}
private string GetValue(string key)
{
return GetValue(key, String.Empty);

View File

@ -42,5 +42,7 @@ public interface IConfigService
Boolean EnableFailedDownloadHandling { get; set; }
Boolean CreateEmptySeriesFolders { get; set; }
void SaveValues(Dictionary<string, object> configValues);
String FileChmod { get; set; }
String FolderChmod { get; set; }
}
}

View File

@ -6,7 +6,7 @@
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Tv;
@ -24,16 +24,19 @@ public class EpisodeFileMovingService : IMoveEpisodeFiles
private readonly IEpisodeService _episodeService;
private readonly IBuildFileNames _buildFileNames;
private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService;
private readonly Logger _logger;
public EpisodeFileMovingService(IEpisodeService episodeService,
IBuildFileNames buildFileNames,
IDiskProvider diskProvider,
IConfigService configService,
Logger logger)
{
_episodeService = episodeService;
_buildFileNames = buildFileNames;
_diskProvider = diskProvider;
_configService = configService;
_logger = logger;
}
@ -85,6 +88,7 @@ private void MoveFile(EpisodeFile episodeFile, Series series, string destination
{
_logger.Trace("Setting last write time on series folder: {0}", series.Path);
_diskProvider.SetFolderWriteTime(series.Path, episodeFile.DateAdded);
SetFolderPermissions(series.Path);
if (series.SeasonFolder)
{
@ -92,6 +96,7 @@ private void MoveFile(EpisodeFile episodeFile, Series series, string destination
_logger.Trace("Setting last write time on season folder: {0}", seasonFolder);
_diskProvider.SetFolderWriteTime(seasonFolder, episodeFile.DateAdded);
SetFolderPermissions(seasonFolder);
}
}
@ -122,6 +127,33 @@ private void MoveFile(EpisodeFile episodeFile, Series series, string destination
}
}
}
else
{
SetPermissions(destinationFilename, _configService.FileChmod);
}
}
private void SetPermissions(string path, string permissions)
{
try
{
_diskProvider.SetPermissions(path, permissions);
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException || ex is InvalidOperationException)
{
_logger.Debug("Unable to apply permissions to: ", path);
_logger.TraceException(ex.Message, ex);
}
}
}
private void SetFolderPermissions(string path)
{
SetPermissions(path, _configService.FolderChmod);
}
}
}

View File

@ -1,5 +1,4 @@
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.Disk;
using NzbDrone.Core.Parser.Model;

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Configuration;
namespace NzbDrone.Core.MediaFiles
{
public interface ISetMediaFilePermissions
{
void SetPermissions(string filename);
}
public class SetMediaFilePermissions : ISetMediaFilePermissions
{
private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService;
private readonly Logger _logger;
public SetMediaFilePermissions(IDiskProvider diskProvider, IConfigService configService, Logger logger)
{
_diskProvider = diskProvider;
_configService = configService;
_logger = logger;
}
public void SetPermissions(string filename)
{
if (OsInfo.IsWindows)
{
//Wrapped in Try/Catch to prevent this from causing issues with remote NAS boxes, the move worked, which is more important.
try
{
_diskProvider.InheritFolderPermissions(filename);
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException || ex is InvalidOperationException)
{
_logger.Debug("Unable to apply folder permissions to: ", filename);
_logger.TraceException(ex.Message, ex);
}
else
{
throw;
}
}
}
else
{
_diskProvider.SetPermissions(filename, _configService.FileChmod);
}
}
}
}

View File

@ -290,6 +290,7 @@
<Compile Include="MediaFiles\MediaFileExtensions.cs" />
<Compile Include="MediaFiles\MediaInfo\VideoFileInfoReader.cs" />
<Compile Include="MediaFiles\RenameEpisodeFilePreview.cs" />
<Compile Include="MediaFiles\SetMediaFilePermissions.cs" />
<Compile Include="Messaging\Commands\CommandExecutor.cs" />
<Compile Include="Messaging\Commands\ICommandExecutor.cs" />
<Compile Include="Messaging\Commands\IExecute.cs" />

View File

@ -49,8 +49,10 @@ public override void InheritFolderPermissions(string filename)
}
}
public override void SetFilePermissions(string path, string mask)
public override void SetPermissions(string path, string mask)
{
Logger.Trace("Setting permissions: {0} on {1}", mask, path);
var filePermissions = NativeConvert.FromOctalPermissionString(mask);
if (Syscall.chmod(path, filePermissions) < 0)

View File

@ -4,7 +4,6 @@
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Instrumentation;
namespace NzbDrone.Windows
@ -41,9 +40,9 @@ public override void InheritFolderPermissions(string filename)
File.SetAccessControl(filename, fs);
}
public override void SetFilePermissions(string path, string mask)
public override void SetPermissions(string path, string mask)
{
throw new NotImplementedException();
}
public override long? GetTotalSize(string path)