public void Init() { provider = new LocalFileSystemProvider(); FileInfo fi = new FileInfo(FileUtil.CreateTempFilePath("txt")); var writer = fi.CreateText(); writer.WriteLine("hello world."); writer.Close(); sourceFile = fi.CreateFileResourceInfo(); Assert.IsTrue(fi.Exists); targetFile = new FileInfo(FileUtil.CreateTempFilePath("txt")); }
public void Creating_Item_From_Existing_File_Should_Include_All_Information() { FileInfo fi = new FileInfo(GetType().Assembly.Location); Assert.IsTrue(fi.Exists); VirtualFileInfo item = fi.CreateFileResourceInfo(); Assert.AreEqual(fi.Name, item.Name); Assert.AreEqual(fi.FullName, item.FullName); //timestamps should match file info Assert.AreEqual((DateTimeOffset)fi.CreationTime, item.CreationTime); Assert.AreEqual((DateTimeOffset)fi.LastAccessTime, item.LastAccessTime); Assert.AreEqual((DateTimeOffset)fi.LastWriteTime, item.LastWriteTime); //length should match file Assert.AreNotEqual(0, item.Length); Assert.AreEqual(fi.Length, item.Length); }
public void Creating_Item_From_Inexisting_File_Should_Provide_Names_And_Default_Values() { FileInfo fi = new FileInfo("test.txt"); Assert.IsFalse(fi.Exists); VirtualFileInfo item = fi.CreateFileResourceInfo(); Assert.AreEqual("test.txt", item.Name); Assert.AreEqual(fi.FullName, item.FullName); //timestamps should be null Assert.IsNull(item.CreationTime); Assert.IsNull(item.LastAccessTime); Assert.IsNull(item.LastWriteTime); //the item should not be hidden/read-only Assert.IsFalse(item.IsHidden); Assert.IsFalse(item.IsReadOnly); //length should be zero Assert.AreEqual(0, item.Length); }
/// <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; }
public void Files_Should_Have_Correct_Hidden_And_Read_Only_Flags() { FileInfo fi = new FileInfo(Path.GetTempFileName()); File.SetAttributes(fi.FullName, FileAttributes.ReadOnly); Assert.IsTrue(fi.CreateFileResourceInfo().IsReadOnly); Assert.IsFalse(fi.CreateFileResourceInfo().IsHidden); File.SetAttributes(fi.FullName, fi.Attributes | FileAttributes.Hidden); Assert.IsTrue(fi.CreateFileResourceInfo().IsHidden); Assert.IsTrue(fi.CreateFileResourceInfo().IsReadOnly); //reset attribute so it can be deleted File.SetAttributes(fi.FullName, FileAttributes.Hidden); Assert.IsTrue(fi.CreateFileResourceInfo().IsHidden); Assert.IsFalse(fi.CreateFileResourceInfo().IsReadOnly); File.Delete(fi.FullName); }
protected VirtualFileInfo GetFileInfoInternal(string virtualFilePath, bool mustExist, out string absolutePath) { try { if (String.IsNullOrEmpty(virtualFilePath)) { VfsLog.Debug("File request without file path received."); throw new ResourceAccessException("An empty or null string is not a valid file path"); } //make sure we operate on absolute paths absolutePath = PathUtil.GetAbsolutePath(virtualFilePath, RootDirectory); if (IsRootPath(absolutePath)) { VfsLog.Debug("Blocked file request with path '{0}' (resolves to root directory).", virtualFilePath); throw new ResourceAccessException("Invalid path submitted: " + virtualFilePath); } var fi = new FileInfo(absolutePath); VirtualFileInfo fileInfo = fi.CreateFileResourceInfo(); //convert to relative paths if required (also prevents qualified paths in validation exceptions) if (UseRelativePaths) fileInfo.MakePathsRelativeTo(RootDirectory); //make sure the user is allowed to access the resource ValidateResourceAccess(fileInfo); //verify file exists on FS if(mustExist) fileInfo.VerifyFileExists(RootDirectory); return fileInfo; } catch(VfsException) { //just bubble internal exceptions throw; } catch (Exception e) { VfsLog.Debug(e, "Could not create file based on path '{0}' with root '{1}'", virtualFilePath, RootDirectory); throw new ResourceAccessException("Invalid path submitted: " + virtualFilePath); } }
public FileItem ResolveFileResourcePath2(string submittedFilePath, FileSystemTask context) { if (String.IsNullOrEmpty(submittedFilePath)) { throw new InvalidResourcePathException("An empty or null string is not a valid file path"); } //make sure we operate on absolute paths var absolutePath = PathUtil.GetAbsolutePath(submittedFilePath, RootDirectory); if (IsRootPath(absolutePath)) { throw new InvalidResourcePathException("Invalid path submitted: " + submittedFilePath); } var localFile = new FileInfo(absolutePath); VirtualFileInfo virtualFile = localFile.CreateFileResourceInfo(); var item = new FileItem(localFile, virtualFile); //convert to relative paths if required (also prevents qualified paths in validation exceptions) if (UseRelativePaths) item.MakePathsRelativeTo(RootDirectory); ValidateFileRequestAccess(item, submittedFilePath, context); return item; }