chmod无法授予所有权限

Pau*_*rev 3 c embedded chmod

我试图在C程序中使用chmod函数更改文件权限

chmod("/tmp/toBoard", S_IRWXU | S_IRWXG | S_IRWXO);
Run Code Online (Sandbox Code Playgroud)

但是在程序运行后我检查权限并获取

-rwxr-xr-x 1 root root
Run Code Online (Sandbox Code Playgroud)

我在Linux嵌入式主板上运行这个程序.toBoard是从/ var目录中的其他文件复制到程序内的文件,源文件具有所有权限(从终端手动设置).当我尝试手动复制它并设置权限时,它工作,但当我复制文件并尝试给它所有权限 - 它失败没有错误

copy("/var/www/defaults.dat", "/tmp/toBoard");
int err;
if(err = chmod("/tmp/toBoard", S_IRWXU | S_IRWXG | S_IRWXO)){
    perror("chmod");
}
struct stat buffer;
int status = stat("/tmp/toBoard", &buffer);
Run Code Online (Sandbox Code Playgroud)

如何将所有权限设置为绿灯?

cni*_*tar 7

就目前而言,您的调用是正确的,但可能是失败的,并且您没有检查返回的代码.你可以尝试:

if (chmod("/tmp/toBoard", S_IRWXU | S_IRWXG | S_IRWXO)) {
    perror("chmod");
    /* more error handling. */
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,可能是"权限被拒绝"的情况.