private byte[] GetIndexHash() { if (this.shouldHashIndex) { using (Stream fileStream = new FileStream(this.indexLockPath, FileMode.Open, FileAccess.Read, FileShare.Write)) using (HashingStream hasher = new HashingStream(fileStream)) { hasher.CopyTo(Stream.Null); return(hasher.Hash); } } return(new byte[20]); }
public virtual string WriteLooseObject(Stream responseStream, string sha, bool overwriteExistingObject, byte[] bufToCopyWith) { try { LooseObjectToWrite toWrite = this.GetLooseObjectDestination(sha); if (this.checkData) { try { using (Stream fileStream = this.OpenTempLooseObjectStream(toWrite.TempFile)) using (SideChannelStream sideChannel = new SideChannelStream(from: responseStream, to: fileStream)) using (InflaterInputStream inflate = new InflaterInputStream(sideChannel)) using (HashingStream hashing = new HashingStream(inflate)) using (NoOpStream devNull = new NoOpStream()) { hashing.CopyTo(devNull); string actualSha = SHA1Util.HexStringFromBytes(hashing.Hash); if (!sha.Equals(actualSha, StringComparison.OrdinalIgnoreCase)) { string message = $"Requested object with hash {sha} but received object with hash {actualSha}."; message += $"\nFind the incorrect data at '{toWrite.TempFile}'"; this.Tracer.RelatedError(message); throw new SecurityException(message); } } } catch (SharpZipBaseException) { string message = $"Requested object with hash {sha} but received data that failed decompression."; message += $"\nFind the incorrect data at '{toWrite.TempFile}'"; this.Tracer.RelatedError(message); throw new RetryableException(message); } } else { using (Stream fileStream = this.OpenTempLooseObjectStream(toWrite.TempFile)) { StreamUtil.CopyToWithBuffer(responseStream, fileStream, bufToCopyWith); fileStream.Flush(); } } this.FinalizeTempFile(sha, toWrite, overwriteExistingObject); return(toWrite.ActualFile); } catch (IOException e) { throw new RetryableException("IOException while writing loose object. See inner exception for details.", e); } catch (UnauthorizedAccessException e) { throw new RetryableException("UnauthorizedAccessException while writing loose object. See inner exception for details.", e); } catch (Win32Exception e) { throw new RetryableException("Win32Exception while writing loose object. See inner exception for details.", e); } }