• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

test file permissions


This seems like it should be obvious, yet I can't find the answer anywhere.

I need to test whether or not the current process can read, write, and execute a given file.  I am not trying to display the file mode as a string (which is what all examples I've found so far show), I want to know what I can do with a file.

I've been testing read and write by opening the file and catching errors, but this seems hackish and doesn't work at all for execute permissions.

Do I really need to deconvolute the mode mask and test user, group, all in turn, by hand?  Surely this already exists somewhere.

1 Solution
You can use 'access()', e.g.

/* ACCESS.C: This example uses _access to check the
 * file named "ACCESS.C" to see if it exists and if
 * writing is allowed.

#include  <io.h>
#include  <stdio.h>
#include  <stdlib.h>

void main( void )
   /* Check for existence */
   if( (access( "ACCESS.C", 0 )) != -1 )
      printf( "File ACCESS.C exists\n" );
      /* Check for write permission */
      if( (access( "ACCESS.C", 2 )) != -1 )
         printf( "File ACCESS.C has write permission\n" );

If you are using a MS compiler, that has to be '_access' instead.
BTW, you'll find more infoo here: http://linux.ctyme.com/man/man0011.htm ("ACCESS (2)
check user's permissions for a file"):


      #include <unistd.h>
        int access(const char * pathname , int  mode );  


      access checks whether the process would be allowed to read, write or test for existence of the file (or other file system object) whose name is pathname . If pathname is a symbolic link permissions of the file referred to by this symbolic link are tested.

      mode is a mask consisting of one or more of R_OK , W_OK , X_OK and F_OK .

      R_OK , W_OK and X_OK request checking whether the file exists and has read, write and execute permissions, respectively. F_OK just requests checking for the existence of the file.

      The tests depend on the permissions of the directories occurring in the path to the file, as given in pathname , and on the permissions of directories and files referred to by symbolic links encountered on the way.

      The check is done with the process's real uid and gid, rather than with the effective ids as is done when actually attempting an operation. This is to allow set-UID programs to easily determine the invoking user's authority.

      Only access bits are checked, not the file type or contents. Therefore, if a directory is found to be "writable," it probably means that files can be created in the directory, and not that the directory can be written as a file. Similarly, a DOS file may be found to be "executable," but the execve (2) call will still fail.

      If the process has appropriate privileges, an implementation may indicate success for X_OK even if none of the execute file permission bits are set.


      On success (all requested permissions granted), zero is returned. On error (at least one bit in mode asked for a permission that is denied, or some other error occurred), -1 is returned, and errno is set appropriately.
Argh, better explanations:


http://www.quepublishing.com/articles/article.asp?p=23618&seqNum=3&rl=1 ("8.2 access: Testing File Permissions"):

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

int main (int argc, char* argv[])
 char* path = argv[1];
 int rval;

 /* Check file existence. */
 rval = access (path, F_OK);
 if (rval == 0)
  printf ("%s exists\n", path);
 else {
  if (errno == ENOENT)
   printf ("%s does not exist\n", path);
  else if (errno == EACCES)
   printf ("%s is not accessible\n", path);
  return 0;

 /* Check read access. */
 rval = access (path, R_OK);
 if (rval == 0)
  printf ("%s is readable\n", path);
  printf ("%s is not readable (access denied)\n", path);

 /* Check write access. */
 rval = access (path, W_OK);
 if (rval == 0)
  printf ("%s is writable\n", path);
 else if (errno == EACCES)
  printf ("%s is not writable (access denied)\n", path);
 else if (errno == EROFS)
  printf ("%s is not writable (read-only filesystem)\n", path);
 return 0;
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

The longer answer it that it depends on the Platform you're using. All the answers assume that you have some "Posix" stuff available. The Windows answer to the question is:
AccessCheck  or proper parameters to CreateFile

Note that stat and access() only tell you what the file permissions were, not what they will be when you actually try to open the file.  The only sure test is to try a file open() and see what happens.  Also consider whether you care about any lockf() or flock() or NFS locks on the file.

_D_Author Commented:
Of course I found access() a few minutes after posting (after days of googling).

Thanks for the answer, and yes, this was for UNIX, and no, open() wouldn't work since testing execute was the most important one for me.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now