c++,linux - mmap() 权限在程序创建的文件上被拒绝,创建的文件具有打开的所有权限
c++, linux - mmap() permission denied on file created by program, created file has all permissions open
权限一直被拒绝,但据我所知,我的权限是宽开放。
//snip
int outfile = creat("outFile.txt", O_RDWR | O_CREAT | S_IRWXU | S_IRWXG | S_IRWXO);
if (outfile < 0)
{
cout << "\n" << "output file cannot be opened, error" << "\n";
cout << strerror(errno) << "\n";
exit(1);
}
int pagesize = getpagesize();
for (int i=0; i<pagesize; i++)
{
write(outfile, "-", 1);
}
//there is code here that outFile.txt has read and write permissions, and it always says read and write are OK
char* target = (char*)mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, outfile, 0);
if (target == MAP_FAILED)
{
cout << "\n" << "output mapping did not succeed" << "\n";
cout << strerror(errno) << "\n";
exit(1);
}
else
{
cout << "\n" << "output mapping succeeded" << "\n";
}
//snip
这是一个学校项目,我们有一个 1GB 的文件,一个 4096 的页面大小,并被告知我们不能只使用 write() 系统调用。
int outfile = creat("outFile.txt", O_RDWR | O_CREAT | S_IRWXU | S_IRWXG | S_IRWXO);
这里有两个问题:
creat
以只写模式创建文件,它的第二个参数是 mode_t
掩码。在这里传递 O_
标志,与 S_
模式位一起拍打,导致无意义的垃圾。
mmap
调用的参数需要一个 O_RDWR
文件描述符,如上所述,O_RDWR
参数被解释为 文件权限位而不是文件打开模式。
这个应该换成open()
,三个参数:
int outfile = open("outFile.txt", O_CREAT | O_TRUNC | O_RDWR,
S_IRWXU | S_IRWXG | S_IRWXO);
权限一直被拒绝,但据我所知,我的权限是宽开放。
//snip
int outfile = creat("outFile.txt", O_RDWR | O_CREAT | S_IRWXU | S_IRWXG | S_IRWXO);
if (outfile < 0)
{
cout << "\n" << "output file cannot be opened, error" << "\n";
cout << strerror(errno) << "\n";
exit(1);
}
int pagesize = getpagesize();
for (int i=0; i<pagesize; i++)
{
write(outfile, "-", 1);
}
//there is code here that outFile.txt has read and write permissions, and it always says read and write are OK
char* target = (char*)mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, outfile, 0);
if (target == MAP_FAILED)
{
cout << "\n" << "output mapping did not succeed" << "\n";
cout << strerror(errno) << "\n";
exit(1);
}
else
{
cout << "\n" << "output mapping succeeded" << "\n";
}
//snip
这是一个学校项目,我们有一个 1GB 的文件,一个 4096 的页面大小,并被告知我们不能只使用 write() 系统调用。
int outfile = creat("outFile.txt", O_RDWR | O_CREAT | S_IRWXU | S_IRWXG | S_IRWXO);
这里有两个问题:
creat
以只写模式创建文件,它的第二个参数是mode_t
掩码。在这里传递O_
标志,与S_
模式位一起拍打,导致无意义的垃圾。mmap
调用的参数需要一个O_RDWR
文件描述符,如上所述,O_RDWR
参数被解释为 文件权限位而不是文件打开模式。
这个应该换成open()
,三个参数:
int outfile = open("outFile.txt", O_CREAT | O_TRUNC | O_RDWR,
S_IRWXU | S_IRWXG | S_IRWXO);