Clear temp directory

This commit is contained in:
crschnick 2023-06-06 22:57:13 +00:00
parent 8406d4c03b
commit e72fc2c487
2 changed files with 18 additions and 16 deletions

View file

@ -15,6 +15,8 @@ import java.util.stream.Stream;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
public interface ShellDialect {
CommandControl prepareTempDirectory(ShellControl shellControl, String directory);
String initFileName(ShellControl sc) throws Exception;
CommandControl directoryExists(ShellControl shellControl, String directory);

View file

@ -1,10 +1,8 @@
package io.xpipe.core.util;
import io.xpipe.core.impl.FileNames;
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellControl;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;
@ -14,6 +12,21 @@ public class XPipeTempDirectory {
return proc.getOsType().getTempDirectory(proc);
}
public static String initXPipeTempDirectory(ShellControl proc) throws Exception {
var base = proc.getOsType().getTempDirectory(proc);
var arr = Stream.of(base, "xpipe").toArray(String[]::new);
var dir = FileNames.join(arr);
var existsCommand = proc.getShellDialect().createFileExistsCommand(proc, dir);
if (existsCommand.executeAndCheck()) {
proc.executeSimpleCommand(proc.getShellDialect().getFileDeleteCommand(dir));
}
proc.getShellDialect().prepareTempDirectory(proc, dir);
return dir;
}
public static String getSubDirectory(ShellControl proc, String... sub) throws Exception {
var base = proc.getOsType().getTempDirectory(proc);
var arr = Stream.concat(Stream.of(base, "xpipe"), Arrays.stream(sub)).toArray(String[]::new);
@ -21,22 +34,9 @@ public class XPipeTempDirectory {
var existsCommand = proc.getShellDialect().createFileExistsCommand(proc, dir);
if (!existsCommand.executeAndCheck()) {
proc.executeSimpleCommand(
proc.getShellDialect().getMkdirsCommand(dir),
"Unable to access or create temporary directory " + dir);
if (proc.getOsType().equals(OsType.LINUX) || proc.getOsType().equals(OsType.MACOS)) {
proc.executeSimpleBooleanCommand("chmod 777 \"" + dir + "\"");
}
proc.getShellDialect().prepareTempDirectory(proc,dir).execute();
}
return dir;
}
public static void clearSubDirectory(ShellControl proc) throws Exception {
var dir = getSubDirectory(proc);
if (!proc.executeSimpleBooleanCommand(proc.getShellDialect().getFileDeleteCommand(dir))) {
throw new IOException("Unable to delete temporary directory " + dir);
}
}
}