Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Write files in an atomic fashion w/configurable ownership

License

NotificationsYou must be signed in to change notification settings

npm/write-file-atomic

Repository files navigation

This is an extension for node'sfs.writeFile that makes its operationatomic and allows you set ownership (uid/gid of the file).

writeFileAtomic(filename, data, [options], [callback])

Description:

Atomically and asynchronously writes data to a file, replacing the file if it alreadyexists. data can be a string or a buffer.

Options:

  • filenameString
  • dataString |Buffer
  • optionsObject |String
    • chownObject default, uid & gid of existing file, if any
      • uidNumber
      • gidNumber
    • encodingString |Null default = 'utf8'
    • fsyncBoolean default = true
    • modeNumber default, from existing file, if any
    • tmpfileCreatedFunction called when the tmpfile is created
  • callbackFunction

Usage:

varwriteFileAtomic=require('write-file-atomic')writeFileAtomic(filename,data,[options],[callback])

The file is initially namedfilename + "." + murmurhex(__filename, process.pid, ++invocations).Note thatrequire('worker_threads').threadId is used in addition toprocess.pid if running inside of a worker thread.If writeFile completes successfully then, if passed thechown option it will changethe ownership of the file. Finally it renames the file back to the filename you specified. Ifit encounters errors at any of these steps it will attempt to unlink the temporary file and thenpass the error back to the caller.If multiple writes are concurrently issued to the same file, the write operations are put into a queue and serialized in the order they were called, using Promises. Writes to different files are still executed in parallel.

If provided, thechown option requires bothuid andgid properties or elseyou'll get an error. Ifchown is not specified it will default to usingthe owner of the previous file. To prevent chown from being ran you canalso passfalse, in which case the file will be created with the current user's credentials.

Ifmode is not specified, it will default to using the permissions froman existing file, if any. Expicitly setting this tofalse remove this default, resultingin a file created with the system default permissions.

If options is a String, it's assumed to be theencoding option. Theencoding option is ignored ifdata is a buffer. It defaults to 'utf8'.

If thefsync option isfalse, writeFile will skip the final fsync call.

If thetmpfileCreated option is specified it will be called with the name of the tmpfile when created.

Example:

writeFileAtomic('message.txt','Hello Node',{chown:{uid:100,gid:50}},function(err){if(err)throwerr;console.log('It\'s saved!');});

This function also supports async/await:

(async()=>{try{awaitwriteFileAtomic('message.txt','Hello Node',{chown:{uid:100,gid:50}});console.log('It\'s saved!');}catch(err){console.error(err);process.exit(1);}})();

writeFileAtomicSync(filename, data, [options])

Description:

The synchronous version ofwriteFileAtomic.

Usage:

varwriteFileAtomicSync=require('write-file-atomic').syncwriteFileAtomicSync(filename,data,[options])

About

Write files in an atomic fashion w/configurable ownership

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp