Skip to content

Commit

Permalink
Added some comments and refactored a little, see GH issue #347
Browse files Browse the repository at this point in the history
  • Loading branch information
wolpi committed Jun 9, 2024
1 parent 7bab635 commit e554564
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions primitiveFTPd/src/org/primftpd/filesystem/FsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,39 @@
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.Map.entry;

public abstract class FsFile<T> extends AbstractFile {

protected final File file;
protected final boolean injectedDirectory;

private final static Map<String, String[]> DIRECTORY_INJECTIONS = Map.ofEntries(
// entry("/", new String[] {"dev", "etc", "mnt", "proc", "product", "storage", "system", "vendor"}), // comment out the line below if you enable this injection!!!
entry("/", new String[] {"storage"}),
entry("/storage/emulated", new String[] {"0"})
);
private final static Map<String, String[]> DIRECTORY_INJECTIONS;
static {
Map<String, String[]> tmp = new HashMap<>();
// more known directories might be added
//tmp.put("/", new String[] {"dev", "etc", "mnt", "proc", "product", "storage", "system", "vendor"});
tmp.put("/", new String[] {"storage"});
tmp.put("/storage/emulated", new String[] {"0"});
DIRECTORY_INJECTIONS = Collections.unmodifiableMap(tmp);
}

private final static Set<String> CUSTOMIZED_DIRECTORIES;
private final static Set<String> INJECTIONS_AND_CHILDREN;
static {
CUSTOMIZED_DIRECTORIES = new HashSet<String>();
Set<String> tmp = new HashSet<>();
for (Map.Entry<String, String[]> entry : DIRECTORY_INJECTIONS.entrySet()) {
String k = entry.getKey();
CUSTOMIZED_DIRECTORIES.add(k);
tmp.add(k);
for (String v : entry.getValue()) {
CUSTOMIZED_DIRECTORIES.add(k + File.separator + v);
tmp.add(k + File.separator + v);
}
}
INJECTIONS_AND_CHILDREN = Collections.unmodifiableSet(tmp);
}

public FsFile(File file, PftpdService pftpdService) {
Expand All @@ -54,7 +60,7 @@ public FsFile(File file, PftpdService pftpdService) {
pftpdService);
this.file = file;
this.name = file.getName();
this.injectedDirectory = file.isDirectory() && CUSTOMIZED_DIRECTORIES.contains(file.getAbsolutePath());
this.injectedDirectory = file.isDirectory() && INJECTIONS_AND_CHILDREN.contains(file.getAbsolutePath());
}

protected abstract T createFile(File file, PftpdService pftpdService);
Expand Down Expand Up @@ -167,7 +173,10 @@ public List<T> listFiles() {
logger.trace("[{}] listFiles()", name);
postClientAction(ClientActionEvent.ClientAction.LIST_DIR);
File[] filesArray = file.listFiles();

// if the OS did not provide child elements:
if (filesArray == null) {
// check if requested file is among injected ones
String[] folders = DIRECTORY_INJECTIONS.get(file.getAbsolutePath());
if (folders != null) {
filesArray = new File[folders.length];
Expand All @@ -176,6 +185,7 @@ public List<T> listFiles() {
}
}
}

if (filesArray != null) {
List<T> files = new ArrayList<>(filesArray.length);
for (File file : filesArray) {
Expand Down

0 comments on commit e554564

Please sign in to comment.