if (fType != SVNFileType.NONE) {
if (fType == SVNFileType.DIRECTORY) {
File[] children = SVNFileListUtil.listFiles(path);
if ( children != null && children.length != 0) {
if (!force) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "''{0}'' already exists; use ''force'' to overwrite existing files", path);
SVNErrorManager.error(err, SVNLogType.FSFS);
} else {
SVNFileUtil.deleteAll(path, true);
}
}
} else {
if (!force) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "''{0}'' already exists; use ''force'' to overwrite existing files", path);
SVNErrorManager.error(err, SVNLogType.FSFS);
} else {
SVNFileUtil.deleteAll(path, true);
}
}
}
//SVNFileUtil.deleteAll(path, true);
// check if path is inside repository
File root = FSFS.findRepositoryRoot(path);
if (root != null) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.REPOS_BAD_ARGS, "''{0}'' is a subdirectory of an existing repository rooted at ''{1}''", new Object[] {path, root});
SVNErrorManager.error(err, SVNLogType.FSFS);
}
if (!path.mkdirs() && !path.exists()) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Can not create directory ''{0}''", path);
SVNErrorManager.error(err, SVNLogType.FSFS);
}
InputStream is = SVNRepositoryFactory.class.getResourceAsStream(REPOSITORY_TEMPLATE_PATH);
if (is == null) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "No repository template found; should be part of SVNKit library jar");
SVNErrorManager.error(err, SVNLogType.FSFS);
}
File jarFile = SVNFileUtil.createUniqueFile(path, ".template", ".jar", true);
OutputStream uuidOS = null;
OutputStream reposFormatOS = null;
OutputStream fsFormatOS = null;
OutputStream txnCurrentOS = null;
OutputStream minUnpacledOS = null;
OutputStream currentOS = null;
try {
copyToFile(is, jarFile);
extract(jarFile, path);
SVNFileUtil.deleteFile(jarFile);
// translate eols.
if (!SVNFileUtil.isWindows) {
translateFiles(path);
translateFiles(new File(path, "conf"));
translateFiles(new File(path, "hooks"));
translateFiles(new File(path, "locks"));
}
// create pre-rev-prop.
if (enableRevisionProperties) {
if (SVNFileUtil.isWindows) {
File hookFile = new File(path, "hooks/pre-revprop-change.bat");
OutputStream os = SVNFileUtil.openFileForWriting(hookFile);
try {
os.write("@echo off".getBytes());
} catch (IOException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Cannot create pre-rev-prop-change hook file at ''{0}'': {1}",
new Object[] {hookFile, e.getLocalizedMessage()});
SVNErrorManager.error(err, SVNLogType.FSFS);
} finally {
SVNFileUtil.closeFile(os);
}
} else {
File hookFile = new File(path, "hooks/pre-revprop-change");
OutputStream os = null;
try {
os = SVNFileUtil.openFileForWriting(hookFile);
os.write("#!/bin/sh\nexit 0".getBytes("US-ASCII"));
} catch (IOException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Cannot create pre-rev-prop-change hook file at ''{0}'': {1}",
new Object[] {hookFile, e.getLocalizedMessage()});
SVNErrorManager.error(err, SVNLogType.FSFS);
} finally {
SVNFileUtil.closeFile(os);
}
SVNFileUtil.setExecutable(hookFile, true);
}
}
// generate and write UUID.
File uuidFile = new File(path, "db/uuid");
if (uuid == null || uuid.length() != 36) {
byte[] uuidBytes = SVNUUIDGenerator.generateUUID();
uuid = SVNUUIDGenerator.formatUUID(uuidBytes);
}
uuid += '\n';
try {
uuidOS = SVNFileUtil.openFileForWriting(uuidFile);
uuidOS.write(uuid.getBytes("US-ASCII"));
} catch (IOException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Error writing repository UUID to ''{0}''", uuidFile);
err.setChildErrorMessage(SVNErrorMessage.create(SVNErrorCode.IO_ERROR, e.getLocalizedMessage()));
SVNErrorManager.error(err, SVNLogType.FSFS);
}
int fsFormat = FSFS.DB_FORMAT;
if (pre14Compatible) {
File reposFormatFile = new File(path, "format");
try {
reposFormatOS = SVNFileUtil.openFileForWriting(reposFormatFile);
String format = String.valueOf(FSFS.REPOSITORY_FORMAT_LEGACY);
format += '\n';
reposFormatOS.write(format.getBytes("US-ASCII"));
} catch (IOException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Error writing repository format to ''{0}''", reposFormatFile);
err.setChildErrorMessage(SVNErrorMessage.create(SVNErrorCode.IO_ERROR, e.getLocalizedMessage()));
SVNErrorManager.error(err, SVNLogType.FSFS);
}
}
if (pre16Compatible) {
fsFormat = 3;
}
if (pre14Compatible || pre15Compatible) {
if (pre14Compatible) {
fsFormat = 1;
} else if (pre15Compatible) {
fsFormat = 2;
} //else
File fsFormatFile = new File(path, "db/format");
try {
fsFormatOS = SVNFileUtil.openFileForWriting(fsFormatFile);
String format = String.valueOf(fsFormat);
format += '\n';
fsFormatOS.write(format.getBytes("US-ASCII"));
} catch (IOException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR,
"Error writing fs format to ''{0}''", fsFormatFile);
err.setChildErrorMessage(SVNErrorMessage.create(SVNErrorCode.IO_ERROR, e.getLocalizedMessage()));
SVNErrorManager.error(err, SVNLogType.FSFS);
}
File davSandboxDir = new File(path, FSFS.DAV_DIR);
davSandboxDir.mkdir();
} else {
final File currentTxnLockFile = new File(path, "db/" + FSFS.TXN_CURRENT_LOCK_FILE);
SVNFileUtil.createEmptyFile(currentTxnLockFile);
}
long maxFilesPerDir = 0;
if (fsFormat >= FSFS.LAYOUT_FORMAT_OPTION_MINIMAL_FORMAT) {
maxFilesPerDir = FSFS.getDefaultMaxFilesPerDirectory();
File fsFormatFile = new File(path, "db/format");
try {
fsFormatOS = SVNFileUtil.openFileForWriting(fsFormatFile);
String format = String.valueOf(fsFormat) + "\n";
if (maxFilesPerDir > 0) {
File revFileBefore = new File(path, "db/revs/0");
File tmpFile = SVNFileUtil.createUniqueFile(new File(path, "db/revs"), "0", "tmp", true);
SVNFileUtil.rename(revFileBefore, tmpFile);
File shardRevDir = new File(path, "db/revs/0");
shardRevDir.mkdirs();
File revFileAfter = new File(shardRevDir, "0");
SVNFileUtil.rename(tmpFile, revFileAfter);
File revPropFileBefore = new File(path, "db/revprops/0");
tmpFile = SVNFileUtil.createUniqueFile(new File(path, "db/revprops"), "0", "tmp", true);
SVNFileUtil.rename(revPropFileBefore, tmpFile);
File shardRevPropDir = new File(path, "db/revprops/0");
shardRevPropDir.mkdirs();
File revPropFileAfter = new File(shardRevPropDir, "0");
SVNFileUtil.rename(tmpFile, revPropFileAfter);
format += "layout sharded " + String.valueOf(maxFilesPerDir) + "\n";
} else {
format += "layout linear\n";
}
fsFormatOS.write(format.getBytes("US-ASCII"));
} catch (IOException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Error writing fs format to ''{0}''", fsFormatFile);
err.setChildErrorMessage(SVNErrorMessage.create(SVNErrorCode.IO_ERROR, e.getLocalizedMessage()));
SVNErrorManager.error(err, SVNLogType.FSFS);
}
}
String currentFileLine = fsFormat >= FSFS.MIN_NO_GLOBAL_IDS_FORMAT ? "0\n" : "0 1 1\n";
File currentFile = new File(path, "db/" + FSFS.CURRENT_FILE);
currentOS = SVNFileUtil.openFileForWriting(currentFile);
try {
currentOS.write(currentFileLine.getBytes("US-ASCII"));
} catch (IOException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR,
"Can not write to ''{0}'' file: {1}",
new Object[]{currentFile.getName(), e.getLocalizedMessage()});
SVNErrorManager.error(err, e, SVNLogType.FSFS);
}
if (fsFormat >= FSFS.MIN_PACKED_FORMAT) {
File minUnpackedFile = new File(path, "db/" + FSFS.MIN_UNPACKED_REV_FILE);
SVNFileUtil.createEmptyFile(minUnpackedFile);
minUnpacledOS = SVNFileUtil.openFileForWriting(minUnpackedFile);
try {
minUnpacledOS.write("0\n".getBytes("US-ASCII"));
} catch (IOException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR,
"Can not write to ''{0}'' file: {1}",
new Object[] { minUnpackedFile.getName(), e.getLocalizedMessage() });
SVNErrorManager.error(err, e, SVNLogType.FSFS);
}
}
if (fsFormat >= FSFS.MIN_CURRENT_TXN_FORMAT) {
File txnCurrentFile = new File(path, "db/" + FSFS.TXN_CURRENT_FILE);
SVNFileUtil.createEmptyFile(txnCurrentFile);
txnCurrentOS = SVNFileUtil.openFileForWriting(txnCurrentFile);
try {
txnCurrentOS.write("0\n".getBytes("US-ASCII"));
} catch (IOException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR,
"Can not write to ''{0}'' file: {1}",
new Object[] { txnCurrentFile.getName(), e.getLocalizedMessage() });
SVNErrorManager.error(err, e, SVNLogType.FSFS);
}
}