public FileWhiteList() { _whiteListedFiles = new SortedList <string, string>(); //Ensure whitelist file exists if (File.Exists(WHITE_LIST_PATH) == false) { //File doesn't exist. Create it File.Create(WHITE_LIST_PATH).Close(); } string[] files = File.ReadAllLines(WHITE_LIST_PATH); Console.WriteLine("Loading " + files.Length + " whitelisted files"); //Clean file //The file is rewritten every time to ensure the files still exist File.WriteAllText(WHITE_LIST_PATH, string.Empty); for (int i = 0; i < files.Length; i++) { //If the item is succsfully added append it to the file //Make temp item DeletableItem item = DeletableItem.Make(files[i], 0); AddToWhiteList(item); } }
public void RunCleaner() { //Run unless we need to stop while (_stopCleaning == false) { //Check if cleaner should stop and again ask later if (_askLater) { Console.WriteLine("Asking later will take " + _config.BreakTime + " minute break!"); Thread.Sleep(1000 * 60 * _config.BreakTime); _askLater = false; } Console.WriteLine("Does anything need to be cleaned?"); //Search for new files SearchForFiles(); //Run through every item to see if it should be deleted List <DeletableItem> deletionList = new List <DeletableItem>(); bool deletingAFile = false; int combiningTime = 0; for (int index = 0; index < _itemsForDeletion.Count; index++) { DeletableItem item = _itemsForDeletion[index]; Console.WriteLine("Considering " + item); if (item.ShouldBeDeleted(combiningTime) && deletingAFile == false) { Console.WriteLine("Should be deleted!"); //Atleast one file must be deleted, so redo the search with a broader time search //to avoid spamming the user deletingAFile = true; combiningTime = _config.CombineTime; //Restart search index = -1; continue; } if (item.ShouldBeDeleted(combiningTime)) { deletionList.Add(item); } } //Clean those files Clean(deletionList); //Only run every so many seconds Console.WriteLine("Waiting on cleaner... " + _itemsForDeletion.Count + " possible files for cleaning"); Thread.Sleep(CLEANER_RUN_INTERVAL * 1000); } //Reset lists and whitelist _itemsForDeletion = new List <DeletableItem>(); _whiteList = new FileWhiteList(); }
public void AddToWhiteList(DeletableItem item) { if (item == null) { return; } //Ensure item actually exists if (item.Exists()) { Console.WriteLine("Added " + item.Path + " to whitelist"); _whiteListedFiles.Add(item.Path, item.Path); File.AppendAllText(WHITE_LIST_PATH, item.Path + "\r\n"); } }
/// <summary> /// Searches for all old files that are ready to be deleted /// </summary> /// <param name="delayedStart">If an item should be deleted now, what time should it be delayed to</param> public void SearchForFiles() { //Search through all directories for (int i = 0; i < _config.SearchedDirectories.Count; i++) { SearchedDirectory searchedDirectory = _config.SearchedDirectories[i]; int deletionTime = _config.SearchedDirectories[i].FileAgeLimit; //Ensure directory exists if (Directory.Exists(searchedDirectory.Path) == false) { continue; } //Find all files in directory DeletableItem[] files = Directory.GetFiles(searchedDirectory.Path, "*").Select(s => DeletableItem.Make(s, deletionTime)).ToArray(); //Search for directories as well DeletableItem[] directories = Directory.GetDirectories(searchedDirectory.Path, "*", SearchOption.TopDirectoryOnly).Select(s => DeletableItem.Make(s, deletionTime)).ToArray(); //Combine files and directories into one array DeletableItem[] itemsInDirectory = new DeletableItem[files.Length + directories.Length]; files.CopyTo(itemsInDirectory, 0); directories.CopyTo(itemsInDirectory, files.Length); for (int j = 0; j < itemsInDirectory.Length; j++) { DeletableItem item = itemsInDirectory[j]; //Ensure file is not whitelisted or already is in cleaner if (_whiteList.IsInWhiteList(item) || _itemsForDeletion.Contains(item)) { continue; } DateTime timeToDelete = item.DeleteTime; Console.WriteLine(item.Path + " is " + (timeToDelete - DateTime.UtcNow).TotalMinutes + " minutes off, deleted: " + item.ShouldBeDeleted()); _itemsForDeletion.Add(item); } } }
public bool IsInWhiteList(DeletableItem item) { return(_whiteListedFiles.ContainsKey(item.Path)); }