| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- using SharpCifs.Smb;
 
- using MediaBrowser.Model.IO;
 
- using MediaBrowser.Model.Logging;
 
- using MediaBrowser.Model.System;
 
- namespace Emby.Common.Implementations.IO
 
- {
 
-     public class SharpCifsFileSystem
 
-     {
 
-         private readonly MediaBrowser.Model.System.OperatingSystem _operatingSystem;
 
-         public SharpCifsFileSystem(MediaBrowser.Model.System.OperatingSystem operatingSystem)
 
-         {
 
-             _operatingSystem = operatingSystem;
 
-         }
 
-         public bool IsEnabledForPath(string path)
 
-         {
 
-             if (_operatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows)
 
-             {
 
-                 return false;
 
-             }
 
-             return path.StartsWith("smb://", StringComparison.OrdinalIgnoreCase) || IsUncPath(path);
 
-         }
 
-         public string NormalizePath(string path)
 
-         {
 
-             if (path.StartsWith("smb://", StringComparison.OrdinalIgnoreCase))
 
-             {
 
-                 return path;
 
-             }
 
-             if (IsUncPath(path))
 
-             {
 
-                 return ConvertUncToSmb(path);
 
-             }
 
-             return path;
 
-         }
 
-         public string GetDirectoryName(string path)
 
-         {
 
-             var separator = GetDirectorySeparatorChar(path);
 
-             var result = Path.GetDirectoryName(path);
 
-             if (separator == '/')
 
-             {
 
-                 result = result.Replace('\\', '/');
 
-             }
 
-             return result;
 
-         }
 
-         public char GetDirectorySeparatorChar(string path)
 
-         {
 
-             if (path.IndexOf('/') != -1)
 
-             {
 
-                 return '/';
 
-             }
 
-             return '\\';
 
-         }
 
-         public FileSystemMetadata GetFileSystemInfo(string path)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             return ToMetadata(file);
 
-         }
 
-         public FileSystemMetadata GetFileInfo(string path)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             return ToMetadata(file, false);
 
-         }
 
-         public FileSystemMetadata GetDirectoryInfo(string path)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             return ToMetadata(file, true);
 
-         }
 
-         private bool IsUncPath(string path)
 
-         {
 
-             return path.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase);
 
-         }
 
-         private string GetReturnPath(SmbFile file)
 
-         {
 
-             return file.GetCanonicalPath().TrimEnd('/');
 
-             //return file.GetPath();
 
-         }
 
-         private string ConvertUncToSmb(string path)
 
-         {
 
-             if (IsUncPath(path))
 
-             {
 
-                 path = path.Replace('\\', '/');
 
-                 path = "smb:" + path;
 
-             }
 
-             return path;
 
-         }
 
-         private string AddAuthentication(string path)
 
-         {
 
-             return path;
 
-         }
 
-         private SmbFile CreateSmbFile(string path)
 
-         {
 
-             path = ConvertUncToSmb(path);
 
-             path = AddAuthentication(path);
 
-             return new SmbFile(path);
 
-         }
 
-         DateTime baseDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 
-         private FileSystemMetadata ToMetadata(SmbFile info, bool? isDirectory = null)
 
-         {
 
-             var result = new FileSystemMetadata();
 
-             result.Exists = info.Exists();
 
-             result.FullName = GetReturnPath(info);
 
-             result.Extension = Path.GetExtension(result.FullName);
 
-             result.Name = info.GetName();
 
-             if (result.Exists)
 
-             {
 
-                 result.IsDirectory = info.IsDirectory();
 
-                 result.IsHidden = info.IsHidden();
 
-                 result.IsReadOnly = !info.CanWrite();
 
-                 if (info.IsFile())
 
-                 {
 
-                     result.Length = info.Length();
 
-                     result.DirectoryName = info.GetParent();
 
-                 }
 
-                 result.CreationTimeUtc = baseDate.AddMilliseconds(info.CreateTime());
 
-                 result.LastWriteTimeUtc = baseDate.AddMilliseconds(info.GetLastModified());
 
-             }
 
-             else
 
-             {
 
-                 if (isDirectory.HasValue)
 
-                 {
 
-                     result.IsDirectory = isDirectory.Value;
 
-                 }
 
-             }
 
-             return result;
 
-         }
 
