Use JGit's LockFile to copy files during init

This prevents us from truncating the file we are running from to 0
bytes.

Bug: 424
Change-Id: I32945d4a738ae832850e05d732181d914e2127a7
This commit is contained in:
Nico Sallembien
2010-02-02 11:06:55 -08:00
parent 9a8e483c9a
commit a1c68fe604

View File

@@ -21,9 +21,9 @@ import org.eclipse.jgit.lib.FileBasedConfig;
import org.eclipse.jgit.lib.LockFile; import org.eclipse.jgit.lib.LockFile;
import org.eclipse.jgit.util.SystemReader; import org.eclipse.jgit.util.SystemReader;
import java.io.OutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.InetAddress; import java.net.InetAddress;
@@ -169,15 +169,27 @@ class InitUtil {
throws FileNotFoundException, IOException { throws FileNotFoundException, IOException {
try { try {
dst.getParentFile().mkdirs(); dst.getParentFile().mkdirs();
final FileOutputStream out = new FileOutputStream(dst); LockFile lf = new LockFile(dst);
if (!lf.lock()) {
throw new IOException("Cannot lock " + dst);
}
try { try {
final byte[] buf = new byte[4096];
int n; final OutputStream out = lf.getOutputStream();
while (0 < (n = in.read(buf))) { try {
out.write(buf, 0, n); final byte[] buf = new byte[4096];
int n;
while (0 < (n = in.read(buf))) {
out.write(buf, 0, n);
}
} finally {
out.close();
}
if (!lf.commit()) {
throw new IOException("Cannot commit " + dst);
} }
} finally { } finally {
out.close(); lf.unlock();
} }
} finally { } finally {
in.close(); in.close();