6

I'm reading the manual for stat method here and it says:

Using fs.stat() to check for the existence of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.

To check if a file exists without manipulating it afterwards, fs.access() is recommended.

So, I've got two questions:

  • Why using error handler is preferred way over fs.stat() to check for file existence?

  • And since I can use fs.access() to check for file existence, is using error handler mechanism still preferred way to ensure file is open?

I think I have found an answer to the second question:

Using fs.access() to check for the accessibility of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.

So probably fs.open() blocks file for other processes, while fs.stat() and fs.access() simply request information and other processes still can change/delete the file.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • Unless the author of those docs has written an explanation of why somewhere (since they didn't feel the need to explain it in the docs), all people can do is speculate, which isn't really useful here on SO. For instance, one speculation is that it does less work, as `fs.stat` has to get more information about the file. But... – T.J. Crowder Oct 26 '16 at 10:38
  • Separately: Your question is just about the *second* quoted paragraph, right? The first quoted paragraph has nothing to do with `fs.access` (and makes the reason for *its* recommendation clear). – T.J. Crowder Oct 26 '16 at 10:39
  • 1
    [Useful comment](http://stackoverflow.com/questions/32748530/on-linux-is-access-faster-than-stat#comment53341880_32748530) – robertklep Oct 26 '16 at 10:44
  • @T.J.Crowder, I've reworded my questions to make it more clear what I'm asking – Max Koretskyi Oct 26 '16 at 10:46
  • @robertklep, thanks – Max Koretskyi Oct 26 '16 at 10:46
  • I also do not agree with the close-vote. This is a pertinent matter that resulted from a peculiar API design decision by the Node.js developers. – E_net4 Oct 26 '16 at 10:48
  • Related: [*Check synchronously if file/directory exists in Node.js*](http://stackoverflow.com/questions/4482686/check-synchronously-if-file-directory-exists-in-node-js) *(answers address async as well)* – T.J. Crowder Oct 26 '16 at 10:48
  • 1
    Also if you do `fs.stat`/`fs.access` and afterwards open the file, it could be removed/renamed in-between. In this case you would still need to handle the open error. – Bjarke Walling Oct 26 '16 at 10:50
  • @BjarkeWalling, thanks, that's relevant – Max Koretskyi Oct 26 '16 at 10:51

1 Answers1

4

I believe that what should be made clear here is that both fs.stat and fs.access are not recommended for the particular case of checking for the accessibility of a file before opening it. As well mentioned in the question, this can trigger race conditions. The functions exists() and existsSync() were deprecated (around version 4) for this reason (and a few others related to the API): they were often exploited for this purpose.

When seeking to open a file, the operation will already trigger an error if the file is inaccessible. Therefore, such checks should be handled here. Otherwise, there is more than one reasonable way to check if a file exists.

Also note that, as of version 6.8.0, existsSync() is undeprecated! See discussion and the 6.8.0 changelog. The same rules above apply: only use it if you do not intend to open the file afterwards.

Community
  • 1
  • 1
E_net4
  • 27,810
  • 13
  • 101
  • 139