/// <summary> /// Read some of a file and return as a string. /// </summary> /// <param name="hash">Hash to look up.</param> /// <param name="sampleSize">Amount of content to return.</param> /// <returns>Byte array representing the object.</returns> public static string Read(Span <byte> hash, uint sampleSize) { if (!IsValid(hash)) { throw new SignatureException("Hash mismatch"); } var fullPath = NitPath.GetFullObjectPath(hash); if (!File.Exists(fullPath)) { // already exists, don't bother return(null); } var buffer = new char[sampleSize]; var bufferSpan = (Span <char>)buffer; using (var sr = new StreamReader(fullPath)) { sr.ReadBlock(bufferSpan); } return(bufferSpan.ToString()); }
/// <summary> /// Gets dictionary of all blob identities to number of keyword hits. /// </summary> /// <param name="tags">Tags to look for.</param> /// <returns>Dictionary of blob hash identities to kit count.</returns> public static Dictionary <byte[], uint> GetKeywordDictionary(string[] tags) { Guard.ThrowIfEmpty(tags, nameof(tags)); var result = new Dictionary <byte[], uint>(new ByteArrayCompare()); var buffer = new byte[Hash.Length]; foreach (var tag in tags) { var tagHash = Hash.HashString(tag.ToUpper()); var tagPath = NitPath.GetFullTagPath(tagHash); if (File.Exists(tagPath)) { HashFileReader.Read(tagPath, (input) => { // if not there, add it with cnt 1 if (!result.TryAdd(input, 1)) { // it already exists, so increment result[input]++; } return(true); }); } } return(result); }
/// <summary> /// Check if file is valid. /// </summary> /// <param name="hash">The file's hash.</param> /// <returns>If the hash still matches.</returns> public static bool IsValid(Span <byte> hash) { var fullPath = NitPath.GetFullObjectPath(hash); var hashCheck = Hash.HashFile(fullPath); return(hash.SequenceEqual(hashCheck)); }
/// <summary> /// Create tags for a give hash code. /// </summary> /// <param name="hash">A hash to associate tags with.</param> /// <param name="tags">The tags to associate.</param> public static void CreateTags(Span <byte> hash, string[] tags) { foreach (var tag in tags) { var tagHash = Hash.HashString(tag.ToUpper()); var fullPath = NitPath.GetFullTagPath(tagHash); var directoryPath = NitPath.GetTagDirectoryPath(tagHash); if (!File.Exists(fullPath)) { Directory.CreateDirectory(directoryPath); using (var s = File.CreateText(fullPath)) { var l = hash.GetHexString(); s.WriteLine(l); } continue; } // need a value type for lambda byte[] temp = hash.ToArray(); // scan hashes in the file and stop when a match is found HashFileReader.Read(fullPath, (input) => !((Span <byte>)temp).SequenceEqual((Span <byte>)input)); // Add the entry to the end var line = hash.GetHexString(); File.AppendAllLines(fullPath, new List <string>() { line }); } }
/// <summary> /// Write a blob to the object index. /// </summary> /// <param name="blob">Blob to write.</param> /// <returns>Hashed blob.</returns> public static Span <byte> Write(Span <byte> blob) { var hash = Hash.HashObject(blob); var blobPath = NitPath.GetFullObjectPath(hash); // ensure folders exist var blobFolder = NitPath.GetObjectDirectoryPath(hash); Directory.CreateDirectory(blobFolder); File.WriteAllBytes(blobPath, blob.ToArray()); return(hash); }
/// <summary> /// Read the entry for a given hash. /// </summary> /// <param name="hash">Hash to look up.</param> /// <returns>Byte array representing the object.</returns> public static Span <byte> Read(Span <byte> hash) { if (!IsValid(hash)) { throw new SignatureException("Hash mismatch"); } var fullPath = NitPath.GetFullObjectPath(hash); if (!File.Exists(fullPath)) { // already exists, don't bother return(null); } return(File.ReadAllBytes(fullPath)); }
/// <summary> /// Copy source file to object index. /// </summary> /// <param name="targetPath">Path to write blob.</param> /// <returns>Hash of blob written.</returns> public static Span <byte> Write(string targetPath) { if (!File.Exists(targetPath)) { throw new ArgumentException("Target does not exist."); } var hash = Hash.HashFile(targetPath); var fullPath = NitPath.GetFullObjectPath(hash); var dirPath = NitPath.GetObjectDirectoryPath(hash); if (File.Exists(fullPath)) { // already exists, don't bother return(hash); } // ensure the folders exist Directory.CreateDirectory(dirPath); // copy the file, using the hex hash as the filename File.Copy(targetPath, fullPath); return(hash); }