1

I'm reviewing the server.c source code to better understand it because I believe this particular pset is largely about understanding source code not written by us and how the internet works generally.

I'm trying to precisely understand what access() does. On a high level I believe it is used to test whether a file exists and/or how it can be manipulated. But how exactly does it do this?

Does it need to be given the full file path (i.e., ~/workspace/pset6/public/test/…)? Otherwise, how would it know where to locate the file to determine its accessibility.

Lastly, when determining accessibility, is it reading through the files access permissions (i.e., read, write and append) to determine this?

Thanks, Craig

1
3
+50
  1. On a high level I believe it is used to test whether a file exists and/or how it can be manipulated. But how exactly does it do this?

    According to the man pages:

    The check is done using the calling process's real UID and GID, rather than the effective IDs as is done when actually attempting an operation (e.g., open(2)) on the file. This allows set-user-ID programs to easily determine the invoking user's authority.

    Where UID and GID are the User IDentifier and Group IDEntifier, which are used by UNIX OSes to determine which system resources a user can access. You can learn more about it here: Wikipedia: User identifier

  2. Does it need to be given the full file path (i.e., ~/workspace/pset6/public/test/…)? Otherwise, how would it know where to locate the file to determine its accessibility.

    No it doesn't need the absolute path. It can use a relative path too. It can use the relative path, because it can find the currently working directory by calling another function, getcwd() for example.

    Here is a demonstration for a file called test.txt in the same folder as the executable:

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        // the currently working directory
        char *cwd = malloc(1024 * sizeof(char));
        if (getcwd(cwd, 1024) != NULL)
            printf("Currently working in: %s\n", cwd);
        else
            printf("getcwd() error\n");
    
        // check if the file exists using a relative path
        if (!access("test.txt", F_OK))
            printf("test.txt exists using a relative path.\n");
        else
            printf("test.txt doesn't exist using a relative path.\n");
    
        // check if the file exists using an absolute path
        if (!access(strcat(cwd, "/test.txt"), F_OK))
            printf("test.txt exists using an absolute path.\n");
        else
            printf("test.txt doesn't exist using an absolute path.\n");
    
        return 0;
    }
    
  3. Lastly, when determining accessibility, is it reading through the files access permissions (i.e., read, write and append) to determine this?

    As stated in 1., it uses the UID and GID of the caller, against the file's access permissions, to see if the user has access to this resource.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .