Search in sources :

Example 1 with LockFile

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;
}
Also used : LockFile(org.eclipse.jgit.internal.storage.file.LockFile) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteBuffer(java.nio.ByteBuffer) JavaSchemaModel(com.google.gwtorm.schema.java.JavaSchemaModel) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 2 with LockFile

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();
    }
}
Also used : LockFile(org.eclipse.jgit.internal.storage.file.LockFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Example 3 with LockFile

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();
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) LockFile(org.eclipse.jgit.internal.storage.file.LockFile) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 4 with LockFile

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();
        }
    }
}
Also used : LockFile(org.eclipse.jgit.internal.storage.file.LockFile) IOException(java.io.IOException) File(java.io.File) LockFile(org.eclipse.jgit.internal.storage.file.LockFile)

Aggregations

LockFile (org.eclipse.jgit.internal.storage.file.LockFile)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 JavaSchemaModel (com.google.gwtorm.schema.java.JavaSchemaModel)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 ByteBuffer (java.nio.ByteBuffer)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Path (java.nio.file.Path)1