Vinay K
Vinay K

Reputation: 473

ReadStream With Lock (NodeJS)

var fileStream = fs.createReadStream(filePath)
how to have readStream with shared/exclusive lock
so, that file can not be deleted or altered

Upvotes: 2

Views: 1045

Answers (1)

Kornel
Kornel

Reputation: 100110

I don't think node exposes any filesystem locking mechanisms.

If you were going to use filesystem for system-wide locks or secure inter-process communication, you'll need to find another way (e.g. sockets).

If it's not security critical there are some ways of making it harder (but not impossible) for other processes to mess with your files:

  • Use unguessable filenames. require('crypto').getRandomBytes('16').toString('hex')

  • Narrow permissions when creating files via options on createReadStream.

  • Run node process as a special user, so files will be owned only by that user. Either configure OS to run node under appropriate user, or have node run as root and switch to another user via process.setuid/setgid.

Upvotes: 1

Related Questions