【发布时间】:2012-09-12 15:08:30
【问题描述】:
我正在编写一个程序来模拟 cp 实用程序。但是,我无法使文件权限正常工作。我知道它们存储在结构stat 中,并存储在st_mode 字段中stat。
我的问题是我没有获得组或其他类别的写权限,即即使源文件是-rwxrwxrwx,我也获得了-rwxr-xr-x 作为文件的权限。我设置权限的语句如下。
if ( (dest_fd = open(dest_file, O_WRONLY|O_CREAT, (stats.st_mode & S_IRUSR)|(stats.st_mode & S_IWUSR)|(stats.st_mode & S_IXUSR)|(stats.st_mode & S_IRGRP)|(stats.st_mode & S_IWGRP)|(stats.st_mode & S_IXGRP)|(stats.st_mode & S_IROTH)|(stats.st_mode & S_IWOTH)| (stats.st_mode & S_IXOTH))) < 0)
{
printf("There was a problem opening the destination file.");
exit(EXIT_FAILURE);
}//ends the if statement opening the destination file.
【问题讨论】:
-
umask惹你生气了吗?无论您在open中设置什么权限,内核都会应用unmask并(可能)删除一些权限。将umask设置为000后试一试。 -
"There was a problem opening the destination file."是无用错误消息的典型示例。man perror
标签: c unix file-io permissions umask