/// <summary> /// Updates the 'folders' dictionary to match the folders that exist on disk. ONLY UPDATES THE LOCAL FOLDER /// </summary> /// <param name="relativePath"></param> /// <param name="physicalPath"></param> protected void populateSubfolders(string relativePath, string physicalPath) { relativePath = checkRelativePath(relativePath); string[] dirs = null; try { dirs = System.IO.Directory.GetDirectories(physicalPath); } catch (DirectoryNotFoundException) { dirs = new string[] {}; //Pretend it's empty. We don't care, the next recursive will get rid of it. } lock (_sync) { CachedFolder f = getOrCreateFolder(relativePath, true); Dictionary <string, CachedFolder> newFolders = new Dictionary <string, CachedFolder>(dirs.Length, KeyComparer); foreach (string s in dirs) { string local = s.Substring(s.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1); if (local.StartsWith(".")) { continue; //Skip folders that start with a period. } if (f.folders.ContainsKey(local)) { newFolders[local] = f.folders[local]; //What if the value is null? does containskey work? } else { newFolders[local] = new CachedFolder(); } } f.folders = newFolders; //Question - why didn't the folders ge tlisted? } }
/// <summary> /// returns a dictionary of files. /// </summary> /// <param name="relativePath"></param> /// <returns></returns> public ICollection <KeyValuePair <string, CachedFileInfo> > getSortedSubfiles(string relativePath) { lock (_sync) { CachedFolder f = getFolder(relativePath); if (f == null || f.files.Count < 1) { return(null); } //Copy pairs to an array. KeyValuePair <string, CachedFileInfo>[] items = new KeyValuePair <string, CachedFileInfo> [f.files.Count]; int i = 0; foreach (KeyValuePair <string, CachedFileInfo> pair in f.files) { items[i] = pair; i++; } //Sort the pairs on accessed date Array.Sort <KeyValuePair <string, CachedFileInfo> >(items, delegate(KeyValuePair <string, CachedFileInfo> a, KeyValuePair <string, CachedFileInfo> b) { return(DateTime.Compare(a.Value.AccessedUtc, b.Value.AccessedUtc)); }); return(items); } }
public int getFileCount(string relativePath) { lock (_sync) { CachedFolder f = getFolder(relativePath); return(f.files.Count); } }
/// <summary> /// returns a list /// </summary> /// <param name="relativePath"></param> /// <returns></returns> public IList <string> getSubfolders(string relativePath) { lock (_sync) { CachedFolder f = getFolder(relativePath); return(new List <string>(f.folders.Keys)); } }
/// <summary> /// returns a dictionary of files. /// </summary> /// <param name="relativePath"></param> /// <returns></returns> public Dictionary <string, CachedFileInfo> getSubfilesCopy(string relativePath) { lock (_sync) { CachedFolder f = getFolder(relativePath); return(new Dictionary <string, CachedFileInfo>(f.files, f.files.Comparer)); } }
/// <summary> /// Tries to set the AccessedUtc of the specified file to the current date (just in memory, not on the filesystem). /// </summary> /// <param name="relativePath"></param> /// <returns></returns> public bool bumpDateIfExists(string relativePath) { relativePath = checkRelativePath(relativePath); lock (_sync) { int slash = relativePath.IndexOf('/'); if (slash < 0) { //Update the accessed date. CachedFileInfo old; if (files.TryGetValue(relativePath, out old)) { files[relativePath] = new CachedFileInfo(old, DateTime.UtcNow); } return(true); //We updated it! } else { //Try to access subfolder string folder = relativePath.Substring(0, slash); CachedFolder f = null; if (!folders.TryGetValue(folder, out f)) { return(false); //If the folder doesn't exist, quit } if (f == null) { return(false); //If the folder is null, quit! } //Recurse if possible return(f.bumpDateIfExists(relativePath.Substring(slash + 1))); } } }
/// <summary> /// Returns the value of IsValid on the specified folder if present, or 'false' if not present. /// </summary> /// <param name="relativePath"></param> /// <returns></returns> public bool GetIsValid(string relativePath) { lock (_sync) { CachedFolder f = getFolder(relativePath); if (f != null) { return(f.IsValid); } return(false); } }
protected CachedFolder getOrCreateFolder(string relativePath, bool createIfMissing) { relativePath = checkRelativePath(relativePath); if (string.IsNullOrEmpty(relativePath)) { return(this); } int slash = relativePath.IndexOf('/'); string folder = relativePath; if (slash > -1) { folder = relativePath.Substring(0, slash); relativePath = relativePath.Substring(slash + 1); } else { relativePath = ""; } CachedFolder f; if (!folders.TryGetValue(folder, out f)) { if (!createIfMissing) { return(null); } else { f = folders[folder] = new CachedFolder(); } } //Recurse if possible if (f != null) { return(f.getFolder(relativePath)); } //Not found return(null); }
/// <summary> /// Sets the CachedFileInfo object for the specified path, creating any needed folders along the way. /// If 'null', the item will be removed, and no missing folder will be created. /// </summary> /// <param name="relativePath"></param> /// <param name="info"></param> public virtual void setCachedFileInfo(string relativePath, CachedFileInfo info) { relativePath = checkRelativePath(relativePath); lock (_sync) { int slash = relativePath.IndexOf('/'); if (slash < 0) { //Set or remove the file if (info == null) { files.Remove(relativePath); } else { files[relativePath] = info; } } else { //Try to access subfolder string folder = relativePath.Substring(0, slash); CachedFolder f; if (!folders.TryGetValue(folder, out f)) { f = null; } if (info == null && f == null) { return; //If the folder doesn't exist, the file definitely doesn't. Already accomplished. } //Create it if it doesn't exist if (f == null) { f = folders[folder] = new CachedFolder(); } //Recurse if possible f.setCachedFileInfo(relativePath.Substring(slash + 1), info); } } }
/// <summary> /// Updates the 'files' dictionary to match the files that exist on disk. Uses the accessedUtc values from the previous dictionary if they are newer. /// </summary> /// <param name="relativePath"></param> /// <param name="physicalPath"></param> protected void populateFiles(string relativePath, string physicalPath) { relativePath = checkRelativePath(relativePath); string[] physicalFiles = null; try { physicalFiles = System.IO.Directory.GetFiles(physicalPath); } catch (DirectoryNotFoundException) { physicalFiles = new string[] { }; //Pretend it's empty. We don't care, the next recursive will get rid of it. } Dictionary <string, CachedFileInfo> newFiles = new Dictionary <string, CachedFileInfo>(physicalFiles.Length, KeyComparer); CachedFolder f = getOrCreateFolder(relativePath, true); foreach (string s in physicalFiles) { string local = s.Substring(s.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1); //Todo, add a callback that handles exclusion of files if (local.EndsWith(".config", StringComparison.OrdinalIgnoreCase)) { continue; } if (local.StartsWith(".")) { continue; //Skip files that start with a period } //What did we have on file? CachedFileInfo old = null; lock (_sync) { if (!f.files.TryGetValue(relativePath, out old)) { old = null; } } newFiles[local] = new CachedFileInfo(new FileInfo(s), old); } lock (_sync) { f.files = newFiles; } }
/// <summary> /// Updates the 'folders' dictionary to match the folders that exist on disk. ONLY UPDATES THE LOCAL FOLDER /// </summary> /// <param name="relativePath"></param> /// <param name="physicalPath"></param> protected void populateSubfolders(string relativePath, string physicalPath) { relativePath = checkRelativePath(relativePath); string[] dirs = null; try { dirs = System.IO.Directory.GetDirectories(physicalPath); } catch (DirectoryNotFoundException) { dirs = new string[]{}; //Pretend it's empty. We don't care, the next recursive will get rid of it. } lock (_sync) { CachedFolder f = getOrCreateFolder(relativePath, true); Dictionary<string, CachedFolder> newFolders = new Dictionary<string, CachedFolder>(dirs.Length, KeyComparer); foreach (string s in dirs) { string local = s.Substring(s.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1); if (local.StartsWith(".")) continue; //Skip folders that start with a period. if (f.folders.ContainsKey(local)) newFolders[local] = f.folders[local]; //What if the value is null? does containskey work? else newFolders[local] = new CachedFolder(); } f.folders = newFolders; //Question - why didn't the folders ge tlisted? } }
protected CachedFolder getOrCreateFolder(string relativePath, bool createIfMissing) { relativePath = checkRelativePath(relativePath); if (string.IsNullOrEmpty(relativePath)) return this; int slash = relativePath.IndexOf('/'); string folder = relativePath; if (slash > -1) { folder = relativePath.Substring(0, slash); relativePath = relativePath.Substring(slash + 1); } else relativePath = ""; CachedFolder f; if (!folders.TryGetValue(folder, out f)) { if (!createIfMissing) return null; else f = folders[folder] = new CachedFolder(); } //Recurse if possible if (f != null) return f.getFolder(relativePath); //Not found return null; }
/// <summary> /// Sets the CachedFileInfo object for the specified path, creating any needed folders along the way. /// If 'null', the item will be removed, and no missing folder will be created. /// </summary> /// <param name="relativePath"></param> /// <param name="info"></param> public virtual void setCachedFileInfo(string relativePath, CachedFileInfo info) { relativePath = checkRelativePath(relativePath); lock (_sync) { int slash = relativePath.IndexOf('/'); if (slash < 0) { //Set or remove the file if (info == null) files.Remove(relativePath); else files[relativePath] = info; } else { //Try to access subfolder string folder = relativePath.Substring(0, slash); CachedFolder f; if (!folders.TryGetValue(folder, out f)) f = null; if (info == null && f == null) return; //If the folder doesn't exist, the file definitely doesn't. Already accomplished. //Create it if it doesn't exist if (f == null) f = folders[folder] = new CachedFolder(); //Recurse if possible f.setCachedFileInfo(relativePath.Substring(slash + 1), info); } } }