Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

'open' creates with wrong permissions

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
kev



Joined: Tue Feb 16, 2010 4:40 pm
Posts: 12

PostPosted: Wed Mar 03, 2010 7:06 pm    Post subject: 'open' creates with wrong permissions Reply with quote

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:

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;}
Back to top
View user's profile Send private message
johnhudson
LXF regular


Joined: Wed Aug 03, 2005 2:37 pm
Posts: 857

PostPosted: Wed Mar 03, 2010 7:34 pm    Post subject: Reply with quote

Sometimes folders have default settings which are applied to all files in the folder. Check that these settings are not overriding your file settings.
Back to top
View user's profile Send private message
micke



Joined: Fri Jan 16, 2009 1:14 pm
Posts: 11

PostPosted: Fri Mar 05, 2010 1:27 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
kev



Joined: Tue Feb 16, 2010 4:40 pm
Posts: 12

PostPosted: Mon Mar 15, 2010 12:45 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
micke



Joined: Fri Jan 16, 2009 1:14 pm
Posts: 11

PostPosted: Mon Mar 15, 2010 2:50 pm    Post subject: Reply with quote

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/
Back to top
View user's profile Send private message
C0ldf1re



Joined: Mon Mar 15, 2010 11:29 pm
Posts: 2

PostPosted: Wed Mar 17, 2010 4:27 am    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
kev



Joined: Tue Feb 16, 2010 4:40 pm
Posts: 12

PostPosted: Sat Mar 20, 2010 3:01 pm    Post subject: Reply with quote

Thanks micke.
Being so new to C++, that's going to keep me busy for quite a while.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast