/// <summary>
        /// Function to delete a file from the file system.
        /// </summary>
        /// <param name="path">The path to the file to delete.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="path"/> is <b>null</b>.</exception>
        /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="path"/> is empty.</exception>
        /// <exception cref="FileNotFoundException">Thrown when the file referenced by the <paramref name="path"/> was not found.</exception>
        /// <remarks>
        /// <para>
        /// This will remove the <see cref="IGorgonVirtualFile"/> from the <see cref="IGorgonFileSystemWriter{T}.FileSystem"/>, and will delete the physical file from the <see cref="IGorgonFileSystemWriter{T}.WriteLocation"/> if it exists there.
        /// </para>
        /// <para>
        /// When a file is removed from the <see cref="IGorgonFileSystemWriter{T}.FileSystem"/>, it will be removed all mounted file systems. However, the actual file in the physical file systems will not be touched and the
        /// deleted file may be restored with a call to <see cref="IGorgonFileSystem.Refresh"/>.
        /// </para>
        /// <para>
        /// <note type="important">
        /// <para>
        /// When restoring files with <see cref="IGorgonFileSystem.Refresh"/>, only the file system object will be updated. The method will not restore any deleted files in the <see cref="IGorgonFileSystemWriter{T}.WriteLocation"/>.
        /// </para>
        /// </note>
        /// </para>
        /// </remarks>
        public void DeleteFile(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentEmptyException(nameof(path));
            }

            VirtualFile file = _fileSystem.InternalGetFile(path);

            if (file == null)
            {
                throw new FileNotFoundException(string.Format(Resources.GORFS_ERR_FILE_NOT_FOUND, path));
            }

            file.Directory.Files.Remove(file);
            _ramFiles.DeleteFile(file.FullPath);
        }
Esempio n. 2
0
        /// <summary>
        /// Function to open a file for reading or for writing.
        /// </summary>
        /// <param name="path">The path to the file to read/write.</param>
        /// <param name="mode">The mode to determine how to read/write the file.</param>
        /// <returns>An open <see cref="FileStream"/> to the file.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="path"/> is <b>null</b>.</exception>
        /// <exception cref="ArgumentEmptyException">Thrown when the <paramref name="path"/> is empty.
        /// <para>-or-</para>
        /// <para>Thrown when the <paramref name="path"/> does not contain a file name.</para>
        /// </exception>
        /// <exception cref="FileNotFoundException">Thrown when the file referenced by the <paramref name="path"/> was not found and the <paramref name="mode"/> is set to <see cref="FileMode.Open"/> or <see cref="FileMode.Truncate"/>.</exception>
        /// <exception cref="DirectoryNotFoundException">Thrown when the directory in the <paramref name="path"/> was not found.</exception>
        /// <remarks>
        /// <para>
        /// This will open a file for reading or writing depending on the value passed to the <paramref name="mode"/> parameter. See the <see cref="FileMode"/> enumeration for more information about how these modes
        /// affect the returned <see cref="Stream"/>.
        /// </para>
        /// <para>
        /// When the <paramref name="mode"/> parameter is set to <see cref="FileMode.Open"/>, or <see cref="FileMode.Truncate"/>, and the file does not exist in the <see cref="IGorgonFileSystemWriter{T}.FileSystem"/>, then an exception will
        /// be thrown.
        /// </para>
        /// <para>
        /// If the <paramref name="path"/> has a directory, for example <c><![CDATA[/MyDirectory/MyFile.txt]]></c>, and the directory <c><![CDATA[/MyDirectory]]></c> does not exist, an exception will be thrown.
        /// </para>
        /// </remarks>
        /// <seealso cref="FileMode"/>
        public FileStream OpenStream(string path, FileMode mode)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentEmptyException(nameof(path));
            }

            VirtualFile file = _fileSystem.InternalGetFile(path);

            // We're opening an existing file, so check it if we require the file to be present.
            if ((file == null) && ((mode == FileMode.Truncate) || (mode == FileMode.Open)))
            {
                throw new FileNotFoundException(string.Format(Resources.GORFS_ERR_FILE_NOT_FOUND, path));
            }

            if (file != null)
            {
                return(new FileSystemWriteStream(_mountPoint, GetWriteFilePath(file.Directory.FullPath, file.Name), file, mode));
            }

            PrepareWriteArea();

            string directoryPath = Path.GetDirectoryName(path);
            string fileName      = Path.GetFileName(path);

            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException(string.Format(Resources.GORFS_ERR_NO_FILENAME, path), nameof(path));
            }

            directoryPath = string.IsNullOrWhiteSpace(directoryPath) ? "/" : directoryPath.FormatDirectory('/');

            VirtualDirectory directory = _fileSystem.InternalGetDirectory(directoryPath);

            if (directory == null)
            {
                throw new DirectoryNotFoundException(string.Format(Resources.GORFS_ERR_DIRECTORY_NOT_FOUND, directoryPath));
            }

            FileStream result = null;

            try
            {
                file   = directory.Files.Add(_mountPoint, new PhysicalFileInfo(GetWriteFilePath(directoryPath, fileName), DateTime.Now, 0, directory.FullPath + fileName.FormatFileName()));
                result = new FileSystemWriteStream(_mountPoint, file.PhysicalFile.FullPath, file, mode);
            }
            catch
            {
                // If we've got an error, and the file's been added, then get rid of it.
                if ((file != null) && (directory.Files.Contains(file.FullPath)))
                {
                    directory.Files.Remove(file);
                }

                result?.Dispose();
                throw;
            }

            return(result);
        }