Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/app/src/main/java/dev/peanuts/ui/LibraryScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.regex.Pattern;

/**
* The library: every game in the roots (the game folder, then the update and DLC folders; each walked two levels deep) as *.nsp, *.xci, *.nsz, *.xcz, *.nup or a dump folder
* The library: every game in the roots (the game folder, then the update and DLC folders; including nested folders) as *.nsp, *.xci, *.nsz, *.xcz, *.nup or a dump folder
* (a version with exefs/main in a layout upstream's DumpLayout reads; mods left out: LibraryWalk), grouped by the base game's title ID, with a status per title.
* <p>
* An NSP/XCI's titles come from the importer's probe (ImporterPort.probe: title id, application id, kind, version, name), kept in files/library-cache.json by
Expand Down Expand Up @@ -299,7 +299,7 @@ private static VersionInfo versionInfo(String titleId) {

// ---- The walk ----

/** A root's dump folders and game files (LibraryWalk: two levels deep, mods left out) */
/** A root's dump folders and game files (LibraryWalk: recursive, mods left out) */
private void walk(File root, List<Source> out) {
LibraryWalk.walk(root, 0, new LibraryWalk.Sink() {
@Override public void dump(File d) { out.addAll(LibraryScanner.this.dump(d)); }
Expand Down
11 changes: 9 additions & 2 deletions android/app/src/main/java/dev/peanuts/ui/LibraryWalk.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package dev.peanuts.ui;

import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/**
* The library's walk over one root (LibraryScanner.scan): the dump folders and the files it finds, each root walked two levels deep. A dump folder holds the
* The library's walk over one root (LibraryScanner.scan): the dump folders and the files it finds, including nested folders. A dump folder holds the
* game's code in a layout upstream's DumpLayout reads ({@link #isDump}), and is not walked further. Mods are left out (issue #46): a mod for Eden or Atmosphere is
* a folder with exefs/ and romfs/ too, but its exefs/ holds patches and at most its own main.npdm and main, never the SDK's module sdk ({@link #wholeCode});
* and the walk does not go into a folder named mods, load (Eden's and yuzu's) or atmosphere below the root. Before, a mod with a main.npdm was taken as a dump
Expand All @@ -16,6 +17,10 @@
final class LibraryWalk {
private LibraryWalk() {}

// PEANUTS(upstream-candidate): bound recursive scans and skip large shared-storage trees at the root.
private static final int MAX_DEPTH = 12;
private static final Set<String> STORAGE_FOLDERS = Set.of("Android", "DCIM");

/** Folders the walk does not go into: where emulators and Atmosphere keep mods */
private static final Set<String> MOD_FOLDERS = new HashSet<>(Arrays.asList("mods", "load", "atmosphere"));

Expand All @@ -33,9 +38,11 @@ static void walk(File dir, int depth, Sink out) {
String n = f.getName();
if (n.startsWith(".") || n.endsWith("-work")) continue;
if (f.isDirectory()) {
// PEANUTS(upstream-candidate): keep nested collections searchable and skip directory symlink loops.
if (depth >= MAX_DEPTH || (depth == 0 && STORAGE_FOLDERS.contains(n)) || Files.isSymbolicLink(f.toPath())) continue;
if (MOD_FOLDERS.contains(n.toLowerCase(Locale.ROOT))) continue;
if (isDump(f)) out.dump(f);
else if (depth < 2) walk(f, depth + 1, out);
else walk(f, depth + 1, out);
} else out.file(f);
}
}
Expand Down
19 changes: 19 additions & 0 deletions peanuts/tests/test_library_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ def setUpClass(cls):
def tearDownClass(cls):
cls.tmp.cleanup()

def test_nested_sources_and_depth_limit(self):
root = Path(self.tmp.name) / 'nested'
deep = 'Games/Publisher/Series/Title'
cap = '/'.join(['nested'] * 12)
files = [f'{deep}/Updates/game.nup', 'Games/Android/game.nsp', f'{cap}/game.xci']
dump = f'{deep}/Dump'
for rel in [*files, f'{dump}/base/exefs/main', f'{dump}/romfs/ignored.nsp',
'Android/ignored.nsp', 'DCIM/ignored.nsp', f'{cap}/extra/ignored.nsp']:
f = root / rel
f.parent.mkdir(parents=True, exist_ok=True)
f.touch()
try:
(root / 'Games/back').symlink_to(root, target_is_directory=True)
except (NotImplementedError, OSError):
pass
out = subprocess.check_output(['java', '-cp', str(self.classes), 'dev.peanuts.ui.LibraryWalkTest', str(root)],
text=True, timeout=10)
self.assertCountEqual(out.splitlines(), [f'dump -1 {dump}', *(f'file {f}' for f in files)])

def test_mods_are_no_dumps(self):
self.assertEqual(sorted(self.dumps), DUMPS, 'only the real dump folders, each once, as the dump and not one of its versions')

Expand Down