“Permission denied” 在 C 中的 open() 函数中。

huangapple go评论66阅读模式
英文:

"Permission denied" in open() function in C

问题

如何修复文件创建的权限问题?

要解决文件创建的权限问题,您可以采取以下步骤:

  1. 检查当前目录权限: 首先,确保您运行程序的当前目录有足够的权限来创建和写入文件。使用ls -l命令可以查看当前目录中文件的权限。

  2. 修改文件创建模式: 在您的代码中,打开文件的代码行如下:

    fd = open(datafile, O_CREAT|O_RDWR,O_APPEND, S_IRUSR, S_IWUSR);
    

    这里您使用了open函数的一些标志,但可能会导致权限问题。您可以尝试修改文件创建模式为更具权限的模式。例如,您可以使用以下模式:

    fd = open(datafile, O_CREAT|O_RDWR|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
    

    这将为文件添加读写权限,即用户、用户组和其他用户都有权限读写文件。

  3. 检查目录权限: 确保存储文件的目录也具有适当的权限,以允许文件的创建和写入。

  4. 运行程序时使用sudo: 如果您在没有足够权限的情况下运行程序,您可以使用sudo来运行它,以获得足够的权限来创建和写入文件。不过,请谨慎使用sudo,因为它可以对系统造成潜在的安全风险。

通过采取这些步骤,您应该能够解决文件创建的权限问题。请注意,为了保持文件系统的安全性,应该仅向具有必要权限的用户提供写入文件的权限。

英文:

I am new to C programming. I am having problem writing to a file using the open() function in C, here is my code for clarity

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

When I execute the program on the first try, the program runs as expected.

first run:

user: ./simplenote "Hello, again"
[DEBUG] buffer @ 0x7fafcb4017a0: 'Hello again'
[DEBUG] datafile @ 0x7fafcb401810: './simplenote.txt'
[DEBUG] file descriptor is 3
Note saved

when I try to open the file and view the text, I get a permission denied error. when I try to open the file with sudo, it opens and the text is in the file. When I run the program a second time, I get an error while opening the file because of permission issues.

second run:

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

How do I fix the permission issues with the file creation?

答案1

得分: 7

File created when program run for first time has permissions that does not allows you to append

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

You should combine mode using | like this:

fd = open(datafile, O_CREAT|O_RDWR|O_APPEND, S_IRUSR | S_IWUSR);

You can check what arguments you should pass to open using man open, on my system it show something like this (trimmed to important parts):

   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);
英文:

File created when program run for first time has permissions that does not allows you to append

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

You should combine mode using | like this:

fd = open(datafile, O_CREAT|O_RDWR|O_APPEND, S_IRUSR | S_IWUSR);

You can check what arguments you should pass to open using man open, on my system it show something like this (trimmed to important parts):

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

答案2

得分: 3

open()的参数不正确。open()的文档说:

> int open(const char *pathname, int flags, mode_t mode);

O_APPEND是一个标志,应该与其他两个标志进行按位或运算,以生成flags参数。

S_IRUSRS_IWUSR是权限。它们应该进行按位或运算,以生成mode参数。

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

fd = open(datafile, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR);
英文:

The arguments of open() are not correct. The documentation of open() says:

> int open(const char *pathname, int flags, mode_t mode);

O_APPEND is a flag, it should be OR-ed with the other two to produce the flags argument.

S_IRUSR and S_IWUSR are permissions. They should be OR-ed to produce the mode argument.

All in all, the call to open() should be:

fd = open(datafile, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR);

huangapple
  • 本文由 发表于 2020年1月6日 02:16:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59602852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定