builtin/fsModule

Synopsis

SilkJS builtin fs object.

Description

The builtin/fs object provides constants and methods to directly access the underlying operating system's file system functions.

Usage

var fs = require('builtin/fs');

See Also

Operating system man pages

Function: fs.error

Synopsis

Returns string version of last OS error.

Usage:

var message = fs.error();

Returns

  • {string} message - error message.

Back to top


Function: fs.chdir

Synopsis

Change current working directory.

Usage:

var success = fs.chdir(dirPath);

Arguments

  • {string} dirPath path in filesystem to set directory to.

Returns

  • {int} success - 0 on success, or -1 if error occurred.

Back to top


Function: fs.getcwd

Synopsis

var path = fs.getcwd();

Get current working directory.

Returns

  • {string} current working directory path or null if error occurred.

Back to top


Function: fs.open

Synopsis

var fd = (filename, flags, mode);

Arguments

  • {string} filename - name of file to open.
  • {int} flags - flags passed to open(2).
  • {int} mode - file permissions for file.

Returns

  • {int} fd - OS file handle, or -1 if error occurred.

Back to top


Function: fs.close

Synopsis

var success = fs.close(fd);

Close an already open file descriptor.

Arguments

  • {int} fd - the file descriptor to close.

Returns

  • {int} success - 0 on success, or -1 if error occurred.

Back to top


Function: fs.flock

Synopsis

var success = fs.flock(fd, operation);

Apply or remove an advisory lock on the open file specified by fd.

Operations

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.

Arguments

  • {int} fd - file descriptor of open file to lock or unlock.
  • {int} operation - see details above, and constants section.

Returns

  • {int} success - 0 on success, or -1 if error occurred.

Back to top


Function: fs.lockf

Synopsis

var success = fs.lockf(fd, operation);

Acquire, test, or release a POSIX lock on an open file.

Operations

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.

Arguments

  • {int} fd - file descriptor of open file to lock or unlock.
  • {int} operation - see details above, and constants section.

Returns

  • {int} success - 0 on success, or -1 if error occurred.

Notes

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.

Back to top


Function: fs.rename

Synopsis

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.

Arguments

  • {string} oldpath - name of existing file or directory to be moved
  • {string} newpath - path to where the file or directory is to be moved

Returns

  • {int} success - 0 on success, or -1 if error occurred

Back to top


Function: fs.truncate

Synopsis

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.

Arguments

  • {int} fd - file descriptor of open file to truncate... or
  • {string} path - full path to file to truncate.
  • {int} length - resulting length of file after truncate.

Returns

  • {int} success - 0 on success, or -1 if error occurred.

Back to top


Function: fs.chmod

Synopsis

var success = fs.chmod(fd, mode);
var success = fs.chmod(path, mode);

Changes the permissions of the specified file.

Modes

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

Arguments

  • {int} fd - file descriptor of open file to change permissions for... or
  • {string} path - full path to file to change permissions for.
  • {int} mode - the resulting file permissions. See description of modes above.

Returns

  • {int} success - 0 on success, or -1 if error occurred.

Back to top


Function: fs.stat

Synopsis

var o = fs.stat(path);

Get a structure describing a file's status.

Status structure

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.

Arguments

  • {string} path - path to file to get status for.

Returns

  • {object} o - status structure described above, or null if error occurred.

Notes

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.

Back to top


Function: fs.lstat

Synopsis

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.

Arguments

  • {string} path - path to file to get status for.

Returns

  • {object} o - status structure described for fs.stat(), or null if error occurred.

Back to top


Function: fs.lstat

Synopsis

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.

Arguments

  • {int} fd - file descriptor of an open file to get status for.

Returns

  • {object} o - status structure described for fs.stat(), or null if error occurred.

Back to top


Function: fs.link

Synopsis

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.

Arguments

  • {string} oldpath - path in filesystem to existing file
  • {string} newpath - path in filesysstem where the hard link will be created.

Returns

  • {int} success - 0 if successful, -1 if an error occurred.

Back to top


Function: fs.symlink

Synopsis

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.

