Exemple #1
0
		/// <exception cref="System.IO.IOException"></exception>
		private static void Write(FilePath[] files, PackWriter pw)
		{
			long begin = files[0].GetParentFile().LastModified();
			NullProgressMonitor m = NullProgressMonitor.INSTANCE;
			OutputStream @out;
			@out = new BufferedOutputStream(new FileOutputStream(files[0]));
			try
			{
				pw.WritePack(m, m, @out);
			}
			finally
			{
				@out.Close();
			}
			@out = new BufferedOutputStream(new FileOutputStream(files[1]));
			try
			{
				pw.WriteIndex(@out);
			}
			finally
			{
				@out.Close();
			}
			Touch(begin, files[0].GetParentFile());
		}
		/// <exception cref="System.IO.FileNotFoundException"></exception>
		private void OpenTempFile()
		{
			string uuid = Misc.TDCreateUUID();
			string filename = string.Format("%s.blobtmp", uuid);
			FilePath tempDir = store.TempDir();
			tempFile = new FilePath(tempDir, filename);
			outStream = new BufferedOutputStream(new FileOutputStream(tempFile));
		}
Exemple #3
0
		public virtual void TestAbbreviateIsActuallyUnique()
		{
			// This test is far more difficult. We have to manually craft
			// an input that contains collisions at a particular prefix,
			// but this is computationally difficult. Instead we force an
			// index file to have what we want.
			//
			ObjectId id = Id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba");
			byte[] idBuf = ToByteArray(id);
			IList<PackedObjectInfo> objects = new AList<PackedObjectInfo>();
			for (int i = 0; i < 256; i++)
			{
				idBuf[9] = unchecked((byte)i);
				objects.AddItem(new PackedObjectInfo(ObjectId.FromRaw(idBuf)));
			}
			string packName = "pack-" + id.Name;
			FilePath packDir = new FilePath(((ObjectDirectory)db.ObjectDatabase).GetDirectory
				(), "pack");
			FilePath idxFile = new FilePath(packDir, packName + ".idx");
			FilePath packFile = new FilePath(packDir, packName + ".pack");
			FileUtils.Mkdir(packDir, true);
			OutputStream dst = new BufferedOutputStream(new FileOutputStream(idxFile));
			try
			{
				PackIndexWriter writer = new PackIndexWriterV2(dst);
				writer.Write(objects, new byte[Constants.OBJECT_ID_LENGTH]);
			}
			finally
			{
				dst.Close();
			}
			new FileOutputStream(packFile).Close();
			NUnit.Framework.Assert.AreEqual(id.Abbreviate(20), reader.Abbreviate(id, 2));
			AbbreviatedObjectId abbrev8 = id.Abbreviate(8);
			ICollection<ObjectId> matches = reader.Resolve(abbrev8);
			NUnit.Framework.Assert.IsNotNull(matches);
			NUnit.Framework.Assert.AreEqual(objects.Count, matches.Count);
			foreach (PackedObjectInfo info in objects)
			{
				NUnit.Framework.Assert.IsTrue(matches.Contains(info), "contains " + info.Name);
			}
			try
			{
				db.Resolve(abbrev8.Name);
				NUnit.Framework.Assert.Fail("did not throw AmbiguousObjectException");
			}
			catch (AmbiguousObjectException err)
			{
				NUnit.Framework.Assert.AreEqual(abbrev8, err.GetAbbreviatedObjectId());
				matches = err.GetCandidates();
				NUnit.Framework.Assert.IsNotNull(matches);
				NUnit.Framework.Assert.AreEqual(objects.Count, matches.Count);
				foreach (PackedObjectInfo info_1 in objects)
				{
					NUnit.Framework.Assert.IsTrue(matches.Contains(info_1), "contains " + info_1.Name
						);
				}
			}
			NUnit.Framework.Assert.AreEqual(id, db.Resolve(id.Abbreviate(20).Name));
		}
Exemple #4
0
		// end finally
		// end encodeFileToFile
		/// <summary>Reads <tt>infile</tt> and decodes it to <tt>outfile</tt>.</summary>
		/// <remarks>Reads <tt>infile</tt> and decodes it to <tt>outfile</tt>.</remarks>
		/// <param name="infile">Input file</param>
		/// <param name="outfile">Output file</param>
		/// <exception cref="System.IO.IOException">if there is an error</exception>
		/// <since>2.2</since>
		public static void DecodeFileToFile(string infile, string outfile)
		{
			byte[] decoded = Couchbase.Lite.Support.Base64.DecodeFromFile(infile);
			OutputStream @out = null;
			try
			{
				@out = new BufferedOutputStream(new FileOutputStream(outfile));
				@out.Write(decoded);
			}
			catch (IOException e)
			{
				// end try
				throw;
			}
			finally
			{
				// Catch and release to execute finally{}
				// end catch
				try
				{
					@out.Close();
				}
				catch (Exception)
				{
				}
			}
		}
