Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

c - Why does open() create my file with the wrong permissions?

I am trying to read some text from a file and write it to another using open(), read() and write().

This is my open() for the file-to-write-to (I want to create a new file and write into it):

fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);

This is setting file-permissions to something I don't understand at all. This is the output of ls -l:

---------T 1 chaitanya chaitanya 0 2010-02-11 09:38 test-1

Even the read permission is locked. I tried searching for this, but could not find ANYTHING. Strangely, write() still successfully writes data to the file.

Also, if I do a 'chmod 777 test-1', things start working properly again.

Could someone please let me know where I am going wrong in my open call?

Thanks!

For your reference, I have pasted the complete program below:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main () {

    char buffer[512], ch;

    int fIn, fOut, i;
    ssize_t bytes;
    FILE *fp = NULL;

    //open a file
    fIn = open ("test", O_RDONLY);
    if (fIn == -1) {
        printf("
failed to open file.");
        return 1;
    }

    //read from file
    bytes =  read (fIn, buffer, sizeof(buffer));
    //and close it
    close (fIn);

    printf("
Successfully read %d bytes.
", bytes);

    //Create a new file
    fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);

    printf("
These are the permissions for test-1
");
    fflush(stdout);
    system("ls -l test-1");

    //write to it and close it.
    write (fOut, buffer, bytes);
    close (fOut);


    //write is somehow locking even the read permission to the file. Change it.
    system("chmod 777 test-1");

    fp = fopen ("test-1", "r");
    if (fp == NULL) {
        printf("
Can't open test-1");
        return 1;
    }

    while (1)
    {
        ch = fgetc(fp);
        if (ch == EOF)
            break;
        printf("
%c", ch);
    }

    fclose (fp);

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

open() takes a third argument which is the set of permissions, i.e.

open(filename, O_RDWR|O_CREAT, 0666)

0666 is an octal number, i.e. every one of the 6's corresponds to three permission bits

6 = rw

7 = rwx

first three bits for owner permission, next three bits for group permission and next is for the world the first digit - represents that is file or directory. (0 - file, d - directory) here we used 0 means file

It's a typical pitfall. The compiler allows you to leave the permission argument away because when you open an existing file the permission bits don't make sense. But when you forget the argument when you create a file, you get a random set of permissions, e.g. 0000 in your case (---).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...