Quark #

Modules #

Quark v1.2.0 Documentation


Table of Contents

quark/fs #

Enum: FileOpenFlag #

FileOpenFlag #

Opening file Flags

  • FOPEN_ACCMODE
  • FOPEN_RDONLY
    Open a read-only file. The file must exist.
  • FOPEN_WRONLY
    Open a write-only file. The file must exist.
  • FOPEN_RDWR
    Open a file for reading and writing. The file must exist.
  • FOPEN_CREAT
    Create the file if it does not exist
  • FOPEN_EXCL
    Create a file as executable
  • FOPEN_NOCTTY
    No Controlling TTY
  • FOPEN_TRUNC
    Clear the file contents if the file exists
  • FOPEN_APPEND
    Move the file read cursor to the end if the file exists
  • FOPEN_NONBLOCK
    Blocking mode, usually using the open function
  • FOPEN_R
    r, open the file for reading only, the file must exist
  • FOPEN_W
    w, Open a write-only file. If the file exists, the file length is cleared to zero, that is,
    the file content will disappear. If the file does not exist, it will be created.
  • FOPEN_A
    a, Open a write-only file in append mode. If the file does not exist, it will be created.
    If the file exists, the written data will be added to the end of the file, that is,
    the original content of the file will be retained.
  • FOPEN_RP
    r+,
    Open a file for reading and writing. The file must exist.
  • FOPEN_WP
    w+, Open the file for reading and writing. If the file exists,
    the file length will be cleared to zero, that is, the file content will disappear.
    If the file does not exist, it will be created.
  • FOPEN_AP
    a+, Open a readable and writable file in append mode. If the file does not exist,
    it will be created. If the file exists, the written data will be added to the end of the file,
    that is, the original content of the file will be retained.

Enum: FileType #

FileType #

File type

  • FTYPE_UNKNOWN
    Unknown Type
  • FTYPE_FILE
    Normal File
  • FTYPE_DIR
    Directory)
  • FTYPE_LINK
    Symbolic link files(Soft Link)
  • FTYPE_FIFO
    Named pipes, a special type of file used for interprocess communication (IPC)
  • FTYPE_SOCKET
    Socket
  • FTYPE_CHAR
    Unbuffered character devices, such as keyboard and mouse (/dev/input/), terminal (/dev/tty)
  • FTYPE_BLOCK
    Provides buffered, block-oriented (fixed-size data blocks) I/O access,
    such as hard disk drives (/dev/sda, /dev/sda1, /dev/nvme0n1p1)

defaultMode #

Create and set the default mode value of the file,
which is related to the file's permissions. This is an int integer type value

  • @const defaultMode:

StreamResponseCallback #

  • @callback StreamResponseCallback(stream)
  • @param stream: {StreamResponse}

Interface: Dirent #

Dirent #

The result returned by calling readdir/readdirSync

dirent.name #

File name

dirent.pathname #

The full path to the file

dirent.type #

File Type

Class: FileStat #

FileStat #

filestat.isValid() #

Is the file valid?

filestat.isFile() #

Is it a normal file?

filestat.isDir() #

Is it a directory?

Is it a symbolic link?

filestat.isSock() #

Is it a socket?

filestat.mode() #

File permission mask

filestat.type() #

File type

filestat.group() #

System Group ID

filestat.owner() #

System User ID

filestat.size() #

File size

Number of hard links to the file

filestat.ino() #

The file system specific "Inode" number of the file

filestat.blksize() #

The file system block size used for I/O operations

filestat.blocks() #

The number of blocks allocated for this file

filestat.flags() #

flags

filestat.gen() #

gen

filestat.dev() #

The device ID that contains the file

filestat.rdev() #

If the file represents a device, the numeric device identifier

filestat.atime() #

Timestamp indicating the last time this file was accessed

filestat.mtime() #

A timestamp indicating when this file was last modified

filestat.ctime() #

Timestamp indicating the last time the file's status changed

filestat.birthtime() #

A timestamp indicating when this file was created

Interface: Stream #

Stream #

stream.pause() #

Pause stream reading

stream.resume() #

Resume stream reading

Interface: StreamResponse #

StreamResponse #

