opendir/readdir
List files in a directory
interface AdbSyncEntry extends AdbSyncStat {
mode: number;
size: bigint;
mtime: bigint;
get type(): LinuxFileType;
get permission(): number;
uid?: number;
gid?: number;
atime?: bigint;
ctime?: bigint;
name: string;
}
declare class AdbSync {
opendir(path: string): AsyncGenerator<AdbSyncEntry, void, void>;
readdir(path: string): Promise<AdbSyncEntry[]>;
}
| Android 10 and below | Android 11 and above | |
|---|---|---|
| Adb feature name | None | ls_v2 |
| Sync command | LIST | LIS2 |
| Size larger than 4GB | No | Yes |
Returns uid, gid, atime and ctime | No | Yes |
opendir returns an async generator that yields file entries in the directory. readdir collects all entries and returns an array.
For a large directory with hundreds of files, readdir may take tens of seconds to finish. opendir can provide a better user experience by yielding entries as they are received.
Example
Use opendir
for await (const entry of sync.opendir("/sdcard")) {
console.log(entry.name);
}
opendir uses streaming output. If you don't read the output, the command will never finish, and blocking future commands from running.
Use readdir
const entries = await sync.readdir("/sdcard");
for (const entry of entries) {
console.log(entry.name);
}
Internal API
Note: This is an internal API that is usually not needed directly. Most users should use the public API (adb.sync.opendir or adb.sync.readdir) instead.
The opendir function uses AdbSync.OpenDir.opendir() internally, which operates on a SocketPool:
import type { SocketPool } from "@yume-chan/adb";
import { AdbSync } from "@yume-chan/adb";
declare const pool: SocketPool;
declare const path: string;
for await (const entry of AdbSync.OpenDir.opendir(pool, path, {
version: 2, // or 1 for legacy protocol
})) {
console.log(entry.name);
}
Protocol versions
Version 1 (legacy):
- Uses
LISTrequest - Returns
DENTresponses - Limited to 32-bit file sizes
- No
uid,gid,atime,ctimefields
Version 2:
- Uses
LIS2request - Returns
DNT2responses - Supports 64-bit file sizes
- Includes
uid,gid,atime,ctimefields - Can return error codes for individual entries (skipped automatically)
How it works
- Acquires a socket from the pool
- Sends a
LISTorLIS2request with the directory path - Streams
DENTorDNT2responses for each entry - Receives
DONEresponse when complete - Automatically releases the socket back to the pool
The socket is automatically released after the stream completes or errors. If a non-sync error occurs (like a network issue), the socket is discarded to prevent connection corruption.