diff src/org/tmatesoft/hg/core/HgCloneCommand.java @ 618:7c0d2ce340b8

Refactor approach how content finds it way down to a commit revision
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 16 May 2013 19:46:13 +0200
parents 5e0313485eef
children 4e6179bde4fc
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/core/HgCloneCommand.java	Wed May 15 20:10:09 2013 +0200
+++ b/src/org/tmatesoft/hg/core/HgCloneCommand.java	Thu May 16 19:46:13 2013 +0200
@@ -30,7 +30,6 @@
 
 import org.tmatesoft.hg.internal.ByteArrayDataAccess;
 import org.tmatesoft.hg.internal.DataAccess;
-import org.tmatesoft.hg.internal.DataAccessProvider;
 import org.tmatesoft.hg.internal.DataSerializer;
 import org.tmatesoft.hg.internal.DigestHelper;
 import org.tmatesoft.hg.internal.FNCacheFile;
@@ -148,6 +147,7 @@
 		private final SessionContext ctx;
 		private final Path.Source pathFactory;
 		private FileOutputStream indexFile;
+		private File currentFile;
 		private String filename; // human-readable name of the file being written, for log/exception purposes 
 
 		private final TreeMap<Nodeid, Integer> changelogIndexes = new TreeMap<Nodeid, Integer>();
@@ -199,7 +199,7 @@
 			try {
 				revlogHeader.offset(0).baseRevision(-1);
 				revisionSequence.clear();
-				indexFile = new FileOutputStream(new File(hgDir, filename = "store/00changelog.i"));
+				indexFile = new FileOutputStream(currentFile = new File(hgDir, filename = "store/00changelog.i"));
 				collectChangelogIndexes = true;
 			} catch (IOException ex) {
 				throw new HgInvalidControlFileException("Failed to write changelog", ex, new File(hgDir, filename));
@@ -223,7 +223,7 @@
 			try {
 				revlogHeader.offset(0).baseRevision(-1);
 				revisionSequence.clear();
-				indexFile = new FileOutputStream(new File(hgDir, filename = "store/00manifest.i"));
+				indexFile = new FileOutputStream(currentFile = new File(hgDir, filename = "store/00manifest.i"));
 			} catch (IOException ex) {
 				throw new HgInvalidControlFileException("Failed to write manifest", ex, new File(hgDir, filename));
 			}
@@ -247,7 +247,7 @@
 				revisionSequence.clear();
 				File file = new File(hgDir, filename = storagePathHelper.rewrite(name).toString());
 				file.getParentFile().mkdirs();
-				indexFile = new FileOutputStream(file);
+				indexFile = new FileOutputStream(currentFile = file);
 			} catch (IOException ex) {
 				String m = String.format("Failed to write file %s", filename);
 				throw new HgInvalidControlFileException(m, ex, new File(filename));
@@ -279,6 +279,7 @@
 			indexFile.close();
 			indexFile = null;
 			filename = null;
+			currentFile = null;
 		}
 
 		private int knownRevision(Nodeid p) {
@@ -367,11 +368,15 @@
 				revlogHeader.length(content.length, compressedLen);
 				
 				// XXX may be wise not to create DataSerializer for each revision, but for a file
-				DataAccessProvider.StreamDataSerializer sds = new DataAccessProvider.StreamDataSerializer(ctx.getLog(), indexFile) {
+				DataSerializer sds = new DataSerializer() {
 					@Override
-					public void done() {
-						// override parent behavior not to close stream in use
-					}
+						public void write(byte[] data, int offset, int length) throws HgIOException {
+							try {
+								indexFile.write(data, offset, length);
+							} catch (IOException ex) {
+								throw new HgIOException("Write failure", ex, currentFile);
+							}
+						}
 				};
 				revlogHeader.serialize(sds);
 
@@ -389,9 +394,12 @@
 				revisionSequence.add(node);
 				prevRevContent.done();
 				prevRevContent = new ByteArrayDataAccess(content);
+			} catch (HgIOException ex) {
+				String m = String.format("Failed to write revision %s of file %s", ge.node().shortNotation(), filename);
+				throw new HgInvalidControlFileException(m, ex, currentFile);
 			} catch (IOException ex) {
 				String m = String.format("Failed to write revision %s of file %s", ge.node().shortNotation(), filename);
-				throw new HgInvalidControlFileException(m, ex, new File(hgDir, filename));
+				throw new HgInvalidControlFileException(m, ex, currentFile);
 			}
 			return cancelException == null;
 		}