'open' creates with wrong permissions

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

'open' creates with wrong permissions

Postby kev » Wed Mar 03, 2010 7:06 pm

I am new to C++ using GCC and I have tried to use the following programme to create a file (ostensibly for writing data to but after that) has permissions of 777 (rwx) for all users. But it only creates with 755 whether I run it as root or as regular user. Can anyone tell me why please?
Code: Select all
using namespace std;
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <cerrno>
#include <errno.h>

int main(){
long int fd=0;
fd=open("/home/test/a",O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO);

if (errno!=0) {cout<<"\nerror number for opening is "<<errno;}

if (fd>=0 && fd<=0x7fffffff )  { cout<<"\nthe fd is:\t"<<fd;
close(fd);}

else {cout<<"\n\nerror,    fd is "<<fd<<"\n";}

cout<<"\nError code values are:-\n";
cout<<EAGAIN<<" "<<EBADF<<" "<<EFAULT<<" "<<EFBIG<<" "<<EINTR<<" "<<EINVAL<<" "<<EIO<<" "<<ENOSPC<<"\n";
cout<<EACCES<<" "<<EEXIST<<" "<<EFAULT<<" "<<EISDIR<<" "<<ELOOP<<" "<<EMFILE<<" "<<ENAMETOOLONG<<"\n";
cout<<ENFILE<<" "<<ENODEV<<" "<<ENXIO<<" "<<ENOENT<<" "<<ENOMEM<<" "<<ENOSPC<<" "<<ENOTDIR<<"\n";
cout<<ENXIO<<" "<<EOVERFLOW<<" "<<EPERM<<" "<<EROFS<<" "<<ETXTBSY<<" "<<EWOULDBLOCK<<"\n";

return 0;}
kev
 
Posts: 12
Joined: Tue Feb 16, 2010 4:40 pm

Postby johnhudson » Wed Mar 03, 2010 7:34 pm

Sometimes folders have default settings which are applied to all files in the folder. Check that these settings are not overriding your file settings.
johnhudson
LXF regular
 
Posts: 876
Joined: Wed Aug 03, 2005 1:37 pm

Postby micke » Fri Mar 05, 2010 1:27 pm

When you create new files in a program and you want to make sure that specific access permission bits are set then you have to modify the umask value. If you set umask to 0 before the call to open then you should get the permissions you expect.
micke
 
Posts: 11
Joined: Fri Jan 16, 2009 1:14 pm

Postby kev » Mon Mar 15, 2010 12:45 pm

Thanks for that micke; (I've been away) It's made me re-read the man pages more carefully. I see '"stat" is useful in this overall context but I cannot get it to work; can you offer any demo codelets for that please?
kev
 
Posts: 12
Joined: Tue Feb 16, 2010 4:40 pm

Postby micke » Mon Mar 15, 2010 2:50 pm

The man page for stat includes an example. If the man page in your linux distribution doesn't include an example then take a look at the man pages at http://www.kernel.org/doc/man-pages/
micke
 
Posts: 11
Joined: Fri Jan 16, 2009 1:14 pm

Postby C0ldf1re » Wed Mar 17, 2010 4:27 am

This thread answered my problem with "open." Thanks! :wink:
And the devil, taking him up into an high mountain, shewed unto him all the kingdoms of the world in a moment of time. (Luke 4:5)
User avatar
C0ldf1re
 
Posts: 2
Joined: Mon Mar 15, 2010 11:29 pm

Postby kev » Sat Mar 20, 2010 3:01 pm

Thanks micke.
Being so new to C++, that's going to keep me busy for quite a while.
kev
 
Posts: 12
Joined: Tue Feb 16, 2010 4:40 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests