Esempio n. 1
0
        private void WriteTo(Stream os)
        {
            MessageDigest foot = Constants.newMessageDigest();
            var           dos  = new DigestOutputStream(os, foot);

            // Write the header.
            //
            var tmp = new byte[128];

            Array.Copy(SigDirc, 0, tmp, 0, SigDirc.Length);
            NB.encodeInt32(tmp, 4, /* version */ 2);
            NB.encodeInt32(tmp, 8, _entryCnt);
            dos.Write(tmp, 0, 12);

            // Write the individual file entries.
            //
            if (_lastModified <= 0)
            {
                // Write a new index, as no entries require smudging.
                //
                for (int i = 0; i < _entryCnt; i++)
                {
                    _sortedEntries[i].write(dos);
                }
            }
            else
            {
                int smudge_s  = (int)(_lastModified / 1000);
                int smudge_ns = ((int)(_lastModified % 1000)) * 1000000;
                for (int i = 0; i < _entryCnt; i++)
                {
                    DirCacheEntry e = _sortedEntries[i];
                    if (e.mightBeRacilyClean(smudge_s, smudge_ns))
                    {
                        e.smudgeRacilyClean();
                    }
                    e.write(dos);
                }
            }

            if (_cacheTree != null)
            {
                var bb = new LocalFileBuffer();
                _cacheTree.write(tmp, bb);
                bb.close();

                NB.encodeInt32(tmp, 0, ExtTree);
                NB.encodeInt32(tmp, 4, (int)bb.Length);
                dos.Write(tmp, 0, 8);
                bb.writeTo(dos, null);
            }
            var hash = foot.Digest();

            os.Write(hash, 0, hash.Length);
            os.Close();
        }
Esempio n. 2
0
        private void WriteTo(Stream os)
        {
            MessageDigest foot = Constants.newMessageDigest();
            var           dos  = new DigestOutputStream(os, foot);

            // Write the header.
            //
            var tmp = new byte[128];

            Array.Copy(SigDirc, 0, tmp, 0, SigDirc.Length);
            NB.encodeInt32(tmp, 4, /* version */ 2);
            NB.encodeInt32(tmp, 8, _entryCnt);
            dos.Write(tmp, 0, 12);

            // Write the individual file entries.
            //
            if (_lastModified == DateTime.MinValue)
            {
                // Write a new index, as no entries require smudging.
                //
                for (int i = 0; i < _entryCnt; i++)
                {
                    _sortedEntries[i].write(dos);
                }
            }
            else
            {
                var smudge_s  = _lastModified.ToUnixTime();
                var smudge_ns = _lastModified.Millisecond * 1000000;                 // [henon] <--- this could be done with much more precision in C# since DateTime has 100 nanosec ticks
                for (int i = 0; i < _entryCnt; i++)
                {
                    DirCacheEntry e = _sortedEntries[i];
                    if (e.mightBeRacilyClean(smudge_s, smudge_ns))
                    {
                        e.smudgeRacilyClean();
                    }
                    e.write(dos);
                }
            }

            if (_cacheTree != null)
            {
                var bb = new TemporaryBuffer();
                _cacheTree.write(tmp, bb);
                bb.close();

                NB.encodeInt32(tmp, 0, ExtTree);
                NB.encodeInt32(tmp, 4, (int)bb.Length);
                dos.Write(tmp, 0, 8);
                bb.writeTo(dos, null);
            }
            var hash = foot.Digest();

            os.Write(hash, 0, hash.Length);
            os.Close();
        }
