/// <summary> /// Creates a new folder in the file system. /// </summary> /// <param name="parentFolderPath">The qualified name of the designated parent folder, which /// needs to exists, and provide write access.</param> /// <param name="folderName">The name of the folder to be created.</param> /// <returns>A <see cref="VirtualFileInfo"/> instance which represents /// the created folder.</returns> /// <exception cref="ArgumentNullException">If <paramref name="parentFolderPath"/> /// is a null reference.</exception> /// <exception cref="ArgumentNullException">If <paramref name="folderName"/> /// is a null reference.</exception> /// <exception cref="ResourceAccessException">In case of invalid or prohibited /// resource access.</exception> /// <exception cref="VirtualResourceNotFoundException">If no folder exists that /// matches the submitted <paramref name="parentFolderPath"/>.</exception> /// <exception cref="ResourceOverwriteException">If the folder already exists on the file /// system.</exception> public override VirtualFolderInfo CreateFolder(string parentFolderPath, string folderName) { if (parentFolderPath == null) { throw new ArgumentNullException("parentFolderPath"); } if (folderName == null) { throw new ArgumentNullException("folderName"); } string absoluteParentPath; var parent = GetFolderInfoInternal(parentFolderPath, true, out absoluteParentPath); if (RootDirectory == null && parent.IsRootFolder) { VfsLog.Debug("Blocked attempt to create a folder '{0}' at system root.", folderName); throw new ResourceAccessException("Folders cannot be created at the system root."); } //create path of the child string childPath = PathUtil.GetAbsolutePath(folderName, new DirectoryInfo(absoluteParentPath)); //make sure the folder name is not a relative path that points outside the scope if (RootDirectory != null && !RootDirectory.IsParentOf(childPath)) { string msg = "Blocked attempt to create folder outside of root through with parent '{0}' and folder name '{1}'"; VfsLog.Warn(msg, absoluteParentPath, folderName); throw new ResourceAccessException("Invalid file path: " + folderName); } var directory = new DirectoryInfo(childPath); if (directory.Exists) { //log and create exception if the directory already exists VfsLog.Debug("Blocked attempt to recreate directory '{0}'", directory.FullName); string relativePath = PathUtil.GetRelativePath(childPath, RootDirectory); string msg = String.Format("The folder '{0}' already exists.", relativePath); throw new ResourceOverwriteException(msg); } try { //create directory directory.Create(); } catch (Exception e) { const string msg = "Exception occurred when trying to create new folder '{0}' for parent '{1}'"; VfsLog.Debug(e, msg, folderName, parent.FullName); throw new ResourceAccessException("Could not create folder", e); } var folder = directory.CreateFolderResourceInfo(); //adjust and return if (UseRelativePaths) { folder.MakePathsRelativeTo(RootDirectory); } return(folder); }
/// <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); }