SitePaths: Convert bin and related paths to Path

Change-Id: I9fc364f4c90465412c3ba4d7e5fa011d47200d4e
This commit is contained in:
Dave Borowitz
2015-02-23 15:42:36 -08:00
parent 7e55f6bbd8
commit 11e969532f
6 changed files with 46 additions and 64 deletions

View File

@@ -157,8 +157,8 @@ public class Init extends BaseInit {
}
void startDaemon(SiteRun run) {
final String[] argv = {run.site.gerrit_sh.getAbsolutePath(), "start"};
final Process proc;
String[] argv = {run.site.gerrit_sh.toAbsolutePath().toString(), "start"};
Process proc;
try {
System.err.println("Executing " + argv[0] + " " + argv[1]);
proc = Runtime.getRuntime().exec(argv);
@@ -177,7 +177,7 @@ public class Init extends BaseInit {
for (;;) {
try {
final int rc = proc.waitFor();
int rc = proc.waitFor();
if (rc != 0) {
System.err.println("error: cannot start Gerrit: exit status " + rc);
}

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.pgm.init;
import static com.google.gerrit.pgm.init.api.InitUtil.die;
import static com.google.gerrit.pgm.init.api.InitUtil.username;
import com.google.common.io.ByteStreams;
import com.google.gerrit.launcher.GerritLauncher;
import com.google.gerrit.pgm.init.api.ConsoleUI;
import com.google.gerrit.pgm.init.api.InitStep;
@@ -28,11 +29,12 @@ import com.google.inject.Singleton;
import org.eclipse.jgit.internal.storage.file.LockFile;
import org.eclipse.jgit.util.FS;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
/** Initialize the {@code container} configuration section. */
@Singleton
@@ -56,9 +58,9 @@ class InitContainer implements InitStep {
container.string("Run as", "user", username());
container.string("Java runtime", "javaHome", javaHome());
File myWar;
Path myWar;
try {
myWar = GerritLauncher.getDistributionArchive();
myWar = GerritLauncher.getDistributionArchive().toPath();
} catch (FileNotFoundException e) {
System.err.println("warn: Cannot find distribution archive (e.g. gerrit.war)");
myWar = null;
@@ -66,53 +68,41 @@ class InitContainer implements InitStep {
String path = container.get("war");
if (path != null) {
path = container.string("Gerrit runtime", "war", //
myWar != null ? myWar.getAbsolutePath() : 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 File siteWar = site.gerrit_war;
if (siteWar.exists()) {
copy = ui.yesno(true, "Upgrade %s", siteWar.getPath());
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.getName(), siteWar.getPath());
copy = ui.yesno(true, "Copy %s to %s", myWar.getFileName(), siteWar);
if (copy) {
container.unset("war");
} else {
container.set("war", myWar.getAbsolutePath());
container.set("war", myWar.toAbsolutePath().toString());
}
}
if (copy) {
if (!ui.isBatch()) {
System.err.format("Copying %s to %s", myWar.getName(), siteWar.getPath());
System.err.format("Copying %s to %s", myWar.getFileName(), siteWar);
System.err.println();
}
FileInputStream in = new FileInputStream(myWar);
try {
siteWar.getParentFile().mkdirs();
try (InputStream in = Files.newInputStream(myWar)) {
Files.createDirectories(siteWar.getParent());
LockFile lf = new LockFile(siteWar, FS.DETECTED);
LockFile lf = new LockFile(siteWar.toFile(), FS.DETECTED);
if (!lf.lock()) {
throw new IOException("Cannot lock " + siteWar);
}
try {
final OutputStream out = lf.getOutputStream();
try {
final byte[] tmp = new byte[4096];
for (;;) {
int n = in.read(tmp);
if (n < 0) {
break;
}
out.write(tmp, 0, n);
}
} finally {
out.close();
try (OutputStream out = lf.getOutputStream()) {
ByteStreams.copy(in, out);
}
if (!lf.commit()) {
throw new IOException("Cannot commit " + siteWar);
@@ -120,8 +110,6 @@ class InitContainer implements InitStep {
} finally {
lf.unlock();
}
} finally {
in.close();
}
}
}

View File

@@ -141,7 +141,7 @@ public class SitePathInitializer {
private void extractMailExample(String orig) throws Exception {
File ex = new File(site.mail_dir, orig + ".example");
extract(ex, OutgoingEmail.class, orig);
extract(ex.toPath(), OutgoingEmail.class, orig);
chmod(0444, ex);
}

View File

@@ -16,14 +16,15 @@ package com.google.gerrit.pgm.init.api;
import static com.google.gerrit.common.FileUtil.modified;
import com.google.common.io.ByteStreams;
import com.google.gerrit.common.Die;
import org.eclipse.jgit.internal.storage.file.LockFile;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.SystemReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -33,9 +34,10 @@ import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Arrays;
/** Utility functions to help initialize a site. */
public class InitUtil {
@@ -120,12 +122,11 @@ public class InitUtil {
return name;
}
public static void extract(final File dst, final Class<?> sibling,
final String name) throws IOException {
public static void extract(Path dst, Class<?> sibling, String name)
throws IOException {
try (InputStream in = open(sibling, name)) {
if (in != null) {
ByteBuffer buf = IO.readWholeStream(in, 8192);
copy(dst, buf);
copy(dst, ByteStreams.toByteArray(in));
}
}
}
@@ -147,35 +148,28 @@ public class InitUtil {
return in;
}
public static void copy(final File dst, final ByteBuffer buf)
public static void copy(Path dst, byte[] buf)
throws FileNotFoundException, IOException {
// If the file already has the content we want to put there,
// don't attempt to overwrite the file.
//
try {
if (buf.equals(ByteBuffer.wrap(IO.readFully(dst)))) {
try (InputStream in = Files.newInputStream(dst)) {
if (Arrays.equals(buf, ByteStreams.toByteArray(in))) {
return;
}
} catch (FileNotFoundException notFound) {
} catch (NoSuchFileException notFound) {
// Fall through and write the file.
}
dst.getParentFile().mkdirs();
LockFile lf = new LockFile(dst, FS.DETECTED);
Files.createDirectories(dst.getParent());
LockFile lf = new LockFile(dst.toFile(), FS.DETECTED);
if (!lf.lock()) {
throw new IOException("Cannot lock " + dst);
}
try {
final OutputStream out = lf.getOutputStream();
try {
final byte[] tmp = new byte[4096];
while (0 < buf.remaining()) {
int n = Math.min(buf.remaining(), tmp.length);
buf.get(tmp, 0, n);
out.write(tmp, 0, n);
}
} finally {
out.close();
try (InputStream in = new ByteArrayInputStream(buf);
OutputStream out = lf.getOutputStream()) {
ByteStreams.copy(in, out);
}
if (!lf.commit()) {
throw new IOException("Cannot commit " + dst);

View File

@@ -30,7 +30,7 @@ public final class SitePaths {
public static final String FOOTER_FILENAME = "GerritSiteFooter.html";
public final File site_path;
public final File bin_dir;
public final Path bin_dir;
public final File etc_dir;
public final File lib_dir;
public final Path tmp_dir;
@@ -43,8 +43,8 @@ public final class SitePaths {
public final File themes_dir;
public final File index_dir;
public final File gerrit_sh;
public final File gerrit_war;
public final Path gerrit_sh;
public final Path gerrit_war;
public final File gerrit_config;
public final File secure_config;
@@ -70,7 +70,7 @@ public final class SitePaths {
site_path = sitePath.toFile();
Path p = sitePath;
bin_dir = new File(site_path, "bin");
bin_dir = p.resolve("bin");
etc_dir = new File(site_path, "etc");
lib_dir = new File(site_path, "lib");
tmp_dir = p.resolve("tmp");
@@ -83,8 +83,8 @@ public final class SitePaths {
themes_dir = new File(site_path, "themes");
index_dir = new File(site_path, "index");
gerrit_sh = new File(bin_dir, "gerrit.sh");
gerrit_war = new File(bin_dir, "gerrit.war");
gerrit_sh = bin_dir.resolve("gerrit.sh");
gerrit_war = bin_dir.resolve("gerrit.war");
gerrit_config = new File(etc_dir, "gerrit.config");
secure_config = new File(etc_dir, "secure.config");

View File

@@ -64,7 +64,7 @@ public class SchemaVersionCheck implements LifecycleListener {
throw new ProvisionException("Unsupported schema version "
+ currentVer.versionNbr + "; expected schema version " + expectedVer
+ ". Run init to upgrade:\n"
+ "$ java -jar " + site.gerrit_war.getAbsolutePath() + " init -d "
+ "$ java -jar " + site.gerrit_war.toAbsolutePath() + " init -d "
+ site.site_path.getAbsolutePath());
} else if (currentVer.versionNbr > expectedVer) {
throw new ProvisionException("Unsupported schema version "