Esempio n. 3
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void WriteTo(OutputStream os)
        {
            MessageDigest      foot = Constants.NewMessageDigest();
            DigestOutputStream dos  = new DigestOutputStream(os, foot);
            bool extended           = false;

            for (int i = 0; i < entryCnt; i++)
            {
                extended |= sortedEntries[i].IsExtended;
            }
            // Write the header.
            //
            byte[] tmp = new byte[128];
            System.Array.Copy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.Length);
            NB.EncodeInt32(tmp, 4, extended ? 3 : 2);
            NB.EncodeInt32(tmp, 8, entryCnt);
            dos.Write(tmp, 0, 12);
            // Write the individual file entries.
            //
            if (snapshot == null)
            {
                // Write a new index, as no entries require smudging.
                //
                for (int i_1 = 0; i_1 < entryCnt; i_1++)
                {
                    sortedEntries[i_1].Write(dos);
                }
            }
            else
            {
                int smudge_s  = (int)(snapshot.LastModified() / 1000);
                int smudge_ns = ((int)(snapshot.LastModified() % 1000)) * 1000000;
                for (int i_1 = 0; i_1 < entryCnt; i_1++)
                {
                    DirCacheEntry e = sortedEntries[i_1];
                    if (e.MightBeRacilyClean(smudge_s, smudge_ns))
                    {
                        e.SmudgeRacilyClean();
                    }
                    e.Write(dos);
                }
            }
            if (tree != null)
            {
                TemporaryBuffer bb = new TemporaryBuffer.LocalFile();
                tree.Write(tmp, bb);
                bb.Close();
                NB.EncodeInt32(tmp, 0, EXT_TREE);
                NB.EncodeInt32(tmp, 4, (int)bb.Length());
                dos.Write(tmp, 0, 8);
                bb.WriteTo(dos, null);
            }
            os.Write(foot.Digest());
            os.Close();
        }
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="System.IO.FileNotFoundException"></exception>
        /// <exception cref="Sharpen.Error"></exception>
        private FilePath ToTemp(MessageDigest md, int type, long len, InputStream @is)
        {
            bool     delete = true;
            FilePath tmp    = NewTempFile();

            try
            {
                FileOutputStream fOut = new FileOutputStream(tmp);
                try
                {
                    OutputStream @out = fOut;
                    if (config.GetFSyncObjectFiles())
                    {
                        @out = Channels.NewOutputStream(fOut.GetChannel());
                    }
                    DeflaterOutputStream cOut = Compress(@out);
                    DigestOutputStream   dOut = new DigestOutputStream(cOut, md);
                    WriteHeader(dOut, type, len);
                    byte[] buf = Buffer();
                    while (len > 0)
                    {
                        int n = @is.Read(buf, 0, (int)Math.Min(len, buf.Length));
                        if (n <= 0)
                        {
                            throw ShortInput(len);
                        }
                        dOut.Write(buf, 0, n);
                        len -= n;
                    }
                    dOut.Flush();
                    cOut.Finish();
                }
                finally
                {
                    if (config.GetFSyncObjectFiles())
                    {
                        fOut.GetChannel().Force(true);
                    }
                    fOut.Close();
                }
                delete = false;
                return(tmp);
            }
            finally
            {
                if (delete)
                {
                    FileUtils.Delete(tmp);
                }
            }
        }
Esempio n. 5
0
 /// <summary>Output the version 2 (and later) TOC header, with version number.</summary>
 /// <remarks>
 /// Output the version 2 (and later) TOC header, with version number.
 /// <p>
 /// Post version 1 all index files start with a TOC header that makes the
 /// file an invalid version 1 file, and then includes the version number.
 /// This header is necessary to recognize a version 1 from a version 2
 /// formatted index.
 /// </remarks>
 /// <param name="version">version number of this index format being written.</param>
 /// <exception cref="System.IO.IOException">an error occurred while writing to the output stream.
 ///     </exception>
 protected internal virtual void WriteTOC(int version)
 {
     @out.Write(TOC);
     NB.EncodeInt32(tmp, 0, version);
     @out.Write(tmp, 0, 4);
 }
Esempio n. 6
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void WriteTo(OutputStream os)
        {
            MessageDigest      foot = Constants.NewMessageDigest();
            DigestOutputStream dos  = new DigestOutputStream(os, foot);
            bool extended           = false;

            for (int i = 0; i < entryCnt; i++)
            {
                extended |= sortedEntries[i].IsExtended;
            }
            // Write the header.
            //
            byte[] tmp = new byte[128];
            System.Array.Copy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.Length);
            NB.EncodeInt32(tmp, 4, extended ? 3 : 2);
            NB.EncodeInt32(tmp, 8, entryCnt);
            dos.Write(tmp, 0, 12);
            // Write the individual file entries.
            int smudge_s;
            int smudge_ns;

            if (myLock != null)
            {
                // For new files we need to smudge the index entry
                // if they have been modified "now". Ideally we'd
                // want the timestamp when we're done writing the index,
                // so we use the current timestamp as a approximation.
                myLock.CreateCommitSnapshot();
                snapshot  = myLock.GetCommitSnapshot();
                smudge_s  = (int)(snapshot.LastModified() / 1000);
                smudge_ns = ((int)(snapshot.LastModified() % 1000)) * 1000000;
            }
            else
            {
                // Used in unit tests only
                smudge_ns = 0;
                smudge_s  = 0;
            }
            // Check if tree is non-null here since calling updateSmudgedEntries
            // will automatically build it via creating a DirCacheIterator
            bool writeTree = tree != null;

            if (repository != null && entryCnt > 0)
            {
                UpdateSmudgedEntries();
            }
            for (int i_1 = 0; i_1 < entryCnt; i_1++)
            {
                DirCacheEntry e = sortedEntries[i_1];
                if (e.MightBeRacilyClean(smudge_s, smudge_ns))
                {
                    e.SmudgeRacilyClean();
                }
                e.Write(dos);
            }
            if (writeTree)
            {
                TemporaryBuffer bb = new TemporaryBuffer.LocalFile();
                tree.Write(tmp, bb);
                bb.Close();
                NB.EncodeInt32(tmp, 0, EXT_TREE);
                NB.EncodeInt32(tmp, 4, (int)bb.Length());
                dos.Write(tmp, 0, 8);
                bb.WriteTo(dos, null);
            }
            writeIndexChecksum = foot.Digest();
            os.Write(writeIndexChecksum);
            os.Close();
        }