private static void IndexDirectory(PackageDirectory dir, string directory) { dir.locations = new Dictionary<string, FileLocation>(); var indexFile = Path.Combine(directory, ".shaman-blob-index"); using (var stream = File.Open(indexFile + "$", FileMode.Create, FileAccess.Write, FileShare.Delete)) using (var bw = new BinaryWriter(stream, Encoding.UTF8)) { string prevPackage = null; foreach (var item in EnumerateFiles(directory)) { if (prevPackage != item.PackageFileName) { prevPackage = item.PackageFileName; bw.Write((byte)1); bw.Write(item.PackageFileName ?? string.Empty); } else { bw.Write((byte)0); } bw.Write(item.Name); bw.Write(item.DataStartOffset); dir.locations[item.Name] = new FileLocation() { BlobName = item.Name, DataStartOffset = item.DataStartOffset, PackageFileName = item.PackageFileName }; } } File.Delete(indexFile); File.Move(indexFile + "$", indexFile); }
private static void IndexDirectory(PackageDirectory dir, string directory) { dir.locations = new Dictionary <string, FileLocation>(); var indexFile = Path.Combine(directory, ".shaman-blob-index"); using (var stream = File.Open(indexFile + "$", FileMode.Create, FileAccess.Write, FileShare.Delete)) using (var bw = new BinaryWriter(stream, Encoding.UTF8)) { string prevPackage = null; foreach (var item in EnumerateFiles(directory, includeDeleted: false, orderByDate: false, singlePackage: null, allowIndex: false)) { if (prevPackage != item.PackageFileName) { prevPackage = item.PackageFileName; bw.Write((byte)1); bw.Write(item.PackageFileName ?? string.Empty); } else { bw.Write((byte)0); } bw.Write((byte)0); var length = (uint)item.Length.Value; if (item.LastWriteTime != null) { length |= (uint)1 << 31; } bw.Write(length); if (item.LastWriteTime != null) { bw.Write(item.LastWriteTime.Value.Ticks); } bw.Write(item.Name); bw.Write(item.DataStartOffset); dir.locations[item.Name] = new FileLocation() { BlobName = item.Name, DataStartOffset = item.DataStartOffset, PackageFileName = item.PackageFileName, Date = item.LastWriteTime, Length = item.Length.Value }; } } File.Delete(indexFile); File.Move(indexFile + "$", indexFile); }
// must hold lock private static PackageDirectory GetDirectory(string dirpath) { Debug.Assert(dirpath[0] == '/' || dirpath[1] == ':'); dirpath = dirpath.TrimEnd(trimChars); PackageDirectory dir; directories.TryGetValue(dirpath, out dir); if (dir == null) { dir = new PackageDirectory(); dir.path = dirpath; directories[dirpath] = dir; var indexFile = Path.Combine(dirpath, ".shaman-blob-index"); if (File.Exists(indexFile)) { using (var stream = File.Open(indexFile, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.Read)) using (var br = new BinaryReader(stream, Encoding.UTF8)) { dir.locations = new Dictionary<string, FileLocation>(); string package = null; while (true) { var tag = stream.ReadByte(); if (tag == -1) break; if (tag == 1) { package = br.ReadString(); } else if (tag != 0) throw new InvalidDataException(); var name = br.ReadString(); var offset = br.ReadInt32(); if (offset == -1) { dir.locations.Remove(name); } else { dir.locations[name] = new FileLocation() { BlobName = name, DataStartOffset = offset, PackageFileName = package, }; } } } } else { dir.locations = new Dictionary<string, FileLocation>(); if ( #if NET35 Directory.GetFiles( #else Directory.EnumerateFiles( #endif dirpath, "*.shaman-blobs" ).Any()) { IndexDirectory(dir, dirpath); } } } return dir; }
// must hold lock private static PackageDirectory GetDirectory(string dirpath) { Debug.Assert(dirpath[0] == '/' || dirpath[1] == ':'); dirpath = dirpath.TrimEnd(trimChars); PackageDirectory dir; directories.TryGetValue(dirpath, out dir); if (dir == null) { dir = new PackageDirectory(); dir.path = dirpath; directories[dirpath] = dir; var indexFile = Path.Combine(dirpath, ".shaman-blob-index"); if (File.Exists(indexFile)) { using (var stream = File.Open(indexFile, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.Read)) using (var br = new BinaryReader(stream, Encoding.UTF8)) { dir.locations = new Dictionary <string, FileLocation>(); string package = null; while (true) { var tag = stream.ReadByte(); if (tag == -1) { break; } if (tag == 1) { package = br.ReadString(); } else if (tag != 0) { throw new InvalidDataException(); } DateTime?date = null; var name = br.ReadString(); int? length = null; if (name.Length == 0) { uint len = br.ReadUInt32(); var hasDate = (len & (1 << 31)) != 0; length = (int)(len & ~(1 << 31)); if (hasDate) { date = new DateTime(br.ReadInt64(), DateTimeKind.Utc); } name = br.ReadString(); } var offset = br.ReadInt32(); if (offset == -1) { dir.locations.Remove(name); } else { dir.locations[name] = new FileLocation() { BlobName = name, DataStartOffset = offset, PackageFileName = package, Date = date, Length = length }; } } } } else { dir.locations = new Dictionary <string, FileLocation>(); if ( #if NET35 Directory.GetFiles( #else Directory.EnumerateFiles( #endif dirpath, "*.shaman-blobs" ).Any()) { IndexDirectory(dir, dirpath); } } } return(dir); }