Оптимизация команды Exists для SSH

This commit is contained in:
2023-12-04 14:42:36 +03:00
parent 1f8ebdc9a2
commit 163552d74f
13 changed files with 93 additions and 100 deletions

View File

@@ -85,7 +85,7 @@ public abstract class ConnectionPass<T> extends Pass_2021<T> {
isConnected = true; // теперь можно прерывать метод.
if (needsInitialize()) {
RemoteFile userWorkspace = new RemoteFile(user.workspace, true);
if (!Exists(sftpChannel.getHome(), userWorkspace.name))
if (!Exists(userWorkspace))
throw new WorkspaceNotFoundException(
"Рабочее пространство пользователя " + Utils.Brackets(user.login)
+ " на машине " + Utils.Brackets(machine.getURL())
@@ -171,8 +171,8 @@ public abstract class ConnectionPass<T> extends Pass_2021<T> {
sftpChannel.get(src, dst);
}
//с проверкой.
public boolean tryGetSingleFile(RemoteFile src, File dst, int maxSize) throws Exception {
if (Exists(src.parent, src.name)) {
public boolean tryGetSingleFileWithMaxSize(RemoteFile src, File dst, int maxSize) throws Exception {
if (Exists(src)) {
if ((maxSize == 0) || (getFileKBSize(src.full_name) <= maxSize)) {
getSingleFile(src.full_name, dst.getAbsolutePath());
return true;
@@ -186,19 +186,11 @@ public abstract class ConnectionPass<T> extends Pass_2021<T> {
sftpChannel.put(src.getAbsolutePath(), dst.full_name);
}
public void tryMKDir(RemoteFile dir) throws Exception {
// System.out.print("try mkdir: '" + dir.full_name);
if (!Exists(dir.parent, dir.name)) sftpChannel.mkdir(dir.full_name);
// System.out.println("..done");
if (!Exists(dir)) sftpChannel.mkdir(dir.full_name);
}
public void tryRM(RemoteFile file) throws Exception {
// System.out.print("try remove: '" + file.full_name);
if (Exists(file.parent, file.name)) {
// System.out.print("' :exists.needs remove..");
if (Exists(file))
sftpChannel.rm(file.full_name);
// System.out.println(" +");
} else {
// System.out.println("no such file");
}
}
public void putSingleFile(String src, String dst) throws Exception {
sftpChannel.put(src, dst);
@@ -213,16 +205,6 @@ public abstract class ConnectionPass<T> extends Pass_2021<T> {
}
protected void ServerAction() throws Exception {
}
//тут имя файла короткое.
public boolean Exists(String folder, String name) throws Exception {
Vector<ChannelSftp.LsEntry> files = sftpChannel.ls(folder);
for (ChannelSftp.LsEntry file : files) {
if (file.getFilename().equals(name)) {
return true;
}
}
return false;
}
//https://losst.ru/komanda-find-v-linux#%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D0%BD%D1%8B%D0%B5_%D0%BF%D0%B0%D1%80%D0%B0%D0%BC%D0%B5%D1%82%D1%80%D1%8B_%D0%BA%D0%BE%D0%BC%D0%B0%D0%BD%D0%B4%D1%8B_find
public String getStarter() {
return String.join("/", user.workspace, modules, starter);
@@ -385,7 +367,7 @@ public abstract class ConnectionPass<T> extends Pass_2021<T> {
//скорее всего,временные методы. они есть в UserConnection, при удаленном запуске тестирования
//--------------------------------------------------------------------------------
public void MKDIR(RemoteFile dir) throws Exception {
if (!Exists(dir.parent, dir.name)) sftpChannel.mkdir(dir.full_name);
if (!Exists(dir)) sftpChannel.mkdir(dir.full_name);
}
//--
public Pair<RemoteFile, RemoteFile> performScript(RemoteFile directory, String... commands) throws Exception {
@@ -398,7 +380,7 @@ public abstract class ConnectionPass<T> extends Pass_2021<T> {
files.add(out);
files.add(err);
for (RemoteFile file : files) {
if (Exists(directory.full_name, file.name))
if (Exists(directory))
sftpChannel.rm(file.full_name);
}
//--
@@ -406,9 +388,27 @@ public abstract class ConnectionPass<T> extends Pass_2021<T> {
//--
ShellCommand("cd " + Utils.DQuotes(directory.full_name),
script_file.full_name + " 1>" + Constants.out_file + " 2>" + Constants.err_file);
return new Pair<>(out, err);
}
//--
//--
public boolean Exists(String file_full_name) throws Exception {
try {
sftpChannel.stat(file_full_name);
return true;
} catch (SftpException e) {
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
// file doesn't exist
return false;
} else {
// something else went wrong
throw e;
}
}
}
public boolean Exists(RemoteFile file) throws Exception {
return Exists(file.full_name);
}
}