public void OpenIndex(ProgressMonitor pm)
            {
                if (Index != null)
                {
                    return;
                }

                if (TmpIdx.IsFile())
                {
                    try
                    {
                        Index = PackIndex.Open(TmpIdx);
                        return;
                    }
                    catch (FileNotFoundException)
                    {
                        // Fall through and get the file.
                    }
                }

                using (Stream s = _connection.open("pack/" + _idxName))
                {
                    pm.BeginTask("Get " + _idxName.Slice(0, 12) + "..idx", !s.CanSeek ? ProgressMonitor.UNKNOWN : (int)(s.Length / 1024));

                    try
                    {
                        using (var fos = new FileStream(TmpIdx.FullName, System.IO.FileMode.CreateNew, FileAccess.Write))
                        {
                            var buf = new byte[2048];
                            int cnt;
                            while (!pm.IsCancelled && (cnt = s.Read(buf, 0, buf.Length)) > 0)
                            {
                                fos.Write(buf, 0, cnt);
                                pm.Update(cnt / 1024);
                            }
                        }
                    }
                    catch (IOException)
                    {
                        TmpIdx.DeleteFile();
                        throw;
                    }
                }

                pm.EndTask();

                if (pm.IsCancelled)
                {
                    TmpIdx.DeleteFile();
                    return;
                }

                try
                {
                    Index = PackIndex.Open(TmpIdx);
                }
                catch (IOException)
                {
                    TmpIdx.DeleteFile();
                    throw;
                }
            }
Esempio n. 2
0
            public void OpenIndex(ProgressMonitor pm)
            {
                if (Index != null)
                {
                    return;
                }

                try
                {
                    Index = PackIndex.Open(TmpIdx);
                    return;
                }
                catch (FileNotFoundException)
                {
                }

                Stream s = _connection.open("pack/" + _idxName);

                pm.BeginTask("Get " + _idxName.Slice(0, 12) + "..idx", s.Length < 0 ? -1 : (int)(s.Length / 1024));
                try
                {
                    var fos = new FileStream(TmpIdx.ToString(), System.IO.FileMode.Open, FileAccess.ReadWrite);
                    try
                    {
                        var buf = new byte[2048];
                        int cnt;
                        while (!pm.IsCancelled && (cnt = s.Read(buf, 0, buf.Length)) >= 0)
                        {
                            fos.Write(buf, 0, cnt);
                            pm.Update(cnt / 1024);
                        }
                    }
                    finally
                    {
                        fos.Close();
                    }
                }
                catch (IOException)
                {
                    TmpIdx.Delete();
                    throw;
                }
                finally
                {
                    s.Close();
                }
                pm.EndTask();

                if (pm.IsCancelled)
                {
                    TmpIdx.Delete();
                    return;
                }

                try
                {
                    Index = PackIndex.Open(TmpIdx);
                }
                catch (IOException)
                {
                    TmpIdx.Delete();
                    throw;
                }
            }