Giter Club home page Giter Club logo

node-fallocate's Introduction

fallocate for node

Because files matter!

Usage

Prefer posix_fallocate over fallocate as it is more portable. offset and length have po be integers.

const {posix_fallocate, fallocate /* linux only */} = require("fallocate");

posix_fallocate("some_file", 0 /* offset */, 1<<20).
  then(console.log).
  catch(console.error);
posix_fallocate("some_file", 0 /* offset */, 1<<20, (err, path) => {
  if (err) {
    console.error(err);
  }
  else {
    console.log(path);
  }
});

fallocate("some_file", 0 /* offset */, 1<<20, 0 /* mode */).
  then(console.log).
  catch(console.error);
fallocate("some_file", 0 /* offset */, 1<<20, 0 /* mode */, (err, path) => {
  if (err) {
    console.error(err);
  }
  else {
    console.log(path);
  }
});

posix_fallocateSync("some_file", 0 /* offset */, 1<<20);
fallocateSync("some_file", 0 /* offset */, 1<<20, 0 /* mode */);

const fd = fs.openSync("some_file", "w", {encoding: null});
try {
  fposix_fallocateSync(fd, 0 /* offset */, 1<<20);
  ffallocateSync(fd, 0 /* offset */, 1<<20, 0 /* mode */);
}
finally {
  fs.closeSync(fd);
}

Errors

Errors are either errors as returned by underlying APIs, or FallocateErrors. FallocateError additionally exposes .errno and .code.

Limitations

Unfortunately the fposix_allocateSync and ffallocateSync functions cannot be implemented in an async variant, due to node not offering a proper way to pass file descriptors to child processes (only sockets and such).

If your OS or rather libc doesn't provide *64 implementations for offsets, the off_t sizes are guesses.

Supported platforms

At the moment only Linux is supported. BSDs might be supported when they provide a posix_fallocate implementation.

Windows are not supported for lack of support of this API. However, maxos offers APIs which can be used to simulate the functionality. This is not implemented at the moment, tho.

Unsupported plaforms will always raise an error.

Implementation details

  • Implemented using ffi, no binary components.
  • Async functions implemented using a child_process performing the actual allocation. That worker is started on demand and kept alive until the parent process exits. If you're messing with the working directory after the first fallocate call make sure to pass a qualified path.
  • Files will be opened with a (unless using the function taking an fd).

node-fallocate's People

Contributors

realdolos avatar

Stargazers

 avatar

Watchers

 avatar  avatar

node-fallocate's Issues

publish to npm?

Iโ€™d like to use this, is there a reason it was never published to npm?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.