Introduce file kinds

This commit is contained in:
crschnick
2023-05-30 10:08:58 +00:00
parent daa011ffe6
commit 4a21dffdab
25 changed files with 94 additions and 164 deletions

View File

@@ -2,131 +2,38 @@ package io.xpipe.core.impl;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.xpipe.core.process.ProcessControlProvider;
import io.xpipe.core.process.ShellDialects;
import io.xpipe.core.process.ShellControl;
import io.xpipe.core.store.ConnectionFileSystem;
import io.xpipe.core.store.FileSystem;
import io.xpipe.core.store.FileSystemStore;
import io.xpipe.core.store.ShellStore;
import io.xpipe.core.util.JacksonizedValue;
import java.nio.file.Path;
@JsonTypeName("local")
public class LocalStore extends JacksonizedValue implements ShellStore {
private static ShellControl local;
private static FileSystem localFileSystem;
public static ShellControl getShell() throws Exception {
public static void init() throws Exception {
local = ProcessControlProvider.createLocal(false).start();
localFileSystem = new LocalStore().createFileSystem();
}
public static ShellControl getShell() {
if (local == null) {
local = ProcessControlProvider.createLocal(false).start();
throw new IllegalStateException("Local shell not initialized yet");
}
return local;
}
public static FileSystem getFileSystem() throws Exception {
public static FileSystem getFileSystem() {
if (localFileSystem == null) {
localFileSystem = new LocalStore().createFileSystem();
throw new IllegalStateException("Local file system not initialized yet");
}
return localFileSystem;
}
@Override
public FileSystem createFileSystem() {
return new ConnectionFileSystem(ShellStore.createLocal().control(), LocalStore.this) {
@Override
public FileSystemStore getStore() {
return LocalStore.this;
}
private Path wrap(String file) {
for (var e : System.getenv().entrySet()) {
file = file.replace(
ShellDialects.getPlatformDefault().environmentVariable(e.getKey()),
e.getValue());
}
return Path.of(file);
}
// @Override
// public boolean exists(String file) {
// return Files.exists(wrap(file));
// }
//
// @Override
// public void delete(String file) throws Exception {
// Files.delete(wrap(file));
// }
//
// @Override
// public void copy(String file, String newFile) throws Exception {
// Files.copy(wrap(file), wrap(newFile), StandardCopyOption.REPLACE_EXISTING);
// }
//
// @Override
// public void move(String file, String newFile) throws Exception {
// Files.move(wrap(file), wrap(newFile), StandardCopyOption.REPLACE_EXISTING);
// }
//
// @Override
// public boolean mkdirs(String file) throws Exception {
// try {
// Files.createDirectories(wrap(file));
// return true;
// } catch (Exception ex) {
// return false;
// }
// }
//
// @Override
// public void touch(String file) throws Exception {
// if (exists(file)) {
// return;
// }
//
// Files.createFile(wrap(file));
// }
//
// @Override
// public boolean isDirectory(String file) throws Exception {
// return Files.isDirectory(wrap(file));
// }
//
// @Override
// public Stream<FileEntry> listFiles(String file) throws Exception {
// return Files.list(wrap(file)).map(path -> {
// try {
// var date = Files.getLastModifiedTime(path);
// var size = Files.isDirectory(path) ? 0 : Files.size(path);
// return new FileEntry(
// this,
// path.toString(),
// date.toInstant(),
// Files.isDirectory(path),
// Files.isHidden(path),
// Files.isExecutable(path),
// size);
// } catch (IOException e) {
// throw new UncheckedIOException(e);
// }
// });
// }
//
// @Override
// public List<String> listRoots() throws Exception {
// return StreamSupport.stream(
// FileSystems.getDefault().getRootDirectories().spliterator(), false)
// .map(path -> path.toString())
// .toList();
// }
};
}
@Override
public ShellControl createBasicControl() {
return ProcessControlProvider.createLocal(true);

View File

@@ -0,0 +1,9 @@
package io.xpipe.core.store;
public enum FileKind {
FILE,
DIRECTORY,
LINK,
OTHER;
}

View File

@@ -22,29 +22,31 @@ public interface FileSystem extends Closeable, AutoCloseable {
@NonNull
String path;
Instant date;
boolean directory;
boolean hidden;
Boolean executable;
long size;
String mode;
@NonNull
FileKind kind;
public FileEntry(
@NonNull FileSystem fileSystem, @NonNull String path, Instant date, boolean directory, boolean hidden, Boolean executable,
@NonNull FileSystem fileSystem, @NonNull String path, Instant date, boolean hidden, Boolean executable,
long size,
String mode
String mode,
@NonNull FileKind kind
) {
this.fileSystem = fileSystem;
this.mode = mode;
this.path = directory ? FileNames.toDirectory(path) : path;
this.kind = kind;
this.path = kind == FileKind.DIRECTORY ? FileNames.toDirectory(path) : path;
this.date = date;
this.directory = directory;
this.hidden = hidden;
this.executable = executable;
this.size = size;
}
public static FileEntry ofDirectory(FileSystem fileSystem, String path) {
return new FileEntry(fileSystem, path, Instant.now(), true, false, false, 0, null);
return new FileEntry(fileSystem, path, Instant.now(), true, false, 0, null, FileKind.DIRECTORY);
}
}
@@ -79,7 +81,7 @@ public interface FileSystem extends Closeable, AutoCloseable {
default Stream<FileEntry> listFilesRecursively(String file) throws Exception {
return listFiles(file).flatMap(fileEntry -> {
if (!fileEntry.isDirectory()) {
if (fileEntry.getKind() != FileKind.DIRECTORY) {
return Stream.of(fileEntry);
}