SilkJS builtin fs object.
The builtin/fs object provides constants and methods to directly access the underlying operating system's file system functions.
var fs = require('builtin/fs');
Operating system man pages
Returns string version of last OS error.
var message = fs.error();
Change current working directory.
var success = fs.chdir(dirPath);
var path = fs.getcwd();
Get current working directory.
var fd = (filename, flags, mode);
var success = fs.close(fd);
Close an already open file descriptor.
var success = fs.flock(fd, operation);
Apply or remove an advisory lock on the open file specified by fd.
The operation parameter may be one of the following values:
fs.LOCK_SH - Obtain a shared lock. More than one process may hold a shared lock for a file at a given time.
fs.LOCK_EX - Obtain an exclusive lock. Only one process may hold an exclusive lock at a given time.
fs.LOCK_UN - Release an existing lock held by this process.
A call to fs.flock() will block if an incompatible lock is held by another process.
var success = fs.lockf(fd, operation);
Acquire, test, or release a POSIX lock on an open file.
The operation parameter may be one of the following:
fs.F_LOCK - set an exclusive lock on the specified file.
fs.F_TLOCK - same as F_LOCK but call never blocks and returns an error instead if the file is already locked.
fs.F_ULOCK - unlock the specified file.
fs.F_TEST - test the lock: return 0 if the file is unlocked or locked by this process, or -1 if another process holds a lock.
lockf() is not fully implemented by SilkJS. The function allows locking of regions of files. We don't typically seek() and modify bits of files in place. This is something to revisit if binary/b specification is implemented.
var success = fs.rename(oldpath, newpath);
Renames a file or directory, moving it between directories if required.
This is the equivalent of the shell mv command.
var success = fs.truncate(fd, length);
var success = fs.truncate(path, length);
The specified file will be truncated to a size of precisely length bytes.
var success = fs.chmod(fd, mode);
var success = fs.chmod(path, mode);
Changes the permissions of the specified file.
The mode parameter may be one or more of the following values, or'ed together (a bit mask):
fs.S_ISUID - set UID bit
fs.S_ISGID - set group id bit
fs.S_ISVTX - sticky bit
fs.S_IRUSR - owner has read permission
fs.S_IWUSR - owner has write permission
fs.S_IXUSR - owner has execute permission
fs.S_IRGRP - group has read permission
fs.S_IWGRP - group has write permission
fs.S_IXGRP - group has execute permission
fs.S_IROTH - others have read permission
fs.S_IWOTH - others have write permission
fs.S_IXOTH - others have execute permission
var o = fs.stat(path);
Get a structure describing a file's status.
The object returned by this function has the following members:
dev: ID of device containing the file.
ino: inode number.
mode: file permissions/protection.
nlink: number of hard links.
uid - user ID of owner.
gid: group ID of owner.
rdev: device ID (if special file)
size: total size of file in bytes.
blksize: block size for file system I/O.
blocks: number of 512B blocks allocated.
atime: timestamp of last access.
mtime: timestamp of last modification.
ctime: timestamp of last status change.
It is a bit more expensive to call this function if you are only interested in one of the fields. This is because the entire result status object must be constructed. SilkJS provides faster convenience methods to obtain the size, type, etc., of a file path.
var o = fs.lstat(path);
This function is identical to fs.stat() except that if path is a symbolic link, then the status of the link itself is returned rather than the file it refers to.
var o = fs.fstat(fd);
This function is identical to fs.stat() except that the file get status for is specified by a file descriptor.
var success = fs.link(oldpath, newpath);
Atomically create a hard link in the filesystem. The file specified by newpath argument will be created linked to the file specified by oldpath.
Both newpath and oldpath must be located in the same file system. If oldpath is a symbolic link, newpath will refer to the file referred to by the symbolic link.
var success = fs.symlink(oldpath, newpath);
A symbolic link newpath is created to oldpath. As far as symbolic links are concerned, oldpath doesn't have to exist, and the files need not be on the same file system.
var actual_path = fs.readlink(path_of_link);
This function returns the content of the symbolic link path_of_link.
var real_path = fs.realpath(path);
This function returns the canonicalized absolute path name of the specified path.
All symbolic links, extra "/" characters, and references to "/./" and "/../" are resolved in the returned path. Both absolute and relative paths are resolved. All components of the input path must exist when this function is called.
var success = fs.unlink(path);
This function removes the specified entry in the file system. If the path is to a hard link, the destination file is not removed.
A file is technically not completely removed if other processes have the file open. The file will be removed when those processes close the file.
var success = fs.rmdir(path);
This function removes the specified directory from the file system. The directory must not have any entries other than "." and "..".
var success = fs.mkdir(path, mode);
var success = fs.mkdir(path);
The directory specified by path is created with the given mode. If mode is not provided, then 0700 is used. The directory will be owned by the process's effective user ID, and the directory's group ID is set to that of the parent directory in which it is created.
The mode parameter may be one or more of the following values, or'ed together (a bit mask):
fs.S_IRUSR - owner has read permission
fs.S_IWUSR - owner has write permission
fs.S_IXUSR - owner has execute permission
fs.S_IRGRP - group has read permission
fs.S_IWGRP - group has write permission
fs.S_IXGRP - group has execute permission
fs.S_IROTH - others have read permission
fs.S_IWOTH - others have write permission
fs.S_IXOTH - others have execute permission
Note that only these 9 bits are used. The function's behavior when attempting to set other bits is undefined.
var filenames = fs.readDir(path);
This function returns an array of file (or directory) names in the specified path (directory).
Only the file names are returned, no other information. The special entries "." and ".." are not returned.
var contents = fs.readFile(filename);
This function reads the entire contents of the specified file into a string.
This function reads 1024 bytes from the file at a time. This is not optimal for big files, but minimizes the amount of memory used by the process.
var contents = fs.readFile64(filename);
This function reads the entire contents of the specified file into a base64 encoded string. JavaScript does not have a native binary type, so we have to use strings.
There are a number of API methods implemented for SilkJS that deal with base64 encoding of binary data. The converse of this function, for example, is fs.writeFile64().
At some point, binary/b will be implemented in SilkJS and additional methods for dealing with binary data will be implemented.
var success = fs.writeFile(filename, contents);
var success = fs.writeFile(filename, contents, mode);
This function creates or overwrites the file specified by filename with the given contents and mode. If mode is not provided, 0644 is used.
The mode parameter may be one or more of the following values, or'ed together (a bit mask):
fs.S_ISUID - set UID bit
fs.S_ISGID - set group id bit
fs.S_ISVTX - sticky bit
fs.S_IRUSR - owner has read permission
fs.S_IWUSR - owner has write permission
fs.S_IXUSR - owner has execute permission
fs.S_IRGRP - group has read permission
fs.S_IWGRP - group has write permission
fs.S_IXGRP - group has execute permission
fs.S_IROTH - others have read permission
fs.S_IWOTH - others have write permission
fs.S_IXOTH - others have execute permission
var success = fs.writeFile64(filename, contents);
var success = fs.writeFile64(filename, contents, mode);
This function creates or overwrites the file specified by filename with the given contents and mode. If mode is not provided, 0644 is used.
The contents parameter is a base64 encoded string. It is decoded to binary data as it is written to the file.
There are a number of API methods implemented for SilkJS that deal with base64 encoding of binary data. The converse of this function, for example, is fs.writeFile64().
The mode parameter may be one or more of the following values, or'ed together (a bit mask):
fs.S_ISUID - set UID bit
fs.S_ISGID - set group id bit
fs.S_ISVTX - sticky bit
fs.S_IRUSR - owner has read permission
fs.S_IWUSR - owner has write permission
fs.S_IXUSR - owner has execute permission
fs.S_IRGRP - group has read permission
fs.S_IWGRP - group has write permission
fs.S_IXGRP - group has execute permission
fs.S_IROTH - others have read permission
fs.S_IWOTH - others have write permission
fs.S_IXOTH - others have execute permission
At some point, binary/b will be implemented in SilkJS and additional methods for dealing with binary data will be implemented.
var success = fs.touch(filename);
Set mtime and atime of a file to now. If the file doesn't exist, it will be created.
var success = fs.copyFile(destination, source);
var success = fs.copyFile(destination, source, mode);
Copy file from source to destination. If mode is omitted, destination file perms will be 0644.
var hex = fs.md5(path);
This function generates the MD5 hash of the contents of the specified file.
var identical = fs.cmp(file1, file2);
This function does a binary comparison of two files.
fs.O_RDONLY - open for reading only
fs.O_WRONLY - open for writing only
fs.O_RDWR - open for reading and writing
fs.O_CREAT - create file if it does not exist
fs.O_TRUNC - truncate file
fs.LOCK_EX - exclusive lock
fs.LOCK_SH - shared lock
fs.LOCK_UN - unlock
fs.F_LOCK - block until an exclusive lock can be acquired
fs.F_TLOCK - acquire an exclusive lock, return error if failure
fs.F_ULOCK - unlock the indicated section of a file
fs.F_TEST - test if lock is unlocked or locked by this process
fs.S_IFMT - bit mask for the file type bit fields
fs_S_IFSOCK - socket
fs.S_IFLNK - symbolic link
fs.S_IFREG - regular file
fs.S_IFBLK - block device
fs.S_IFHCR - character device
fs.S_IFIFO - FIFO
fs.S_ISUID - set UID bit
fs.S_ISGID - set group id bit
fs.S_ISVTX - sticky bit
fs.S_IRMXU - mask for file owner permissions
fs.S_IRUSR - owner has read permission
fs.S_IWUSR - owner has write permission
fs.S_IXUSR - owner has execute permission
fs.S_IRMXG - mask for group permissions
fs.S_IRGRP - group has read permission
fs.S_IWGRP - group has write permission
fs.S_IXGRP - group has execute permission
fs.S_IRMXO - mask for others (not in group) permissions
fs.S_IROTH - others have read permission
fs.S_IWOTH - others have write permission
fs.S_IXOTH - others have execute permission