Skip to main content
Version: v3+

FileSystem

Start from API_LEVEL 3.0 . Please refer to API_LEVEL.

Read-only file system across applications. Application B uses application A's appId to inspect, open and read a known file path from application A; this class does not provide write operations.

Constructor

Create a read-only file system instance for the target application.

constructor(appId: number)

Methods

openSync

Open a file in read-only mode

openSync(options: { path: string }): number

closeSync

Close a file descriptor

closeSync(fd: number): number

statSync

Get file information, or undefined when the file does not exist

statSync(options: {
path: string
}): { size: number; mtimeMs: number; isDir: boolean; isFile: boolean } | undefined

readSync

Read file content into a buffer

readSync(options: {
fd: number
buffer: ArrayBuffer
options?: { offset?: number; length?: number; position?: number }
}): number

readFileSync

Read an entire file. Returns a string when encoding is specified, otherwise an ArrayBuffer.

readFileSync(options: {
path: string
options?: { encoding?: string }
}): string | ArrayBuffer

Example

// ==================== Application A (data provider) ====================
import { writeFileSync } from '@zos/fs'

const path = 'shared.json'
writeFileSync({
path,
data: JSON.stringify({ theme: 'dark' }),
options: { encoding: 'utf8' },
})

// ==================== Application B (data consumer) ====================
import { FileSystem } from '@zos/share-storage'

const fs = new FileSystem(100001) // 100001 is application A's appId
const stat = fs.statSync({ path })

if (stat && stat.isFile) {
const fd = fs.openSync({ path })
const buffer = new ArrayBuffer(stat.size)
fs.readSync({ fd, buffer })
fs.closeSync(fd)
}

const content = fs.readFileSync({
path,
options: { encoding: 'utf8' },
})