Skip to main content
Version: next

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 belowAndroid 11 and above
Adb feature nameNonels_v2
Sync commandLISTLIS2
Size larger than 4GBNoYes
Returns uid, gid, atime and ctimeNoYes

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);
}
output must be used!

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

info

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 LIST request
  • Returns DENT responses
  • Limited to 32-bit file sizes
  • No uid, gid, atime, ctime fields

Version 2:

  • Uses LIS2 request
  • Returns DNT2 responses
  • Supports 64-bit file sizes
  • Includes uid, gid, atime, ctime fields
  • Can return error codes for individual entries (skipped automatically)

How it works

  1. Acquires a socket from the pool
  2. Sends a LIST or LIS2 request with the directory path
  3. Streams DENT or DNT2 responses for each entry
  4. Receives DONE response when complete
  5. 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.