Extract reusable parts from InitUtil

Moves reusable parts of InitUtil to FileUtil class in gerrit-common

Change-Id: Ia24a34028ec27d98cd7927d97822eb218c5719a8
Signed-off-by: Dariusz Luksza <dariusz@luksza.org>
This commit is contained in:
Dariusz Luksza 2014-03-24 17:11:29 +01:00 committed by David Pursehouse
parent 10cec6f11c
commit 5632a81aba
6 changed files with 77 additions and 36 deletions

View File

@ -6,6 +6,10 @@ ANNOTATIONS = [
SRC + 'common/auth/SignInRequired.java',
]
EXCLUDES = [
SRC + 'common/FileUtil.java',
]
java_library(
name = 'annotations',
srcs = ANNOTATIONS,
@ -14,7 +18,7 @@ java_library(
gwt_module(
name = 'client',
srcs = glob([SRC + 'common/**/*.java']),
srcs = glob([SRC + 'common/**/*.java'], excludes = EXCLUDES),
gwtxml = SRC + 'Common.gwt.xml',
deps = [
'//gerrit-patch-jgit:client',

View File

@ -0,0 +1,66 @@
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.common;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.IO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class FileUtil {
public static boolean modified(FileBasedConfig cfg) throws IOException {
byte[] curVers;
try {
curVers = IO.readFully(cfg.getFile());
} catch (FileNotFoundException notFound) {
return true;
}
byte[] newVers = Constants.encode(cfg.toText());
return !Arrays.equals(curVers, newVers);
}
public static void mkdir(final File path) {
if (!path.isDirectory() && !path.mkdir()) {
throw new Die("Cannot make directory " + path);
}
}
public static void chmod(final int mode, final File path) {
path.setReadable(false, false /* all */);
path.setWritable(false, false /* all */);
path.setExecutable(false, false /* all */);
path.setReadable((mode & 0400) == 0400, true /* owner only */);
path.setWritable((mode & 0200) == 0200, true /* owner only */);
if (path.isDirectory() || (mode & 0100) == 0100) {
path.setExecutable(true, true /* owner only */);
}
if ((mode & 0044) == 0044) {
path.setReadable(true, false /* all */);
}
if ((mode & 0011) == 0011) {
path.setExecutable(true, false /* all */);
}
}
private FileUtil() {
}
}

View File

@ -14,7 +14,7 @@
package com.google.gerrit.pgm.init;
import static com.google.gerrit.pgm.init.InitUtil.chmod;
import static com.google.gerrit.common.FileUtil.chmod;
import static com.google.gerrit.pgm.init.InitUtil.die;
import static com.google.gerrit.pgm.init.InitUtil.domainOf;
import static com.google.gerrit.pgm.init.InitUtil.isAnyAddress;

View File

@ -14,7 +14,7 @@
package com.google.gerrit.pgm.init;
import static com.google.gerrit.pgm.init.InitUtil.chmod;
import static com.google.gerrit.common.FileUtil.chmod;
import static com.google.gerrit.pgm.init.InitUtil.die;
import static com.google.gerrit.pgm.init.InitUtil.hostname;

View File

@ -14,6 +14,9 @@
package com.google.gerrit.pgm.init;
import static com.google.gerrit.common.FileUtil.chmod;
import static com.google.gerrit.common.FileUtil.modified;
import com.google.gerrit.common.Die;
import org.eclipse.jgit.internal.storage.file.LockFile;
@ -33,7 +36,6 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Arrays;
/** Utility functions to help initialize a site. */
class InitUtil {
@ -71,43 +73,12 @@ class InitUtil {
}
}
private static boolean modified(FileBasedConfig cfg) throws IOException {
byte[] curVers;
try {
curVers = IO.readFully(cfg.getFile());
} catch (FileNotFoundException notFound) {
return true;
}
byte[] newVers = Constants.encode(cfg.toText());
return !Arrays.equals(curVers, newVers);
}
static void mkdir(final File path) {
if (!path.isDirectory() && !path.mkdir()) {
throw die("Cannot make directory " + path);
}
}
static void chmod(final int mode, final File path) {
path.setReadable(false, false /* all */);
path.setWritable(false, false /* all */);
path.setExecutable(false, false /* all */);
path.setReadable((mode & 0400) == 0400, true /* owner only */);
path.setWritable((mode & 0200) == 0200, true /* owner only */);
if (path.isDirectory() || (mode & 0100) == 0100) {
path.setExecutable(true, true /* owner only */);
}
if ((mode & 0044) == 0044) {
path.setReadable(true, false /* all */);
}
if ((mode & 0011) == 0011) {
path.setExecutable(true, false /* all */);
}
}
static String version() {
return com.google.gerrit.common.Version.getVersion();
}

View File

@ -14,7 +14,7 @@
package com.google.gerrit.pgm.init;
import static com.google.gerrit.pgm.init.InitUtil.chmod;
import static com.google.gerrit.common.FileUtil.chmod;
import static com.google.gerrit.pgm.init.InitUtil.die;
import static com.google.gerrit.pgm.init.InitUtil.extract;
import static com.google.gerrit.pgm.init.InitUtil.mkdir;