streamresponse.size #

Data size

streamresponse.total #

Total data size

streamresponse.data #

It is Data

streamresponse.ended #

Is it over

Class: AsyncTask #

AsyncTask #

  • @extends Promise
  • @template <T>

asynctask.id #

Asynchronous I/O task id

  • @get id:

asynctask.complete #

Whether the asynchronous task is completed

  • @get complete:

asynctask.abort(reason?) #

Abort I/O task

  • @param reason?: Error If the parameter is passed in, an exception will be thrown
  • @return void

chmodSync(path,mode?) #

Synchronously change file permissions

chmodSync('my_file.txt', 0o775)

chownSync(path,owner,group) #

Synchronize the owner and group of the configuration file

mkdirSync(path,mode?) #

Create a directory synchronously. If the path already exists, an exception will be thrown.

mkdirsSync(path,mode?) #

Synchronously recursively create directories,
and no exception is thrown if the directory exists

renameSync(name,newName) #

Synchronously rename files and directories

linkSync(src,target) #

Synchronously create file hard links

  • @param src: string Original file path
  • @param target: string Link target path
  • @return void

unlinkSync(path) #

Synchronously delete the file hard link.
If the file has only one link, the file will be physically deleted.

rmdirSync(path) #

Synchronously delete the file directory, the file directory must be empty

readdirSync(path) #

Synchronously read the file directory file list

statSync(path) #

Synchronously read file status information

existsSync(path) #

Synchronously check if a file exists

isFileSync(path) #

Synchronously checks if the path is a file, and returns false if the path does not exist

isDirectorySync(path) #

Synchronously checks if the path is a directory, and returns false if the path does not exist

readableSync(path) #

Synchronously checks if the path is readable, returning false if the path does not exist

writableSync(path) #

Synchronously checks if the path is writable, returning false if the path does not exist

executableSync(path) #

Synchronously checks if it is executable, returns false if the path does not exist

chmodRecursionSync(path,mode?) #

Synchronously recursively set the file permission attribute mode
(TODO: will block the calling thread, please use with caution)

For example:

fs.chmodRecursionSync(mypath, 0755);

chownRecursionSync(path,owner,group) #

Synchronously recursively set file owner and group attributes
(TODO: will block the calling thread, please use with caution)

  • @param owner: Uint System User ID
  • @param group: Uint System Group ID
  • @param path: string
  • @return void

removeRecursionSync(path) #

Synchronous recursive deletion of directories or files,
use this method with caution, it may cause the thread to be blocked for a long time

copyRecursionSync(path,target) #

Synchronous recursive copy of files,
use this method with caution, it may cause the thread to be blocked for a long time

copySync(path,target) #

Copy files synchronously (TODO: will block the calling thread, please use with caution)
Same as: copy files and directoriescopyRecursionSync(path,target)

writeFileSync(path,data,size?) #

Synchronously write data to a file

  • @param size?: Uint Write all data when no parameters are passed
  • @param path: string
  • @param data: Uint8Array
  • @return Uint

writeFileSync(path,data,encoding?) #

Synchronously write a string to a file

  • @param encoding?: Encoding If not passed in, the default encoding is utf-8
  • @param path: string
  • @param data: string
  • @return Uint

readFileSync(path) #

Reading files synchronously

readFileSync(path,encoding) #

Synchronously read a file as a string

openSync(path,flags?) #

Opens a file by path synchronously and returns an open file handle

  • @param flags?: FileOpenFlag Open file flags mask Default as FileOpenFlag.FOPEN_R
  • @param path: string
  • @return Uint

closeSync(fd) #

Close the file handle synchronously

readSync(fd,out,size?,offsetFd?) #

Synchronously read the file contents from the file handle

  • @param fd: Uint Open file handles
  • @param out: Uint8Array Read the file and save it here
  • @param size?: Int If not passed or passed in -1, the length of the out parameter is used
  • @param offsetFd?: Int If not passed or -1 is passed,
    the internal offset value of the file handle is used (it will advance after each read)
  • @return Uint Returns the size of the data actually read

writeSync(fd,data,size?,offsetFd?) #

