| View previous topic :: View next topic |
| Author |
Message |
kev
Joined: Tue Feb 16, 2010 4:40 pm Posts: 12
|
Posted: Wed Mar 03, 2010 7:06 pm Post subject: 'open' creates with wrong permissions |
|
|
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 |
|
 |
johnhudson LXF regular
Joined: Wed Aug 03, 2005 2:37 pm Posts: 767
|
Posted: Wed Mar 03, 2010 7:34 pm Post subject: |
|
|
| 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 |
|
 |
micke
Joined: Fri Jan 16, 2009 1:14 pm Posts: 11
|
Posted: Fri Mar 05, 2010 1:27 pm Post subject: |
|
|
| 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 |
|
 |
kev
Joined: Tue Feb 16, 2010 4:40 pm Posts: 12
|
Posted: Mon Mar 15, 2010 12:45 pm Post subject: |
|
|
| 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 |
|
 |
micke
Joined: Fri Jan 16, 2009 1:14 pm Posts: 11
|
Posted: Mon Mar 15, 2010 2:50 pm Post subject: |
|
|
| 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 |
|
 |
C0ldf1re

Joined: Mon Mar 15, 2010 11:29 pm Posts: 2
|
Posted: Wed Mar 17, 2010 4:27 am Post subject: |
|
|
This thread answered my problem with "open." Thanks!  _________________ 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 |
|
 |
kev
Joined: Tue Feb 16, 2010 4:40 pm Posts: 12
|
Posted: Sat Mar 20, 2010 3:01 pm Post subject: |
|
|
Thanks micke.
Being so new to C++, that's going to keep me busy for quite a while. |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|