Beispiel #1
0
        /// <exception cref="System.IO.IOException"></exception>
        private PackFile WritePack <_T0, _T1>(ICollection <_T0> want, ICollection <_T1> have
                                              , ICollection <ObjectId> tagTargets, IList <PackIndex> excludeObjects) where _T0 : ObjectId
            where _T1 : ObjectId
        {
            FilePath   tmpPack = null;
            FilePath   tmpIdx  = null;
            PackWriter pw      = new PackWriter(repo);

            try
            {
                // prepare the PackWriter
                pw.SetDeltaBaseAsOffset(true);
                pw.SetReuseDeltaCommits(false);
                if (tagTargets != null)
                {
                    pw.SetTagTargets(tagTargets);
                }
                if (excludeObjects != null)
                {
                    foreach (PackIndex idx in excludeObjects)
                    {
                        pw.ExcludeObjects(idx);
                    }
                }
                pw.PreparePack(pm, want, have);
                if (pw.GetObjectCount() == 0)
                {
                    return(null);
                }
                // create temporary files
                string   id      = pw.ComputeName().GetName();
                FilePath packdir = new FilePath(repo.ObjectsDirectory, "pack");
                tmpPack = FilePath.CreateTempFile("gc_", ".pack_tmp", packdir);
                tmpIdx  = new FilePath(packdir, Sharpen.Runtime.Substring(tmpPack.GetName(), 0, tmpPack
                                                                          .GetName().LastIndexOf('.')) + ".idx_tmp");
                if (!tmpIdx.CreateNewFile())
                {
                    throw new IOException(MessageFormat.Format(JGitText.Get().cannotCreateIndexfile,
                                                               tmpIdx.GetPath()));
                }
                // write the packfile
                FileChannel  channel       = new FileOutputStream(tmpPack).GetChannel();
                OutputStream channelStream = Channels.NewOutputStream(channel);
                try
                {
                    pw.WritePack(pm, pm, channelStream);
                }
                finally
                {
                    channel.Force(true);
                    channelStream.Close();
                    channel.Close();
                }
                // write the packindex
                FileChannel  idxChannel = new FileOutputStream(tmpIdx).GetChannel();
                OutputStream idxStream  = Channels.NewOutputStream(idxChannel);
                try
                {
                    pw.WriteIndex(idxStream);
                }
                finally
                {
                    idxChannel.Force(true);
                    idxStream.Close();
                    idxChannel.Close();
                }
                // rename the temporary files to real files
                FilePath realPack = NameFor(id, ".pack");
                tmpPack.SetReadOnly();
                FilePath realIdx = NameFor(id, ".idx");
                realIdx.SetReadOnly();
                bool delete = true;
                try
                {
                    if (!tmpPack.RenameTo(realPack))
                    {
                        return(null);
                    }
                    delete = false;
                    if (!tmpIdx.RenameTo(realIdx))
                    {
                        FilePath newIdx = new FilePath(realIdx.GetParentFile(), realIdx.GetName() + ".new"
                                                       );
                        if (!tmpIdx.RenameTo(newIdx))
                        {
                            newIdx = tmpIdx;
                        }
                        throw new IOException(MessageFormat.Format(JGitText.Get().panicCantRenameIndexFile
                                                                   , newIdx, realIdx));
                    }
                }
                finally
                {
                    if (delete && tmpPack.Exists())
                    {
                        tmpPack.Delete();
                    }
                    if (delete && tmpIdx.Exists())
                    {
                        tmpIdx.Delete();
                    }
                }
                return(((ObjectDirectory)repo.ObjectDatabase).OpenPack(realPack, realIdx));
            }
            finally
            {
                pw.Release();
                if (tmpPack != null && tmpPack.Exists())
                {
                    tmpPack.Delete();
                }
                if (tmpIdx != null && tmpIdx.Exists())
                {
                    tmpIdx.Delete();
                }
            }
        }