How do you create a directory with the same access permissions as an existing one?

d0m1n1c

I need to make a new folder that has the same permissions as another folder. My end goal for the assignment is to copy a directory and it's contents entirely. This is one of my first steps and I can't seem to get it to work.

What I have is below:

struct stat srcstat;
int srcstatus = stat(source, &srcstat);
if (chdir(dest))
    if (mkdir(dest, srcstat.st_mode)){
        printf("error: could not create <dest>\n");
        exit(1);
    }

source is a cstring with the path to the folder whose permissions I want to use. dest is a c string with the path to the new folder.

the ls with the old and new folders permissions is below

drwxrwxrwx 2 kingacev CS-Majors 4096 Apr 18 17:03 test
drwxr-xr-x 2 kingacev CS-Majors 4096 Apr 18 18:12 test3

my first thought is that I can't use srcstat.st_mode in the way I'm attempting. If that's the case, is there an equally simple way to do what I'm trying to do? If not, how far from the mark am I?

zwol

You are running foul of the "umask", a per-process setting which masks out permission bits in file and directory creation operations.

There is no safe way to disable the umask. What you should do instead is create the directory with mode zero (i.e. all access denied) and then use chmod (the system call, not the shell command of the same name) to adjust the permissions to what you want.

Your program fragment has many other bugs. This is tricky to get right, and might well be a security hole if you get it wrong, so I shall write out the correct code in detail:

int
make_directory_like(const char *to_create,
                    const char *prototype)        
{
    struct stat st;
    if (lstat(prototype, &st)) {
        fprintf(stderr, "lstat: %s: %s\n", prototype, strerror(errno));
        return -1;
    }
    if (!S_ISDIR(st.st_mode)) {
        fprintf(stderr, "lstat: %s: %s\n", prototype, strerror(ENOTDIR));
        return -1;
    }
    /* create directory initially with no perms at all - this is the only
       safe way to suppress the effects of the umask.  */
    if (mkdir(to_create, 0000)) {
        if (errno != EEXIST) {
            fprintf(stderr, "mkdir: %s: %s\n", to_create, strerror(errno));
            return -1;
        } else {
            /* check whether the thing that exists is a directory */
            struct stat st2;
            if (lstat(to_create, &st2)) {
                fprintf(stderr, "lstat: %s: %s\n", to_create, strerror(errno));
                return -1;
            }
            if (!S_ISDIR(st2.st_mode)) {
                fprintf(stderr, "mkdir: %s: %s\n", to_create, strerror(EEXIST));
                return -1;
            }
        }
    }
    if (chmod(to_create, st.st_mode & ~S_IFMT)) {
        fprintf(stderr, "chmod: %s: %s\n", to_create, strerror(errno));
        return -1;
    }
    return 0;
}

Exercises for you:

  1. Why is this the only safe way to suppress the effects of the umask? (Hint: threads. But that's only one of several reasons.)
  2. Why do I use lstat rather than stat?
  3. Why is it necessary to stat the to-create path if mkdir fails with errno == EEXIST?
  4. Why is it incorrect to use chdir as you were doing? (There are two reasons.)
  5. Why is it safe to go ahead and call mkdir on the to-create path when we don't know whether there is already something there?
  6. Why is that & ~S_IFMT thing necessary?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do you create a specialized version of an existing directive?

From Dev

Python: How to create a directory and overwrite an existing one if necessary?

From Dev

How do you add the same images of different pixel densities to the drawable directory (android studio)?

From Dev

How do you create in python a file with permissions other users can write

From Dev

How do you fix file permissions for root directory?

From Dev

Permissions issue: how can Apache access files in my Home directory?

From Dev

How do I force Windows 7 to create a new domain profile with same name as an existing one?

From Dev

How do I change permissions on a directory

From Dev

What is a virtual optical disk file and how do you create one?

From Dev

Access permissions for directory in Linux

From Dev

Permissions issue: how can Apache access files in my Home directory?

From Dev

How do you fix file permissions for root directory?

From Dev

How to set a directory's permissions for group read/execute access

From Dev

sshfs You do not have the permissions necessary to view the contents of mounted directory

From Dev

How do I recursively create a Textures directory in every existing directory in Bash and then delete them?

From Dev

How do you create a specialized version of an existing directive?

From Dev

You do not have sufficient permissions to access this page while making a plugin

From Dev

How do you create a directory with the same access permissions as an existing one?

From Dev

How do I set the permissions and timestamps of one directory to match anothers?

From Dev

How do you bulk move files up one directory safely?

From Dev

How do permissions on a directory affect files in it?

From Dev

How do you create a sub-directory for a website?

From Dev

How can you hide shared folders from those who do not have permissions in Window server 2016 without Active directory

From Dev

How do you create an access method to convert a string to a boolean

From Dev

How to create Maildir directory with enough permissions for Postfix?

From Dev

How do you list the folder permissions of only certain directories that follow a common pattern in a particular directory?

From Dev

Folder access permissions - how do they work?

From Dev

How do you create one rule overriding rules for individual pages?

From Dev

permissions to create file in directory

Related Related

  1. 1

    How do you create a specialized version of an existing directive?

  2. 2

    Python: How to create a directory and overwrite an existing one if necessary?

  3. 3

    How do you add the same images of different pixel densities to the drawable directory (android studio)?

  4. 4

    How do you create in python a file with permissions other users can write

  5. 5

    How do you fix file permissions for root directory?

  6. 6

    Permissions issue: how can Apache access files in my Home directory?

  7. 7

    How do I force Windows 7 to create a new domain profile with same name as an existing one?

  8. 8

    How do I change permissions on a directory

  9. 9

    What is a virtual optical disk file and how do you create one?

  10. 10

    Access permissions for directory in Linux

  11. 11

    Permissions issue: how can Apache access files in my Home directory?

  12. 12

    How do you fix file permissions for root directory?

  13. 13

    How to set a directory's permissions for group read/execute access

  14. 14

    sshfs You do not have the permissions necessary to view the contents of mounted directory

  15. 15

    How do I recursively create a Textures directory in every existing directory in Bash and then delete them?

  16. 16

    How do you create a specialized version of an existing directive?

  17. 17

    You do not have sufficient permissions to access this page while making a plugin

  18. 18

    How do you create a directory with the same access permissions as an existing one?

  19. 19

    How do I set the permissions and timestamps of one directory to match anothers?

  20. 20

    How do you bulk move files up one directory safely?

  21. 21

    How do permissions on a directory affect files in it?

  22. 22

    How do you create a sub-directory for a website?

  23. 23

    How can you hide shared folders from those who do not have permissions in Window server 2016 without Active directory

  24. 24

    How do you create an access method to convert a string to a boolean

  25. 25

    How to create Maildir directory with enough permissions for Postfix?

  26. 26

    How do you list the folder permissions of only certain directories that follow a common pattern in a particular directory?

  27. 27

    Folder access permissions - how do they work?

  28. 28

    How do you create one rule overriding rules for individual pages?

  29. 29

    permissions to create file in directory

HotTag

Archive