Arguments

  • {string} oldpath - destination for soft/symbolic link.
  • {string} newpath - path to where the link will physically be created

Returns

  • {int} success - 0 if successful, -1 if an error occurred.

Back to top


Function: fs.readlink

Synopsis

var actual_path = fs.readlink(path_of_link);

This function returns the content of the symbolic link path_of_link.

Arguments

  • {string} path_of_link - path in the file system of the link to be read

Returns

  • {string} actual_path - path that the link points to, or false if error.

Back to top


Function: fs.realpath

Synopsis

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.

Arguments

  • {string} path - path to resolve to absolute path.

Returns

  • {string} real_path - absolute path, or false if an error occurred.

Back to top


Function: fs.unlink

Synopsis

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.

Arguments

  • {string} path - path of file system entry to be removed.

Returns

  • {boolean} success - true if the link/file was removed, false if an error occurred.

Back to top


Function: fs.rmdir

Synopsis

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 "..".

Arguments

  • {string} path - path to directory to be removed.

Returns

  • {boolean} success - true if the directory was removed, false if there was an error.

Back to top


Function: fs.mkdir

Synopsis

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.

Modes

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.

Arguments

  • {string} path - absolute or relative path of directory to be created.
  • {int} mode - some combination of the mode mask bits. See above.

Returns

  • {boolean} success - true if the directory was created, false if an error occurred.

Back to top


Function: fs.readDir

Synopsis

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.

Arguments

  • {string} path - absolute or relative path to a directory to get the contents of.

Returns

  • {array} filenames - array of {string} filenames, or null if the directory oculd not be opened.

Back to top


Function: fs.readFile

Synopsis

var contents = fs.readFile(filename);

This function reads the entire contents of the specified file into a string.

Arguments

  • {string} filename - name of file to read.

Returns

  • {string} contents - content of the file, or null if an error occurred.

Notes

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.

Back to top


Function: fs.readFile64

Synopsis

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().

Arguments

  • {string} filename - name of file to read.

Returns

  • {string} contents - content of the file, or null if an error occurred.

Notes

At some point, binary/b will be implemented in SilkJS and additional methods for dealing with binary data will be implemented.

Back to top


Function: fs.writeFile

Synopsis

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.

Modes

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

Arguments

  • {string} filename - name of file to read.
  • {string} contents - content of the file.
  • {int} mode - mode that the file will have after the contents are written. See above.

Returns

  • {boolean} success - true if file was written.

Back to top


Function: fs.writeFile64

Synopsis

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().

Modes

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

Arguments

  • {string} filename - name of file to read.
  • {string} contents - content of the file.
  • {int} mode - mode that the file will have after the contents are written. See above.

Returns

  • {boolean} success - true if file was written.

Notes

At some point, binary/b will be implemented in SilkJS and additional methods for dealing with binary data will be implemented.

Back to top


Function: fs.touch

Synopsis

var success = fs.touch(filename);

Set mtime and atime of a file to now. If the file doesn't exist, it will be created.

Arguments

  • {string} filename - name of file to update
  • {boolean} success - true if the operations succeeded, false if an error occurred.

Back to top


Function: fs.copyFile

Synsopsis

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.

Arguments

  • {string) destination - destination path (file to be written).
  • (string} source - source path (file to be copied).
  • {int} mode - file permissions for destination after copy.

Returns

  • {boolean} success - true if file was copied successfully, false if an error occurred.

Back to top


Function: fs.md5

Synopsis

var hex = fs.md5(path);

This function generates the MD5 hash of the contents of the specified file.

Arguments

  • {string} path - path of file

Returns

  • {string} hex - md5 hash

Back to top


Function: fs.cmp

Synopsis

var identical = fs.cmp(file1, file2);

This function does a binary comparison of two files.

Arguments

  • {string} file1 - path to first file to compare.
  • {string} file2 - path to second file to compare.

Returns

  • {boolean{ identical - true if the files are identical, or a string describing the difference if not.

Back to top


Constants

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

Constants

fs.LOCK_EX - exclusive lock
fs.LOCK_SH - shared lock
fs.LOCK_UN - unlock

Constants

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

Constants

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

blog comments powered by Disqus