Skip to content

Commit

Permalink
tool(db):db path move, print error and exit if path or config error.
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 committed Aug 2, 2022
1 parent 2866ea7 commit e5f84fc
Showing 1 changed file with 77 additions and 2 deletions.
79 changes: 77 additions & 2 deletions plugins/src/main/java/org/tron/plugins/DbMove.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.tron.plugins;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import me.tongfei.progressbar.ProgressBar;
Expand All @@ -34,11 +37,12 @@ public class DbMove implements Callable<Integer> {
defaultValue = "output-directory",
converter = Db.PathConverter.class,
description = "database directory path. Default: ${DEFAULT-VALUE}")
Path database;
static Path database;

@CommandLine.Option(names = {"-c", "--config"},
defaultValue = "config.conf",
converter = Db.ConfigConverter.class,
converter = ConfigConverter.class,
order = Integer.MAX_VALUE,
description = " config file. Default: ${DEFAULT-VALUE}")
Config config;

Expand Down Expand Up @@ -170,7 +174,78 @@ public Property(String name, Path original, Path destination) throws IOException
if (this.original.toFile().isFile()) {
throw new IOException(this.original + " is a file!");
}
if (isSymbolicLink(original.toFile())) {
throw new IOException(original + " is symbolicLink!");
}
this.destination = destination.toFile().getCanonicalFile().toPath();
if (this.destination.toFile().exists()) {
throw new IOException(this.destination + " already exist!");
}
if (this.destination.equals(this.original)) {
throw new IOException("destination and original can not be same:[" + this.original + "]!");
}
}

public boolean isSymbolicLink(File file) throws IOException {
if (file == null) {
throw new NullPointerException("File must not be null");
}

File canon;
if (file.getParent() == null) {
canon = file;
} else {
File canonDir = file.getParentFile().getCanonicalFile();
canon = new File(canonDir, file.getName());
}
return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());
}
}

static class ConfigConverter implements CommandLine.ITypeConverter<Config> {
private final Exception notFind =
new IllegalArgumentException("There is no database to be moved,please check.");

ConfigConverter() {
}

public Config convert(String value) throws Exception {
File file = Paths.get(value).toFile();
if (file.exists() && file.isFile()) {
Config config = ConfigFactory.parseFile(Paths.get(value).toFile());
if (config.hasPath(PROPERTIES_CONFIG_KEY)) {
List<? extends Config> dbs = config.getConfigList(PROPERTIES_CONFIG_KEY);
if (dbs.isEmpty()) {
throw notFind;
}
String dbPath = config.hasPath(DB_DIRECTORY_CONFIG_KEY)
? config.getString(DB_DIRECTORY_CONFIG_KEY) : DEFAULT_DB_DIRECTORY;

dbs = dbs.stream()
.filter(c -> c.hasPath(NAME_CONFIG_KEY) && c.hasPath(PATH_CONFIG_KEY))
.collect(Collectors.toList());

if (dbs.isEmpty()) {
throw notFind;
}
Set<String> toBeMove = new HashSet<>();
for (Config c : dbs) {
if (!toBeMove.add(new Property(c.getString(NAME_CONFIG_KEY),
Paths.get(database.toString(), dbPath, c.getString(NAME_CONFIG_KEY)),
Paths.get(c.getString(PATH_CONFIG_KEY), dbPath,
c.getString(NAME_CONFIG_KEY))).name)) {
throw new IllegalArgumentException(
"DB config has duplicate key:[" + c.getString(NAME_CONFIG_KEY)
+ "],please check! ");
}
}
} else {
throw notFind;
}
return config;
} else {
throw new IOException("DB config [" + value + "] not exist!");
}
}
}
}

0 comments on commit e5f84fc

Please sign in to comment.