title: What is the most concise way to ascertain whether the program itself has a certain access right to a file?
body: <p><strong>Question:</strong></p>
  
  <p>Note that for the purpose of this question I will assume that a directory is a special kind of file.</p>
  
  <p>Furthermore, I will assume (and please do correct me if I am wrong), that writing rights to a directory encompass the right to create new files within said directory.</p>
  
  <p><code>std::filesystem::perms</code> lists the following kinds of permission, each for the three operation types <code>read</code>, <code>write</code> and <code>execute</code>:</p>
  
  <ul>
  <li><code>owner</code> : The owner <em>of the file</em>.</li>
  <li><code>group</code> : The user group <em>of the file</em>.</li>
  <li><code>others</code>: Everyone? (Not at all sure about that; please correct if wrong.)</li>
  </ul>
  
  <p>Now, as can easily be seen, if one wishes to know whether the running program has a given right, this information is non-trivial to derive from these:</p>
  
  <ul>
  <li>Is the program's user the file's user?</li>
  <li>Is any of the file's user group one of or a child of the program's user group?</li>
  </ul>
  
  <p><strong>Interestingly, there does not seem to exist any function in the library, which would correspond to the following function declaration:</strong></p>
  
  <pre><code>enum FileAction { READ = 1, WRITE = 2, RW = READ|WRITE };
  
  bool canDo(const std::filesystem::path &amp; path, FileAction action);
  </code></pre>
  
  <p><strong>How could such a function be implemented then?</strong></p>
  
  <p>Of course, the good old pre-C++17 way would be to just try and start reading/writing and catch the exception if it fails. I would like to think that the Filesystem library has to offer a more elegant solution.</p>
  
  <p>I am interested in answers applicable to Windows (XP+) and Linux. Please remark, if your answer only applies to one (even if one should hope that a universal answer does exist and can be found).</p>
  
  <hr>
  
  <p><strong>Use case:</strong> (Belatedly added to ward against "you should not ask this" answers.)</p>
  
  <p>Ascertain at start of <em>a game</em> whether its own directory is in a valid state: No need to run, after all, if it would fail anyway.</p>
  
  <p>We <em>can</em> assume that no other program will mess with the installation directory of an application. If we <em>cannot</em>, then the user and their system have bigger problems than a <em>malfunctioning game</em> and <em>we do not care</em>.</p>
  
  <p>This is typically a local directory on a physical disk in a personal computer, which will not fail during operation (again, if it does <em>we stop caring</em>). Neither the user nor any other program will try to move files out of our installation directory or delete them or steal our permissions <em>because that is stupid</em> (or deliberate) and so, should that happen, <em>we stop caring</em>.</p>
  
  <p><strong>As long as we still care, we wish to provide a <em>best effort</em> aka <em>greedy algorithm</em> aka <em>educated guess</em> as to whether we can expect to run successfully.</strong></p>
  
  <p>The user might be a non-professional in dealing with their operating system and <em>require hints</em> to provide a working environment. We wish to provide all the hints we can and only then <em>stop caring</em>, because we are an application, not an OS and certainly not the system administrator.</p>
  
  <p>We want to help the user if helping is possible with little effort, because we are nice. We are no samaritan and no superhero, just nice. We also have zero interest to try and intercept shenanigans the user might deliberately be up to, because then any resulting troubles are <em>their own fault</em> and therefore <em>we stop caring</em>.</p>
  
  <p>It is <em>well established practice</em> for games to heuristically check their installation directory for integrity. Steam offers this as a service for all games installed through it. Star Trek Online, League of Legends, Warframe have launchers that do the same.</p>
  
  <p>These games typically check file integrity by calculating (via <em>reading</em>) hashes of their files and comparing them to known valid hashes.</p>
  
  <p>A game wherein <em>almost all files are intended for modification by the user</em>, such as Dwarf Fortress cannot do that. It <em>could</em>, however check for reading rights to files, which must be read, and for writing rights to files, which must be written. Again: there is no <em>purpose</em> in manipulating such rights while the game is running and therefore it should not be assumed to occur.</p>