-         public void SetHidden(string path, bool isHidden)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             var isCurrentlyHidden = file.IsHidden();
 
-             if (isCurrentlyHidden && !isHidden)
 
-             {
 
-                 file.SetAttributes(file.GetAttributes() & ~SmbFile.AttrReadonly);
 
-             }
 
-             else if (!isCurrentlyHidden && isHidden)
 
-             {
 
-                 file.SetAttributes(file.GetAttributes() | SmbFile.AttrReadonly);
 
-             }
 
-         }
 
-         public void SetReadOnly(string path, bool isReadOnly)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             var isCurrentlyReadOnly = !file.CanWrite();
 
-             if (isCurrentlyReadOnly && !isReadOnly)
 
-             {
 
-                 file.SetReadWrite();
 
-             }
 
-             else if (!isCurrentlyReadOnly && isReadOnly)
 
-             {
 
-                 file.SetReadOnly();
 
-             }
 
-         }
 
-         public void DeleteFile(string path)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             AssertFileExists(file, path);
 
-             file.Delete();
 
-         }
 
-         public void DeleteDirectory(string path, bool recursive)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             AssertDirectoryExists(file, path);
 
-             file.Delete();
 
-         }
 
-         public void CreateDirectory(string path)
 
-         {
 
-         }
 
-         public string[] ReadAllLines(string path)
 
-         {
 
-             var lines = new List<string>();
 
-             using (var stream = OpenRead(path))
 
-             {
 
-                 using (var reader = new StreamReader(stream))
 
-                 {
 
-                     while (!reader.EndOfStream)
 
-                     {
 
-                         lines.Add(reader.ReadLine());
 
-                     }
 
-                 }
 
-             }
 
-             return lines.ToArray();
 
-         }
 
-         public void WriteAllLines(string path, IEnumerable<string> lines)
 
