protected FSEntry(string fullPath, FSDirectory parent, bool whatIf) { if (string.IsNullOrEmpty(fullPath)) { throw new ArgumentNullException(nameof(fullPath)); } fullPath = HelixUtil.PathUniversal(fullPath); if (!Path.IsPathRooted(fullPath)) { throw new ArgumentException("path must be rooted (ie start with c:\\)", nameof(fullPath)); } if (parent != null && !fullPath.StartsWith(parent.FullName + HelixUtil.UniversalDirectorySeparatorChar)) { throw new ArgumentException($"path must be a child of the parent (fullPath: {fullPath}, parent: {parent.FullName}", fullPath); } FullName = fullPath; Parent = parent; Root = parent?.Root ?? (this as FSDirectory); WhatIf = whatIf; }
/// <summary> /// Returns a path relative to the root, in native format /// </summary> public string PathRelative(string path) { if (path == null) { throw new ArgumentNullException(nameof(path)); } path = HelixUtil.PathUniversal(path); if (Path.IsPathRooted(path)) { path = RemoveRootFromPath(path, FullName); } return(path); }
/// <summary> /// Moves or renames the file. /// </summary> /// <param name="destinationPath">Should be an absolute path or relitive to the root</param> public FSFile MoveTo(string destinationPath) { if (string.IsNullOrEmpty(destinationPath)) { throw new ArgumentNullException(nameof(destinationPath)); } destinationPath = HelixUtil.PathUniversal(destinationPath); if (Path.IsPathRooted(destinationPath)) { destinationPath = RemoveRootFromPath(destinationPath, Root.FullName); } var destinationDirectory = Path.GetDirectoryName(destinationPath); if (Root.TryGetEntry(destinationDirectory) as FSDirectory == null) { throw new System.IO.DirectoryNotFoundException("Could not find a part of the path"); } if ((Root.TryGetEntry(destinationDirectory) as FSDirectory).TryGetEntry(Path.GetFileName(destinationPath)) != null) { throw new System.IO.DirectoryNotFoundException("Cannot move a file when that file already exists."); } var newEntry = new FSFile(HelixUtil.JoinUniversal(Root.FullName, destinationPath), Root.TryGetEntry(destinationDirectory) as FSDirectory, WhatIf); newEntry.PopulateFromInfo(this.LastWriteTimeUtc, this.Length); if (!WhatIf) { File.Move(HelixUtil.PathNative(this.FullName), HelixUtil.PathNative(Path.Combine(Root.FullName, destinationPath))); } ((IFSDirectoryCore)Parent).Remove(this); ((IFSDirectoryCore)(Root.TryGetEntry(destinationDirectory) as FSDirectory)).Add(newEntry); return(newEntry); }
/// <summary> /// Returns if the file or directory exists. /// </summary> /// <param name="path">Path can be relative to the directory or absolute</param> public bool ChildExists(string path) { path = HelixUtil.PathUniversal(path); return(TryGetEntry(path) != null); }
/// <summary> /// Forces an entry to be refreshed based of the Filesystem /// </summary> public void RefreshEntry(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } this.Load(); path = HelixUtil.PathUniversal(path); if (Path.IsPathRooted(path)) { path = RemoveRootFromPath(path, FullName); } var split = path.Split(HelixUtil.UniversalDirectorySeparatorChar); FSEntry newEntry; FileSystemInfo info; string fullPath = HelixUtil.JoinUniversal(this.FullName, split[0]); if (System.IO.Directory.Exists(fullPath)) { info = new DirectoryInfo(fullPath); } else if (System.IO.File.Exists(fullPath)) { info = new FileInfo(fullPath); } else { info = null; } if (info is DirectoryInfo dirInfo) { newEntry = new FSDirectory(dirInfo, this, this.WhatIf, isRoot: false); } else if (info is FileInfo fileInfo) { newEntry = new FSFile(fileInfo, this, this.WhatIf); } else { newEntry = null; //not found } children.TryGetValue(split[0], out FSEntry oldEntry); if (newEntry?.EntryType != oldEntry?.EntryType) { if (oldEntry != null) { children.Remove(oldEntry); } if (newEntry != null) { children.Add(newEntry); } } else if (newEntry != null && oldEntry != null) { oldEntry.PopulateFromInfo(newEntry.LastWriteTimeUtc, newEntry.Length); } else if (newEntry != null) { children.Add(newEntry); if (split.Length > 1) { (newEntry as FSDirectory)?.RefreshEntry(HelixUtil.JoinUniversal(split.Skip(1).ToArray())); } } }