// // Copyright (c) 2008-2011, Kenneth Bell // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // using System; using System.IO; using DiscUtils.Streams; namespace DiscUtils { /// /// Provides the base class for all file systems. /// public abstract class DiscFileSystem : #if !NETSTANDARD MarshalByRefObject, #endif IFileSystem, IDisposable { /// /// Initializes a new instance of the DiscFileSystem class. /// protected DiscFileSystem() { Options = new DiscFileSystemOptions(); } /// /// Initializes a new instance of the DiscFileSystem class. /// /// The options instance to use for this file system instance. protected DiscFileSystem(DiscFileSystemOptions defaultOptions) { Options = defaultOptions; } /// /// Finalizes an instance of the DiscFileSystem class. /// ~DiscFileSystem() { Dispose(false); } /// /// Gets the file system options, which can be modified. /// public virtual DiscFileSystemOptions Options { get; } /// /// Gets a friendly description of the file system type. /// public abstract string FriendlyName { get; } /// /// Gets a value indicating whether the file system is read-only or read-write. /// /// true if the file system is read-write. public abstract bool CanWrite { get; } /// /// Gets the root directory of the file system. /// public virtual DiscDirectoryInfo Root { get { return new DiscDirectoryInfo(this, string.Empty); } } /// /// Gets the volume label. /// public virtual string VolumeLabel { get { return string.Empty; } } /// /// Gets a value indicating whether the file system is thread-safe. /// public virtual bool IsThreadSafe { get { return false; } } /// /// Copies an existing file to a new file. /// /// The source file. /// The destination file. public virtual void CopyFile(string sourceFile, string destinationFile) { CopyFile(sourceFile, destinationFile, false); } /// /// Copies an existing file to a new file, allowing overwriting of an existing file. /// /// The source file. /// The destination file. /// Whether to permit over-writing of an existing file. public abstract void CopyFile(string sourceFile, string destinationFile, bool overwrite); /// /// Creates a directory. /// /// The path of the new directory. public abstract void CreateDirectory(string path); /// /// Deletes a directory. /// /// The path of the directory to delete. public abstract void DeleteDirectory(string path); /// /// Deletes a directory, optionally with all descendants. /// /// The path of the directory to delete. /// Determines if the all descendants should be deleted. public virtual void DeleteDirectory(string path, bool recursive) { if (recursive) { foreach (string dir in GetDirectories(path)) { DeleteDirectory(dir, true); } foreach (string file in GetFiles(path)) { DeleteFile(file); } } DeleteDirectory(path); } /// /// Deletes a file. /// /// The path of the file to delete. public abstract void DeleteFile(string path); /// /// Indicates if a directory exists. /// /// The path to test. /// true if the directory exists. public abstract bool DirectoryExists(string path); /// /// Indicates if a file exists. /// /// The path to test. /// true if the file exists. public abstract bool FileExists(string path); /// /// Indicates if a file or directory exists. /// /// The path to test. /// true if the file or directory exists. public virtual bool Exists(string path) { return FileExists(path) || DirectoryExists(path); } /// /// Gets the names of subdirectories in a specified directory. /// /// The path to search. /// Array of directories. public virtual string[] GetDirectories(string path) { return GetDirectories(path, "*.*", SearchOption.TopDirectoryOnly); } /// /// Gets the names of subdirectories in a specified directory matching a specified /// search pattern. /// /// The path to search. /// The search string to match against. /// Array of directories matching the search pattern. public virtual string[] GetDirectories(string path, string searchPattern) { return GetDirectories(path, searchPattern, SearchOption.TopDirectoryOnly); } /// /// Gets the names of subdirectories in a specified directory matching a specified /// search pattern, using a value to determine whether to search subdirectories. /// /// The path to search. /// The search string to match against. /// Indicates whether to search subdirectories. /// Array of directories matching the search pattern. public abstract string[] GetDirectories(string path, string searchPattern, SearchOption searchOption); /// /// Gets the names of files in a specified directory. /// /// The path to search. /// Array of files. public virtual string[] GetFiles(string path) { return GetFiles(path, "*.*", SearchOption.TopDirectoryOnly); } /// /// Gets the names of files in a specified directory. /// /// The path to search. /// The search string to match against. /// Array of files matching the search pattern. public virtual string[] GetFiles(string path, string searchPattern) { return GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly); } /// /// Gets the names of files in a specified directory matching a specified /// search pattern, using a value to determine whether to search subdirectories. /// /// The path to search. /// The search string to match against. /// Indicates whether to search subdirectories. /// Array of files matching the search pattern. public abstract string[] GetFiles(string path, string searchPattern, SearchOption searchOption); /// /// Gets the names of all files and subdirectories in a specified directory. /// /// The path to search. /// Array of files and subdirectories matching the search pattern. public abstract string[] GetFileSystemEntries(string path); /// /// Gets the names of files and subdirectories in a specified directory matching a specified /// search pattern. /// /// The path to search. /// The search string to match against. /// Array of files and subdirectories matching the search pattern. public abstract string[] GetFileSystemEntries(string path, string searchPattern); /// /// Moves a directory. /// /// The directory to move. /// The target directory name. public abstract void MoveDirectory(string sourceDirectoryName, string destinationDirectoryName); /// /// Moves a file. /// /// The file to move. /// The target file name. public virtual void MoveFile(string sourceName, string destinationName) { MoveFile(sourceName, destinationName, false); } /// /// Moves a file, allowing an existing file to be overwritten. /// /// The file to move. /// The target file name. /// Whether to permit a destination file to be overwritten. public abstract void MoveFile(string sourceName, string destinationName, bool overwrite); /// /// Opens the specified file. /// /// The full path of the file to open. /// The file mode for the created stream. /// The new stream. public virtual SparseStream OpenFile(string path, FileMode mode) { return OpenFile(path, mode, FileAccess.ReadWrite); } /// /// Opens the specified file. /// /// The full path of the file to open. /// The file mode for the created stream. /// The access permissions for the created stream. /// The new stream. public abstract SparseStream OpenFile(string path, FileMode mode, FileAccess access); /// /// Gets the attributes of a file or directory. /// /// The file or directory to inspect. /// The attributes of the file or directory. public abstract FileAttributes GetAttributes(string path); /// /// Sets the attributes of a file or directory. /// /// The file or directory to change. /// The new attributes of the file or directory. public abstract void SetAttributes(string path, FileAttributes newValue); /// /// Gets the creation time (in local time) of a file or directory. /// /// The path of the file or directory. /// The creation time. public virtual DateTime GetCreationTime(string path) { return GetCreationTimeUtc(path).ToLocalTime(); } /// /// Sets the creation time (in local time) of a file or directory. /// /// The path of the file or directory. /// The new time to set. public virtual void SetCreationTime(string path, DateTime newTime) { SetCreationTimeUtc(path, newTime.ToUniversalTime()); } /// /// Gets the creation time (in UTC) of a file or directory. /// /// The path of the file or directory. /// The creation time. public abstract DateTime GetCreationTimeUtc(string path); /// /// Sets the creation time (in UTC) of a file or directory. /// /// The path of the file or directory. /// The new time to set. public abstract void SetCreationTimeUtc(string path, DateTime newTime); /// /// Gets the last access time (in local time) of a file or directory. /// /// The path of the file or directory. /// The last access time. public virtual DateTime GetLastAccessTime(string path) { return GetLastAccessTimeUtc(path).ToLocalTime(); } /// /// Sets the last access time (in local time) of a file or directory. /// /// The path of the file or directory. /// The new time to set. public virtual void SetLastAccessTime(string path, DateTime newTime) { SetLastAccessTimeUtc(path, newTime.ToUniversalTime()); } /// /// Gets the last access time (in UTC) of a file or directory. /// /// The path of the file or directory. /// The last access time. public abstract DateTime GetLastAccessTimeUtc(string path); /// /// Sets the last access time (in UTC) of a file or directory. /// /// The path of the file or directory. /// The new time to set. public abstract void SetLastAccessTimeUtc(string path, DateTime newTime); /// /// Gets the last modification time (in local time) of a file or directory. /// /// The path of the file or directory. /// The last write time. public virtual DateTime GetLastWriteTime(string path) { return GetLastWriteTimeUtc(path).ToLocalTime(); } /// /// Sets the last modification time (in local time) of a file or directory. /// /// The path of the file or directory. /// The new time to set. public virtual void SetLastWriteTime(string path, DateTime newTime) { SetLastWriteTimeUtc(path, newTime.ToUniversalTime()); } /// /// Gets the last modification time (in UTC) of a file or directory. /// /// The path of the file or directory. /// The last write time. public abstract DateTime GetLastWriteTimeUtc(string path); /// /// Sets the last modification time (in UTC) of a file or directory. /// /// The path of the file or directory. /// The new time to set. public abstract void SetLastWriteTimeUtc(string path, DateTime newTime); /// /// Gets the length of a file. /// /// The path to the file. /// The length in bytes. public abstract long GetFileLength(string path); /// /// Gets an object representing a possible file. /// /// The file path. /// The representing object. /// The file does not need to exist. public virtual DiscFileInfo GetFileInfo(string path) { return new DiscFileInfo(this, path); } /// /// Gets an object representing a possible directory. /// /// The directory path. /// The representing object. /// The directory does not need to exist. public virtual DiscDirectoryInfo GetDirectoryInfo(string path) { return new DiscDirectoryInfo(this, path); } /// /// Gets an object representing a possible file system object (file or directory). /// /// The file system path. /// The representing object. /// The file system object does not need to exist. public virtual DiscFileSystemInfo GetFileSystemInfo(string path) { return new DiscFileSystemInfo(this, path); } /// /// Reads the boot code of the file system into a byte array. /// /// The boot code, or null if not available. public virtual byte[] ReadBootCode() { return null; } /// /// Size of the Filesystem in bytes /// public abstract long Size { get; } /// /// Used space of the Filesystem in bytes /// public abstract long UsedSpace { get; } /// /// Available space of the Filesystem in bytes /// public abstract long AvailableSpace { get; } #region IDisposable Members /// /// Disposes of this instance, releasing all resources. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Disposes of this instance. /// /// The value true if Disposing. protected virtual void Dispose(bool disposing) {} #endregion } }