From 4c8bd648b6250d6eb5603420fa6280e948a00dd6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Sep 2022 16:53:02 -0500 Subject: stash OriginalFilename if we manipulate the path. use this when resolving if it is set --- src/slskd/Shares/SharedFileCache.cs | 43 +++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 3a41f61b..31a179f2 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -279,20 +279,38 @@ namespace slskd.Shares // merge the new dictionary with the rest this will overwrite any duplicate keys, but keys are the fully // qualified name the only time this *should* cause problems is if one of the shares is a subdirectory of another. - foreach (var file in newFiles) + foreach (var record in newFiles) { - if (filters.Any(filter => filter.IsMatch(file.Key))) + var (key, value) = record; + + if (filters.Any(filter => filter.IsMatch(key))) { filteredFiles++; continue; } - if (files.ContainsKey(file.Key)) + if (files.ContainsKey(key)) + { + Log.Warning($"File {key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories (is a subdirectory being re-shared?)."); + } + + // if we're on an operating system that uses forward slashes, it is possible for filenames to contain + // backslashes. because we normalize directory separators to backslashes before we send results to the network, + // we need to replace any backslashes with a placeholder to prevent things from breaking. we'll capture the original + // filename if we do this so we can resolve the original filename given the replacement without doing any guessing. + if (Path.DirectorySeparatorChar == '/' && value.Filename.Contains('\\')) { - Log.Warning($"File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories (is a subdirectory being re-shared?)."); + // todo: store OriginalFilename for all files once masked filenames are stored in the db. we aren't doing this now to reduce memory footprint. + value = new File( + value.Code, + value.Filename.Replace('\\', '_'), + value.Size, + value.Extension, + value.Attributes, + originalFilename: value.Filename); } - files[file.Key] = file.Value; + files[key] = value; } } catch (Exception ex) @@ -395,11 +413,24 @@ namespace slskd.Shares public string Resolve(string filename) { // ensure this is a tracked file - if (!MaskedFiles.TryGetValue(filename, out _)) + if (!MaskedFiles.TryGetValue(filename, out var maskedRecord)) { return null; } + // if the OriginalFilename property of the record was set during the scan, + // one or more characters was substituted, and we can't trust the manipulated + // path to find the correct file without some degree of guessing. return the + // value of OriginalFilename which definitively points to the location on disk. + if (!string.IsNullOrEmpty(maskedRecord.Filename)) + { + // todo: use OriginalFilename for all files once masked files are stored in the db + return maskedRecord.OriginalFilename; + } + + // if OriginalFilename isn't set, derive the location of the file from the masked + // filename and share configuration. we don't store OriginalFilename for all records + // to reduce memory footprint var resolved = filename; // a well-formed path will consist of a mask, either an alias or a local directory, and a fully qualified path to a -- cgit v1.2.3 From 7f26c010f6282d9ea6e597d74441a27d4dade869 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Sep 2022 16:58:19 -0500 Subject: log replacements --- src/slskd/Shares/SharedFileCache.cs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 31a179f2..eb0450d8 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -308,6 +308,8 @@ namespace slskd.Shares value.Extension, value.Attributes, originalFilename: value.Filename); + + Log.Warning($"Substituting {value.Filename} for {value.Filename.Replace('\\', '_')}"); } files[key] = value; -- cgit v1.2.3 From 0e74f054f89fba1037536c80713e889a6379e928 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Sep 2022 17:01:03 -0500 Subject: tweak --- src/slskd/Shares/SharedFileCache.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index eb0450d8..73e82e40 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -300,6 +300,8 @@ namespace slskd.Shares // filename if we do this so we can resolve the original filename given the replacement without doing any guessing. if (Path.DirectorySeparatorChar == '/' && value.Filename.Contains('\\')) { + Log.Warning($"Substituting {value.Filename} for {value.Filename.Replace('\\', '_')}"); + // todo: store OriginalFilename for all files once masked filenames are stored in the db. we aren't doing this now to reduce memory footprint. value = new File( value.Code, @@ -309,7 +311,6 @@ namespace slskd.Shares value.Attributes, originalFilename: value.Filename); - Log.Warning($"Substituting {value.Filename} for {value.Filename.Replace('\\', '_')}"); } files[key] = value; -- cgit v1.2.3 From 5387e5bde7cef5c8546f6bf5dd024dc61dd42b25 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Sep 2022 17:05:21 -0500 Subject: replace the key too --- src/slskd/Shares/SharedFileCache.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 73e82e40..a8a8badb 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -300,9 +300,10 @@ namespace slskd.Shares // filename if we do this so we can resolve the original filename given the replacement without doing any guessing. if (Path.DirectorySeparatorChar == '/' && value.Filename.Contains('\\')) { - Log.Warning($"Substituting {value.Filename} for {value.Filename.Replace('\\', '_')}"); + Log.Warning($"Substituting {value.Filename} with {value.Filename.Replace('\\', '_')}"); // todo: store OriginalFilename for all files once masked filenames are stored in the db. we aren't doing this now to reduce memory footprint. + key = key.Replace('\\', '_'); value = new File( value.Code, value.Filename.Replace('\\', '_'), @@ -310,7 +311,6 @@ namespace slskd.Shares value.Extension, value.Attributes, originalFilename: value.Filename); - } files[key] = value; -- cgit v1.2.3 From 594718ccb9925ee722bdd3b9928839acd7b6a821 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Sep 2022 17:08:38 -0500 Subject: more logging --- src/slskd/Shares/SharedFileCache.cs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index a8a8badb..2bb495b5 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -415,9 +415,12 @@ namespace slskd.Shares /// The unmasked filename. public string Resolve(string filename) { + Log.Warning($"Resolving: {filename}"); + // ensure this is a tracked file if (!MaskedFiles.TryGetValue(filename, out var maskedRecord)) { + Log.Warning("Not tracked"); return null; } @@ -427,6 +430,7 @@ namespace slskd.Shares // value of OriginalFilename which definitively points to the location on disk. if (!string.IsNullOrEmpty(maskedRecord.Filename)) { + Log.Warning($"OriginalFilename populated: {}"); // todo: use OriginalFilename for all files once masked files are stored in the db return maskedRecord.OriginalFilename; } -- cgit v1.2.3 From 7ba85bf91c35dd8a365f2680329ef152ed88ee8c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Sep 2022 17:10:00 -0500 Subject: fix bug --- src/slskd/Shares/SharedFileCache.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 2bb495b5..e7b1cee5 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -428,9 +428,9 @@ namespace slskd.Shares // one or more characters was substituted, and we can't trust the manipulated // path to find the correct file without some degree of guessing. return the // value of OriginalFilename which definitively points to the location on disk. - if (!string.IsNullOrEmpty(maskedRecord.Filename)) + if (!string.IsNullOrEmpty(maskedRecord.OriginalFilename)) { - Log.Warning($"OriginalFilename populated: {}"); + Log.Warning($"OriginalFilename populated: {maskedRecord.OriginalFilename}"); // todo: use OriginalFilename for all files once masked files are stored in the db return maskedRecord.OriginalFilename; } -- cgit v1.2.3 From dd3e83bd845043cd86d196f5f51eca3756e9c68c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Sep 2022 17:14:47 -0500 Subject: still need to build the filename from share --- src/slskd/Shares/SharedFileCache.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index e7b1cee5..666fd16c 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -432,7 +432,7 @@ namespace slskd.Shares { Log.Warning($"OriginalFilename populated: {maskedRecord.OriginalFilename}"); // todo: use OriginalFilename for all files once masked files are stored in the db - return maskedRecord.OriginalFilename; + filename = maskedRecord.OriginalFilename; } // if OriginalFilename isn't set, derive the location of the file from the masked -- cgit v1.2.3 From e609fe8e4e7fca623a939cb6ecf73979a8666a45 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 4 Sep 2022 11:41:16 -0500 Subject: try a different approach --- src/slskd/Shares/SharedFileCache.cs | 50 +++++-------------------------------- src/slskd/Shares/Types/File.cs | 33 ------------------------ 2 files changed, 6 insertions(+), 77 deletions(-) delete mode 100644 src/slskd/Shares/Types/File.cs (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 666fd16c..3a41f61b 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -279,41 +279,20 @@ namespace slskd.Shares // merge the new dictionary with the rest this will overwrite any duplicate keys, but keys are the fully // qualified name the only time this *should* cause problems is if one of the shares is a subdirectory of another. - foreach (var record in newFiles) + foreach (var file in newFiles) { - var (key, value) = record; - - if (filters.Any(filter => filter.IsMatch(key))) + if (filters.Any(filter => filter.IsMatch(file.Key))) { filteredFiles++; continue; } - if (files.ContainsKey(key)) - { - Log.Warning($"File {key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories (is a subdirectory being re-shared?)."); - } - - // if we're on an operating system that uses forward slashes, it is possible for filenames to contain - // backslashes. because we normalize directory separators to backslashes before we send results to the network, - // we need to replace any backslashes with a placeholder to prevent things from breaking. we'll capture the original - // filename if we do this so we can resolve the original filename given the replacement without doing any guessing. - if (Path.DirectorySeparatorChar == '/' && value.Filename.Contains('\\')) + if (files.ContainsKey(file.Key)) { - Log.Warning($"Substituting {value.Filename} with {value.Filename.Replace('\\', '_')}"); - - // todo: store OriginalFilename for all files once masked filenames are stored in the db. we aren't doing this now to reduce memory footprint. - key = key.Replace('\\', '_'); - value = new File( - value.Code, - value.Filename.Replace('\\', '_'), - value.Size, - value.Extension, - value.Attributes, - originalFilename: value.Filename); + Log.Warning($"File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories (is a subdirectory being re-shared?)."); } - files[key] = value; + files[file.Key] = file.Value; } } catch (Exception ex) @@ -415,29 +394,12 @@ namespace slskd.Shares /// The unmasked filename. public string Resolve(string filename) { - Log.Warning($"Resolving: {filename}"); - // ensure this is a tracked file - if (!MaskedFiles.TryGetValue(filename, out var maskedRecord)) + if (!MaskedFiles.TryGetValue(filename, out _)) { - Log.Warning("Not tracked"); return null; } - // if the OriginalFilename property of the record was set during the scan, - // one or more characters was substituted, and we can't trust the manipulated - // path to find the correct file without some degree of guessing. return the - // value of OriginalFilename which definitively points to the location on disk. - if (!string.IsNullOrEmpty(maskedRecord.OriginalFilename)) - { - Log.Warning($"OriginalFilename populated: {maskedRecord.OriginalFilename}"); - // todo: use OriginalFilename for all files once masked files are stored in the db - filename = maskedRecord.OriginalFilename; - } - - // if OriginalFilename isn't set, derive the location of the file from the masked - // filename and share configuration. we don't store OriginalFilename for all records - // to reduce memory footprint var resolved = filename; // a well-formed path will consist of a mask, either an alias or a local directory, and a fully qualified path to a diff --git a/src/slskd/Shares/Types/File.cs b/src/slskd/Shares/Types/File.cs deleted file mode 100644 index e7740613..00000000 --- a/src/slskd/Shares/Types/File.cs +++ /dev/null @@ -1,33 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Shares -{ - using System.Collections.Generic; - using Soulseek; - - public class File : Soulseek.File - { - public File(int code, string filename, long size, string extension, IEnumerable attributeList = null, string originalFilename = null) - : base(code, filename, size, extension, attributeList) - { - OriginalFilename = originalFilename; - } - - public string OriginalFilename { get; set; } - } -} -- cgit v1.2.3 From 731b371d25e84536745f3987cc83344d8e7e0262 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 4 Sep 2022 12:23:54 -0500 Subject: log warning for unsupported directories and files --- src/slskd/Shares/SharedFileCache.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/slskd/Shares/SharedFileCache.cs') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 3a41f61b..9902c951 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -253,6 +253,12 @@ namespace slskd.Shares { Log.Debug("Starting scan of {Directory} ({Current}/{Total})", directory, current + 1, unmaskedDirectories.Count); + if (Path.DirectorySeparatorChar == '/' && directory.Contains('\\')) + { + Log.Warning("Directory name {Directory} contains one or more backslashes, which are not currently supported. This directory will not be shared.", directory); + continue; + } + var addedFiles = 0L; var filteredFiles = 0L; @@ -287,6 +293,12 @@ namespace slskd.Shares continue; } + if (Path.DirectorySeparatorChar == '/' && file.Value.Filename.Contains('\\')) + { + Log.Warning("Filename {Filename} contains one or more backslashes, which are not currently supported. This file will not be shared.", file.Value.Filename); + continue; + } + if (files.ContainsKey(file.Key)) { Log.Warning($"File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories (is a subdirectory being re-shared?)."); -- cgit v1.2.3