fs (Filesystem module)
Does filesystem work - access to files and directories.
Usage:
var fs=require("fs");
Access to directories:
var dir=new fs.Directory("/etc/");
var html="<h1>List of subdirectories in /etc/</h1>";
dir.listDirectories().forEach(function(dirname) {
html+="<div class='dir'>"+dirname+"</div>\n";
});
Access to files:
var file=new fs.File("/etc/passwd");
var html="<h1>List of users on this machine</h1>";
file.open("r");
file.read().toString("utf-8").split(/\n/).forEach(function(userline) {
var tmp=userline.split(/:/);
html+="<div class='user-login'>"+tmp[0]+"</div>\n";
});
file.close();
List of methods of Directory class
Name | Arguments | Returns | Description |
---|---|---|---|
new | String dirname | Creates an object of Directory class, with dirname an absolute or relative directory name. | |
create | int mode | Makes a directory with given permissions. If argument is missing, 0777 is assumed. Throws an error if directory cannot be made. | |
listFiles | Array of String | Returns an array of file names in given directory. Throws an error if directory cannot be listed. | |
listDirectories | Array of String | Returns an array of subdirectory names in given directory. Throw an error if directory cannot be listed. | |
toString | String | Returns a directory name (an argument given at new Directory call) | |
exists | Boolean | Returns if directory exists, can be accessed and dirname type is directory (not a file, symlink etc) | |
remove | Removes directory. Throws an error if cannot be done. | ||
stat | Object | Returns stats of directory as of C stat() function. See below for details. | |
isDirectory | Returns if directory exists and dirname type is directory (does not check for access rights) |
List of methods of File class
Name | Arguments | Returns | Description |
---|---|---|---|
new | String filename |
Create an object of File class, with filename an absolute or relative file name. Also can be used with file descriptor (for example, from exec3). |
|
open | String mode | Opens file, valid modes are same as C fopen() function ("r", "w", "a","r+",...). Throws an error if file cannot be opened. | |
read | Buffer | Reads a full file into binary Buffer. | |
readLine | Buffer | Reads a line into binary Buffer. | |
rewind | Rewinds file. Throws an error if file is not opened. | ||
close | Closes file. Throws an error if file cannot be closed. | ||
flush | Calls C fflush() function. Throws an error if file is closed. | ||
write | Buffer or String data | Writes contents to (previously opened for writting) file. | |
writeLine | Buffer or String data | Writes contents to (previously opened for writting) file. | |
remove | Deletes file | ||
toString | Returns a file name (an argument given at new new File call) | ||
exists | Boolean | Returns if file exists, can be accessed and filename type is file (not a directory, symlink etc) | |
move | String newname | Renames file to newname. Changes filename inside object. | |
copy | String newname | Copies file to newname. Object is not changed. | |
stat | Object | Returns stats of file as of C stat() function. See below for details. | |
isFile | Boolean | Returns is file exists and filename type is file (does not check for access rights) | |
isEOF | Boolean | Returns if file position indicator status is EOF as of C feof() function. Throws an error if file is closed. |
Details of dir.stat() and file.stat() methods
Both methods return same data, as of C stat() function. You can see man 2 stat for details.
var f=new fs.File("/etc/passwd");
var stat=f.stat();
var html="<h1>File stats are</h1>\n";
html+="<p>Size: "+stat.size+" bytes</p>\n";
html+="<p>Creation date: "+(new Date(stat.ctime*1000))+"</p>\n";
html+="<p>Last modification date: "+(new Date(stat.mtime*1000))+"</p>\n";
Argument | Type | Description |
---|---|---|
size | BigInt | File size in bytes |
mtime | BigInt | Time in seconds since start of epoch. Must be multiplied by 1000 to use with new Date() |
atime | BigInt | |
ctime | BigInt | |
mode | Int | Access rights |
uid | Int | Owner id |
gid | Int | Group id |