Exemplo n.º 1
0
 /// <summary>Empty this index, removing all entries.</summary>
 /// <remarks>Empty this index, removing all entries.</remarks>
 public virtual void Clear()
 {
     snapshot      = null;
     sortedEntries = NO_ENTRIES;
     entryCnt      = 0;
     tree          = null;
 }
Exemplo n.º 2
0
 public void Entry_id_is_extracted_from_snapshot_filename()
 {
     var dt = DateTime.Now;
     Snapshot ss = FileSnapshot.FromFileInfo("000467000.snapshot", dt);
     Assert.AreEqual(dt,ss.Created);
     Assert.AreEqual(467000, ss.Revision);
 }
Exemplo n.º 3
0
 /// <summary>Empty this index, removing all entries.</summary>
 /// <remarks>Empty this index, removing all entries.</remarks>
 public virtual void Clear()
 {
     snapshot          = null;
     sortedEntries     = NO_ENTRIES;
     entryCnt          = 0;
     tree              = null;
     readIndexChecksum = NO_CHECKSUM;
 }
Exemplo n.º 4
0
        protected override Snapshot WriteSnapshotImpl(Model model)
        {
            var fileSnapshot = new FileSnapshot(DateTime.Now, model.Revision);
            var fileName     = Path.Combine(_config.GetSnapshotPath(), fileSnapshot.Name);

            using (Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                _formatter.Serialize(stream, model);
            }
            return(fileSnapshot);
        }
Exemplo n.º 5
0
        protected override IEnumerable <Snapshot> ReadSnapshotMetaData()
        {
            var snapshots = new List <FileSnapshot>();

            foreach (var file in Directory.GetFiles(_config.GetSnapshotPath(), "*.snapshot"))
            {
                var fileInfo = new FileInfo(file);
                snapshots.Add(FileSnapshot.FromFileInfo(fileInfo.Name, fileInfo.CreationTime));
            }

            snapshots.Sort((a, b) => a.Revision.CompareTo(b.Revision));
            return(snapshots);
        }
Exemplo n.º 6
0
        /// <summary>Commit this change and release the lock.</summary>
        /// <remarks>
        /// Commit this change and release the lock.
        /// <p>
        /// If this method fails (returns false) the lock is still released.
        /// </remarks>
        /// <returns>
        /// true if the commit was successful and the file contains the new
        /// data; false if the commit failed and the file remains with the
        /// old data.
        /// </returns>
        /// <exception cref="System.InvalidOperationException">the lock is not held.</exception>
        public virtual bool Commit()
        {
            LockFile tmp = myLock;

            RequireLocked(tmp);
            myLock = null;
            if (!tmp.Commit())
            {
                return(false);
            }
            snapshot = tmp.GetCommitSnapshot();
            return(true);
        }
Exemplo n.º 7
0
        public static JournalFile Parse(string filename)
        {
            Match match = journalFilenameParser.Match(filename);

            if (!match.Success)
            {
                throw new ArgumentException("bad journal filename format");
            }

            long  fileNr  = (long)FileSnapshot.ParsePadded(match.Groups["fileNr"].Value);
            ulong entryNr = FileSnapshot.ParsePadded(match.Groups["entryNr"].Value);

            return(new JournalFile(fileNr, entryNr));
        }
Exemplo n.º 8
0
 /// <summary>Read the index from disk, if it has changed on disk.</summary>
 /// <remarks>
 /// Read the index from disk, if it has changed on disk.
 /// <p>
 /// This method tries to avoid loading the index if it has not changed since
 /// the last time we consulted it. A missing index file will be treated as
 /// though it were present but had no file entries in it.
 /// </remarks>
 /// <exception cref="System.IO.IOException">
 /// the index file is present but could not be read. This
 /// DirCache instance may not be populated correctly.
 /// </exception>
 /// <exception cref="NGit.Errors.CorruptObjectException">
 /// the index file is using a format or extension that this
 /// library does not support.
 /// </exception>
 public virtual void Read()
 {
     if (liveFile == null)
     {
         throw new IOException(JGitText.Get().dirCacheDoesNotHaveABackingFile);
     }
     if (!liveFile.Exists())
     {
         Clear();
     }
     else
     {
         if (snapshot == null || snapshot.IsModified(liveFile))
         {
             try
             {
                 FileInputStream inStream = new FileInputStream(liveFile);
                 try
                 {
                     Clear();
                     ReadFrom(inStream);
                 }
                 finally
                 {
                     try
                     {
                         inStream.Close();
                     }
                     catch (IOException)
                     {
                     }
                 }
             }
             catch (FileNotFoundException)
             {
                 // Ignore any close failures.
                 // Someone must have deleted it between our exists test
                 // and actually opening the path. That's fine, its empty.
                 //
                 Clear();
             }
             snapshot = FileSnapshot.Save(liveFile);
         }
     }
 }