Synchronously write data to a file handle

  • @param fd: Uint Open file handles
  • @param data: Uint8Array Data to be written
  • @param size?: Int If not passed or -1 is passed, all data will be written
  • @param offsetFd?: Int If not passed or -1 is passed, the internal offset value of
    the file handle is used (it will advance after each write)
  • @return Uint The actual size of the data written

writeSync(fd,data,offsetFd?) #

Write data to the file handle synchronously and encode the data using utf-8

  • @param fd: Uint Open file handles
  • @param data: string The string to be written
  • @param offsetFd?: Int If not passed or -1 is passed, the internal offset value of
    the file handle is used (it will advance after each write)
  • @return Uint The actual size of the data written

writeSync(fd,data,encoding,offsetFd?) #

Synchronously write data to a file handle

  • @param fd: Uint Open file handles
  • @param data: string Data to be written
  • @param encoding: Encoding Encoding Type
  • @param offsetFd?: Int If not passed or -1 is passed, the internal offset value of
    the file handle is used (it will advance after each write)
  • @return Uint The actual size of the data written

chmod(path,mode?) #

Please ref: sync method chmodSync(path,mode?)

chown(path,owner,group) #

Please ref: sync method chownSync(path,owner,group)

chmod(path,mode?) #

Please ref: sync method mkdirSync(path,mode?)

mkdirs(path,mode?) #

Recursively create directories. This method will create
a directory tree in sequence, and will not throw an exception if the directory exists.

Ref: sync method [mkdirsSync(path[,mode])]

For example:

fs.mkdirs(mypath).then(()=>{
    // Success
}).catch(err=>{
    // Fail
});

rename(name,newName) #

Please ref: sync method renameSync(name,newName)

Please ref: sync method linkSync(src,target)

Please ref: sync method unlinkSync(path)

rmdir(path) #

Please ref: sync method rmdirSync(path)

readdir(path) #

Read directory listing information. Throws an exception if failed.
Returns an Array of Dirent if successful.

For example:

// Prints:
// {
//   name: "cp.txt",
//   pathname: "file:///var/mobile/Containers/Data/Application/64DAC3FC-A4FD-4274-A2E7-B834EE4930B4/Documents/test/cp.txt",
//   type: 1
// }
fs.readdir(mydir).then(dirents=>{
    for (var dirent of dirents) {
        // TODO...
        console.log(dirent);
    }
}).catch(err=>{
    // Fail
});

stat(path) #

Please ref: sync method statSync(path)

exists(path) #

Please ref: sync method existsSync(path)

isFile(path) #

Please ref: sync method isFileSync(path)

isDirectory(path) #

Please ref: sync method isDirectorySync(path)

readable(path) #

Please ref: sync method readableSync(path)

writable(path) #

Please ref: sync method writableSync(path)

executable(path) #

Please ref: sync method executableSync(path)

chmodRecursion(path,mode?) #

Asynchronously recursively set the mode attribute of a file or directory

Example:

// `mypath`For the file path, it can be a file or a directory
fs.chmodR(mypath, 0755).then(function() {
    console.log('Success');
}).catch(err=>{
    console.log('Fail');
});
var id = fs.chmodR(mydir, 0775);
fs.abort(id);

chownRecursion(path,owner,group) #

Asynchronously recursively set the owner and group attributes of files or directories

For example:

var task = chownRecursion(mypath, 501, 501);
fs.abort(task.id); // force abort task

removeRecursion(path) #

Recursively delete files and directories

For example:

var task = fs.removeRecursion(mypath);
task.then(()=>{
    // Success
}).catch(err=>{
    // Fail
});
// The deletion task can be aborted by id
fs.abort(task.id);

copyRecursion(path,target) #

Recursively copy files
The difference between copyRecursion() and copy() is that copy() can only copy a single file

For example:

fs.copy(source, target).then(()=>{
    // Success
}).catch(err=>{
    // Fail
});

copy(path,target) #

Copy a single file
Ref: copyRecursion(path,target)

readStream(path,cb) #

Read file contents using asynchronous streaming

abort(id) #

Force abort a running asynchronous task by id
If a meaningless id is passed in or the task to which
the id belongs has been completed, no processing will be done

For example:

