/// <summary> /// Открывет на чтение файловый поток /// </summary> /// <param name="name"></param> /// <param name="versionHash"></param> /// <returns></returns> public Stream Open(string name, string versionHash = null) { string datahash = null; string namehash = null; using (var o = _mapper.Open(name)) { if (string.IsNullOrWhiteSpace(versionHash)) { versionHash = o.GetHeadHash(); if (versionHash == Const.DETACHEDHEAD) { throw new Exception("head is detached"); } } var c = o.Resolve(versionHash); if (null == c) { throw new Exception("commit " + versionHash + " not resolved"); } datahash = c.Hash; namehash = o.MappingInfo.NameHash; } var binrecord = new HashedDirectoryRecord { DataHash = datahash, NameHash = namehash }; return(_hashed.Open(binrecord)); }
/// <summary> /// /// </summary> /// <param name="file"></param> /// <returns></returns> public HashedDirectoryRecord FindFirst(HashedDirectoryRecord file) { return (EnumerateFiles(file) .Where(_ => _.LastWriteTime > file.LastWriteTime) .OrderBy(_ => _.LastWriteTime) .FirstOrDefault()); }
/// <summary> /// Считывает последнюю или указанную версию файла /// </summary> /// <param name="record"></param> /// <returns></returns> public Stream Open(HashedDirectoryRecord record) { var fname = ResolveNativeFileName(record); var result = (Stream)File.Open(fname, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); if (_compress) { result = new GZipStream(result, CompressionMode.Decompress); } return(result); }
/// <summary> /// Проверка на существовании по записи /// </summary> /// <param name="record"></param> /// <returns></returns> public bool Exists(HashedDirectoryRecord record) { var dir = ConvertToHasedFileName(_rootDirectory, record.NameHash); if (string.IsNullOrWhiteSpace(record.DataHash)) { return(Directory.Exists(dir) && Directory.EnumerateFiles(dir, "*", SearchOption.AllDirectories).Any()); } var fname = ConvertToHasedFileName(dir, record.DataHash); return(File.Exists(fname)); }
/// <summary> /// Перечисляет все записи для указанного файла (хэши) /// </summary> /// <param name="file"></param> /// <returns></returns> public IEnumerable <HashedDirectoryRecord> EnumerateFiles(HashedDirectoryRecord file) { if (!Exists(file)) { yield break; } var dir = ConvertToHasedFileName(_rootDirectory, file.NameHash); foreach (var filename in Directory.EnumerateFiles(dir, "*", SearchOption.AllDirectories)) { yield return(ConvertToResultFileRecord(file, filename)); } }
private HashedDirectoryRecord ConvertToResultFileRecord(HashedDirectoryRecord file, string filename) { if (null == filename) { return(null); } var hash = ConvertToHash(filename); return(new HashedDirectoryRecord { NameHash = file.NameHash, DataHash = hash, LastWriteTime = File.GetLastWriteTime(filename) }); }
private Commit MakeCommit(string name, string basecommit, HashedDirectoryRecord saved) { Commit result; using (var o = _mapper.Open(name)) { if (string.IsNullOrWhiteSpace(basecommit)) { result = o.Commit(saved.DataHash, CommitHeadBehavior.Override); } else { result = o.Commit(saved.DataHash, CommitHeadBehavior.Auto, basecommit); } o.Commit(); } return(result); }
/// <summary> /// Возвращает реальный полный путь к файлу /// </summary> /// <param name="record"></param> /// <returns></returns> public string ResolveNativeFileName(HashedDirectoryRecord record) { string fname; var dir = ConvertToHasedFileName(_rootDirectory, record.NameHash); if (!Exists(record)) { throw new Exception("Запись не существует"); } if (string.IsNullOrWhiteSpace(record.DataHash)) { var last = FindLast(record); if (null == last) { throw new Exception("last record not found"); } fname = ConvertToHasedFileName(dir, last.DataHash); } else { fname = ConvertToHasedFileName(dir, record.DataHash); } return(fname); }