IFileSystem.cs 15 KB

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