var a = fs.chmod(mypath, 0o755);
var b = fs.chown(mypath, 501, 501);
var c = fs.copy(mypath, newpath);
// force abort task
fs.abort(a.id);
fs.abort(b.id);
fs.abort(c.id);

writeFile(path,data,size?) #

Please ref: sync method writeFileSync(path,data,size?)

writeFile(path,data,encoding?) #

Please ref: sync method writeFileSync(path,data,encoding?)

readFile(path) #

Please ref: sync method readFileSync(path)

readFile(path,encoding) #

Please ref: sync method readFileSync(path,encoding)

open(path,flags?) #

Please ref: sync method openSync(path,flags?)

close(fd) #

Please ref: sync method closeSync(fd)

read(fd,out,size?,offsetFd?) #

Please ref: sync method readSync(fd,out,size?,offsetFd?)

write(fd,data,size?,offsetFd?) #

Please ref: sync method writeSync(fd,data,size?,offsetFd?)

write(fd,data,offsetFd?) #

Please ref: sync method writeSync(fd,data,offsetFd?)

write(fd,data,encoding,offsetFd?) #

Please ref: sync method writeSync(fd,data,encoding,offsetFd?)

Interface: Reader #

Reader #

The methods provided here can perform basic read operations on URI paths of different protocols

Currently supported path types:

  • http:// or https:// - can be read synchronously or asynchronously, but cannot read directories or test existence,
    readdirSync() returns an empty array and isFileSync() always returns false.


  • file:// local file path. /var/data or var/data can be used as local paths without error.


  • zip:// This is a way to represent the path in the zip package, zip:///var/data/test.zip@/a.txt
    This path represents the a.txt file in zip:///var/data/test.zip. Note that this path must exist in the local file system


reader.readFile(path) #

Reading file data

For example:

reader.readFile('[`http://xxx.com/test.txt').then(()=>{`](http://xxx.com/test.txt').then(()=>{)
    // Success
}).catch(err=>{
    // Fail
})

reader.readFile(path,encoding) #

Read file data and decode it into a string

reader.readStream(path,cb) #

Read file data by asynchronous streaming

For example:

// async read file stream 
reader.readStream('[`http://www.baidu.com',`](http://www.baidu.com',) function(d){}));
reader.readStream('file:///var/data/test.txt', function(d){}));
reader.readStream('zip:///var/data/test.zip@aa.txt', function(d){
    // Success
    console.log(d.data.length, d.ended);
});

reader.readFileSync(path) #

Please ref: method Reader.readFile(path)

reader.readFileSync(path,encoding) #

Please ref: method Reader.readFile(path,encoding)

reader.existsSync(path) #

同步测试文件或目录是否存在,如果文件存在会返回false
这个方法不能处理http://https://类型的路径,如果传入这种路径立即返回false
Synchronously test whether a file or directory exists. If the file exists, it will return false
This method cannot handle paths of the http:// and https:// type. If such a path is passed, it will immediately return false

reader.isFileSync(path) #

同步检查路径是否为文件,如果路径不存在返回 false
这个方法不能处理http://https://类型的路径,如果传入这种路径立即返回false
Synchronously check if the path is a file, and return false if the path does not exist
This method cannot handle http:// and https:// type paths, and immediately returns false if such a path is passed

reader.isDirectorySync(path) #

同步检查路径是否为目录,如果路径不存在返回 false
这个方法不能处理http://https://类型的路径,如果传入这种路径立即返回false
Synchronously check if the path is a directory, and return false if the path does not exist
This method cannot handle http:// and https:// type paths, and immediately returns false if such a path is passed in

reader.readdirSync(path) #

同步读取目录文件列表,
这个方法不能处理http://https://类型的路径,如果传入这种路径立即返回一个空数组Array,
这个方法也不会抛出异常,如果不能读取路径,只会返回空数组Array
Synchronously read the directory file list,
This method cannot handle http:// and https:// type paths. If such a path is passed, an empty array Array will be immediately returned.
This method will not throw an exception. If the path cannot be read, it will only return an empty array Array

reader.abort(id) #

Abort an asynchronous task by its id

reader.clear() #

To clear cache of reader

reader #