Exemplo n.º 9
0
        /// <summary>Commit this change and release the lock.</summary>
        /// <remarks>
        /// Commit this change and release the lock.
        /// <p>
        /// If this method fails (returns false) the lock is still released.
        /// </remarks>
        /// <returns>
        /// true if the commit was successful and the file contains the new
        /// data; false if the commit failed and the file remains with the
        /// old data.
        /// </returns>
        /// <exception cref="System.InvalidOperationException">the lock is not held.</exception>
        public virtual bool Commit()
        {
            LockFile tmp = myLock;

            RequireLocked(tmp);
            myLock = null;
            if (!tmp.Commit())
            {
                return(false);
            }
            snapshot = tmp.GetCommitSnapshot();
            if (indexChangedListener != null && !Arrays.Equals(readIndexChecksum, writeIndexChecksum
                                                               ))
            {
                indexChangedListener.OnIndexChanged(new IndexChangedEvent());
            }
            return(true);
        }
Exemplo n.º 10
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="NGit.Errors.CorruptObjectException"></exception>
        private void ReadFrom(InputStream inStream)
        {
            BufferedInputStream @in = new BufferedInputStream(inStream);
            MessageDigest       md  = Constants.NewMessageDigest();

            // Read the index header and verify we understand it.
            //
            byte[] hdr = new byte[20];
            IOUtil.ReadFully(@in, hdr, 0, 12);
            md.Update(hdr, 0, 12);
            if (!Is_DIRC(hdr))
            {
                throw new CorruptObjectException(JGitText.Get().notADIRCFile);
            }
            int  ver      = NB.DecodeInt32(hdr, 4);
            bool extended = false;

            if (ver == 3)
            {
                extended = true;
            }
            else
            {
                if (ver != 2)
                {
                    throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().unknownDIRCVersion
                                                                          , ver));
                }
            }
            entryCnt = NB.DecodeInt32(hdr, 8);
            if (entryCnt < 0)
            {
                throw new CorruptObjectException(JGitText.Get().DIRCHasTooManyEntries);
            }
            // Load the individual file entries.
            //
            int infoLength = DirCacheEntry.GetMaximumInfoLength(extended);

            byte[] infos = new byte[infoLength * entryCnt];
            sortedEntries = new DirCacheEntry[entryCnt];
            MutableInteger infoAt = new MutableInteger();

            for (int i = 0; i < entryCnt; i++)
            {
                sortedEntries[i] = new DirCacheEntry(infos, infoAt, @in, md);
            }
            snapshot = FileSnapshot.Save(liveFile);
            // After the file entries are index extensions, and then a footer.
            //
            for (; ;)
            {
                @in.Mark(21);
                IOUtil.ReadFully(@in, hdr, 0, 20);
                if (@in.Read() < 0)
                {
                    // No extensions present; the file ended where we expected.
                    //
                    break;
                }
                @in.Reset();
                md.Update(hdr, 0, 8);
                IOUtil.SkipFully(@in, 8);
                long sz = NB.DecodeUInt32(hdr, 4);
                switch (NB.DecodeInt32(hdr, 0))
                {
                case EXT_TREE:
                {
                    if (int.MaxValue < sz)
                    {
                        throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().DIRCExtensionIsTooLargeAt
                                                                              , FormatExtensionName(hdr), sz));
                    }
                    byte[] raw = new byte[(int)sz];
                    IOUtil.ReadFully(@in, raw, 0, raw.Length);
                    md.Update(raw, 0, raw.Length);
                    tree = new DirCacheTree(raw, new MutableInteger(), null);
                    break;
                }

                default:
                {
                    if (hdr[0] >= 'A' && ((sbyte)hdr[0]) <= 'Z')
                    {
                        // The extension is optional and is here only as
                        // a performance optimization. Since we do not
                        // understand it, we can safely skip past it, after
                        // we include its data in our checksum.
                        //
                        SkipOptionalExtension(@in, md, hdr, sz);
                    }
                    else
                    {
                        // The extension is not an optimization and is
                        // _required_ to understand this index format.
                        // Since we did not trap it above we must abort.
                        //
                        throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().DIRCExtensionNotSupportedByThisVersion
                                                                              , FormatExtensionName(hdr)));
                    }
                    break;
                }
                }
            }
            byte[] exp = md.Digest();
            if (!Arrays.Equals(exp, hdr))
            {
                throw new CorruptObjectException(JGitText.Get().DIRCChecksumMismatch);
            }
        }
Exemplo n.º 11
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();
        }