Exemple #5
0
		// end encodeFromFile
		/// <summary>Reads <tt>infile</tt> and encodes it to <tt>outfile</tt>.</summary>
		/// <remarks>Reads <tt>infile</tt> and encodes it to <tt>outfile</tt>.</remarks>
		/// <param name="infile">Input file</param>
		/// <param name="outfile">Output file</param>
		/// <exception cref="System.IO.IOException">if there is an error</exception>
		/// <since>2.2</since>
		public static void EncodeFileToFile(string infile, string outfile)
		{
			string encoded = Couchbase.Lite.Support.Base64.EncodeFromFile(infile);
			OutputStream @out = null;
			try
			{
				@out = new BufferedOutputStream(new FileOutputStream(outfile));
				@out.Write(Sharpen.Runtime.GetBytesForString(encoded, "US-ASCII"));
			}
			catch (IOException e)
			{
				// Strict, 7-bit output.
				// end try
				throw;
			}
			finally
			{
				// Catch and release to execute finally{}
				// end catch
				try
				{
					@out.Close();
				}
				catch (Exception)
				{
				}
			}
		}
			/// <exception cref="NGit.Errors.TransportException"></exception>
			public TcpPushConnection(TransportGitAnon _enclosing) : base(_enclosing)
			{
				this._enclosing = _enclosing;
				this.sock = this._enclosing.OpenConnection();
				try
				{
					InputStream sIn = this.sock.GetInputStream();
					OutputStream sOut = this.sock.GetOutputStream();
					sIn = new BufferedInputStream(sIn);
					sOut = new BufferedOutputStream(sOut);
					this.Init(sIn, sOut);
					this._enclosing.Service("git-receive-pack", this.pckOut);
				}
				catch (IOException err)
				{
					this.Close();
					throw new TransportException(this.uri, JGitText.Get().remoteHungUpUnexpectedly, err
						);
				}
				this.ReadAdvertisedRefs();
			}
Exemple #7
0
 /// <exception cref="NGit.Errors.TransportException"></exception>
 public ForkLocalPushConnection(TransportLocal _enclosing)
     : base(_enclosing)
 {
     this._enclosing = _enclosing;
     MessageWriter msg = new MessageWriter();
     this.SetMessageWriter(msg);
     this.receivePack = this._enclosing.Spawn(this._enclosing.GetOptionReceivePack());
     InputStream rpErr = this.receivePack.GetErrorStream();
     this.errorReaderThread = new StreamCopyThread(rpErr, msg.GetRawStream());
     this.errorReaderThread.Start();
     InputStream rpIn = this.receivePack.GetInputStream();
     OutputStream rpOut = this.receivePack.GetOutputStream();
     rpIn = new BufferedInputStream(rpIn);
     rpOut = new BufferedOutputStream(rpOut);
     this.Init(rpIn, rpOut);
     this.ReadAdvertisedRefs();
 }
		/// <exception cref="NGit.Errors.TransportException"></exception>
		private void Sendpack(IList<RemoteRefUpdate> updates, ProgressMonitor monitor)
		{
			string pathPack = null;
			string pathIdx = null;
			PackWriter writer = new PackWriter(transport.GetPackConfig(), local.NewObjectReader
				());
			try
			{
				IList<ObjectId> need = new AList<ObjectId>();
				IList<ObjectId> have = new AList<ObjectId>();
				foreach (RemoteRefUpdate r in updates)
				{
					need.AddItem(r.GetNewObjectId());
				}
				foreach (Ref r_1 in GetRefs())
				{
					have.AddItem(r_1.GetObjectId());
					if (r_1.GetPeeledObjectId() != null)
					{
						have.AddItem(r_1.GetPeeledObjectId());
					}
				}
				writer.PreparePack(monitor, need, have);
				// We don't have to continue further if the pack will
				// be an empty pack, as the remote has all objects it
				// needs to complete this change.
				//
				if (writer.GetObjectCount() == 0)
				{
					return;
				}
				packNames = new LinkedHashMap<string, string>();
				foreach (string n in dest.GetPackNames())
				{
					packNames.Put(n, n);
				}
				string @base = "pack-" + writer.ComputeName().Name;
				string packName = @base + ".pack";
				pathPack = "pack/" + packName;
				pathIdx = "pack/" + @base + ".idx";
				if (Sharpen.Collections.Remove(packNames, packName) != null)
				{
					// The remote already contains this pack. We should
					// remove the index before overwriting to prevent bad
					// offsets from appearing to clients.
					//
					dest.WriteInfoPacks(packNames.Keys);
					dest.DeleteFile(pathIdx);
				}
				// Write the pack file, then the index, as readers look the
				// other direction (index, then pack file).
				//
				string wt = "Put " + Sharpen.Runtime.Substring(@base, 0, 12);
				OutputStream os = dest.WriteFile(pathPack, monitor, wt + "..pack");
				try
				{
					os = new BufferedOutputStream(os);
					writer.WritePack(monitor, monitor, os);
				}
				finally
				{
					os.Close();
				}
				os = dest.WriteFile(pathIdx, monitor, wt + "..idx");
				try
				{
					os = new BufferedOutputStream(os);
					writer.WriteIndex(os);
				}
				finally
				{
					os.Close();
				}
				// Record the pack at the start of the pack info list. This
				// way clients are likely to consult the newest pack first,
				// and discover the most recent objects there.
				//
				AList<string> infoPacks = new AList<string>();
				infoPacks.AddItem(packName);
				Sharpen.Collections.AddAll(infoPacks, packNames.Keys);
				dest.WriteInfoPacks(infoPacks);
			}
			catch (IOException err)
			{
				SafeDelete(pathIdx);
				SafeDelete(pathPack);
				throw new TransportException(uri, JGitText.Get().cannotStoreObjects, err);
			}
			finally
			{
				writer.Release();
			}
		}