From 733b891f529c2458d65560f556edf68052be2846 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 1 Apr 2017 20:36:06 -0400 Subject: stub out cifs support --- .../IO/ManagedFileSystem.cs | 163 ++++++++++++++++++++- 1 file changed, 157 insertions(+), 6 deletions(-) (limited to 'Emby.Common.Implementations/IO/ManagedFileSystem.cs') diff --git a/Emby.Common.Implementations/IO/ManagedFileSystem.cs b/Emby.Common.Implementations/IO/ManagedFileSystem.cs index 4d7a86821..f1f044bc5 100644 --- a/Emby.Common.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Common.Implementations/IO/ManagedFileSystem.cs @@ -23,6 +23,8 @@ namespace Emby.Common.Implementations.IO private string _tempPath; + private SharpCifsFileSystem _sharpCifsFileSystem; + public ManagedFileSystem(ILogger logger, IEnvironmentInfo environmentInfo, string tempPath) { Logger = logger; @@ -34,6 +36,8 @@ namespace Emby.Common.Implementations.IO environmentInfo.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.OSX; SetInvalidFileNameChars(environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows); + + _sharpCifsFileSystem = new SharpCifsFileSystem(environmentInfo.OperatingSystem); } public void AddShortcutHandler(IShortcutHandler handler) @@ -168,6 +172,11 @@ namespace Emby.Common.Implementations.IO throw new ArgumentNullException("path"); } + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetFileSystemInfo(path); + } + // Take a guess to try and avoid two file system hits, but we'll double-check by calling Exists if (Path.HasExtension(path)) { @@ -208,6 +217,11 @@ namespace Emby.Common.Implementations.IO throw new ArgumentNullException("path"); } + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetFileInfo(path); + } + var fileInfo = new FileInfo(path); return GetFileSystemMetadata(fileInfo); @@ -228,6 +242,11 @@ namespace Emby.Common.Implementations.IO throw new ArgumentNullException("path"); } + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetDirectoryInfo(path); + } + var fileInfo = new DirectoryInfo(path); return GetFileSystemMetadata(fileInfo); @@ -374,6 +393,11 @@ namespace Emby.Common.Implementations.IO /// FileStream. public Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetFileStream(path, mode, access, share); + } + if (_supportsAsyncFileStreams && isAsync) { return new FileStream(path, GetFileMode(mode), GetFileAccess(access), GetFileShare(share), 262144, true); @@ -386,8 +410,8 @@ namespace Emby.Common.Implementations.IO { switch (mode) { - case FileOpenMode.Append: - return FileMode.Append; + //case FileOpenMode.Append: + // return FileMode.Append; case FileOpenMode.Create: return FileMode.Create; case FileOpenMode.CreateNew: @@ -396,8 +420,8 @@ namespace Emby.Common.Implementations.IO return FileMode.Open; case FileOpenMode.OpenOrCreate: return FileMode.OpenOrCreate; - case FileOpenMode.Truncate: - return FileMode.Truncate; + //case FileOpenMode.Truncate: + // return FileMode.Truncate; default: throw new Exception("Unrecognized FileOpenMode"); } @@ -407,8 +431,8 @@ namespace Emby.Common.Implementations.IO { switch (mode) { - case FileAccessMode.ReadWrite: - return FileAccess.ReadWrite; + //case FileAccessMode.ReadWrite: + // return FileAccess.ReadWrite; case FileAccessMode.Write: return FileAccess.Write; case FileAccessMode.Read: @@ -437,6 +461,12 @@ namespace Emby.Common.Implementations.IO public void SetHidden(string path, bool isHidden) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.SetHidden(path, isHidden); + return; + } + var info = GetFileInfo(path); if (info.Exists && info.IsHidden != isHidden) @@ -456,6 +486,12 @@ namespace Emby.Common.Implementations.IO public void SetReadOnly(string path, bool isReadOnly) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.SetReadOnly(path, isReadOnly); + return; + } + var info = GetFileInfo(path); if (info.Exists && info.IsReadOnly != isReadOnly) @@ -609,6 +645,12 @@ namespace Emby.Common.Implementations.IO public void DeleteFile(string path) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.DeleteFile(path); + return; + } + var fileInfo = GetFileInfo(path); if (fileInfo.Exists) @@ -628,11 +670,23 @@ namespace Emby.Common.Implementations.IO public void DeleteDirectory(string path, bool recursive) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.DeleteDirectory(path, recursive); + return; + } + Directory.Delete(path, recursive); } public void CreateDirectory(string path) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.CreateDirectory(path); + return; + } + Directory.CreateDirectory(path); } @@ -655,6 +709,11 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetDirectories(string path, bool recursive = false) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetDirectories(path, recursive); + } + var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; return ToMetadata(new DirectoryInfo(path).EnumerateDirectories("*", searchOption)); @@ -667,6 +726,11 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetFiles(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetFiles(path, extensions, enableCaseSensitiveExtensions, recursive); + } + var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; // On linux and osx the search pattern is case sensitive @@ -696,6 +760,11 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetFileSystemEntries(string path, bool recursive = false) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetFileSystemEntries(path, recursive); + } + var directoryInfo = new DirectoryInfo(path); var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; @@ -715,76 +784,148 @@ namespace Emby.Common.Implementations.IO public string[] ReadAllLines(string path) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.ReadAllLines(path); + } return File.ReadAllLines(path); } public void WriteAllLines(string path, IEnumerable lines) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.WriteAllLines(path, lines); + return; + } File.WriteAllLines(path, lines); } public Stream OpenRead(string path) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.OpenRead(path); + } return File.OpenRead(path); } public void CopyFile(string source, string target, bool overwrite) { + if (_sharpCifsFileSystem.IsEnabledForPath(source)) + { + _sharpCifsFileSystem.CopyFile(source, target, overwrite); + return; + } File.Copy(source, target, overwrite); } public void MoveFile(string source, string target) { + if (_sharpCifsFileSystem.IsEnabledForPath(source)) + { + _sharpCifsFileSystem.MoveFile(source, target); + return; + } File.Move(source, target); } public void MoveDirectory(string source, string target) { + if (_sharpCifsFileSystem.IsEnabledForPath(source)) + { + _sharpCifsFileSystem.MoveDirectory(source, target); + return; + } Directory.Move(source, target); } public bool DirectoryExists(string path) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.DirectoryExists(path); + } return Directory.Exists(path); } public bool FileExists(string path) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.FileExists(path); + } return File.Exists(path); } public string ReadAllText(string path) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.ReadAllText(path); + } return File.ReadAllText(path); } public byte[] ReadAllBytes(string path) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.ReadAllBytes(path); + } return File.ReadAllBytes(path); } public void WriteAllText(string path, string text, Encoding encoding) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.WriteAllText(path, text, encoding); + return; + } + File.WriteAllText(path, text, encoding); } public void WriteAllText(string path, string text) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.WriteAllText(path, text); + return; + } + File.WriteAllText(path, text); } public void WriteAllBytes(string path, byte[] bytes) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.WriteAllBytes(path, bytes); + return; + } + File.WriteAllBytes(path, bytes); } public string ReadAllText(string path, Encoding encoding) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.ReadAllText(path, encoding); + } + return File.ReadAllText(path, encoding); } public IEnumerable GetDirectoryPaths(string path, bool recursive = false) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetDirectoryPaths(path, recursive); + } + var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; return Directory.EnumerateDirectories(path, "*", searchOption); } @@ -796,6 +937,11 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetFilePaths(path, extensions, enableCaseSensitiveExtensions, recursive); + } + var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; // On linux and osx the search pattern is case sensitive @@ -825,6 +971,11 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetFileSystemEntryPaths(string path, bool recursive = false) { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + return _sharpCifsFileSystem.GetFileSystemEntryPaths(path, recursive); + } + var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; return Directory.EnumerateFileSystemEntries(path, "*", searchOption); } -- cgit v1.2.3 From 6e4d2342120c4bde4fdc99bc17dea0aec7b75ced Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 2 Apr 2017 00:08:07 -0400 Subject: update directory picker --- .../IO/ManagedFileSystem.cs | 28 ++++++++++++---------- .../IO/SharpCifsFileSystem.cs | 21 ++++++++++------ .../HttpServer/HttpListenerHost.cs | 13 ++++++++-- MediaBrowser.Model/IO/IFileSystem.cs | 1 - 4 files changed, 41 insertions(+), 22 deletions(-) (limited to 'Emby.Common.Implementations/IO/ManagedFileSystem.cs') diff --git a/Emby.Common.Implementations/IO/ManagedFileSystem.cs b/Emby.Common.Implementations/IO/ManagedFileSystem.cs index f1f044bc5..d1903851f 100644 --- a/Emby.Common.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Common.Implementations/IO/ManagedFileSystem.cs @@ -69,14 +69,6 @@ namespace Emby.Common.Implementations.IO } } - public char PathSeparator - { - get - { - return Path.PathSeparator; - } - } - public string GetFullPath(string path) { return Path.GetFullPath(path); @@ -544,6 +536,16 @@ namespace Emby.Common.Implementations.IO CopyFile(temp1, file2, true); } + private char GetSeparatorChar(string path) + { + if (path.IndexOf('/') != -1) + { + return '/'; + } + + return '\\'; + } + public bool AreEqual(string path1, string path2) { if (path1 == null && path2 == null) @@ -556,8 +558,8 @@ namespace Emby.Common.Implementations.IO return false; } - path1 = path1.TrimEnd(DirectorySeparatorChar); - path2 = path2.TrimEnd(DirectorySeparatorChar); + path1 = path1.TrimEnd(GetSeparatorChar(path1)); + path2 = path2.TrimEnd(GetSeparatorChar(path2)); return string.Equals(path1, path2, StringComparison.OrdinalIgnoreCase); } @@ -574,7 +576,9 @@ namespace Emby.Common.Implementations.IO throw new ArgumentNullException("path"); } - return path.IndexOf(parentPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase) != -1; + var separatorChar = GetSeparatorChar(parentPath); + + return path.IndexOf(parentPath.TrimEnd(separatorChar) + separatorChar, StringComparison.OrdinalIgnoreCase) != -1; } public bool IsRootPath(string path) @@ -606,7 +610,7 @@ namespace Emby.Common.Implementations.IO return path; } - return path.TrimEnd(Path.DirectorySeparatorChar); + return path.TrimEnd(GetSeparatorChar(path)); } public string GetFileNameWithoutExtension(FileSystemMetadata info) diff --git a/Emby.Common.Implementations/IO/SharpCifsFileSystem.cs b/Emby.Common.Implementations/IO/SharpCifsFileSystem.cs index fb32669f1..f2157b7af 100644 --- a/Emby.Common.Implementations/IO/SharpCifsFileSystem.cs +++ b/Emby.Common.Implementations/IO/SharpCifsFileSystem.cs @@ -55,7 +55,7 @@ namespace Emby.Common.Implementations.IO private string GetReturnPath(SmbFile file) { - return file.GetCanonicalPath(); + return file.GetCanonicalPath().TrimEnd('/'); //return file.GetPath(); } @@ -419,9 +419,16 @@ namespace Emby.Common.Implementations.IO } } + private SmbFile CreateSmbDirectoryForListFiles(string path) + { + // In order to call ListFiles, it has to end with the separator + + return CreateSmbFile(path.TrimEnd('/') + '/'); + } + public IEnumerable GetDirectories(string path, bool recursive = false) { - var dir = CreateSmbFile(path); + var dir = CreateSmbDirectoryForListFiles(path); AssertDirectoryExists(dir, path); var list = ListFiles(dir, recursive); @@ -437,7 +444,7 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetFiles(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false) { - var dir = CreateSmbFile(path); + var dir = CreateSmbDirectoryForListFiles(path); AssertDirectoryExists(dir, path); var list = ListFiles(dir, recursive); @@ -459,7 +466,7 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetFileSystemEntries(string path, bool recursive = false) { - var dir = CreateSmbFile(path); + var dir = CreateSmbDirectoryForListFiles(path); AssertDirectoryExists(dir, path); var list = ListFiles(dir, recursive); @@ -472,7 +479,7 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetFileSystemEntryPaths(string path, bool recursive = false) { - var dir = CreateSmbFile(path); + var dir = CreateSmbDirectoryForListFiles(path); AssertDirectoryExists(dir, path); var list = ListFiles(dir, recursive); @@ -485,7 +492,7 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false) { - var dir = CreateSmbFile(path); + var dir = CreateSmbDirectoryForListFiles(path); AssertDirectoryExists(dir, path); var list = ListFiles(dir, recursive); @@ -507,7 +514,7 @@ namespace Emby.Common.Implementations.IO public IEnumerable GetDirectoryPaths(string path, bool recursive = false) { - var dir = CreateSmbFile(path); + var dir = CreateSmbDirectoryForListFiles(path); AssertDirectoryExists(dir, path); var list = ListFiles(dir, recursive); diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 6d15cc619..ee5245a69 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -273,10 +273,19 @@ namespace Emby.Server.Implementations.HttpServer return 400; } + var exceptionType = ex.GetType(); + int statusCode; - if (!_mapExceptionToStatusCode.TryGetValue(ex.GetType(), out statusCode)) + if (!_mapExceptionToStatusCode.TryGetValue(exceptionType, out statusCode)) { - statusCode = 500; + if (string.Equals(exceptionType.Name, "DirectoryNotFoundException", StringComparison.OrdinalIgnoreCase)) + { + statusCode = 404; + } + else + { + statusCode = 500; + } } return statusCode; diff --git a/MediaBrowser.Model/IO/IFileSystem.cs b/MediaBrowser.Model/IO/IFileSystem.cs index 59e31debd..6773acbfa 100644 --- a/MediaBrowser.Model/IO/IFileSystem.cs +++ b/MediaBrowser.Model/IO/IFileSystem.cs @@ -312,7 +312,6 @@ namespace MediaBrowser.Model.IO void SetReadOnly(string path, bool isHidden); char DirectorySeparatorChar { get; } - char PathSeparator { get; } string GetFullPath(string path); -- cgit v1.2.3 From 4a8960fc866143e7107a5bc41527db6c5581974f Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 2 Apr 2017 00:45:57 -0400 Subject: 3.2.10.4 --- Emby.Common.Implementations/IO/ManagedFileSystem.cs | 16 ++++++++-------- Emby.Common.Implementations/IO/SharpCifsFileSystem.cs | 10 ++++++++++ SharedVersion.cs | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) (limited to 'Emby.Common.Implementations/IO/ManagedFileSystem.cs') diff --git a/Emby.Common.Implementations/IO/ManagedFileSystem.cs b/Emby.Common.Implementations/IO/ManagedFileSystem.cs index d1903851f..ba73c1ba2 100644 --- a/Emby.Common.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Common.Implementations/IO/ManagedFileSystem.cs @@ -536,14 +536,14 @@ namespace Emby.Common.Implementations.IO CopyFile(temp1, file2, true); } - private char GetSeparatorChar(string path) + private char GetDirectorySeparatorChar(string path) { - if (path.IndexOf('/') != -1) + if (_sharpCifsFileSystem.IsEnabledForPath(path)) { - return '/'; + return _sharpCifsFileSystem.GetDirectorySeparatorChar(path); } - return '\\'; + return Path.DirectorySeparatorChar; } public bool AreEqual(string path1, string path2) @@ -558,8 +558,8 @@ namespace Emby.Common.Implementations.IO return false; } - path1 = path1.TrimEnd(GetSeparatorChar(path1)); - path2 = path2.TrimEnd(GetSeparatorChar(path2)); + path1 = path1.TrimEnd(GetDirectorySeparatorChar(path1)); + path2 = path2.TrimEnd(GetDirectorySeparatorChar(path2)); return string.Equals(path1, path2, StringComparison.OrdinalIgnoreCase); } @@ -576,7 +576,7 @@ namespace Emby.Common.Implementations.IO throw new ArgumentNullException("path"); } - var separatorChar = GetSeparatorChar(parentPath); + var separatorChar = GetDirectorySeparatorChar(parentPath); return path.IndexOf(parentPath.TrimEnd(separatorChar) + separatorChar, StringComparison.OrdinalIgnoreCase) != -1; } @@ -610,7 +610,7 @@ namespace Emby.Common.Implementations.IO return path; } - return path.TrimEnd(GetSeparatorChar(path)); + return path.TrimEnd(GetDirectorySeparatorChar(path)); } public string GetFileNameWithoutExtension(FileSystemMetadata info) diff --git a/Emby.Common.Implementations/IO/SharpCifsFileSystem.cs b/Emby.Common.Implementations/IO/SharpCifsFileSystem.cs index f2157b7af..c1e429dc9 100644 --- a/Emby.Common.Implementations/IO/SharpCifsFileSystem.cs +++ b/Emby.Common.Implementations/IO/SharpCifsFileSystem.cs @@ -30,6 +30,16 @@ namespace Emby.Common.Implementations.IO return path.StartsWith("smb://", StringComparison.OrdinalIgnoreCase) || IsUncPath(path); } + public char GetDirectorySeparatorChar(string path) + { + if (path.IndexOf('/') != -1) + { + return '/'; + } + + return '\\'; + } + public FileSystemMetadata GetFileSystemInfo(string path) { var file = CreateSmbFile(path); diff --git a/SharedVersion.cs b/SharedVersion.cs index 6f5bd9b1f..6f3c5261f 100644 --- a/SharedVersion.cs +++ b/SharedVersion.cs @@ -1,3 +1,3 @@ using System.Reflection; -[assembly: AssemblyVersion("3.2.10.3")] +[assembly: AssemblyVersion("3.2.10.4")] -- cgit v1.2.3