-         {
 
-             using (var stream = GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
 
-             {
 
-                 using (var writer = new StreamWriter(stream))
 
-                 {
 
-                     foreach (var line in lines)
 
-                     {
 
-                         writer.WriteLine(line);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         private void AssertFileExists(SmbFile file, string path)
 
-         {
 
-             if (!file.Exists())
 
-             {
 
-                 throw new FileNotFoundException("File not found.", path);
 
-             }
 
-         }
 
-         private void AssertDirectoryExists(SmbFile file, string path)
 
-         {
 
-             if (!file.Exists())
 
-             {
 
-                 throw new FileNotFoundException("File not found.", path);
 
-             }
 
-         }
 
-         public Stream OpenRead(string path)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             AssertFileExists(file, path);
 
-             return file.GetInputStream();
 
-         }
 
-         private Stream OpenWrite(string path)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             AssertFileExists(file, path);
 
-             return file.GetInputStream();
 
-         }
 
-         public void CopyFile(string source, string target, bool overwrite)
 
-         {
 
-             if (string.Equals(source, target, StringComparison.Ordinal))
 
-             {
 
-                 throw new ArgumentException("Cannot CopyFile when source and target are the same");
 
-             }
 
-             using (var input = OpenRead(source))
 
-             {
 
-                 using (var output = GetFileStream(target, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
 
-                 {
 
-                     input.CopyTo(output);
 
-                 }
 
-             }
 
-         }
 
-         public void MoveFile(string source, string target)
 
-         {
 
-             if (string.Equals(source, target, StringComparison.Ordinal))
 
-             {
 
-                 throw new ArgumentException("Cannot MoveFile when source and target are the same");
 
-             }
 
-             using (var input = OpenRead(source))
 
-             {
 
-                 using (var output = GetFileStream(target, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
 
-                 {
 
-                     input.CopyTo(output);
 
-                 }
 
-             }
 
-             DeleteFile(source);
 
-         }
 
-         public void MoveDirectory(string source, string target)
 
-         {
 
-             throw new NotImplementedException();
 
-         }
 
-         public bool DirectoryExists(string path)
 
-         {
 
-             var dir = CreateSmbFile(path);
 
-             return dir.Exists() && dir.IsDirectory();
 
-         }
 
-         public bool FileExists(string path)
 
-         {
 
-             var file = CreateSmbFile(path);
 
-             return file.Exists();
 
-         }
 
-         public string ReadAllText(string path, Encoding encoding)
 
-         {
 
-             using (var stream = OpenRead(path))
 
-             {
 
-                 using (var reader = new StreamReader(stream, encoding))
 
-                 {
 
-                     return reader.ReadToEnd();
 
-                 }
 
-             }
 
-         }
 
-         public Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share)
 
-         {
 
-             if (mode == FileOpenMode.OpenOrCreate)
 
-             {
 
-                 var file = CreateSmbFile(path);
 
-                 if (!file.Exists())
 
-                 {
 
-                     file.CreateNewFile();
 
-                 }
 
-                 mode = FileOpenMode.Open;
 
-             }
 
-             if (mode == FileOpenMode.CreateNew)
 
-             {
 
-                 var file = CreateSmbFile(path);
 
-                 if (file.Exists())
 
-                 {
 
-                     throw new IOException("File already exists");
 
-                 }
 
-                 file.CreateNewFile();
 
-                 mode = FileOpenMode.Open;
 
-             }
 
-             if (mode == FileOpenMode.Create)
 
-             {
 
-                 var file = CreateSmbFile(path);
 
-                 if (file.Exists())
 
-                 {
 
-                     if (file.IsHidden())
 
-                     {
 
-                         throw new UnauthorizedAccessException(string.Format("File {0} already exists and is hidden", path));
 
-                     }
 
-                     file.Delete();
 
-                     file.CreateNewFile();
 
-                 }
 
-                 else
 
-                 {
 
-                     file.CreateNewFile();
 
-                 }
 
-                 mode = FileOpenMode.Open;
 
-             }
 
-             if (mode == FileOpenMode.Open)
 
-             {
 
-                 if (access == FileAccessMode.Read)
 
-                 {
 
-                     return OpenRead(path);
 
-                 }
 
-                 if (access == FileAccessMode.Write)
 
-                 {
 
-                     return OpenWrite(path);
 
-                 }
 
-                 throw new NotImplementedException();
 
-             }
 
-             throw new NotImplementedException();
 
-         }
 
-         public void WriteAllBytes(string path, byte[] bytes)
 
-         {
 
-             using (var stream = GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
 
-             {
 
-                 stream.Write(bytes, 0, bytes.Length);
 
-             }
 
-         }
 
-         public void WriteAllText(string path, string text)
 
-         {
 
-             using (var stream = GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
 
-             {
 
-                 using (var writer = new StreamWriter(stream))
 
-                 {
 
-                     writer.Write(text);
 
-                 }
 
-             }
 
-         }
 
-         public void WriteAllText(string path, string text, Encoding encoding)
 
-         {
 
-             using (var stream = GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None))
 
-             {
 
-                 using (var writer = new StreamWriter(stream, encoding))
 
-                 {
 
-                     writer.Write(text);
 
-                 }
 
-             }
 
-         }
 
-         public string ReadAllText(string path)
 
-         {
 
-             using (var stream = OpenRead(path))
 
-             {
 
-                 using (var reader = new StreamReader(stream))
 
-                 {
 
-                     return reader.ReadToEnd();
 
-                 }
 
-             }
 
-         }
 
-         public byte[] ReadAllBytes(string path)
 
-         {
 
-             using (var stream = OpenRead(path))
 
-             {
 
-                 using (var ms = new MemoryStream())
 
-                 {
 
-                     stream.CopyTo(ms);
 
-                     ms.Position = 0;
 
-                     return ms.ToArray();
 
-                 }
 
-             }
 
-         }
 
-         private SmbFile CreateSmbDirectoryForListFiles(string path)
 
-         {
 
-             // In order to call ListFiles, it has to end with the separator
 
-             return CreateSmbFile(path.TrimEnd('/') + '/');
 
-         }
 
-         public IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false)
 
-         {
 
-             var dir = CreateSmbDirectoryForListFiles(path);
 
-             AssertDirectoryExists(dir, path);
 
-             var list = ListFiles(dir, recursive);
 
-             foreach (var file in list)
 
-             {
 
-                 if (file.IsDirectory())
 
-                 {
 
-                     yield return ToMetadata(file);
 
-                 }
 
-             }
 
-         }
 
-         public IEnumerable<FileSystemMetadata> GetFiles(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
 
-         {
 
-             var dir = CreateSmbDirectoryForListFiles(path);
 
-             AssertDirectoryExists(dir, path);
 
-             var list = ListFiles(dir, recursive);
 
-             foreach (var file in list)
 
-             {
 
-                 if (file.IsFile())
 
-                 {
 
-                     var filePath = GetReturnPath(file);
 
-                     var extension = Path.GetExtension(filePath);
 
-                     if (extensions == null || extensions.Length == 0 || extensions.Contains(extension ?? string.Empty, StringComparer.OrdinalIgnoreCase))
 
-                     {
 
-                         yield return ToMetadata(file);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         public IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false)
 
-         {
 
-             var dir = CreateSmbDirectoryForListFiles(path);
 
-             AssertDirectoryExists(dir, path);
 
-             var list = ListFiles(dir, recursive);
 
-             foreach (var file in list)
 
-             {
 
-                 yield return ToMetadata(file);
 
-             }
 
-         }
 
-         public IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false)
 
-         {
 
-             var dir = CreateSmbDirectoryForListFiles(path);
 
-             AssertDirectoryExists(dir, path);
 
-             var list = ListFiles(dir, recursive);
 
-             foreach (var file in list)
 
-             {
 
-                 yield return GetReturnPath(file);
 
-             }
 
-         }
 
-         public IEnumerable<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive = false)
 
-         {
 
-             var dir = CreateSmbDirectoryForListFiles(path);
 
-             AssertDirectoryExists(dir, path);
 
-             var list = ListFiles(dir, recursive);
 
-             foreach (var file in list)
 
-             {
 
-                 if (file.IsFile())
 
-                 {
 
-                     var filePath = GetReturnPath(file);
 
-                     var extension = Path.GetExtension(filePath);
 
-                     if (extensions == null || extensions.Length == 0 || extensions.Contains(extension ?? string.Empty, StringComparer.OrdinalIgnoreCase))
 
-                     {
 
-                         yield return filePath;
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         public IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false)
 
-         {
 
-             var dir = CreateSmbDirectoryForListFiles(path);
 
-             AssertDirectoryExists(dir, path);
 
-             var list = ListFiles(dir, recursive);
 
-             foreach (var file in list)
 
-             {
 
-                 if (file.IsDirectory())
 
-                 {
 
-                     yield return GetReturnPath(file);
 
-                 }
 
-             }
 
-         }
 
-         private IEnumerable<SmbFile> ListFiles(SmbFile dir, bool recursive)
 
-         {
 
-             var list = dir.ListFiles();
 
-             foreach (var file in list)
 
-             {
 
-                 yield return file;
 
-                 if (recursive && file.IsDirectory())
 
-                 {
 
-                     foreach (var subFile in ListFiles(file, recursive))
 
-                     {
 
-                         yield return subFile;
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |