Пример #1
0
        private void WriteAllEntries()
        {
            try
            {
                using (Stream indexStream = new FileStream(this.indexLockPath, FileMode.Create, FileAccess.Write, FileShare.None))
                    using (BinaryWriter writer = new BinaryWriter(indexStream))
                    {
                        writer.Write(IndexHeader);

                        LsTreeEntry entry;
                        while (this.entryQueue.TryTake(out entry, millisecondsTimeout: -1))
                        {
                            this.WriteEntry(writer, entry.Sha, entry.Filename);
                        }

                        // Update entry count
                        writer.BaseStream.Position = EntryCountOffset;
                        writer.Write(EndianHelper.Swap(this.entryCount));
                        writer.Flush();
                    }

                this.AppendIndexSha();
                this.ReplaceExistingIndex();
            }
            catch (Exception e)
            {
                this.tracer.RelatedError("Failed to generate index: {0}", e.ToString());
                this.HasFailures = true;
            }
        }
Пример #2
0
        private void WriteEntry(BinaryWriter writer, string sha, string filename)
        {
            this.entryCount++;

            writer.Write(EntryHeader, 0, EntryHeader.Length);

            writer.Write(SHA1Util.BytesFromHexString(sha));

            byte[] filenameBytes = Encoding.UTF8.GetBytes(filename);
            writer.Write(EndianHelper.Swap((ushort)(filenameBytes.Length & 0xFFF)));

            writer.Write(filenameBytes);

            const long EntryLengthWithoutFilename = 62;

            // Between 1 and 8 padding bytes.
            int numPaddingBytes = 8 - ((int)(EntryLengthWithoutFilename + filenameBytes.Length) % 8);

            if (numPaddingBytes == 0)
            {
                numPaddingBytes = 8;
            }

            writer.Write(PaddingBytes, 0, numPaddingBytes);
            writer.Flush();
        }
Пример #3
0
        private void WriteEntry(BinaryWriter writer, uint version, string sha, string filename, bool skipWorktree, ref uint lastStringLength)
        {
            long startPosition = writer.BaseStream.Position;

            this.entryCount++;

            writer.Write(EntryHeader, 0, EntryHeader.Length);

            writer.Write(SHA1Util.BytesFromHexString(sha));

            byte[] filenameBytes = Encoding.UTF8.GetBytes(filename);

            ushort flags = (ushort)(filenameBytes.Length & 0xFFF);

            flags |= version >= 3 && skipWorktree ? ExtendedBit : (ushort)0;
            writer.Write(EndianHelper.Swap(flags));

            if (version >= 3 && skipWorktree)
            {
                writer.Write(EndianHelper.Swap(SkipWorktreeBit));
            }

            if (version >= 4)
            {
                this.WriteReplaceLength(writer, lastStringLength);
                lastStringLength = (uint)filenameBytes.Length;
            }

            writer.Write(filenameBytes);

            writer.Flush();
            long endPosition = writer.BaseStream.Position;

            // Version 4 requires a nul-terminated string.
            int numPaddingBytes = 1;

            if (version < 4)
            {
                // Version 2-3 has between 1 and 8 padding bytes including nul-terminator.
                numPaddingBytes = 8 - ((int)(endPosition - startPosition) % 8);
                if (numPaddingBytes == 0)
                {
                    numPaddingBytes = 8;
                }
            }

            writer.Write(PaddingBytes, 0, numPaddingBytes);

            writer.Flush();
        }
Пример #4
0
        private void WriteAllEntries(uint version, HashSet <string> sparseCheckoutEntries)
        {
            try
            {
                using (Stream indexStream = new FileStream(this.indexLockPath, FileMode.Create, FileAccess.Write, FileShare.None))
                    using (BinaryWriter writer = new BinaryWriter(indexStream))
                    {
                        writer.Write(IndexHeader);
                        writer.Write(EndianHelper.Swap(version));
                        writer.Write((uint)0); // Number of entries placeholder

                        uint        lastStringLength = 0;
                        LsTreeEntry entry;
                        while (this.entryQueue.TryTake(out entry, Timeout.Infinite))
                        {
                            bool skipWorkTree =
                                sparseCheckoutEntries != null &&
                                !sparseCheckoutEntries.Contains(entry.Filename) &&
                                !sparseCheckoutEntries.Contains(this.GetDirectoryNameForGitPath(entry.Filename));
                            this.WriteEntry(writer, version, entry.Sha, entry.Filename, skipWorkTree, ref lastStringLength);
                        }

                        // Update entry count
                        writer.BaseStream.Position = EntryCountOffset;
                        writer.Write(EndianHelper.Swap(this.entryCount));
                        writer.Flush();
                    }

                this.AppendIndexSha();
                this.ReplaceExistingIndex();
            }
            catch (Exception e)
            {
                this.tracer.RelatedError("Failed to generate index: {0}", e.ToString());
                this.HasFailures = true;
            }
        }
Пример #5
0
 public override ulong ReadUInt64()
 {
     return(EndianHelper.Swap(base.ReadUInt64()));
 }
Пример #6
0
 public override uint ReadUInt32()
 {
     return(EndianHelper.Swap(base.ReadUInt32()));
 }
Пример #7
0
 public override ushort ReadUInt16()
 {
     return(EndianHelper.Swap(base.ReadUInt16()));
 }