2013-12-11 20:15:35 +00:00
[![NPM ](https://nodei.co/npm/filer.png?downloads=true&stars=true )](https://nodei.co/npm/filer/)
2013-11-30 02:38:18 +00:00
2014-09-18 17:07:59 +00:00
[![Build Status ](https://secure.travis-ci.org/filerjs/filer.png?branch=develop )](http://travis-ci.org/filerjs/filer)
2014-02-17 23:16:32 +00:00
2018-11-29 00:24:03 +00:00
[![codecov ](https://codecov.io/gh/filerjs/filer/branch/master/graph/badge.svg )](https://codecov.io/gh/filerjs/filer)
2017-04-18 05:34:52 +00:00
### Filer
2013-08-06 16:33:22 +00:00
2014-06-25 18:28:53 +00:00
Filer is a POSIX-like file system interface for node.js and browser-based JavaScript.
2013-08-06 16:33:22 +00:00
2017-04-18 05:34:52 +00:00
### Compatibility
2014-03-18 15:10:43 +00:00
Filer is known to work in the following browsers/versions, with the specified [Storage Providers ](#providers ):
2014-06-25 18:28:53 +00:00
* node.js: v0.10.*+
2014-03-18 15:10:43 +00:00
* IE: 10+ (IndexedDB)
* Firefox: 26+ (IndexedDB)
* Chrome: 31+ (IndexedDB, WebSQL)
* Safari: 7.0+ (WebSQL)
* Opera: 19+ (IndexedDB, WebSQL)
* iOS: 3.2+ (WebSQL)
* Android Browser: 2.1-4.4 (WebSQL), 4.4+ (IndexedDB)
NOTE: if you're interested in maximum compatibility, use the `Fallback` provider instead of `Default` .
See the section on [Storage Providers ](#providers ).
2013-11-29 16:09:53 +00:00
### Contributing
2014-09-18 17:07:59 +00:00
Want to join the fun? We'd love to have you! See [CONTRIBUTING ](https://github.com/filerjs/filer/blob/develop/CONTRIBUTING.md ).
2013-11-29 16:09:53 +00:00
2014-06-25 18:28:53 +00:00
### How to Get It
2013-11-15 19:37:29 +00:00
2014-06-25 18:28:53 +00:00
Filer can be obtained in a number of ways:
2013-11-15 19:37:29 +00:00
2018-09-17 18:37:28 +00:00
2014-06-25 18:28:53 +00:00
1. npm - `npm install filer`
2. bower - `bower install filer`
2014-09-18 17:07:59 +00:00
3. download pre-built versions: [filer.js ](https://raw.github.com/filerjs/filer/develop/dist/filer.js ), [filer.min.js ](https://raw.github.com/filerjs/filer/develop/dist/filer.min.js )
2014-06-25 18:28:53 +00:00
### Loading and Usage
Filer is built as a UMD module and can therefore be loaded as a CommonJS or AMD module, or used via the global.
```javascript
// Option 1: Filer loaded via require() in node/browserify
var Filer = require('filer');
// Option 2: Filer loaded via RequireJS
requirejs.config({
baseUrl: '/',
paths: {
2014-06-25 18:30:39 +00:00
'filer': 'filer/dist/filer'
2014-06-25 18:28:53 +00:00
}
});
2014-06-25 18:30:39 +00:00
requirejs(['filer'], function(Filer) {...}
2014-06-25 18:28:53 +00:00
// Option 3: Filer on global
var Filer = window.Filer;
```
2013-11-15 19:37:29 +00:00
2013-08-06 16:33:22 +00:00
### Getting Started
2013-12-09 16:29:13 +00:00
Filer is as close to the node.js [fs module ](http://nodejs.org/api/fs.html ) as possible,
with the following differences:
* No synchronous versions of methods (e.g., `mkdir()` but not `mkdirSync()` ).
* No support for stream-based operations (e.g., `fs.ReadStream` , `fs.WriteStream` ).
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Filer has other features lacking in node.js (e.g., swappable backend
2014-06-25 18:28:53 +00:00
storage providers, extended attributes, etc).
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Like node.js, the API is asynchronous and most methods expect the caller to provide
a callback function (note: like node.js, Filer will supply one if it's missing).
Errors are passed to callbacks through the first parameter. As with node.js,
there is no guarantee that file system operations will be executed in the order
they are invoked. Ensure proper ordering by chaining operations in callbacks.
2013-11-15 19:37:29 +00:00
2014-02-19 20:22:12 +00:00
### Overview
2013-12-09 16:29:13 +00:00
To create a new file system or open an existing one, create a new `FileSystem`
instance. By default, a new [IndexedDB ](https://developer.mozilla.org/en/docs/IndexedDB )
database is created for each file system. The file system can also use other
backend storage providers, for example [WebSQL ](http://en.wikipedia.org/wiki/Web_SQL_Database )
or even RAM (i.e., for temporary storage). See the section on [Storage Providers ](#providers ).
2018-08-27 23:50:41 +00:00
< a name = "overviewExample" > < / a >
2013-08-06 16:33:22 +00:00
2013-11-29 15:34:38 +00:00
```javascript
2013-12-09 16:29:13 +00:00
var fs = new Filer.FileSystem();
2013-11-29 15:34:38 +00:00
fs.open('/myfile', 'w+', function(err, fd) {
if (err) throw err;
fs.close(fd, function(err) {
2013-08-06 16:33:22 +00:00
if (err) throw err;
2013-11-29 15:34:38 +00:00
fs.stat('/myfile', function(err, stats) {
2013-08-06 16:33:22 +00:00
if (err) throw err;
2013-11-29 15:34:38 +00:00
console.log('stats: ' + JSON.stringify(stats));
2013-08-06 16:33:22 +00:00
});
});
2013-11-29 15:34:38 +00:00
});
```
2013-08-06 16:33:22 +00:00
2014-02-19 20:22:12 +00:00
For a complete list of `FileSystem` methods and examples, see the [FileSystem Instance Methods ](#FileSystemMethods )
section below.
Filer also supports node's Path module. See the [Filer.Path ](#FilerPath ) section below.
In addition, common shell operations (e.g., rm, touch, cat, etc.) are supported via the
2014-02-19 22:29:11 +00:00
`FileSystemShell` object, which can be obtained from, and used with a `FileSystem` .
See the[FileSystemShell](#FileSystemShell) section below.
2014-02-19 20:22:12 +00:00
2013-08-06 16:33:22 +00:00
### API Reference
2013-12-09 16:29:13 +00:00
Like node.js, callbacks for methods that accept them are optional but suggested (i.e., if
you omit the callback, errors will be thrown as exceptions). The first callback parameter is
reserved for passing errors. It will be `null` if no errors occurred and should always be checked.
2013-11-29 15:34:38 +00:00
2018-08-27 23:50:41 +00:00
#### Support for Promises
The Promise based API mimics the way Node [implements ](https://nodejs.org/api/fs.html#fs_fs_promises_api ) them. Both `Shell` and `FileSystem` now have a `promises` object attached alongside the regular callback style methods. Method names are identical to their callback counterparts with the difference that instead of receiving a final argument as a callback, they return a Promise that is resolved or rejected based on the success of method execution.
> Please note that `exists` method will always throw, since it was [deprecated](https://nodejs.org/api/fs.html#fs_fs_exists_path_callback) by Node.
See example below:
```javascript
const fs = new Filer.FileSystem().promises;
fs.open('/myfile', 'w+')
.then(fd => fs.close(fd))
.then(() => fs.stat('/myfile'))
.then(stats => { console.log(`stats: ${JSON.stringify(stats)}`); })
.catch(err => { console.error(err); })
```
> The callback style usage could be found [here](#overviewExample).
2013-12-09 16:29:13 +00:00
#### Filer.FileSystem(options, callback) constructor
2013-11-29 15:34:38 +00:00
2013-12-09 16:29:13 +00:00
File system constructor, invoked to open an existing file system or create a new one.
Accepts two arguments: an `options` object, and an optional `callback` . The `options`
object can specify a number of optional arguments, including:
* `name` : the name of the file system, defaults to `'"local'`
2014-03-06 01:03:27 +00:00
* `flags` : an Array of one or more flags to use when creating/opening the file system:
2014-06-25 18:28:53 +00:00
* `'FORMAT'` to force Filer to format (i.e., erase) the file system
* `'NOCTIME'` to force Filer to not update `ctime` on nodes when metadata changes (i.e., for better performance)
* `'NOMTIME'` to force Filer to not update `mtime` on nodes when data changes (i.e., for better performance)
2013-12-09 16:29:13 +00:00
* `provider` : an explicit storage provider to use for the file system's database context provider. See the section on [Storage Providers ](#providers ).
2013-11-29 15:34:38 +00:00
The `callback` function indicates when the file system is ready for use. Depending on the storage provider used, this might
2014-01-21 21:25:09 +00:00
be right away, or could take some time. The callback should expect two arguments: first, an `error` argument, which will be
null if everything worked; second, an instance, such that you can access the newly ready FileSystem instance. Also users
should check the file system's `readyState` and `error` properties to make sure it is usable.
2013-11-29 15:34:38 +00:00
```javascript
var fs;
2014-01-21 21:25:09 +00:00
function fsReady(err, fs) {
2013-11-29 15:34:38 +00:00
if(err) throw err;
// Safe to use fs now...
}
2013-12-09 16:29:13 +00:00
fs = new Filer.FileSystem({
2013-11-29 15:34:38 +00:00
name: "my-filesystem",
2014-03-06 01:03:27 +00:00
flags: [ 'FORMAT' ],
2013-12-09 16:29:13 +00:00
provider: new Filer.FileSystem.providers.Memory()
2013-11-29 16:19:54 +00:00
}, fsReady);
2013-11-29 15:34:38 +00:00
```
2013-12-09 16:29:13 +00:00
NOTE: if the optional callback argument is not passed to the `FileSystem` constructor,
operations done on the resulting file system will be queued and run in sequence when
it becomes ready.
2017-04-18 05:34:52 +00:00
#### Filer.FileSystem.providers - Storage Providers<a name="providers"></a>
2013-11-29 15:34:38 +00:00
2013-12-09 16:29:13 +00:00
Filer can be configured to use a number of different storage providers. The provider object encapsulates all aspects
2013-11-29 15:34:38 +00:00
of data access, making it possible to swap in different backend storage options. There are currently 4 different
providers to choose from:
2013-12-09 16:29:13 +00:00
2013-11-29 15:34:38 +00:00
* `FileSystem.providers.IndexedDB()` - uses IndexedDB
* `FileSystem.providers.WebSQL()` - uses WebSQL
* `FileSystem.providers.Fallback()` - attempts to use IndexedDB if possible, falling-back to WebSQL if necessary
* `FileSystem.providers.Memory()` - uses memory (not suitable for data that needs to survive the current session)
You can choose your provider when creating a `FileSystem` :
```javascript
2013-12-09 16:29:13 +00:00
var FileSystem = Filer.FileSystem;
2013-11-29 15:34:38 +00:00
var providers = FileSystem.providers;
// Example 1: Use the default provider (currently IndexedDB)
var fs1 = new FileSystem();
// Example 2: Explicitly use IndexedDB
var fs2 = new FileSystem({ provider: new providers.IndexedDB() });
// Example 3: Use one of IndexedDB or WebSQL, whichever is supported
var fs3 = new FileSystem({ provider: new providers.Fallback() });
```
Every provider has an `isSupported()` method, which returns `true` if the browser supports this provider:
2013-08-06 16:33:22 +00:00
2013-11-29 15:34:38 +00:00
```javascript
2013-12-09 16:29:13 +00:00
if( Filer.FileSystem.providers.WebSQL.isSupported() ) {
// WebSQL provider will work in current environment...
2013-11-29 15:34:38 +00:00
}
```
2013-08-06 16:33:22 +00:00
2013-11-29 15:34:38 +00:00
You can also write your own provider if you need a different backend. See the code in `src/providers` for details.
2013-08-06 16:33:22 +00:00
2014-06-25 18:28:53 +00:00
A number of other providers have been written, including:
* node.js fs provider: https://github.com/humphd/filer-fs
* node.js Amazon S3 provider: https://github.com/alicoding/filer-s3
2017-04-18 05:34:52 +00:00
#### Filer.Buffer<a name="FilerBuffer"></a>
2014-06-25 18:28:53 +00:00
When reading and writing data, Filer follows node.js and uses [`Buffer` ](http://nodejs.org/api/buffer.html ).
When in a node.js environment, native `Buffer` s can be used, or Filer.Buffer, which is a shortcut
to node's `Buffer` . In a browser, you can use also use `Filer.Buffer` .
NOTE: a `Filer.Buffer` in a browser is really an augmented `Uint8Array` (i.e., the node `Buffer` api
2018-12-02 00:57:09 +00:00
methods are added to the instance). See https://github.com/feross/buffer for more details.
NOTE: `Filer.Buffer` currently includes the older, deprecated [constructor functions ](https://nodejs.org/api/buffer.html#buffer_new_buffer_array ), but these will be removed
at some point. You are encouraged to switch to use the newer class methods `Buffer.from()`
and `Buffer.alloc()` . See the [node.js Buffer docs ](https://nodejs.org/api/buffer.html ).
```js
/* Deprecated - see https://nodejs.org/api/buffer.html#buffer_new_buffer_array */
new Buffer(array)
new Buffer(arrayBuffer[, byteOffset[, length]])
new Buffer(buffer)
new Buffer(string[, encoding])
new Buffer(size)
/* Use Instead */
Buffer.from(array)
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(buffer)
Buffer.from(string[, encoding])
Buffer.alloc(size)
Buffer.allocUnsafe(size)
```
2014-06-25 18:28:53 +00:00
2017-04-18 05:34:52 +00:00
#### Filer.Path<a name="FilerPath"></a>
2013-11-29 16:37:58 +00:00
2013-12-09 16:29:13 +00:00
The node.js [path module ](http://nodejs.org/api/path.html ) is available via the `Filer.Path` object. It is
2013-11-29 16:37:58 +00:00
identical to the node.js version with the following differences:
* No notion of a current working directory in `resolve` (the root dir is used instead)
```javascript
2013-12-09 16:29:13 +00:00
var path = Filer.Path;
2013-11-29 16:37:58 +00:00
var dir = path.dirname('/foo/bar/baz/asdf/quux');
// dir is now '/foo/bar/baz/asdf'
var base = path.basename('/foo/bar/baz/asdf/quux.html');
// base is now 'quux.html'
var ext = path.extname('index.html');
// ext is now '.html'
var newpath = path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
// new path is now '/foo/bar/baz/asdf'
```
For more info see the docs in the [path module ](http://nodejs.org/api/path.html ) for a particular method:
* `path.normalize(p)`
* `path.join([path1], [path2], [...])`
* `path.resolve([from ...], to)`
* `path.relative(from, to)`
* `path.dirname(p)`
* `path.basename(p, [ext])`
* `path.extname(p)`
* `path.sep`
* `path.delimiter`
2017-04-18 05:34:52 +00:00
#### Filer.Errors<a name="Errors"></a>
2014-03-10 18:28:22 +00:00
The error objects used internally by Filer are also exposed via the `Filer.Errors` object. As much as possible
2014-03-12 20:23:03 +00:00
these match their node.js counterparts, with a few Filer-specifc additions.
2014-09-18 17:07:59 +00:00
See [src/errors.js ](https://github.com/filerjs/filer/blob/develop/src/errors.js ) for the complete
2014-03-12 20:23:03 +00:00
list. Errors can be used, or compared, like so:
2014-03-10 18:28:22 +00:00
Examples:
```javascript
2014-03-12 20:23:03 +00:00
// Example 1: create an EExist error
var err1 = new Filer.Errors.EEXIST();
var err2 = new Filer.Errors[47];
2014-03-10 18:28:22 +00:00
// Example 2: compare an error to see if it is EInvalid
function callback(err) {
2014-03-12 20:23:03 +00:00
if(err instanceof Filer.Errors.EINVAL){
2014-03-10 18:28:22 +00:00
...
}
// Or compare the error's code
2014-03-12 20:23:03 +00:00
if(err.code === 'EINVAL') {
2014-03-10 18:28:22 +00:00
...
}
}
2014-03-12 20:23:03 +00:00
// Example 4: compare an error using errno
function callback(err) {
if(err.errno === 47){
...
}
// Example 5: display the error message
2014-03-10 18:28:22 +00:00
console.log(err.message);
```
2017-04-18 05:34:52 +00:00
### FileSystem Instance Methods<a name="FileSystemMethods"></a>
2013-12-09 16:29:13 +00:00
Once a `FileSystem` is created, it has the following methods. NOTE: code examples below assume
a `FileSystem` instance named `fs` has been created like so:
```javascript
var fs = new Filer.FileSystem();
```
* [fs.rename(oldPath, newPath, callback) ](#rename )
* [fs.ftruncate(fd, len, callback) ](#ftruncate )
* [fs.truncate(path, len, callback) ](#truncate )
* [fs.stat(path, callback) ](#stat )
* [fs.fstat(fd, callback) ](#fstat )
* [fs.lstat(path, callback) ](#lstat )
2014-02-21 11:53:12 +00:00
* [fs.exists(path, callback) ](#exists )
2013-12-09 16:29:13 +00:00
* [fs.link(srcpath, dstpath, callback) ](#link )
* [fs.symlink(srcpath, dstpath, [type], callback)](#symlink)
* [fs.readlink(path, callback) ](#readlink )
* [fs.realpath(path, [cache], callback)](#realpath)
* [fs.unlink(path, callback) ](#unlink )
2014-05-13 22:10:11 +00:00
* [fs.mknod(path, mode, callback) ](#mknod )
2013-12-09 16:29:13 +00:00
* [fs.rmdir(path, callback) ](#rmdir )
* [fs.mkdir(path, [mode], callback)](#mkdir)
2018-11-17 20:31:20 +00:00
* [fs.access(path, [mode], callback)](#access)
2013-12-09 16:29:13 +00:00
* [fs.readdir(path, callback) ](#readdir )
* [fs.close(fd, callback) ](#close )
* [fs.open(path, flags, [mode], callback)](#open)
* [fs.utimes(path, atime, mtime, callback) ](#utimes )
2018-06-25 12:57:17 +00:00
* [fs.chown(path, uid, gid, callback) ](#chown )
* [fs.fchown(fd, uid, gid, callback) ](#fchown )
* [fs.chmod(path, mode, callback) ](#chmod )
* [fs.fchmod(fd, mode, callback) ](#fchmod )
2013-12-09 16:29:13 +00:00
* [fs.futimes(fd, atime, mtime, callback) ](#fsutimes )
* [fs.fsync(fd, callback) ](#fsync )
* [fs.write(fd, buffer, offset, length, position, callback) ](#write )
* [fs.read(fd, buffer, offset, length, position, callback) ](#read )
* [fs.readFile(filename, [options], callback)](#readFile)
* [fs.writeFile(filename, data, [options], callback)](#writeFile)
* [fs.appendFile(filename, data, [options], callback)](#appendFile)
* [fs.setxattr(path, name, value, [flag], callback)](#setxattr)
* [fs.fsetxattr(fd, name, value, [flag], callback)](#fsetxattr)
* [fs.getxattr(path, name, callback) ](#getxattr )
* [fs.fgetxattr(fd, name, callback) ](#fgetxattr )
* [fs.removexattr(path, name, callback) ](#removexattr )
* [fs.fremovexattr(fd, name, callback) ](#fremovexattr )
2014-03-07 20:22:06 +00:00
* [fs.watch(filename, [options], [listener])](#watch)
2013-12-09 16:29:13 +00:00
#### fs.rename(oldPath, newPath, callback)<a name="rename"></a>
Renames the file at `oldPath` to `newPath` . Asynchronous [rename(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/rename.html ).
Callback gets no additional arguments.
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Example:
```javascript
// Rename myfile.txt to myfile.bak
fs.rename("/myfile.txt", "/myfile.bak", function(err) {
if(err) throw err;
// myfile.txt is now myfile.bak
});
```
#### fs.ftruncate(fd, len, callback)<a name="ftruncate"></a>
Change the size of the file represented by the open file descriptor `fd` to be length
`len` bytes. Asynchronous [ftruncate(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/ftruncate.html ).
If the file is larger than `len` , the extra bytes will be discarded; if smaller, its size will
be increased, and the extended area will appear as if it were zero-filled. See also [fs.truncate() ](#truncate ).
Example:
```javascript
// Create a file, shrink it, expand it.
2018-12-02 00:57:09 +00:00
var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
2013-12-09 16:29:13 +00:00
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw error;
fs.write(fd, buffer, 0, buffer.length, 0, function(err, result) {
if(err) throw error;
fs.ftruncate(fd, 3, function(err) {
if(err) throw error;
// /myfile is now 3 bytes in length, rest of data discarded
fs.ftruncate(fd, 50, function(err) {
if(err) throw error;
// /myfile is now 50 bytes in length, with zero padding at end
fs.close(fd);
});
});
});
});
});
```
#### fs.truncate(path, len, callback)<a name="truncate"></a>
Change the size of the file at `path` to be length `len` bytes. Asynchronous [truncate(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/truncate.html ). If the file is larger than `len` , the extra bytes will be discarded; if smaller, its size will
be increased, and the extended area will appear as if it were zero-filled. See also [fs.ftruncate() ](#ftruncate ).
Example:
```javascript
// Create a file, shrink it, expand it.
2018-12-02 00:57:09 +00:00
var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
2013-12-09 16:29:13 +00:00
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw error;
fs.write(fd, buffer, 0, buffer.length, 0, function(err, result) {
if(err) throw error;
fs.close(fd, function(err) {
if(err) throw error;
fs.truncate('/myfile', 3, function(err) {
if(err) throw error;
// /myfile is now 3 bytes in length, rest of data discarded
fs.truncate('/myfile', 50, function(err) {
if(err) throw error;
// /myfile is now 50 bytes in length, with zero padding at end
});
});
});
});
});
```
#### fs.stat(path, callback)<a name="stat"></a>
Obtain file status about the file at `path` . Asynchronous [stat(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/stat.html ).
2014-03-08 18:53:53 +00:00
Callback gets `(error, stats)` , where `stats` is an object with the following properties:
2013-08-06 16:33:22 +00:00
2013-11-29 15:34:38 +00:00
```
{
2018-05-24 19:41:54 +00:00
node: < string > // internal node id (unique)
dev: < string > // file system name
name: < string > // the entry's name (basename)
size: < number > // file size in bytes
nlinks: < number > // number of links
atime: < number > // last access time
mtime: < number > // last modified time
ctime: < number > // creation time
type: < string > // file type (FILE, DIRECTORY, SYMLINK),
gid: < number > // group name
uid: < number > // owner name
mode: < number > // permissions
version: < number > // version of the node
2013-12-09 16:29:13 +00:00
}
```
2014-03-08 19:12:47 +00:00
The following convenience methods are also present on the callback's `stats` :
2014-03-07 23:10:02 +00:00
2014-03-08 19:12:47 +00:00
```
2014-03-08 18:53:53 +00:00
isFile(): Returns true if the node is a file.
isDirectory(): Returns true if the node is a directory.
isBlockDevice(): Not implemented, returns false.
isCharacterDevice(): Not implemented, returns false.
isSymbolicLink(): Returns true if the node is a symbolic link.
isFIFO(): Not implemented, returns false.
isSocket(): Not implemented, returns false.
2014-03-08 19:12:47 +00:00
```
2014-03-07 23:10:02 +00:00
If the file at `path` is a symbolic link, the file to which it links will be used instead.
2013-12-09 16:29:13 +00:00
To get the status of a symbolic link file, use [fs.lstat() ](#lstat ) instead.
Examples:
```javascript
// Check if a directory exists
function dirExists(path, callback) {
fs.stat(path, function(err, stats) {
if(err) return callback(err);
var exists = stats.type === "DIRECTORY";
callback(null, exists);
});
};
// Get the size of a file in KB
function fileSize(path, callback) {
fs.stat(path, function(err, stats) {
if(err) return callback(err);
var kb = stats.size / 1000;
callback(null, kb);
});
2013-11-29 15:34:38 +00:00
}
```
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
#### fs.fstat(fd, callback)<a name="fstat"></a>
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Obtain information about the open file known by the file descriptor `fd` .
Asynchronous [fstat(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/fstat.html ).
Callback gets `(error, stats)` . `fstat()` is identical to `stat()` , except that the file to be stat-ed is
specified by the open file descriptor `fd` instead of a path. See also [fs.stat ](#stat )
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Example:
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
```javascript
fs.open("/file.txt", "r", function(err, fd) {
if(err) throw err;
fs.fstat(fd, function(err, stats) {
if(err) throw err;
// do something with stats object
// ...
fs.close(fd);
});
});
```
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
#### fs.lstat(path, callback)<a name="lstat"></a>
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Obtain information about the file at `path` (i.e., the symbolic link file itself) vs.
the destination file to which it links. Asynchronous [lstat(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/lstat.html ).
Callback gets `(error, stats)` . See also [fs.stat ](#stat ).
2013-11-20 22:05:31 +00:00
2013-12-09 16:29:13 +00:00
Example:
2013-11-20 22:05:31 +00:00
2013-12-09 16:29:13 +00:00
```javascript
// Create a symbolic link, /data/logs/current to /data/logs/august
// and get info about the symbolic link file, and linked file.
fs.link("/data/logs/august", "/data/logs/current", function(err) {
if(err) throw err;
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
// Get status of linked file, /data/logs/august
fs.stat("/data/logs/current", function(err, stats) {
if(err) throw err;
// Size of /data/logs/august
var size = stats.size;
});
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
// Get status of symbolic link file itself
fs.lstat("/data/logs/current", function(err, stats) {
if(err) throw err;
// Size of /data/logs/current
var size = stats.size;
});
});
2014-02-21 11:53:12 +00:00
```
#### fs.exists(path, callback)<a name="exists"></a>
2014-10-17 16:28:03 +00:00
Test whether or not the given path exists by checking with the file system.
Then call the callback argument with either true or false.
2014-02-21 11:53:12 +00:00
Example:
```javascript
//Test if the file exists
fs.exists('/myfile', function (exists) {
console.log(exists ? "file exists" : "file not found");
});
```
fs.exists() is an anachronism and exists only for historical reasons. There should almost never be a reason to use it in your own code.
In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to fs.exists() and fs.open(). Just open the file and handle the error when it's not there.
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
#### fs.link(srcPath, dstPath, callback)<a name="link"></a>
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Create a (hard) link to the file at `srcPath` named `dstPath` . Asynchronous [link(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/link.html ). Callback gets no additional arguments. Links are directory entries that point to the same file node.
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Example:
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
```javascript
fs.link('/logs/august.log', '/logs/current', function(err) {
if(err) throw err;
fs.readFile('/logs/current', 'utf8', function(err, data) {
// data is the contents of /logs/august.log
var currentLog = data;
});
});
```
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
#### fs.symlink(srcPath, dstPath, [type], callback)<a name="symlink"></a>
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Create a symbolic link to the file at `dstPath` containing the path `srcPath` . Asynchronous [symlink(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/symlink.html ). Callback gets no additional arguments.
Symbolic links are files that point to other paths.
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
NOTE: Filer allows for, but ignores the optional `type` parameter used in node.js.
2018-11-29 21:49:19 +00:00
The `srcPath` may be a relative path, which will be resolved relative to `dstPath`
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Example:
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
```javascript
2018-11-29 21:49:19 +00:00
// Absolute path
2013-12-09 16:29:13 +00:00
fs.symlink('/logs/august.log', '/logs/current', function(err) {
if(err) throw err;
fs.readFile('/logs/current', 'utf8', function(err, data) {
// data is the contents of /logs/august.log
var currentLog = data;
});
});
2018-11-29 21:49:19 +00:00
// Relative path
fs.symlink('../file', '/dir/symlink', function(err) {
if(err) throw err;
// The /dir/symlink file is now a symlink to /file
});
2013-12-09 16:29:13 +00:00
```
#### fs.readlink(path, callback)<a name="readlink"></a>
2018-11-29 21:49:19 +00:00
Reads the contents of a symbolic link. Asynchronous [readlink(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/readlink.html ).
Callback gets `(error, linkContents)` , where `linkContents` is a string
containing the symbolic link's link path. If the original `srcPath` given
to `symlink()` was a relative path, it will be fully resolved relative
to `dstPath` when returned by `readlink()` .
2013-12-09 16:29:13 +00:00
Example:
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
```javascript
fs.symlink('/logs/august.log', '/logs/current', function(error) {
if(error) throw error;
fs.readlink('/logs/current', function(error, linkContents) {
// linkContents is now '/logs/august.log'
});
});
```
#### fs.realpath(path, [cache], callback)<a name="realpath"></a>
2013-08-06 16:33:22 +00:00
2014-09-18 17:07:59 +00:00
NOTE: Not implemented, see https://github.com/filerjs/filer/issues/85
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
#### fs.unlink(path, callback)<a name="unlink"></a>
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
Removes the directory entry located at `path` . Asynchronous [unlink(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/unlink.html ).
Callback gets no additional arguments. If `path` names a symbolic link, the symbolic link will be removed
(i.e., not the linked file). Otherwise, the filed named by `path` will be removed (i.e., deleted).
2013-11-18 19:49:16 +00:00
2013-12-09 16:29:13 +00:00
Example:
2013-11-18 19:49:16 +00:00
```javascript
2013-12-09 16:29:13 +00:00
// Delete regular file /backup.old
fs.unlink('/backup.old', function(err) {
if(err) throw err;
// /backup.old is now removed
2013-11-18 19:49:16 +00:00
});
2013-12-09 16:29:13 +00:00
```
2013-11-18 19:49:16 +00:00
2014-05-13 22:10:11 +00:00
#### fs.mknod(path, mode, callback)<a name="mknod"></a>
Creates a node at `path` based on the mode passed which is either `FILE` or `DIRECTORY` . Asynchronous [mknod(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/mknod.html ). Callback gets no additional arguments.
Example:
```javascript
// Create a /dir directory
fs.mknod('/dir', 'DIRECTORY', function(err) {
if(err) throw err;
// /dir is now created
2014-10-17 16:28:03 +00:00
2014-05-13 22:10:11 +00:00
// Create a file inside /dir
fs.mknod('/dir/myfile', 'FILE', function(err) {
if(err) throw err;
// /dir/myfile now exists
});
});
```
2013-12-09 16:29:13 +00:00
#### fs.rmdir(path, callback)<a name="rmdir"></a>
Removes the directory at `path` . Asynchronous [rmdir(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/rmdir.html ).
Callback gets no additional arguments. The operation will fail if the directory at `path` is not empty.
Example:
```javascript
/**
* Given the following dir structure, remove docs/
* /docs
* a.txt
*/
// Start by deleting the files in docs/, then remove docs/
fs.unlink('/docs/a.txt', function(err) {
if(err) throw err;
fs.rmdir('/docs', function(err) {
if(err) throw err;
});
});
```
#### fs.mkdir(path, [mode], callback)<a name="mkdir"></a>
Makes a directory with name supplied in `path` argument. Asynchronous [mkdir(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/mkdir.html ). Callback gets no additional arguments.
NOTE: Filer allows for, but ignores the optional `mode` argument used in node.js.
Example:
```javascript
// Create /home and then /home/carl directories
fs.mkdir('/home', function(err) {
if(err) throw err;
fs.mkdir('/home/carl', function(err) {
if(err) throw err;
// directory /home/carl now exists
});
});
```
2018-11-17 20:31:20 +00:00
#### fs.access(path, [mode], callback)<a name="access"></a>
Tests a user's permissions for the file or directory supplied in `path` argument. Asynchronous [access(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/access.html ). Callback gets no additional arguments. The `mode` argument can be one of the following (constants are available on `fs.constants` ):
* `F_OK` : Test for existence of file.
* `R_OK` : Test whether the file exists and grants read permission.
* `W_OK` : Test whether the file exists and grants write permission.
* `X_OK` : Test whether the file exists and grants execute permission.
NOTE: you can also create a mask consisting of the bitwise OR of two or more values (e.g. `fs.constants.W_OK | fs.constants.R_OK` ).
Example:
```javascript
// Check if the file exists in the current directory.
fs.access(file, fs.constants.F_OK, function(err) {
console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
});
```
2013-12-09 16:29:13 +00:00
#### fs.readdir(path, callback)<a name="readdir"></a>
Reads the contents of a directory. Asynchronous [readdir(3) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/readdir.html ).
Callback gets `(error, files)` , where `files` is an array containing the names of each directory entry (i.e., file, directory, link) in the directory, excluding `.` and `..` .
Example:
```javascript
/**
* Given the following dir structure:
* /docs
* a.txt
* b.txt
* c/
*/
fs.readdir('/docs', function(err, files) {
if(err) throw err;
// files now contains ['a.txt', 'b.txt', 'c']
});
```
#### fs.close(fd, callback)<a name="close"></a>
Closes a file descriptor. Asynchronous [close(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/close.html ).
Callback gets no additional arguments.
Example:
```javascript
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw error;
// Do something with open file descriptor `fd`
// Close file descriptor when done
fs.close(fd);
});
```
#### fs.open(path, flags, [mode], callback)<a name="open"></a>
Opens a file. Asynchronous [open(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/open.html ).
Callback gets `(error, fd)` , where `fd` is the file descriptor. The `flags` argument can be:
* `'r'` : Open file for reading. An exception occurs if the file does not exist.
* `'r+'` : Open file for reading and writing. An exception occurs if the file does not exist.
* `'w'` : Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
* `'w+'` : Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
* `'a'` : Open file for appending. The file is created if it does not exist.
* `'a+'` : Open file for reading and appending. The file is created if it does not exist.
NOTE: Filer allows for, but ignores the optional `mode` argument used in node.js.
Example:
```javascript
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw error;
// Do something with open file descriptor `fd`
// Close file descriptor when done
fs.close(fd);
});
```
#### fs.utimes(path, atime, mtime, callback)<a name="utimes"></a>
Changes the file timestamps for the file given at path `path` . Asynchronous [utimes(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/utimes.html ). Callback gets no additional arguments. Both `atime` (access time) and `mtime` (modified time) arguments should be a JavaScript Date.
Example:
```javascript
var now = Date.now();
fs.utimes('/myfile.txt', now, now, function(err) {
if(err) throw err;
// Access Time and Modified Time for /myfile.txt are now updated
});
```
#### fs.futimes(fd, atime, mtime, callback)<a name="futimes"></a>
Changes the file timestamps for the open file represented by the file descriptor `fd` . Asynchronous [utimes(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/utimes.html ). Callback gets no additional arguments. Both `atime` (access time) and `mtime` (modified time) arguments should be a JavaScript Date.
Example:
```javascript
fs.open('/myfile.txt', function(err, fd) {
if(err) throw err;
var now = Date.now();
fs.futimes(fd, now, now, function(err) {
if(err) throw err;
// Access Time and Modified Time for /myfile.txt are now updated
fs.close(fd);
});
2013-11-18 19:49:16 +00:00
});
```
2018-06-25 12:57:17 +00:00
#### fs.chown(path, uid, gid, callback)<a name="chown"></a>
Changes the owner and group of a file. Asynchronous [chown(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/chown.html ). Callback gets no additional arguments. Both `uid` (user id) and `gid` (group id) arguments should be a JavaScript Number. By default, `0x0` is used (i.e., `root:root` ownership).
Example:
```javascript
fs.chown('/myfile.txt', 500, 500, function(err) {
if(err) throw err;
// /myfile.txt is now owned by user with id 500, group 500
});
```
#### fs.fchown(fd, uid, gid, callback)<a name="fchown"></a>
Changes the owner and group of a file. Asynchronous [chown(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/chown.html ). Callback gets no additional arguments. Both `uid` (user id) and `gid` (group id) arguments should be a JavaScript Number. By default, `0x0` is used (i.e., `root:root` ownership).
Example:
```javascript
fs.open('/myfile.txt', function(err, fd) {
if(err) throw err;
fs.fchown(fd, 500, 500, function(err) {
if(err) throw err;
// /myfile.txt is now owned by user with id 500, group 500
fs.close(fd);
});
});
```
#### fs.chmod(path, mode, callback)<a name="chmod"></a>
Changes the mode of a file. Asynchronous [chmod(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/chmod.html ). Callback gets no additional arguments. The `mode` argument should be a JavaScript Number, which combines file type and permission information. Here are a list of common values useful for setting the `mode` :
* File type `S_IFREG=0x8000`
* Dir type `S_IFDIR=0x4000`
* Link type `S_IFLNK=0xA000`
* Permissions `755=0x1ED`
* Permissions `644=0x1A4`
* Permissions `777=0x1FF`
* Permissions `666=0x1B6`
By default, directories use `(0x4000 | 0x1ED)` and files use `(0x8000 | 0x1A4)` .
Example:
```javascript
// S_IFREG | 0o777
var mode = 0x8000 | 0x1FF
fs.chmod('/myfile.txt', mode, function(err) {
if(err) throw err;
// /myfile.txt is a regular file with permissions 777
});
```
#### fs.fchmod(fd, mode, callback)<a name="fchmod"></a>
Changes the mode of a file. Asynchronous [chmod(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/chmod.html ). Callback gets no additional arguments. The `mode` argument should be a JavaScript Number, which combines file type and permission information. By default, `755` (dir) and `644` (file) are used.
Example:
```javascript
fs.open('/myfile.txt', function(err, fd) {
if(err) throw err;
// S_IFREG | 0o777
var mode = 0x8000 | 0x1FF
fs.fchmod(fd, mode, function(err) {
if(err) throw err;
// /myfile.txt is a regular file with permissions 777
fs.close(fd);
});
});
```
2013-12-09 16:29:13 +00:00
#### fs.fsync(fd, callback)<a name="fsync"></a>
2018-12-02 04:59:36 +00:00
Synchronize the data and metadata for the file referred to by `fd` to disk.
Asynchronous [fsync(2) ](http://man7.org/linux/man-pages/man2/fsync.2.html ).
The callback gets `(error)` .
```js
fs.open('/myfile', 'r', function(error, fd) {
if(err) throw err;
// Use fd, then sync
fs.fsync(fd, function(error) {
if(err) throw err;
fs.close(fd, done);
});
});
```
2013-12-09 16:29:13 +00:00
#### fs.write(fd, buffer, offset, length, position, callback)<a name="write"></a>
2014-06-25 18:30:39 +00:00
Writes bytes from `buffer` to the file specified by `fd` . Asynchronous [write(2), pwrite(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/write.html ). The `offset` and `length` arguments describe the part of the buffer to be written. The `position` refers to the offset from the beginning of the file where this data should be written. If `position` is `null` , the data will be written at the current position. The callback gets `(error, nbytes)` , where `nbytes` is the number of bytes written.
2013-12-09 16:29:13 +00:00
NOTE: Filer currently writes the entire buffer in a single operation. However, future versions may do it in chunks.
Example:
```javascript
// Create a file with the following bytes.
2018-12-02 00:57:09 +00:00
var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
2013-12-09 16:29:13 +00:00
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw error;
var expected = buffer.length, written = 0;
function writeBytes(offset, position, length) {
length = length || buffer.length - written;
fs.write(fd, buffer, offset, length, position, function(err, nbytes) {
if(err) throw error;
// nbytes is now the number of bytes written, between 0 and buffer.length.
// See if we still have more bytes to write.
written += nbytes;
if(written < expected )
writeBytes(written, null);
else
fs.close(fd);
});
}
writeBytes(0, 0);
});
```
#### fs.read(fd, buffer, offset, length, position, callback)<a name="read"></a>
2014-06-25 18:28:53 +00:00
Read bytes from the file specified by `fd` into `buffer` . Asynchronous [read(2), pread(2) ](http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html ). The `offset` and `length` arguments describe the part of the buffer to be used. The `position` refers to the offset from the beginning of the file where this data should be read. If `position` is `null` , the data will be written at the current position. The callback gets `(error, nbytes)` , where `nbytes` is the number of bytes read.
2013-12-09 16:29:13 +00:00
NOTE: Filer currently reads into the buffer in a single operation. However, future versions may do it in chunks.
Example:
```javascript
fs.open('/myfile', 'r', function(err, fd) {
2018-09-24 18:08:49 +00:00
if(err) throw err;
2013-12-09 16:29:13 +00:00
// Determine size of file
fs.fstat(fd, function(err, stats) {
2018-09-24 18:08:49 +00:00
if(err) throw err;
2013-12-09 16:29:13 +00:00
// Create a buffer large enough to hold the file's contents
var nbytes = expected = stats.size;
2018-12-02 00:57:09 +00:00
var buffer = Filer.Buffer.alloc(nbytes);
2013-12-09 16:29:13 +00:00
var read = 0;
function readBytes(offset, position, length) {
length = length || buffer.length - read;
fs.read(fd, buffer, offset, length, position, function(err, nbytes) {
2018-09-24 18:08:49 +00:00
if(err) throw err;
2013-12-09 16:29:13 +00:00
// nbytes is now the number of bytes read, between 0 and buffer.length.
// See if we still have more bytes to read.
read += nbytes;
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
if(read < expected )
readBytes(read, null);
else
fs.close(fd);
});
}
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
readBytes(0, 0);
});
});
```
2013-08-06 16:33:22 +00:00
2013-12-09 16:29:13 +00:00
#### fs.readFile(filename, [options], callback)<a name="readFile"></a>
2013-11-18 19:49:16 +00:00
2013-12-09 16:29:13 +00:00
Reads the entire contents of a file. The `options` argument is optional, and can take the form `"utf8"` (i.e., an encoding) or be an object literal: `{ encoding: "utf8", flag: "r" }` . If no encoding is specified, the raw binary buffer is returned via the callback. The callback gets `(error, data)` , where data is the contents of the file.
Examples:
2013-11-18 19:49:16 +00:00
```javascript
// Read UTF8 text file
fs.readFile('/myfile.txt', 'utf8', function (err, data) {
if (err) throw err;
2013-12-09 16:29:13 +00:00
// data is now the contents of /myfile.txt (i.e., a String)
2013-11-18 19:49:16 +00:00
});
// Read binary file
fs.readFile('/myfile.txt', function (err, data) {
if (err) throw err;
2014-06-25 18:28:53 +00:00
// data is now the contents of /myfile.txt (i.e., a Buffer with the bytes)
2013-11-18 19:49:16 +00:00
});
```
2013-12-09 16:29:13 +00:00
#### fs.writeFile(filename, data, [options], callback)<a name="writeFile"></a>
2013-08-06 16:33:22 +00:00
2014-06-25 18:28:53 +00:00
Writes data to a file. `data` can be a string or `Buffer` , in which case any encoding option is ignored. The `options` argument is optional, and can take the form `"utf8"` (i.e., an encoding) or be an object literal: `{ encoding: "utf8", flag: "w" }` . If no encoding is specified, and `data` is a string, the encoding defaults to `'utf8'` . The callback gets `(error)` .
2013-11-07 20:55:14 +00:00
2013-12-09 16:29:13 +00:00
Examples:
2013-11-07 20:55:14 +00:00
2013-12-09 16:29:13 +00:00
```javascript
// Write UTF8 text file
fs.writeFile('/myfile.txt', "...data...", function (err) {
if (err) throw err;
});
2013-11-23 18:34:32 +00:00
2013-12-09 16:29:13 +00:00
// Write binary file
2018-12-02 00:57:09 +00:00
var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
2013-12-09 16:29:13 +00:00
fs.writeFile('/myfile', buffer, function (err) {
if (err) throw err;
});
```
2013-11-23 18:34:40 +00:00
2013-12-09 16:29:13 +00:00
#### fs.appendFile(filename, data, [options], callback)<a name="appendFile"></a>
2013-11-23 18:34:40 +00:00
2014-06-25 18:28:53 +00:00
Writes data to the end of a file. `data` can be a string or a `Buffer` , in which case any encoding option is ignored. The `options` argument is optional, and can take the form `"utf8"` (i.e., an encoding) or be an object literal: `{ encoding: "utf8", flag: "w" }` . If no encoding is specified, and `data` is a string, the encoding defaults to `'utf8'` . The callback gets `(error)` .
2014-01-30 19:12:32 +00:00
Examples:
```javascript
// Append UTF8 text file
fs.writeFile('/myfile.txt', "More...", function (err) {
if (err) throw err;
});
fs.appendFile('/myfile.txt', "Data...", function (err) {
if (err) throw err;
});
// '/myfile.txt' would now read out 'More...Data...'
// Append binary file
2018-12-02 00:57:09 +00:00
var data = Filer.Buffer.from([1, 2, 3, 4]);
var more = Filer.Buffer.from([5, 6, 7, 8]);
2014-06-25 18:28:53 +00:00
fs.writeFile('/myfile', data, function (err) {
2014-01-30 19:12:32 +00:00
if (err) throw err;
2014-06-25 18:28:53 +00:00
fs.appendFile('/myfile', more, function (err) {
if (err) throw err;
2014-10-17 16:28:03 +00:00
2014-06-25 18:28:53 +00:00
// '/myfile' would now contain [1, 2, 3, 4, 5, 6, 7, 8]
});
2014-01-30 19:12:32 +00:00
});
```
2013-11-23 18:34:40 +00:00
2013-12-09 16:29:13 +00:00
#### fs.setxattr(path, name, value, [flag], callback)<a name="setxattr"></a>
2013-11-23 18:34:32 +00:00
2013-12-09 16:29:13 +00:00
Sets an extended attribute of a file or directory named `path` . Asynchronous [setxattr(2) ](http://man7.org/linux/man-pages/man2/setxattr.2.html ).
The optional `flag` parameter can be set to the following:
* `XATTR_CREATE` : ensures that the extended attribute with the given name will be new and not previously set. If an attribute with the given name already exists, it will return an `EExists` error to the callback.
* `XATTR_REPLACE` : ensures that an extended attribute with the given name already exists. If an attribute with the given name does not exist, it will return an `ENoAttr` error to the callback.
2013-11-23 18:34:57 +00:00
2013-12-09 16:29:13 +00:00
Callback gets no additional arguments.
2013-11-23 18:34:57 +00:00
2013-12-09 16:29:13 +00:00
Example:
2013-12-10 16:59:44 +00:00
2013-12-09 16:29:13 +00:00
```javascript
fs.writeFile('/myfile', 'data', function(err) {
if(err) throw err;
2013-12-10 16:59:44 +00:00
2013-12-09 16:29:13 +00:00
// Set a simple extended attribute on /myfile
fs.setxattr('/myfile', 'extra', 'some-information', function(err) {
if(err) throw err;
2013-12-10 16:59:44 +00:00
2013-12-09 16:29:13 +00:00
// /myfile now has an added attribute of extra='some-information'
});
2013-12-10 16:59:44 +00:00
2013-12-09 16:29:13 +00:00
// Set a complex object attribute on /myfile
fs.setxattr('/myfile', 'extra-complex', { key1: 'value1', key2: 103 }, function(err) {
if(err) throw err;
2013-12-16 02:22:36 +00:00
2013-12-09 16:29:13 +00:00
// /myfile now has an added attribute of extra={ key1: 'value1', key2: 103 }
});
});
```
2013-12-16 02:22:36 +00:00
2013-12-09 16:29:13 +00:00
#### fs.fsetxattr(fd, name, value, [flag], callback)<a name="fsetxattr"></a>
2013-12-16 02:22:36 +00:00
2013-12-09 16:29:13 +00:00
Sets an extended attribute of the file represented by the open file descriptor `fd` . Asynchronous [setxattr(2) ](http://man7.org/linux/man-pages/man2/setxattr.2.html ). See `fs.setxattr` for more details. Callback gets no additional arguments.
2013-12-16 02:22:36 +00:00
2013-12-09 16:29:13 +00:00
Example:
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
```javascript
fs.open('/myfile', 'w', function(err, fd) {
if(err) throw err;
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
// Set a simple extended attribute on fd for /myfile
fs.fsetxattr(fd, 'extra', 'some-information', function(err) {
if(err) throw err;
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
// /myfile now has an added attribute of extra='some-information'
});
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
// Set a complex object attribute on fd for /myfile
fs.fsetxattr(fd, 'extra-complex', { key1: 'value1', key2: 103 }, function(err) {
if(err) throw err;
// /myfile now has an added attribute of extra={ key1: 'value1', key2: 103 }
});
fs.close(fd);
});
```
#### fs.getxattr(path, name, callback)<a name="getxattr"></a>
Gets an extended attribute value for a file or directory. Asynchronous [getxattr(2) ](http://man7.org/linux/man-pages/man2/getxattr.2.html ).
Callback gets `(error, value)` , where `value` is the value for the extended attribute named `name` .
Example:
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
```javascript
// Get the value of the extended attribute on /myfile named `extra`
fs.getxattr('/myfile', 'extra', function(err, value) {
if(err) throw err;
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
// `value` is now the value of the extended attribute named `extra` for /myfile
});
```
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
#### fs.fgetxattr(fd, name, callback)<a name="fgetxattr"></a>
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
Gets an extended attribute value for the file represented by the open file descriptor `fd` .
Asynchronous [getxattr(2) ](http://man7.org/linux/man-pages/man2/getxattr.2.html ).
See `fs.getxattr` for more details. Callback gets `(error, value)` , where `value` is the value for the extended attribute named `name` .
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
Example:
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
```javascript
// Get the value of the extended attribute on /myfile named `extra`
fs.open('/myfile', 'r', function(err, fd) {
if(err) throw err;
fs.fgetxattr(fd, 'extra', function(err, value) {
if(err) throw err;
// `value` is now the value of the extended attribute named `extra` for /myfile
});
2013-12-23 01:58:00 +00:00
2013-12-09 16:29:13 +00:00
fs.close(fd);
});
```
#### fs.removexattr(path, name, callback)<a name="removexattr"></a>
Removes the extended attribute identified by `name` for the file given at `path` . Asynchronous [removexattr(2) ](http://man7.org/linux/man-pages/man2/removexattr.2.html ). Callback gets no additional arguments.
Example:
```javascript
// Remove an extended attribute on /myfile
fs.removexattr('/myfile', 'extra', function(err) {
if(err) throw err;
// The `extra` extended attribute on /myfile is now gone
});
```
#### fs.fremovexattr(fd, name, callback)<a name="fremovexattr"></a>
Removes the extended attribute identified by `name` for the file represented by the open file descriptor `fd` .
Asynchronous [removexattr(2) ](http://man7.org/linux/man-pages/man2/removexattr.2.html ). See `fs.removexattr` for more details.
Callback gets no additional arguments.
Example:
```javascript
// Remove an extended attribute on /myfile
fs.open('/myfile', 'r', function(err, fd) {
if(err) throw err;
fs.fremovexattr(fd, 'extra', function(err) {
if(err) throw err;
// The `extra` extended attribute on /myfile is now gone
});
fs.close(fd);
});
```
2014-02-19 20:22:12 +00:00
2014-03-07 20:22:06 +00:00
#### fs.watch(filename, [options], [listener])<a name="watch"></a>
Watch for changes to a file or directory at `filename` . The object returned is an `FSWatcher` ,
which is an [`EventEmitter` ](http://nodejs.org/api/events.html ) with the following additional method:
* `close()` - stops listening for changes, and removes all listeners from this instance. Use this
to stop watching a file or directory after calling `fs.watch()` .
The only supported option is `recursive` , which if `true` will cause a watch to be placed
on a directory, and all sub-directories and files beneath it.
2014-03-07 20:24:58 +00:00
The `listener` callback gets two arguments `(event, filename)` . `event` is either `'rename'` or `'change'` ,
(currenty only `'rename'` is supported) and `filename` is the name of the file/dir which triggered the event.
2014-03-07 20:22:06 +00:00
Unlike node.js, all watch events return a path. Also, all returned paths are absolute from the root
vs. just a relative filename.
Examples:
```javascript
// Example 1: create a watcher to see when a file is created
var watcher = fs.watch('/myfile', function(event, filename) {
2014-03-07 20:24:58 +00:00
// event could be 'change' or 'rename' and filename will be '/myfile'
2014-03-07 20:22:06 +00:00
// Stop watching for changes
watcher.close();
});
fs.writeFile('/myfile', 'data');
// Example 2: add the listener via watcher.on()
var watcher = fs.watch('/myfile2');
watcher.on('change', function(event, filename) {
// event will be 'change' and filename will be '/myfile2'
// Stop watching for changes
watcher.close();
});
fs.writeFile('/myfile2', 'data2');
// Example 3: recursive watch on /data dir
var watcher = fs.watch('/data', { recursive: true }, function(event, filename) {
2014-03-07 20:24:58 +00:00
// event could be 'change' or 'rename' and filename will be '/data/subdir/file'
2014-03-07 20:22:06 +00:00
// Stop watching for changes
watcher.close();
});
fs.writeFile('/data/subdir/file', 'data');
```
2014-02-19 22:29:11 +00:00
### FileSystemShell<a name="FileSystemShell"></a>
2014-02-19 20:22:12 +00:00
Many common file system shell operations are available by using a `FileSystemShell` object.
2015-10-07 18:47:53 +00:00
The `FileSystemShell` is used in conjunction with a `FileSystem` ,
2014-02-19 22:29:11 +00:00
and provides augmented features. Many separate `FileSystemShell` objects can exist per
`FileSystem` , but each `FileSystemShell` is bound to a single instance of a `FileSystem`
for its lifetime.
2014-02-19 20:22:12 +00:00
2014-11-14 17:04:51 +00:00
A `FileSystemShell` is created by instantiating `Filer.FileSystem().Shell` :
2014-02-19 20:22:12 +00:00
```javascript
var fs = new Filer.FileSystem();
2014-11-14 17:04:51 +00:00
var sh = new fs.Shell(options);
var sh2 = new fs.Shell(options);
2014-02-19 22:29:11 +00:00
// sh and sh2 are two separate shells, each bound to fs
2014-02-19 20:22:12 +00:00
```
2014-11-14 17:04:51 +00:00
In addition, the constructor function can be accessed through `Filer` :
2014-10-17 16:28:03 +00:00
```javascript
var fs = new Filer.FileSystem();
2014-11-14 17:04:51 +00:00
var sh = new fs.Shell();
2014-10-17 16:28:03 +00:00
Filer.Shell.prototype.newFunction = ...;
sh.newFunction();
```
2014-02-19 20:22:12 +00:00
The `FileSystemShell` can take an optional `options` object. The `options` object
2014-02-19 20:25:52 +00:00
can include `env` , which is a set of environment variables. Currently supported variables
2014-02-19 22:29:11 +00:00
include `TMP` (the path to the temporary directory), and `PATH` (the list of known paths) and
others may be added in the future. You can also add your own, or update existing variables.
2014-02-19 20:22:12 +00:00
```javascript
var fs = new Filer.FileSystem();
2014-11-14 17:04:51 +00:00
var sh = new fs.Shell({
2014-02-19 20:22:12 +00:00
env: {
TMP: '/tempdir',
PATH: '/one:/two'
}
});
2014-02-19 22:29:11 +00:00
var tempPath = sh.env.get('TMP');
sh.env.set('TMP', '/newtempdir');
2014-02-19 20:22:12 +00:00
```
NOTE: unless otherwise stated, all `FileSystemShell` methods can take relative or absolute
paths. Relative paths are resolved relative to the shell's current working directory (`sh.cwd`).
This is different from the `FileSystem` , which requires absolute paths, and has no notion
of a current working directory.
#### FileSystemShell Properties
A `FileSystemShell` has a number of properties, including:
* `fs` - (readonly) a reference to the bound `FileSystem`
2014-02-19 22:29:11 +00:00
* `env` - (readonly) the shell's environment. The shell's environemnt `env` object has `get(name)`
and `set(name, value)` methods.
2014-02-19 20:22:12 +00:00
Example:
```javascript
var fs = new Filer.FileSystem();
2014-11-14 17:04:51 +00:00
var sh = new fs.Shell();
2014-02-19 22:29:11 +00:00
var p = sh.env.get('PATH');
2014-02-19 20:22:12 +00:00
// Store the current location
2014-02-19 22:29:11 +00:00
var before = sh.pwd();
2014-02-19 20:22:12 +00:00
var after;
sh.cd('/newdir', function(err) {
if(err) throw err;
// Get the new location
2014-02-19 22:29:11 +00:00
after = sh.pwd();
2014-02-19 20:22:12 +00:00
});
```
#### FileSystemShell Instance Methods
Once a `FileSystemShell` object is created, it has the following methods. NOTE: code
examples below assume a `FileSystemShell` instance named `sh` has been created like so:
```javascript
var fs = new Filer.FileSystem();
2014-11-14 17:04:51 +00:00
var sh = new fs.Shell();
2014-02-19 20:22:12 +00:00
```
* [sh.cd(path, callback) ](#cd )
2014-02-19 22:29:11 +00:00
* [sh.pwd() ](#pwd )
2014-11-28 17:48:10 +00:00
* [sh.find(dir, [options], callback)](#find)
2014-02-19 20:22:12 +00:00
* [sh.ls(dir, [options], callback)](#ls)
2014-02-19 22:36:26 +00:00
* [sh.exec(path, [args], callback)](#exec)
2014-02-19 20:25:52 +00:00
* [sh.touch(path, [options], callback)](#touch)
2014-02-19 20:22:12 +00:00
* [sh.cat(files, callback) ](#cat )
* [sh.rm(path, [options], callback)](#rm)
* [sh.tempDir(callback) ](#tempDir )
2014-03-14 04:03:29 +00:00
* [sh.mkdirp(path, callback) ](#mkdirp )
2014-03-26 20:42:17 +00:00
2014-02-19 20:22:12 +00:00
#### sh.cd(path, callback)<a name="cd"></a>
2014-02-19 20:25:52 +00:00
Changes the current working directory to the directory at `path` . The callback returns
2014-02-19 20:22:12 +00:00
an error if `path` does not exist, or is not a directory. Once the callback occurs
2014-02-19 22:29:11 +00:00
the shell's cwd is updated to the new path (you can access it via `sh.pwd()` ).
2014-02-19 20:22:12 +00:00
Example:
```javascript
sh.cd('/dir1', function(err) {
if(err) throw err;
2014-02-19 22:29:11 +00:00
// sh.pwd() is now '/dir1'
2014-02-19 20:22:12 +00:00
});
2014-02-19 20:25:52 +00:00
```
2014-02-19 20:22:12 +00:00
2014-02-19 22:29:11 +00:00
#### sh.pwd()<a name="pwd"></a>
Returns the shell's current working directory. See [sh.cd() ](#cd ).
2014-11-28 17:48:10 +00:00
#### sh.find(dir, [options], callback)<a name="find"></a>
Recursively walk a directory tree, reporting back all paths that were
found along the way. Asynchronous [find(1)](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html
)
If given no options, `find` walks the given dir path
and the callback gives `function(err, found)` , where `found` is an array of
all paths discovered during a depth-first walk.
Valid options include a `regex` for pattern matching paths, allowing paths
to be ignored (e.g., ```regex: /\.bak$/``` to find all `.bak` files). You can
also use `name` and `path` to provide a [match pattern ](https://github.com/isaacs/minimatch ) for the basename and
dirname respectively (e.g., `{name: '*.js'}` to find all JavaScript files or
`{path: '*-modules'}` to only look in folders named `base-modules` , `foo-modules` , etc.).
Finally, you can also provide an `exec` function of the form `function(path, next)` where
`path` is the current path that was found and matches any provided `regex`
(NOTE: dir paths have an '/' appended), and `next` is a callback to call
when you are done processing the path.
Example:
```javascript
function processPath(path, next) {
// Process the path somehow, in this case we print it.
// Dir paths end with /
if(path.endsWith('/')) {
console.log('Found dir: ' + path);
} else {
console.log('Found file: ' + path);
}
// All done, let the process continue by invoking second arg:
next();
}
// Get every path (NOTE: no name or regex provided) below the root, depth first
sh.find('/', {exec: processPath}, function(err, found) {
/* find command is finished, `found` contains the flattened list as an Array */
});
// Find all files that look like map201.jpg, map202.jpg in the /data dir
sh.find('/data', {regex: /map20\d\.jpg$/, exec: processPath}, function(err) {
/* find command is finished */
});
// Find and delete all *.bak files under /app/user
sh.find('/app/user', {
name: '*.bak',
exec: function(path, next) {
sh.rm(path, next);
}
}, function callback(err, found) {
if(err) throw err;
if(found.length) {
console.log('Deleted the following ' + found.length + ' files: ', found);
}
});
```
2014-02-19 20:22:12 +00:00
#### sh.ls(dir, [options], callback)<a name="ls"></a>
Get the listing of a directory, returning an array of directory entries
2018-05-24 19:41:54 +00:00
in the same form as [fs.stat() ](#stat ), with the exception that a new Array named
`contents` is added for directory entries, containing child entries.
2014-02-19 20:22:12 +00:00
By default `sh.ls()` gives a shallow listing. If you want to follow
directories as they are encountered, use the `recursive=true` option. NOTE:
you should not count on the order of the returned entries always being the same.
Example:
```javascript
/**
* Given a dir structure of:
*
* /dir
* file1
* file2
* dir2/
* file3
*/
// Shallow listing
sh.ls('/dir', function(err, entries) {
if(err) throw err;
// entries is now an array of 3 file/dir entries under /dir
});
// Deep listing
sh.ls('/dir', { recursive: true }, function(err, entries) {
if(err) throw err;
// entries is now an array of 3 file/dir entries under /dir.
// The entry object for '/dir2' also includes a `contents` property,
// which is an array of 1 entry element for `file3` .
});
```
2014-02-19 22:36:26 +00:00
#### sh.exec(path, [args], callback)<a name="exec"></a>
2014-02-19 20:22:12 +00:00
2014-02-19 20:32:01 +00:00
Attempts to execute the .js command located at `path` . The `sh.exec` method
enables apps to install larger programs into the file system and run them
later without having to re-download. Such commands should be written so as
to assume the existence of 3 global variables, which will be defined at runtime:
2014-02-19 22:36:26 +00:00
* `fs` - [FileSystem] the `FileSystem` object bound to this shell.
* `args` - [Array] a list of any arguments for the command, or the empty list
* `callback` - [Function] a callback function of the form `function callback(error, result)`
2014-02-19 20:27:58 +00:00
to call when done.
2014-02-19 20:22:12 +00:00
The .js command's contents should be the body of a function that
looks like this:
```javascript
2014-02-19 22:36:26 +00:00
function(fs, args, callback) {
2014-02-19 20:22:12 +00:00
//-------------------------commmand code here---------
// ...
//----------------------------------------------------
}
```
Example:
```javascript
// Simple command to delete a file.
2014-02-19 22:36:26 +00:00
var cmd = "fs.unlink(args[0], callback);"
2014-02-19 20:22:12 +00:00
// Write the file to the filesystem
fs.writeFile('/cmd.js', cmd, callback(err) {
if(err) throw err;
// Execute the command
2014-02-19 22:36:26 +00:00
sh.exec('/cmd.js', [ '/file' ], function(err, result) {
2014-02-19 20:22:12 +00:00
if(err) throw err;
});
});
2014-02-19 20:27:58 +00:00
```
2014-02-19 20:22:12 +00:00
#### sh.touch(path, [options], callback)<a name="touch"></a>
Create a file if it does not exist, or update the access and modified
times if it does. Valid options include:
2014-02-19 20:32:01 +00:00
* `updateOnly` - `true` if the file's access/modified dates are to be updated
only (but missing file not to be)
* `date` - a date to use instead of the current date and time when updating
2014-02-19 20:22:12 +00:00
access and modified dates.
Example:
```javascript
sh.touch('/newfile', function(err) {
if(err) throw err;
fs.exists('/newfile', function(exists) {
// exists is now true.
}
});
```
#### sh.cat(files, callback)<a name="cat"></a>
Concatenates multiple files into a single string, with each file
separated by a newline character. The `files` argument should be
a String (i.e., path to a single file) or an Array of Strings (i.e.,
multiple paths for multiple files).
Example:
```javascript
sh.cat([ './file1', '../file2' ], function(err, data) {
if(err) throw err;
// data is now the contents of file1 and file2 joined
});
```
#### sh.rm(path, [options], callback)<a name="rm"></a>
Removes (deletes) the file or directory at `path` . If `path` is a file, it will
be removed. If `path` is a directory, it will be removed if it is empty, otherwise
the callback will receive an error. In order to remove non-empty directories,
use the `recursive=true` option.
Example:
```javascript
sh.rm('./file', function(err) {
if(err) throw err;
// ./file is now removed
});
sh.rm('/dir', { recursive: true }, function(err) {
if(err) throw err;
// /dir and all its children are now removed
});
```
#### sh.tempDir(callback)<a name="tempDir"></a>
Gets the path to the shell's temporary directory, creating it if it
does not already exist. The temp directory to use is specified in the
`env.TMP` environment variable. The callback receives an error
and the `tempDir` path. NOTE: it is safe to call this many times (i.e.,
the temp dir will only be created once). No effort is made to clean-up
the temp dir, and it is up to the caller to destroy it if desired.
Example:
```javascript
// Default /tmp dir
sh.tempDir(function(err, tmp) {
if(err) throw err;
// tmp is now '/tmp' by default, and /tmp exists
});
// Specify a tmp dir path
sh.env.TMP = '/temporary'
sh.tempDir(function(err, tmp) {
if(err) throw err;
// tmp is now '/temporary', and /temporary exists
});
```
2014-03-14 04:03:29 +00:00
2014-03-30 04:13:25 +00:00
#### sh.mkdirp(path, callback)<a name="mkdirp"></a>
2014-03-14 04:03:29 +00:00
2014-10-17 16:28:03 +00:00
Recursively creates the directory at the provided path. If the
2014-03-14 04:03:29 +00:00
directory already exists, no error is returned. All parents must
2014-10-17 16:28:03 +00:00
be valid directories (not files).
2014-03-14 04:03:29 +00:00
Example:
```javascript
// Default empty filesystem
sh.mkdirp('/test/mkdirp', function(err) {
if(err) throw err;
// the root '/' now contains a directory 'test' containing the directory 'mkdirp'
});
```