use of org.eclipse.jgit.internal.storage.file.LockFile in project gerrit by GerritCodeReview.
the class ProtoGen method run.
@Override
public int run() throws Exception {
LockFile lock = new LockFile(file.getAbsoluteFile());
if (!lock.lock()) {
throw die("Cannot lock " + file);
}
try {
JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class);
try (OutputStream o = lock.getOutputStream();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(o, UTF_8)))) {
String header;
try (InputStream in = getClass().getResourceAsStream("ProtoGenHeader.txt")) {
ByteBuffer buf = IO.readWholeStream(in, 1024);
int ptr = buf.arrayOffset() + buf.position();
int len = buf.remaining();
header = new String(buf.array(), ptr, len, UTF_8);
}
String version = com.google.gerrit.common.Version.getVersion();
out.write(header.replace("@@VERSION@@", version));
jsm.generateProto(out);
out.flush();
}
if (!lock.commit()) {
throw die("Could not write to " + file);
}
} finally {
lock.unlock();
}
System.out.println("Created " + file.getPath());
return 0;
}
use of org.eclipse.jgit.internal.storage.file.LockFile in project gerrit by GerritCodeReview.
the class InitUtil method copy.
public static void copy(Path dst, byte[] buf) throws FileNotFoundException, IOException {
//
try (InputStream in = Files.newInputStream(dst)) {
if (Arrays.equals(buf, ByteStreams.toByteArray(in))) {
return;
}
} catch (NoSuchFileException notFound) {
// Fall through and write the file.
}
Files.createDirectories(dst.getParent());
LockFile lf = new LockFile(dst.toFile());
if (!lf.lock()) {
throw new IOException("Cannot lock " + dst);
}
try {
try (InputStream in = new ByteArrayInputStream(buf);
OutputStream out = lf.getOutputStream()) {
ByteStreams.copy(in, out);
}
if (!lf.commit()) {
throw new IOException("Cannot commit " + dst);
}
} finally {
lf.unlock();
}
}
use of org.eclipse.jgit.internal.storage.file.LockFile in project gerrit by GerritCodeReview.
the class InitContainer method run.
@Override
public void run() throws FileNotFoundException, IOException {
ui.header("Container Process");
container.string("Run as", "user", username());
container.string("Java runtime", "javaHome", javaHome());
Path myWar;
try {
myWar = GerritLauncher.getDistributionArchive().toPath();
} catch (FileNotFoundException e) {
System.err.println("warn: Cannot find distribution archive (e.g. gerrit.war)");
myWar = null;
}
String path = container.get("war");
if (path != null) {
path = container.string("Gerrit runtime", "war", myWar != null ? myWar.toAbsolutePath().toString() : null);
if (path == null || path.isEmpty()) {
throw die("container.war is required");
}
} else if (myWar != null) {
final boolean copy;
final Path siteWar = site.gerrit_war;
if (Files.exists(siteWar)) {
copy = ui.yesno(true, "Upgrade %s", siteWar);
} else {
copy = ui.yesno(true, "Copy %s to %s", myWar.getFileName(), siteWar);
if (copy) {
container.unset("war");
} else {
container.set("war", myWar.toAbsolutePath().toString());
}
}
if (copy) {
if (!ui.isBatch()) {
System.err.format("Copying %s to %s", myWar.getFileName(), siteWar);
System.err.println();
}
try (InputStream in = Files.newInputStream(myWar)) {
Files.createDirectories(siteWar.getParent());
LockFile lf = new LockFile(siteWar.toFile());
if (!lf.lock()) {
throw new IOException("Cannot lock " + siteWar);
}
try {
try (OutputStream out = lf.getOutputStream()) {
ByteStreams.copy(in, out);
}
if (!lf.commit()) {
throw new IOException("Cannot commit " + siteWar);
}
} finally {
lf.unlock();
}
}
}
}
}
use of org.eclipse.jgit.internal.storage.file.LockFile in project gerrit by GerritCodeReview.
the class DefaultSecureStore method saveSecure.
private static void saveSecure(final FileBasedConfig sec) throws IOException {
if (FileUtil.modified(sec)) {
final byte[] out = Constants.encode(sec.toText());
final File path = sec.getFile();
final LockFile lf = new LockFile(path);
if (!lf.lock()) {
throw new IOException("Cannot lock " + path);
}
try {
FileUtil.chmod(0600, new File(path.getParentFile(), path.getName() + ".lock"));
lf.write(out);
if (!lf.commit()) {
throw new IOException("Cannot commit write to " + path);
}
} finally {
lf.unlock();
}
}
}
Aggregations