Fixed: Copy linux permission mask when moving to recycle bin

Co-Authored-By: taloth <taloth@users.noreply.github.com>
pull/2/head
Qstick 5 years ago
parent 22062931fe
commit 37d004d78d

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -1046,6 +1046,9 @@ namespace NzbDrone.Common.Test.DiskTests
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(v => v.GetFileInfos(It.IsAny<string>())) .Setup(v => v.GetFileInfos(It.IsAny<string>()))
.Returns(new List<FileInfo>()); .Returns(new List<FileInfo>());
Mocker.GetMock<IDiskProvider>()
.Setup(v => v.CopyPermissions(It.IsAny<string>(), It.IsAny<string>(), false));
} }
private void WithRealDiskProvider() private void WithRealDiskProvider()
@ -1105,6 +1108,9 @@ namespace NzbDrone.Common.Test.DiskTests
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(v => v.OpenReadStream(It.IsAny<string>())) .Setup(v => v.OpenReadStream(It.IsAny<string>()))
.Returns<string>(s => new FileStream(s, FileMode.Open, FileAccess.Read)); .Returns<string>(s => new FileStream(s, FileMode.Open, FileAccess.Read));
Mocker.GetMock<IDiskProvider>()
.Setup(v => v.CopyPermissions(It.IsAny<string>(), It.IsAny<string>(), false));
} }
private void WithMockMount(string root) private void WithMockMount(string root)

@ -32,6 +32,7 @@ namespace NzbDrone.Common.Disk
public abstract long? GetAvailableSpace(string path); public abstract long? GetAvailableSpace(string path);
public abstract void InheritFolderPermissions(string filename); public abstract void InheritFolderPermissions(string filename);
public abstract void SetPermissions(string path, string mask, string user, string group); public abstract void SetPermissions(string path, string mask, string user, string group);
public abstract void CopyPermissions(string sourcePath, string targetPath, bool includeOwner);
public abstract long? GetTotalSize(string path); public abstract long? GetTotalSize(string path);
public DateTime FolderGetCreationTime(string path) public DateTime FolderGetCreationTime(string path)

@ -1,4 +1,4 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -75,6 +75,8 @@ namespace NzbDrone.Common.Disk
if (!_diskProvider.FolderExists(targetPath)) if (!_diskProvider.FolderExists(targetPath))
{ {
_diskProvider.CreateFolder(targetPath); _diskProvider.CreateFolder(targetPath);
_diskProvider.CopyPermissions(sourcePath, targetPath);
} }
var result = mode; var result = mode;

@ -11,6 +11,7 @@ namespace NzbDrone.Common.Disk
long? GetAvailableSpace(string path); long? GetAvailableSpace(string path);
void InheritFolderPermissions(string filename); void InheritFolderPermissions(string filename);
void SetPermissions(string path, string mask, string user, string group); void SetPermissions(string path, string mask, string user, string group);
void CopyPermissions(string sourcePath, string targetPath, bool includeOwner = false);
long? GetTotalSize(string path); long? GetTotalSize(string path);
DateTime FolderGetCreationTime(string path); DateTime FolderGetCreationTime(string path);
DateTime FolderGetLastWrite(string path); DateTime FolderGetLastWrite(string path);

@ -4,6 +4,7 @@ using System.IO;
using System.Linq; using System.Linq;
using FluentAssertions; using FluentAssertions;
using Mono.Unix; using Mono.Unix;
using Mono.Unix.Native;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
@ -127,5 +128,34 @@ namespace NzbDrone.Mono.Test.DiskProviderTests
mount.Should().NotBeNull(); mount.Should().NotBeNull();
mount.RootDirectory.Should().Be(rootDir); mount.RootDirectory.Should().Be(rootDir);
} }
[Test]
public void should_copy_folder_permissions()
{
var src = GetTempFilePath();
var dst = GetTempFilePath();
Directory.CreateDirectory(src);
// Toggle one of the permission flags
Syscall.stat(src, out var origStat);
Syscall.chmod(src, origStat.st_mode ^ FilePermissions.S_IWGRP);
// Verify test setup
Syscall.stat(src, out var srcStat);
srcStat.st_mode.Should().NotBe(origStat.st_mode);
Subject.CreateFolder(dst);
// Verify test setup
Syscall.stat(dst, out var dstStat);
dstStat.st_mode.Should().Be(origStat.st_mode);
Subject.CopyPermissions(src, dst, false);
// Verify CopyPermissions
Syscall.stat(dst, out dstStat);
dstStat.st_mode.Should().Be(srcStat.st_mode);
}
} }
} }

@ -76,6 +76,29 @@ namespace NzbDrone.Mono.Disk
SetOwner(path, user, group); SetOwner(path, user, group);
} }
public override void CopyPermissions(string sourcePath, string targetPath, bool includeOwner)
{
try
{
Syscall.stat(sourcePath, out var srcStat);
Syscall.stat(targetPath, out var tgtStat);
if (srcStat.st_mode != tgtStat.st_mode)
{
Syscall.chmod(targetPath, srcStat.st_mode);
}
if (includeOwner && (srcStat.st_uid != tgtStat.st_uid || srcStat.st_gid != tgtStat.st_gid))
{
Syscall.chown(targetPath, srcStat.st_uid, srcStat.st_gid);
}
}
catch (Exception ex)
{
_logger.Debug(ex, "Failed to copy permissions from {0} to {1}", sourcePath, targetPath);
}
}
protected override List<IMount> GetAllMounts() protected override List<IMount> GetAllMounts()
{ {
return _procMountProvider.GetMounts() return _procMountProvider.GetMounts()

@ -1,4 +1,4 @@
using System; using System;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using NLog; using NLog;
@ -51,6 +51,10 @@ namespace NzbDrone.Windows.Disk
{ {
} }
public override void CopyPermissions(string sourcePath, string targetPath, bool includeOwner)
{
}
public override long? GetTotalSize(string path) public override long? GetTotalSize(string path)
{ {
Ensure.That(path, () => path).IsValidPath(); Ensure.That(path, () => path).IsValidPath();

Loading…
Cancel
Save