resin-io/node-ext2fs

Name: node-ext2fs

Owner: Resin.io

Description: NodeJS native bindings to the libext2fs for cross-platform ext{2,3,4} filesystem handling

Created: 2017-03-02 06:24:57.0

Updated: 2017-06-20 01:20:27.0

Pushed: 2017-11-21 20:56:29.0

Homepage: null

Size: 203

Language: C

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

node-ext2fs

NodeJS native bindings to the linux ext{2,3,4} filesystem library

Build Status

node-ext2fs uses the e2fsprogs project to provide access to ext filesystem from NodeJS in a cross-platform way.

Some things you can do with this module:

Warning: The API exposed by this library is still forming and can change at any time!

Installation

To install node-ext2fs you need to have gcc and make available to your environment. For Linux and Mac having a working node-gyp installation is enough. To install on windows, you have to install MingW64 and make sure mingw32-make and gcc are available in your Powershell or cmd.exe terminal.

Simply compile and install node-ext2fs using npm:

m install ext2fs
Usage

Mount a disk image and use the returned fs object. The fs returned object behaves like node's fs except it doesn't provide any xxxxSync method. You can also issue DISCARD requests using ext2fs.trim(filesystem, callback)

See the examples below.

Example
t ext2fs = require('ext2fs');
t filedisk = require('file-disk');
t fs = require('fs');

t fd = fs.openSync('/path/to/ext4_filesystem.img', 'r+');
t disk = new filedisk.FileDisk(fd);

fs.mount(disk, function(err, filesystem) {
if (err) {
    return;
}
// filesystem behaves like node's fs
console.log('Mounted filesystem successfully');
filesystem.readFile('/some_file', 'utf8', function(err, contents) {
    if (err) {
        return;
    }
    console.log('contents:', contents);
    ext2fs.trim(filesystem, function(err) {
        if (err) {
            return;
        }
        console.log('TRIMed filesystem');
        // don't forget to umount
        ext2fs.umount(filesystem, function(err) {
            if (err) {
                return;
            }
            console.log('filesystem umounted')
            fs.closeSync(fd)
        });
    });
});

Example using promises

The code above isn't very practical as it requires a new level of indentation for each call. Let's simplify it using promises.

You can use ext2fs.mountDisposer with Promise.using so the filesystem is umounted automatically when you're done using it.

t Promise = require('bluebird')
t ext2fs = Promise.promisifyAll(require('ext2fs'));
t filedisk = require('file-disk');

t path = 'test/fixtures/ext2.img';

ise.using(filedisk.openFile(path, 'r+'), function(fd) {
const disk = new filedisk.FileDisk(fd);
return Promise.using(ext2fs.mountDisposer(disk), function(filesystem) {
    filesystem = Promise.promisifyAll(filesystem);
    // filesystem behaves like node's fs
    console.log('Mounted filesystem successfully');
    return filesystem.readFileAsync('/1', 'utf8')
    .then(function(contents) {
        console.log('contents:', contents);
        return ext2fs.trimAsync(filesystem);
    })
    .then(function() {
        console.log('TRIMed filesystem');
    });
});

Support

If you're having any problems, please raise an issue on GitHub.

Package fails to install as no pre-built package is available

Node-ext2fs is pre-built for a range of OSs and Node versions, but we don't have perfect coverage here yet, and it may fail to install if you're not on an pre-built version and you don't have local build tools available.

If you have an issue with this, and your platform is one you feel we should support, please raise an issue on this repo, so we can look at adding your configuration to the pre-built versions that works automatically.

In the meantime, you can typically install this package by updating to a newer Node release which does have pre-built binaries, or by setting up a local environment so the build is successful (see 'Installation' above).

License

node-ext2fs is free software, and may be redistributed under the terms specified in the license.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.