isDirectory
This method uses a trick of lstat to check if a path is a directory, or a symlink to a directory.
declare class AdbSync {
isDirectory(path: string): Promise<boolean>;
}
As stated in lstat/stat page, sync protocol doesn't support stat system call (which follows symlinks) until Android 8. This method doesn't use stat to follow symlinks, but appends a / to the path and calls lstat instead. Which means this method works on all Android versions.
If a file system error (e.g. target not exist) occurs, this method will return false.
Example
const isDirectory = await sync.isDirectory("/sdcard");
Although not exactly the same:
adb shell stat -L -c %F /sdcard
Internal API
Note: This is an internal API that is usually not needed directly. Most users should use the public API (adb.sync.isDirectory) instead.
The isDirectory method uses AdbSync.Stat.lstat() internally with a path modification:
import type { SocketPool } from "@yume-chan/adb";
import { AdbSync } from "@yume-chan/adb";
declare const pool: SocketPool;
declare const path: string;
try {
// Appends "/" to path and calls lstat
await AdbSync.Stat.lstat(pool, path + "/", {
version: 2, // or 1 for legacy protocol
});
return true; // If lstat succeeds, it's a directory
} catch {
return false; // If lstat fails, it's not a directory
}
How it works
The trick works because:
- On Unix-like systems (including Android), appending
/to a path forces the kernel to check if the path is a directory - If the path is a directory or symlink to a directory,
lstatsucceeds - If the path is a file or doesn't exist,
lstatfails with an error
This approach works on all Android versions, unlike stat which requires Android 8+ for symlink resolution.