user4435781
user4435781

Reputation:

problems with the open() function in different file permissions

I am implementing the virtual disk system in C, which includes handling the file system as well. I just want to know, why the open function in C returns -1 when I try to open a file with some group permission or otherspermissions.

Lets say we have file mode that is 040 (Read permission bit for the group owner of the file):

int main(){
    int filedes;
    filedes = open(filename, O_RDWR, 040);
    if(filedes < 0)
        return -1;

    printf("Open success\n");    

}

This snippet return without printing open success. Where this code with file mode 0644 works perfectly fine

int main(){
    int filedes;
    filedes = open(filename, O_RDWR, 0644);
    if(filedes < 0)
        return -1;

    printf("Open success\n");    

}

I don't understand why is this happening?

Upvotes: 0

Views: 181

Answers (1)

Aida Paul
Aida Paul

Reputation: 2722

040 specifically disallows the owner of said file to do anything with it. Even though your group can, you've explicitly defined that your own user can't use it. It may seem weird, but OS only does what you've told it to do.

Upvotes: 1

Related Questions