/// <summary> /// Resolves the parent folder for a virtual resource. /// </summary> /// <param name="child">The child file or folder.</param> /// <returns>The parent folder info.</returns> private VirtualFolderInfo GetParentInternal(VirtualResourceInfo child) { if (child == null) { throw new ArgumentNullException("child"); } string path = PathUtil.GetAbsolutePath(child.FullName, RootDirectory); if (IsRootPath(path)) { //only log the request for the root's parent - do not include that //info in an exception in case we're hiding path information string msg = "Error while requesting parent of resource {0} - the folder itself already is the root."; VfsLog.Error(msg, child.FullName); //create exception with a relative path, if required string exceptionPath = UseRelativePaths ? PathUtil.RelativeRootPrefix : child.FullName; msg = String.Format(msg, exceptionPath); throw new ResourceAccessException(msg); } //make sure the processed directory exists VirtualFolderInfo folder = child as VirtualFolderInfo; if (folder != null) { folder.VerifyDirectoryExists(RootDirectory); } else { VirtualFileInfo file = (VirtualFileInfo)child; file.VerifyFileExists(RootDirectory); } //get the path of the parent (returned value may be null!) string parentPath = Path.GetDirectoryName(path); //get folder info return(GetFolderInfo(parentPath)); }
/// <summary> /// Gets the binary contents as a stream in a blocking operation. /// Use the methods in <see cref="ContentUtil"/> class for simplified stream /// handling. /// </summary> /// <param name="virtualFilePath">The path of the file to be read.</param> /// <returns>A stream that allows the contents of the file to be read.</returns> /// <exception cref="ArgumentNullException">If <paramref name="virtualFilePath"/> /// is a null reference.</exception> /// <exception cref="VirtualResourceNotFoundException">If the file that is represented /// by <paramref name="virtualFilePath"/> does not exist in the file system.</exception> /// <exception cref="ResourceAccessException">In case of invalid or prohibited /// resource access.</exception> public override Stream ReadFileContents(string virtualFilePath) { if (virtualFilePath == null) { throw new ArgumentNullException("virtualFilePath"); } string absolutePath; GetFileInfoInternal(virtualFilePath, true, out absolutePath); try { return(File.OpenRead(absolutePath)); } catch (Exception e) { VfsLog.Error(e, "Could not open file [{0}] requested through virtual file path [{1}]", absolutePath, virtualFilePath); string msg = String.Format("An error occurred while attempting to open file [{0}].", virtualFilePath); throw new ResourceAccessException(msg, e); } }
/// <summary> /// Creates or updates a given file resource in the file system. /// </summary> /// <param name="parentFolderPath">The qualified path of the parent folder that will /// contain the file.</param> /// <param name="fileName">The name of the file to be created.</param> /// <param name="input">A stream that provides the file's contents.</param> /// <param name="overwrite">Whether an existing file should be overwritten /// or not. If this parameter is false and the file already exists, a /// <see cref="ResourceOverwriteException"/> is thrown.</param> /// <exception cref="VirtualResourceNotFoundException">If the parent folder /// does not exist.</exception> /// <exception cref="ResourceAccessException">In case of invalid or prohibited /// resource access.</exception> /// <exception cref="ResourceOverwriteException">If a file already exists at the /// specified location, and the <paramref name="overwrite"/> flag was not set.</exception> /// <exception cref="ArgumentNullException">If any of the parameters is a null reference.</exception> public override VirtualFileInfo WriteFile(string parentFolderPath, string fileName, Stream input, bool overwrite) { if (parentFolderPath == null) { throw new ArgumentNullException("parentFolderPath"); } if (fileName == null) { throw new ArgumentNullException("fileName"); } if (input == null) { throw new ArgumentNullException("input"); } //get the parent and make sure it exists string absoluteParentPath; var parent = GetFolderInfoInternal(parentFolderPath, true, out absoluteParentPath); if (RootDirectory == null && parent.IsRootFolder) { VfsLog.Debug("Blocked attempt to create a file '{0}' at system root (which is the machine itself - no root directory was set).", fileName); throw new ResourceAccessException("Files cannot be created at the system root."); } //combine to file path and get virtual file (also makes sure we don't get out of scope) string absoluteFilePath = PathUtil.GetAbsolutePath(fileName, new DirectoryInfo(absoluteParentPath)); FileInfo fi = new FileInfo(absoluteFilePath); if (fi.Exists && !overwrite) { VfsLog.Debug("Blocked attempt to overwrite file '{0}'", fi.FullName); string msg = String.Format("The file [{0}] already exists.", fileName); throw new ResourceOverwriteException(msg); } try { using (Stream writeStream = new FileStream(fi.FullName, FileMode.Create, FileAccess.Write, FileShare.None)) { input.WriteTo(writeStream); } } catch (Exception e) { //log exception with full path string msg = "Could not write write submitted content to file '{0}'."; VfsLog.Error(e, msg, fi.FullName); //generate exception with relative path msg = String.Format(msg, PathUtil.GetRelativePath(fi.FullName, RootDirectory)); throw new ResourceAccessException(msg, e); } //return update file info var file = fi.CreateFileResourceInfo(); //adjust and return if (UseRelativePaths) { file.MakePathsRelativeTo(RootDirectory); } return(file); }