internal void WhatIfAddFile(string relativeName, long fileSize) { if (!WhatIf) { throw new InvalidOperationException("FSDirectory not in WhatIf mode"); } if (!Exists) { throw new DirectoryNotFoundException(); } FSFile entry = new FSFile(PathFull(relativeName), GetDirectory(PathDirectory(relativeName)), this.WhatIf); //entry.Length = fileSize; entry.Parent.children.Add(entry); }
internal void WhatIfReplaceFile(string relativeName, long fileSize, DateTime lastWriteTimeUtc = default) { if (!WhatIf) { throw new InvalidOperationException("FSDirectory not in WhatIf mode"); } if (!Exists) { throw new DirectoryNotFoundException(); } (this.TryGetEntry(relativeName) as FSFile)?.Delete(); FSFile entry = new FSFile(PathFull(relativeName), GetDirectory(PathDirectory(relativeName)), this.WhatIf) { LastWriteTimeUtc = lastWriteTimeUtc }; //entry.Length = fileSize; entry.Parent.children.Add(entry); }
/// <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> /// 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())); } } }