为什么 open() 使用错误的权限创建我的文件? [英] Why does open() create my file with the wrong permissions?

查看:16
本文介绍了为什么 open() 使用错误的权限创建我的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件中读取一些文本,然后使用 open()read()write() 将其写入另一个文件>.

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

这是我的 open() 用于文件写入(我想创建一个新文件并写入其中):

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);

这是将文件权限设置为我完全不理解的内容.这是 ls -l 的输出:

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

连读权限都被锁定了.我试图搜索这个,但找不到任何东西.奇怪的是,write() 仍然成功地将数据写入文件.

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.

此外,如果我执行chmod 777 test-1",一切都会再次正常工作.

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?

谢谢!

为了您的参考,我已经粘贴了完整的程序如下:

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;
}

推荐答案

open() 接受第三个参数,即权限集,即

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

open(filename, O_RDWR|O_CREAT, 0666)

0666 是一个八进制数,即 6 中的每一个对应三个权限位

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

6 = rw

7 = rwx

前三位为所有者权限,后三位为组权限,接下来为世界第一个数字 - 表示是文件或目录.(0 - 文件,d - 目录)这里我们用 0 表示文件

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

这是一个典型的陷阱.编译器允许您保留权限参数,因为当您打开现有文件时,权限位没有意义.但是当您在创建文件时忘记参数时,您将获得一组随机权限,例如在您的情况下为 0000 (---).

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 (---).

这篇关于为什么 open() 使用错误的权限创建我的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