首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C中open()函数中的“拒绝权限”

C中open()函数中的“拒绝权限”
EN

Stack Overflow用户
提问于 2020-01-05 18:16:53
回答 2查看 8.3K关注 0票数 2

我对C编程很陌生。我在使用C中的open()函数编写文件时遇到了问题,下面是我的代码

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

void usage(char *prog_name, char *filename){
    printf("Usage: %s <data to add to %s> \n",prog_name, filename);
    exit(0);
}

void fatal(char *);                          
void *errchck_malloc(unsigned int);     

int main(int argc, char *argv[]){
    int fd; // File descriptor
    char *buffer, *datafile;

    buffer = (char *) errchck_malloc(100);
    datafile = (char *) errchck_malloc(20);
    strcpy(datafile, "./simplenote.txt");

    if (argc < 2)
        usage(argv[0], datafile);

    strcpy(buffer, argv[1]);

    printf("[DEBUG] buffer @ %p: \'%s\'\n", buffer, buffer);
    printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);

    strncat(buffer, "\n", 1);

    // Open file 
    fd = open(datafile, O_CREAT|O_RDWR,O_APPEND, S_IRUSR, S_IWUSR);
    if(fd == -1)
        fatal("in main() while opening file");
    printf("[DEBUG] file descriptor is %d\n", fd);

    // Writing data to file
    if(write(fd, buffer, strlen(buffer))==-1)
        fatal("in main() while writing buffer to file");

    // Closing file
    if(close(fd) == -1)
        fatal("in main() while closing file");

    printf("Note saved\n");
    free(buffer);
    free(datafile);
}

// fatal(): Function to display error message then exit
void fatal(char *message){
    char err_msg[100];

    strcpy(err_msg, "[!!] Fatal Error ");
    strncat(err_msg, message, 83);
    perror(err_msg);
    exit(-1);
}

// errchck_malloc(): An error check malloc wrapper function
void *errchck_malloc(unsigned int size){
    void *ptr;
    ptr = malloc(size);
    if(ptr == NULL)
        fatal("in errchck_malloc() on memory allocation");
    return ptr;
}

当我在第一次尝试时执行程序时,程序将按预期运行。

第一轮:

代码语言:javascript
复制
user: ./simplenote "Hello, again"
[DEBUG] buffer @ 0x7fafcb4017a0: 'Hello again'
[DEBUG] datafile @ 0x7fafcb401810: './simplenote.txt'
[DEBUG] file descriptor is 3
Note saved

当我试图打开文件并查看文本时,我会得到一个拒绝权限的错误。当我尝试用sudo打开文件时,它会打开,并且文本在文件中。当我第二次运行程序时,由于权限问题,打开文件时会出现错误。

第二轮:

代码语言:javascript
复制
user: ./simplenote "just checking if it is still working"                                                           
[DEBUG] buffer @ 0x7face4c017a0: 'just checking if it is still working'
[DEBUG] datafile @ 0x7face4c01810: './simplenote.txt'
[!!] Fatal Error in main() while opening file: Permission denied

如何解决文件创建中的权限问题?

EN

回答 2

Stack Overflow用户

发布于 2020-01-05 18:35:41

当程序第一次运行时创建的文件具有不允许追加的权限。

代码语言:javascript
复制
stat simplenote.txt 
  File: simplenote.txt
  Size: 5           Blocks: 8          IO Block: 4096   regular file
Device: 2fh/47d Inode: 32810078    Links: 1
Access: (2000/------S---)  Uid: ( 1000/ user)   Gid: ( 1000/ user)
Access: 2020-01-05 19:29:34.317872734 +0100
Modify: 2020-01-05 19:29:34.317872734 +0100
Change: 2020-01-05 19:29:34.317872734 +0100

您应该使用这样的|组合模式:

代码语言:javascript
复制
fd = open(datafile, O_CREAT|O_RDWR|O_APPEND, S_IRUSR | S_IWUSR);

您可以使用open检查哪些参数应该传递给man open,在我的系统中,它显示了如下内容(裁剪到重要部分):

代码语言:javascript
复制
   int open(const char *pathname, int flags);
   int open(const char *pathname, int flags, mode_t mode);

   int creat(const char *pathname, mode_t mode);

   int openat(int dirfd, const char *pathname, int flags);
   int openat(int dirfd, const char *pathname, int flags, mode_t mode);
票数 7
EN

Stack Overflow用户

发布于 2020-01-05 18:39:02

open()的论点是不正确的。documentation of open()说:

int (const*路径名、int标志、mode_t模式);

O_APPEND是一个标志,它应该与其他两个一起使用或编辑来生成flags参数。

S_IRUSRS_IWUSR是权限。应该对它们进行OR编辑,以生成mode参数。

总之,对open()的调用应该是:

代码语言:javascript
复制
fd = open(datafile, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59602852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档