// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Spludlow.Io { public class FileStore { // Int64 // ROOT\1234\5678\9012\1234567890123456.dat // ROOT\1234\5678\9012\1234567890123456 // ROOT\0000\0000\0000\000000000000FFFF.dat // 4 = 65,535 // Int32 // ROOT\1234\12345678.dat // ROOT\0000\0000FFFF.dat public static string TempName(string rootPath) { return TempName(rootPath, ".tmp"); } public static string TempName(string rootPath, string extention) { string tempRoot = rootPath + @"\TEMP"; if (Directory.Exists(tempRoot) == false) Directory.CreateDirectory(tempRoot); Guid guid = Guid.NewGuid(); string tempName = tempRoot + @"\" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-") + guid.ToString() + extention; return tempName; } public static string FilePath(Int32 id, string rootPath, string extension, bool write) { return MakePath((UInt32)id, rootPath, extension, write, '\\'); } public static string FilePath(UInt32 id, string rootPath, string extension, bool write) { return MakePath(id, rootPath, extension, write, '\\'); } public static string WebPath(Int32 id, string rootPath, string extension, bool write) { return MakePath((UInt32)id, rootPath, extension, write, '/'); } public static string WebPath(UInt32 id, string rootPath, string extension, bool write) { return MakePath(id, rootPath, extension, write, '/'); } public static string FilePath(Int64 id, string rootPath, string extension, bool write) { return MakePath((UInt64)id, rootPath, extension, write, '\\'); } public static string FilePath(UInt64 id, string rootPath, string extension, bool write) { return MakePath(id, rootPath, extension, write, '\\'); } public static string WebPath(Int64 id, string rootPath, string extension, bool write) { return MakePath((UInt64)id, rootPath, extension, write, '/'); } public static string WebPath(UInt64 id, string rootPath, string extension, bool write) { return MakePath(id, rootPath, extension, write, '/'); } public static string MakePath(UInt32 id, string rootPath, string extension, bool write, char delimeter) { StringBuilder path = new StringBuilder(); path.Append(rootPath); if (path[path.Length - 1] != delimeter) path.Append(delimeter); string dir = ((id & 0xFFFF0000) >> 16).ToString("X04"); path.Append(dir); if (write == true) { string dirPath = path.ToString(); if (Directory.Exists(dirPath) == false) Directory.CreateDirectory(dirPath); } path.Append(delimeter); path.Append(id.ToString("X08")); return MakePath(path, extension, write); } public static string MakePath(UInt64 id, string rootPath, string extension, bool write, char delimeter) { StringBuilder path = new StringBuilder(); path.Append(rootPath); string[] dirs = new string[] { ((id & 0xFFFF000000000000) >> 48).ToString("X04"), ((id & 0x0000FFFF00000000) >> 32).ToString("X04"), ((id & 0x00000000FFFF0000) >> 16).ToString("X04"), }; foreach (string dir in dirs) { if (path[path.Length - 1] != delimeter) path.Append(delimeter); path.Append(dir); if (write == true) { string dirPath = path.ToString(); if (Directory.Exists(dirPath) == false) Directory.CreateDirectory(dirPath); } } path.Append(delimeter); path.Append(id.ToString("X016")); return MakePath(path, extension, write); } public static string MakePath(StringBuilder path, string extension, bool write) { if (extension == null && write == false) { string partPath = path.ToString(); string[] filenames = Directory.GetFiles(Path.GetDirectoryName(partPath), Path.GetFileName(partPath) + "*"); if (filenames.Length != 1) throw new ApplicationException("Did not find 1 file:\t" + filenames.Length + ", " + partPath); return filenames[0]; } if (extension != null && extension.Length > 0) path.Append(extension); return path.ToString(); } public static void CleanStore(uint[] currentIds, string rootPath, string extention) { int reportFreq = 1024; List currentIdList = new List(currentIds); currentIdList.Sort(); uint lastId = currentIdList[currentIdList.Count - 1]; uint deleteCount = 0; for (uint id = 0; id <= lastId; ++id) { if ((id % reportFreq) == 0) Spludlow.Log.Info("Clean Store; Progress " + id + "/" + lastId); if (currentIdList.Contains(id) == true) continue; string filename = FilePath(id, rootPath, extention, false); if (File.Exists(filename) == false) continue; File.Delete(filename); ++deleteCount; } Spludlow.Log.Finish("Clean Store; Finished, delete count: " + deleteCount); } } }