Skip to content

A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.

Rust

Clear search results and validate query byte length (#231)

Vishnu Srivatsava0aee25b86182
diff --git a/src/SearchFiles.tsx b/src/SearchFiles.tsx
index dc3cba7..0f08ba7 100644
--- a/src/SearchFiles.tsx
+++ b/src/SearchFiles.tsx
@@ -8,6 +8,9 @@ import { formatBytes } from "./formatBytes";
import { usePagingFocus } from "./usePagingFocus";
const isSelectableFile = (node: ScanNode) => node.type === "file" && node.counted;
+// Keep in sync with MAX_SEARCH_QUERY_BYTES in scan_store.rs.
+const MAX_SEARCH_QUERY_BYTES = 255;
+const utf8 = new TextEncoder();
export function SearchFiles({ scope, folder, category, busy, onZoom, onNuke }: {
scope: ScanResponse;
@@ -20,6 +23,7 @@ export function SearchFiles({ scope, folder, category, busy, onZoom, onNuke }: {
const { generation, revision, root_path, read_only, total_size } = scope;
const [input, setInput] = useState("");
const [query, setQuery] = useState("");
+ const tooLong = input.length > MAX_SEARCH_QUERY_BYTES || utf8.encode(input).length > MAX_SEARCH_QUERY_BYTES;
const [selection, setSelection] = useState<{ scope: string; ids: Set<number> } | null>(null);
const searchInput = useRef<HTMLInputElement>(null);
const [entireScan, setEntireScan] = useState(false);
@@ -58,11 +62,20 @@ export function SearchFiles({ scope, folder, category, busy, onZoom, onNuke }: {
setPagination({ scope: pageScope, offsets: nextOffsets });
};
const submit = () => {
- if (!input) return;
+ if (!input || tooLong) return;
if (input !== query) setSelection(null);
setQuery(input);
setPagination({ scope: pageScope, offsets: [0] });
};
+ const clearSearch = () => {
+ setInput("");
+ setQuery("");
+ setPagination({ scope: pageScope, offsets: [0] });
+ setSelection(null);
+ setRetry(0);
+ setState({ key: "" });
+ searchInput.current?.focus();
+ };
const results = data?.results ?? [];
const pageFileIds = category ? [] : results.filter(({ node }) => isSelectableFile(node)).map(({ node }) => node.id);
const pageSelected = pageFileIds.every(id => selected?.has(id));
@@ -94,13 +107,22 @@ export function SearchFiles({ scope, folder, category, busy, onZoom, onNuke }: {
<h2 className="text-sm font-semibold text-zinc-200">{FILE_TYPE_LABELS[category.kind]}</h2>
<button type="button" autoFocus disabled={busy} onClick={category.clear}
className="text-xs text-accent-400 hover:underline">Clear filter</button>
- </div> : <form className="flex items-center gap-2 py-3" onSubmit={event => { event.preventDefault(); submit(); }}>
- <input id="search-input" ref={searchInput} value={input} disabled={busy}
- onChange={event => setInput(event.target.value)}
- placeholder={activeFolder ? "Search names in this folder" : "Search names in this scan"}
- className="flex-1 bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm text-zinc-200" />
- <button type="submit" id="search-submit" disabled={busy || !input}
- className="px-3 py-1.5 rounded-lg text-xs font-medium bg-zinc-800 text-zinc-300 border border-zinc-700/50 disabled:text-zinc-600">Search</button>
+ </div> : <form className="py-3" onSubmit={event => { event.preventDefault(); submit(); }}>
+ <div className="flex items-center gap-2">
+ <input id="search-input" ref={searchInput} value={input} disabled={busy}
+ aria-label="Search file and folder names" aria-invalid={tooLong}
+ aria-describedby={tooLong ? "search-length-error" : undefined}
+ onChange={event => setInput(event.target.value)}
+ placeholder={activeFolder ? "Search names in this folder" : "Search names in this scan"}
+ className="min-w-0 flex-1 bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm text-zinc-200" />
+ <button type="submit" id="search-submit" disabled={busy || !input || tooLong}
+ className="px-3 py-1.5 rounded-lg text-xs font-medium bg-zinc-800 text-zinc-300 border border-zinc-700/50 disabled:text-zinc-600">Search</button>
+ {(input || query) && <button type="button" disabled={busy} onClick={clearSearch}
+ className="shrink-0 text-xs text-accent-400 hover:underline disabled:text-zinc-600">Clear search</button>}
+ </div>
+ <p id="search-length-error" role="status" className={tooLong ? "mt-2 text-xs text-red-400" : "sr-only"}>
+ {tooLong ? `Search text is too long (maximum ${MAX_SEARCH_QUERY_BYTES} UTF-8 bytes).` : ""}
+ </p>
</form>}
{folder && <div className="flex flex-wrap items-center gap-2 pb-3 text-xs text-zinc-400">
<span className="min-w-0 break-all">{activeFolder ? "Folder: " + activeFolder.name : "Entire scan"}</span>
diff --git a/tests/scan_ui_fixture.js b/tests/scan_ui_fixture.js
index d582cc4..be62d7f 100644
--- a/tests/scan_ui_fixture.js
+++ b/tests/scan_ui_fixture.js
@@ -222,7 +222,7 @@
}
if (command === "get_search_results") {
const category = args.fileType;
- check(typeof args.query === "string" && args.query.length <= 255
+ check(typeof args.query === "string" && new TextEncoder().encode(args.query).length <= 255
&& (category ? args.query === "" && args.folderId === null : args.query !== ""), "Invalid search filter");
check(Number.isInteger(args.skip) && args.skip >= 0, "Invalid search skip");
check(args.folderId === null || args.folderId === 1, "Unexpected search folder");
@@ -694,12 +694,17 @@
const searchRows = () => [...document.querySelectorAll(".search-result-row")];
const searchCheckbox = (row = searchRows()[0]) => row?.querySelector('input[type="checkbox"]');
const selectionCount = () => document.querySelector("#search-selection-count")?.textContent ?? "";
- const searchFor = text => {
- const input = document.querySelector("#search-input");
+ const searchField = () => document.querySelector("#search-input");
+ const searchSubmit = () => document.querySelector("#search-submit");
+ const setSearchText = (text, inputType = "insertText") => {
+ const input = searchField();
const set = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
set.call(input, text);
- input.dispatchEvent(new Event("input", { bubbles: true }));
- document.querySelector("#search-submit").click();
+ input.dispatchEvent(new InputEvent("input", { bubbles: true, inputType, data: text }));
+ };
+ const searchFor = text => {
+ setSearchText(text);
+ searchSubmit().click();
};
const searchNext = () => [...document.querySelectorAll("button")].find(item => item.textContent.trim() === "Next" && !item.disabled);
zoomFolder();
@@ -714,6 +719,40 @@
check(document.activeElement === searchCheckbox(), "First search checkbox is missing from the tab order");
await pressKey("Space");
check(searchCheckbox().checked && selectionCount() === "1 file selected", "Space did not select a search result");
+ check(searchField().getAttribute("aria-label") === "Search file and folder names", "Search field has no persistent name");
+ holdSearch = true;
+ searchNext().click();
+ await waitFor(() => resolveSearch, "Folder page to clear did not start");
+ setSearchText("", "deleteContentBackward");
+ button("Clear search").focus();
+ await pressKey("Enter");
+ await waitFor(() => !selectionCount() && document.activeElement === searchField(), "Clear search did not reset selection and focus");
+ resolveSearch();
+ resolveSearch = undefined;
+ await pause(30);
+ check(searchField().value === "" && searchRows().length === 0 && !searchNext() && !button("Clear search")
+ && button("Search entire scan"), "Cleared search restored a stale page or changed scope");
+
+ const searchCalls = () => calls.filter(call => call.command === "get_search_results");
+ const queriesBeforePaste = searchCalls().length;
+ const atLimit = " " + "\u00e9".repeat(126) + "x ";
+ setSearchText(atLimit + " ", "insertFromPaste");
+ check(searchSubmit().disabled && searchField().getAttribute("aria-invalid") === "true"
+ && document.getElementById(searchField().getAttribute("aria-describedby"))?.textContent.includes("255 UTF-8 bytes"),
+ "Oversized multibyte paste was not described or blocked");
+ await pressKey("Enter");
+ check(searchCalls().length === queriesBeforePaste, "Oversized search reached IPC");
+ setSearchText(atLimit, "insertFromPaste");
+ check(!searchSubmit().disabled && searchField().getAttribute("aria-invalid") === "false", "255-byte multibyte search was blocked");
+ await pressKey("Enter");
+ await waitFor(() => searchCalls().length === queriesBeforePaste + 1, "255-byte search did not reach IPC");
+ check(searchCalls().at(-1).args.query === atLimit && searchCalls().at(-1).args.folderId === 1
+ && searchCalls().at(-1).args.skip === 0, "Search altered whitespace, scope, or offset after clearing");
+ searchFor("nested");
+ await waitFor(() => searchRows().length === 37, "Cleared query could not be entered again");
+ check(!searchCheckbox().checked && !selectionCount(), "Cleared query restored its selection");
+ searchCheckbox().click();
+
holdSearch = true;
searchNext().focus();
await pressKey("Enter");