IFileSystem.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace MediaBrowser.Model.IO
  5. {
  6. /// <summary>
  7. /// Interface IFileSystem
  8. /// </summary>
  9. public interface IFileSystem
  10. {
  11. void AddShortcutHandler(IShortcutHandler handler);
  12. /// <summary>
  13. /// Determines whether the specified filename is shortcut.
  14. /// </summary>
  15. /// <param name="filename">The filename.</param>
  16. /// <returns><c>true</c> if the specified filename is shortcut; otherwise, <c>false</c>.</returns>
  17. bool IsShortcut(string filename);
  18. /// <summary>
  19. /// Resolves the shortcut.
  20. /// </summary>
  21. /// <param name="filename">The filename.</param>
  22. /// <returns>System.String.</returns>
  23. string ResolveShortcut(string filename);
  24. /// <summary>
  25. /// Creates the shortcut.
  26. /// </summary>
  27. /// <param name="shortcutPath">The shortcut path.</param>
  28. /// <param name="target">The target.</param>
  29. void CreateShortcut(string shortcutPath, string target);
  30. string MakeAbsolutePath(string folderPath, string filePath);
  31. /// <summary>
  32. /// Returns a <see cref="FileSystemMetadata" /> object for the specified file or directory path.
  33. /// </summary>
  34. /// <param name="path">A path to a file or directory.</param>
  35. /// <returns>A <see cref="FileSystemMetadata" /> object.</returns>
  36. /// <remarks>If the specified path points to a directory, the returned <see cref="FileSystemMetadata" /> object's
  37. /// <see cref="FileSystemMetadata.IsDirectory" /> property will be set to true and all other properties will reflect the properties of the directory.</remarks>
  38. FileSystemMetadata GetFileSystemInfo(string path);
  39. /// <summary>
  40. /// Returns a <see cref="FileSystemMetadata" /> object for the specified file path.
  41. /// </summary>
  42. /// <param name="path">A path to a file.</param>
  43. /// <returns>A <see cref="FileSystemMetadata" /> object.</returns>
  44. /// <remarks><para>If the specified path points to a directory, the returned <see cref="FileSystemMetadata" /> object's
  45. /// <see cref="FileSystemMetadata.IsDirectory" /> property and the <see cref="FileSystemMetadata.Exists" /> property will both be set to false.</para>
  46. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="M:IFileSystem.GetFileSystemInfo(System.String)" />.</para></remarks>
  47. FileSystemMetadata GetFileInfo(string path);
  48. /// <summary>
  49. /// Returns a <see cref="FileSystemMetadata" /> object for the specified directory path.
  50. /// </summary>
  51. /// <param name="path">A path to a directory.</param>
  52. /// <returns>A <see cref="FileSystemMetadata" /> object.</returns>
  53. /// <remarks><para>If the specified path points to a file, the returned <see cref="FileSystemMetadata" /> object's
  54. /// <see cref="FileSystemMetadata.IsDirectory" /> property will be set to true and the <see cref="FileSystemMetadata.Exists" /> property will be set to false.</para>
  55. /// <para>For automatic handling of files <b>and</b> directories, use <see cref="M:IFileSystem.GetFileSystemInfo(System.String)" />.</para></remarks>
  56. FileSystemMetadata GetDirectoryInfo(string path);
  57. /// <summary>
  58. /// Gets the valid filename.
  59. /// </summary>
  60. /// <param name="filename">The filename.</param>
  61. /// <returns>System.String.</returns>
  62. string GetValidFilename(string filename);
  63. /// <summary>
  64. /// Gets the creation time UTC.
  65. /// </summary>
  66. /// <param name="info">The information.</param>
  67. /// <returns>DateTime.</returns>
  68. DateTime GetCreationTimeUtc(FileSystemMetadata info);
  69. /// <summary>
  70. /// Gets the creation time UTC.
  71. /// </summary>
  72. /// <param name="path">The path.</param>
  73. /// <returns>DateTime.</returns>
  74. DateTime GetCreationTimeUtc(string path);
  75. /// <summary>
  76. /// Gets the last write time UTC.
  77. /// </summary>
  78. /// <param name="info">The information.</param>
  79. /// <returns>DateTime.</returns>
  80. DateTime GetLastWriteTimeUtc(FileSystemMetadata info);
  81. /// <summary>
  82. /// Gets the last write time UTC.
  83. /// </summary>
  84. /// <param name="path">The path.</param>
  85. /// <returns>DateTime.</returns>
  86. DateTime GetLastWriteTimeUtc(string path);
  87. /// <summary>
  88. /// Gets the file stream.
  89. /// </summary>
  90. /// <param name="path">The path.</param>
  91. /// <param name="mode">The mode.</param>
  92. /// <param name="access">The access.</param>
  93. /// <param name="share">The share.</param>
  94. /// <param name="isAsync">if set to <c>true</c> [is asynchronous].</param>
  95. /// <returns>FileStream.</returns>
  96. Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false);
  97. Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share,
  98. FileOpenOptions fileOpenOptions);
  99. /// <summary>
  100. /// Swaps the files.
  101. /// </summary>
  102. /// <param name="file1">The file1.</param>
  103. /// <param name="file2">The file2.</param>
  104. void SwapFiles(string file1, string file2);
  105. bool AreEqual(string path1, string path2);
  106. /// <summary>
  107. /// Determines whether [contains sub path] [the specified parent path].
  108. /// </summary>
  109. /// <param name="parentPath">The parent path.</param>
  110. /// <param name="path">The path.</param>
  111. /// <returns><c>true</c> if [contains sub path] [the specified parent path]; otherwise, <c>false</c>.</returns>
  112. bool ContainsSubPath(string parentPath, string path);
  113. /// <summary>
  114. /// Determines whether [is root path] [the specified path].
  115. /// </summary>
  116. /// <param name="path">The path.</param>
  117. /// <returns><c>true</c> if [is root path] [the specified path]; otherwise, <c>false</c>.</returns>
  118. bool IsRootPath(string path);
  119. /// <summary>
  120. /// Normalizes the path.
  121. /// </summary>
  122. /// <param name="path">The path.</param>
  123. /// <returns>System.String.</returns>
  124. string NormalizePath(string path);
  125. /// <summary>
  126. /// Gets the file name without extension.
  127. /// </summary>
  128. /// <param name="info">The information.</param>
  129. /// <returns>System.String.</returns>
  130. string GetFileNameWithoutExtension(FileSystemMetadata info);
  131. /// <summary>
  132. /// Determines whether [is path file] [the specified path].
  133. /// </summary>
  134. /// <param name="path">The path.</param>
  135. /// <returns><c>true</c> if [is path file] [the specified path]; otherwise, <c>false</c>.</returns>
  136. bool IsPathFile(string path);
  137. /// <summary>
  138. /// Deletes the file.
  139. /// </summary>
  140. /// <param name="path">The path.</param>
  141. void DeleteFile(string path);
  142. /// <summary>
  143. /// Gets the directories.
  144. /// </summary>
  145. /// <param name="path">The path.</param>
  146. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  147. /// <returns>IEnumerable&lt;DirectoryInfo&gt;.</returns>
  148. IEnumerable<FileSystemMetadata> GetDirectories(string path, bool recursive = false);
  149. /// <summary>
  150. /// Gets the files.
  151. /// </summary>
  152. IEnumerable<FileSystemMetadata> GetFiles(string path, bool recursive = false);
  153. IEnumerable<FileSystemMetadata> GetFiles(string path, IReadOnlyList<string> extensions, bool enableCaseSensitiveExtensions, bool recursive);
  154. /// <summary>
  155. /// Gets the file system entries.
  156. /// </summary>
  157. /// <param name="path">The path.</param>
  158. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  159. /// <returns>IEnumerable&lt;FileSystemMetadata&gt;.</returns>
  160. IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool recursive = false);
  161. /// <summary>
  162. /// Gets the directory paths.
  163. /// </summary>
  164. /// <param name="path">The path.</param>
  165. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  166. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  167. IEnumerable<string> GetDirectoryPaths(string path, bool recursive = false);
  168. /// <summary>
  169. /// Gets the file paths.
  170. /// </summary>
  171. /// <param name="path">The path.</param>
  172. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  173. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  174. IEnumerable<string> GetFilePaths(string path, bool recursive = false);
  175. IEnumerable<string> GetFilePaths(string path, string[] extensions, bool enableCaseSensitiveExtensions, bool recursive);
  176. /// <summary>
  177. /// Gets the file system entry paths.
  178. /// </summary>
  179. /// <param name="path">The path.</param>
  180. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  181. /// <returns>IEnumerable&lt;System.String&gt;.</returns>
  182. IEnumerable<string> GetFileSystemEntryPaths(string path, bool recursive = false);
  183. void SetHidden(string path, bool isHidden);
  184. void SetReadOnly(string path, bool readOnly);
  185. void SetAttributes(string path, bool isHidden, bool readOnly);
  186. List<FileSystemMetadata> GetDrives();
  187. void SetExecutable(string path);
  188. }
  189. //TODO Investigate if can be replaced by the one from System.IO ?
  190. public enum FileOpenMode
  191. {
  192. //
  193. // Summary:
  194. // Specifies that the operating system should create a new file. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  195. // permission. If the file already exists, an System.IO.IOException exception is
  196. // thrown.
  197. CreateNew = 1,
  198. //
  199. // Summary:
  200. // Specifies that the operating system should create a new file. If the file already
  201. // exists, it will be overwritten. This requires System.Security.Permissions.FileIOPermissionAccess.Write
  202. // permission. FileMode.Create is equivalent to requesting that if the file does
  203. // not exist, use System.IO.FileMode.CreateNew; otherwise, use System.IO.FileMode.Truncate.
  204. // If the file already exists but is a hidden file, an System.UnauthorizedAccessException
  205. // exception is thrown.
  206. Create = 2,
  207. //
  208. // Summary:
  209. // Specifies that the operating system should open an existing file. The ability
  210. // to open the file is dependent on the value specified by the System.IO.FileAccess
  211. // enumeration. A System.IO.FileNotFoundException exception is thrown if the file
  212. // does not exist.
  213. Open = 3,
  214. //
  215. // Summary:
  216. // Specifies that the operating system should open a file if it exists; otherwise,
  217. // a new file should be created. If the file is opened with FileAccess.Read, System.Security.Permissions.FileIOPermissionAccess.Read
  218. // permission is required. If the file access is FileAccess.Write, System.Security.Permissions.FileIOPermissionAccess.Write
  219. // permission is required. If the file is opened with FileAccess.ReadWrite, both
  220. // System.Security.Permissions.FileIOPermissionAccess.Read and System.Security.Permissions.FileIOPermissionAccess.Write
  221. // permissions are required.
  222. OpenOrCreate = 4
  223. }
  224. public enum FileAccessMode
  225. {
  226. //
  227. // Summary:
  228. // Read access to the file. Data can be read from the file. Combine with Write for
  229. // read/write access.
  230. Read = 1,
  231. //
  232. // Summary:
  233. // Write access to the file. Data can be written to the file. Combine with Read
  234. // for read/write access.
  235. Write = 2
  236. }
  237. public enum FileShareMode
  238. {
  239. //
  240. // Summary:
  241. // Declines sharing of the current file. Any request to open the file (by this process
  242. // or another process) will fail until the file is closed.
  243. None = 0,
  244. //
  245. // Summary:
  246. // Allows subsequent opening of the file for reading. If this flag is not specified,
  247. // any request to open the file for reading (by this process or another process)
  248. // will fail until the file is closed. However, even if this flag is specified,
  249. // additional permissions might still be needed to access the file.
  250. Read = 1,
  251. //
  252. // Summary:
  253. // Allows subsequent opening of the file for writing. If this flag is not specified,
  254. // any request to open the file for writing (by this process or another process)
  255. // will fail until the file is closed. However, even if this flag is specified,
  256. // additional permissions might still be needed to access the file.
  257. Write = 2,
  258. //
  259. // Summary:
  260. // Allows subsequent opening of the file for reading or writing. If this flag is
  261. // not specified, any request to open the file for reading or writing (by this process
  262. // or another process) will fail until the file is closed. However, even if this
  263. // flag is specified, additional permissions might still be needed to access the
  264. // file.
  265. ReadWrite = 3
  266. }
  267. //
  268. // Summary:
  269. // Represents advanced options for creating a System.IO.FileStream object.
  270. [Flags]
  271. public enum FileOpenOptions
  272. {
  273. //
  274. // Summary:
  275. // Indicates that the system should write through any intermediate cache and go
  276. // directly to disk.
  277. WriteThrough = int.MinValue,
  278. //
  279. // Summary:
  280. // Indicates that no additional options should be used when creating a System.IO.FileStream
  281. // object.
  282. None = 0,
  283. //
  284. // Summary:
  285. // Indicates that a file is encrypted and can be decrypted only by using the same
  286. // user account used for encryption.
  287. Encrypted = 16384,
  288. //
  289. // Summary:
  290. // Indicates that a file is automatically deleted when it is no longer in use.
  291. DeleteOnClose = 67108864,
  292. //
  293. // Summary:
  294. // Indicates that the file is to be accessed sequentially from beginning to end.
  295. // The system can use this as a hint to optimize file caching. If an application
  296. // moves the file pointer for random access, optimum caching may not occur; however,
  297. // correct operation is still guaranteed.
  298. SequentialScan = 134217728,
  299. //
  300. // Summary:
  301. // Indicates that the file is accessed randomly. The system can use this as a hint
  302. // to optimize file caching.
  303. RandomAccess = 268435456,
  304. //
  305. // Summary:
  306. // Indicates that a file can be used for asynchronous reading and writing.
  307. Asynchronous = 1073741824
  308. }
  309. }