fix(adb): preserve device paths and prevent destination collisions
Interpret device paths once as POSIX paths and apply only the selected host's filename constraints. Preserve original PM paths and diagnostics, reject duplicate destinations before pulls, and use exact-name filesystem reservations to detect case and Unicode aliases without guessing volume rules. Consolidate failed-package cleanup ownership and strengthen no-output assertions. Validated 44 provider fixture tests, 47 filename/parser tests, six compiled CLI tests, static/docs checks, and package checks. No Android hardware was used.
This commit is contained in:
+34
-42
@@ -1,61 +1,53 @@
|
||||
import { posix } from "node:path";
|
||||
|
||||
/** Why a device-reported path cannot be used as a host file name. */
|
||||
export type AdbHostFileNameReason =
|
||||
| "empty_file_name"
|
||||
| "not_absolute_path"
|
||||
| "dot_segment"
|
||||
| "path_separator"
|
||||
| "control_character"
|
||||
| "unsupported_character"
|
||||
| "reserved_name"
|
||||
| "trailing_dot_or_space"
|
||||
| "alternate_data_stream";
|
||||
| "trailing_dot_or_space";
|
||||
|
||||
/** A portable host file name, or the exact reason the device name was refused. */
|
||||
export type AdbHostFileNameResult =
|
||||
| { readonly ok: true; readonly fileName: string }
|
||||
| { readonly ok: false; readonly reason: AdbHostFileNameReason };
|
||||
|
||||
/**
|
||||
* Win32 resolves these basenames to a device regardless of case and of any
|
||||
* extension (`NUL`, `nul.txt`), so a portable host file name must avoid them on
|
||||
* every host. `(?:\.|$)` keeps ordinary names such as `console.txt` admissible.
|
||||
*/
|
||||
const WINDOWS_RESERVED_BASENAME =
|
||||
/^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/iu;
|
||||
|
||||
/** Characters that are not portable in a host file name on Windows or POSIX. */
|
||||
const UNSUPPORTED_CHARACTER = /[*?"<>|]/u;
|
||||
|
||||
/** Control characters (`Cc`), including C0 controls and DEL. */
|
||||
const CONTROL_CHARACTER = /\p{Cc}/u;
|
||||
/^(?:con|prn|aux|nul|com[1-9¹²³]|lpt[1-9¹²³])(?:\.|$)/iu;
|
||||
|
||||
/**
|
||||
* Derive a portable host file name from a device-reported path.
|
||||
*
|
||||
* The device path is a POSIX path on the device filesystem, so its last
|
||||
* `/`-separated segment is taken with POSIX semantics rather than the host
|
||||
* `basename` (which would vary between POSIX and Windows hosts). A name is
|
||||
* accepted only when every host can store it and address it back unambiguously:
|
||||
* no separator, control, or reserved-device characters, no trailing dot or
|
||||
* space, no `:` alternate-data-stream marker, and no `.`/`..` segment.
|
||||
* Derive the final component of a device POSIX path without normalizing the
|
||||
* source. A trailing slash has no filename and must not silently become the
|
||||
* preceding component as `basename` would make it.
|
||||
*/
|
||||
export const adbHostFileName = (devicePath: string): AdbHostFileNameResult => {
|
||||
const fileName = posix.basename(devicePath);
|
||||
if (fileName === "") return { ok: false, reason: "empty_file_name" };
|
||||
export const adbHostFileName = (
|
||||
devicePath: string,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
): AdbHostFileNameResult => {
|
||||
if (devicePath === "" || devicePath.endsWith("/"))
|
||||
return { ok: false, reason: "empty_file_name" };
|
||||
if (devicePath.includes("\0"))
|
||||
return { ok: false, reason: "unsupported_character" };
|
||||
if (!devicePath.startsWith("/"))
|
||||
return { ok: false, reason: "not_absolute_path" };
|
||||
const fileName = devicePath.slice(devicePath.lastIndexOf("/") + 1);
|
||||
if (fileName === "." || fileName === "..")
|
||||
return { ok: false, reason: "dot_segment" };
|
||||
if (fileName.includes("\\") || fileName.includes("/"))
|
||||
return { ok: false, reason: "path_separator" };
|
||||
if (CONTROL_CHARACTER.test(fileName))
|
||||
return { ok: false, reason: "control_character" };
|
||||
if (fileName.includes(":"))
|
||||
return { ok: false, reason: "alternate_data_stream" };
|
||||
if (UNSUPPORTED_CHARACTER.test(fileName))
|
||||
return { ok: false, reason: "unsupported_character" };
|
||||
if (WINDOWS_RESERVED_BASENAME.test(fileName))
|
||||
return { ok: false, reason: "reserved_name" };
|
||||
if (/[. ]$/u.test(fileName))
|
||||
return { ok: false, reason: "trailing_dot_or_space" };
|
||||
if (platform === "win32") {
|
||||
if (/[\\:*?"<>|]/u.test(fileName) || hasWindowsControl(fileName))
|
||||
return { ok: false, reason: "unsupported_character" };
|
||||
if (WINDOWS_RESERVED_BASENAME.test(fileName))
|
||||
return { ok: false, reason: "reserved_name" };
|
||||
if (/[. ]$/u.test(fileName))
|
||||
return { ok: false, reason: "trailing_dot_or_space" };
|
||||
}
|
||||
|
||||
return { ok: true, fileName };
|
||||
};
|
||||
|
||||
const hasWindowsControl = (fileName: string): boolean => {
|
||||
for (const character of fileName) {
|
||||
const code = character.codePointAt(0)!;
|
||||
if (code > 0 && code < 0x20) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user