diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-10-16 08:46:10 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-10-16 08:46:10 -0500 |
| commit | a972672bb344899686d450254d34944a9caab805 (patch) | |
| tree | 33d9c22226cb6a144c6da3a11647a74c9c762504 /src | |
| parent | c60539838966fdf54eb9a5f78d7fb8c1c3335b76 (diff) | |
| download | slskd-a972672bb344899686d450254d34944a9caab805.tar.gz | |
set unix file mode after moving downloaded files
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Files/FileService.cs | 19 | ||||
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 9 |
2 files changed, 24 insertions, 4 deletions
diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index a07bd051..e4adc7e7 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -379,6 +379,7 @@ namespace slskd.Files /// </remarks> /// <param name="sourceFilename">The fully qualified filename of the file to move.</param> /// <param name="destinationDirectory">The fully qualified directory to which to move the source file.</param> + /// <param name="unixFileMode">An optional Unix file mode to apply to the destination file.</param> /// <param name="overwrite">An optional value indicating whether the destination file should be overwritten if it already exists.</param> /// <param name="deleteSourceDirectoryIfEmptyAfterMove"> /// An optional value indicating whether the parent directory of the source file should be deleted if it is empty after the move. @@ -387,7 +388,7 @@ namespace slskd.Files /// <exception cref="ArgumentNullException">Thrown if either of the specified file or directories are null or contain only whitespace.</exception> /// <exception cref="FileNotFoundException">Thrown if the specified <paramref name="sourceFilename"/> does not exist.</exception> /// <exception cref="IOException">Thrown if the file can't be moved, or the <paramref name="deleteSourceDirectoryIfEmptyAfterMove"/> option is set and the operation fails.</exception> - public virtual string MoveFile(string sourceFilename, string destinationDirectory, bool overwrite = false, bool deleteSourceDirectoryIfEmptyAfterMove = false) + public virtual string MoveFile(string sourceFilename, string destinationDirectory, UnixFileMode? unixFileMode = null, bool overwrite = false, bool deleteSourceDirectoryIfEmptyAfterMove = false) { ArgumentNullException.ThrowIfNullOrWhiteSpace(sourceFilename, nameof(sourceFilename)); ArgumentNullException.ThrowIfNullOrWhiteSpace(destinationDirectory, nameof(destinationDirectory)); @@ -425,6 +426,22 @@ namespace slskd.Files Log.Debug("Successfully moved {Source} to {Destination}", sourceFilename, destinationFilename); + // attempting to use UnixCreateMode on Windows raises an Exception + // we *MUST* check the OS and skip this on Windows + if (!OperatingSystem.IsWindows()) + { + var appOption = OptionsMonitor.CurrentValue.Permissions.File.Mode; + var mode = unixFileMode ?? appOption?.ToUnixFileMode(); + + // if options haven't been passed in and none have been set in app config, omit this + // so that the application's umask will be applied, saving users the hassle of setting it twice. + if (mode.HasValue) + { + File.SetUnixFileMode(destinationFilename, mode.Value); + Log.Debug("Successfully set Unix file mode to {Mode}", mode); + } + } + // if the parent directory is empty after the move, delete it if (deleteSourceDirectoryIfEmptyAfterMove && !Directory.EnumerateFileSystemEntries(Path.GetDirectoryName(sourceFilename)).Any()) { diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 75a3c383..e66808a4 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -325,6 +325,10 @@ namespace slskd.Transfers.Downloads // register the cancellation token CancellationTokens.TryAdd(transfer.Id, cts); + System.IO.UnixFileMode? unixFileMode = !string.IsNullOrEmpty(OptionsMonitor.CurrentValue.Permissions.File.Mode) + ? OptionsMonitor.CurrentValue.Permissions.File.Mode.ToUnixFileMode() + : null; + var completedTransfer = await Client.DownloadAsync( username: transfer.Username, remoteFilename: transfer.Filename, @@ -336,9 +340,7 @@ namespace slskd.Transfers.Downloads Access = System.IO.FileAccess.Write, Mode = System.IO.FileMode.Create, // overwrites file if it exists Share = System.IO.FileShare.None, // exclusive access for the duration of the download - UnixCreateMode = !string.IsNullOrEmpty(OptionsMonitor.CurrentValue.Permissions.File.Mode) - ? OptionsMonitor.CurrentValue.Permissions.File.Mode.ToUnixFileMode() - : null, + UnixCreateMode = unixFileMode, })), size: transfer.Size, startOffset: 0, @@ -363,6 +365,7 @@ namespace slskd.Transfers.Downloads var finalFilename = Files.MoveFile( sourceFilename: transfer.Filename.ToLocalFilename(baseDirectory: OptionsMonitor.CurrentValue.Directories.Incomplete), destinationDirectory: destinationDirectory, + unixFileMode: unixFileMode, overwrite: false, deleteSourceDirectoryIfEmptyAfterMove: true); |
