Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 3
    This does not work for files larger than 2GB on all systems. Commented Feb 27, 2013 at 12:23
  • 5
    @LudvigANorin: on such systems, the chances are that access() also has problems, and there are options to use to make access() and stat() work with large files (bigger than 2 GB). Commented Mar 18, 2013 at 6:26
  • 20
    Could either of you point to documentation regarding the failure after 2 GB? Also, what is the alternative in such cases? Commented Apr 23, 2013 at 5:37
  • 11
    Both stat() and access() suffer from the TOCTOU vulnerability (so does lstat(), but fstat() is safe). It depends what you're going to do based on the presence or absence of the file. Using the correct options to open() is usually the best way of dealing with the problems, but it can be tricky formulating the right options. See also discussions on EAFP (Easier to Ask for Forgiveness than Permission) and LBYL (Look Before You Leap) -- see LBYL vs EAFP in Java, for example. Commented May 1, 2014 at 23:50
  • 4
    @Telemachus if you need to avoid TOCTOU, at least on Linux systems you can just open() the file in TOC (the open() result then becomes the check for file-exisence), then use this descriptor in TOU. This way even if file does not exists anymore at TOU, you still can access it through file-descriptor. Its content will be kept as long as there are processes that have it opened. Commented Jan 11, 2021 at 9:38