2017-12-25 02:45:28 +00:00
|
|
|
using System;
|
2015-03-19 00:17:25 +00:00
|
|
|
using System.IO;
|
2014-11-18 15:38:31 +00:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Disk;
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
{
|
|
|
|
public interface IMediaFileAttributeService
|
|
|
|
{
|
|
|
|
void SetFilePermissions(string path);
|
|
|
|
void SetFolderPermissions(string path);
|
|
|
|
void SetFolderLastWriteTime(string path, DateTime time);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class MediaFileAttributeService : IMediaFileAttributeService
|
|
|
|
{
|
|
|
|
private readonly IConfigService _configService;
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public MediaFileAttributeService(IConfigService configService,
|
|
|
|
IDiskProvider diskProvider,
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
_configService = configService;
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetFilePermissions(string path)
|
|
|
|
{
|
|
|
|
if (OsInfo.IsWindows)
|
|
|
|
{
|
2022-11-22 01:51:00 +00:00
|
|
|
// Wrapped in Try/Catch to prevent this from causing issues with remote NAS boxes
|
2014-11-18 15:38:31 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_diskProvider.InheritFolderPermissions(path);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2015-03-19 00:17:25 +00:00
|
|
|
if (ex is UnauthorizedAccessException || ex is InvalidOperationException || ex is FileNotFoundException)
|
2014-11-18 15:38:31 +00:00
|
|
|
{
|
2017-01-05 23:32:17 +00:00
|
|
|
_logger.Debug("Unable to apply folder permissions to {0}", path);
|
2016-02-11 21:13:42 +00:00
|
|
|
_logger.Debug(ex, ex.Message);
|
2014-11-18 15:38:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-05 23:32:17 +00:00
|
|
|
_logger.Warn("Unable to apply folder permissions to: {0}", path);
|
2016-02-11 21:13:42 +00:00
|
|
|
_logger.Warn(ex, ex.Message);
|
2014-11-18 15:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-05 23:00:47 +00:00
|
|
|
SetMonoPermissions(path);
|
2014-11-18 15:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetFolderPermissions(string path)
|
|
|
|
{
|
2014-12-07 20:54:07 +00:00
|
|
|
if (OsInfo.IsNotWindows)
|
2014-11-18 15:38:31 +00:00
|
|
|
{
|
2020-10-05 23:00:47 +00:00
|
|
|
SetMonoPermissions(path);
|
2014-11-18 15:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetFolderLastWriteTime(string path, DateTime time)
|
|
|
|
{
|
|
|
|
if (OsInfo.IsWindows)
|
|
|
|
{
|
2017-12-25 02:45:28 +00:00
|
|
|
_logger.Debug("Setting last write time on artist folder: {0}", path);
|
2014-11-18 15:38:31 +00:00
|
|
|
_diskProvider.FolderSetLastWriteTime(path, time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:00:47 +00:00
|
|
|
private void SetMonoPermissions(string path)
|
2014-11-18 15:38:31 +00:00
|
|
|
{
|
|
|
|
if (!_configService.SetPermissionsLinux)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-10-05 23:00:47 +00:00
|
|
|
_diskProvider.SetPermissions(path, _configService.ChmodFolder, _configService.ChownGroup);
|
2014-11-18 15:38:31 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2016-02-11 21:13:42 +00:00
|
|
|
_logger.Warn(ex, "Unable to apply permissions to: " + path);
|
2014-11-18 15:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|