Issue on created folder permissions: temporary flag
Solution 1
t
is not "temporary", it means that the sticky bit is set. From man ls
:
t [means that the] sticky bit is set (mode 1000), and is searchable or executable. (See chmod(1) or sticky(8).)
The sticky bit is set here because you set decimal 777 (octal 1411), not octal 777 (decimal 511). You need to write 0777
to use octal, not 777
.
You should also note that the ultimate effect of the mode
argument to mkdir
also involves ANDing against your umask. From man 2 mkdir
:
The argument mode specifies the permissions to use. It is modified by the process's umask in the usual way: the permissions of the created directory are (mode & ~umask & 0777).
I would suggest that if this affects you, you chmod
after mkdir
instead of using the mode argument.
A final word of warning: mode 777 is almost never what you really want to do. Instead of opening up the directory globally to all users, consider setting a proper mode and owner/group on the directory. If you need more complicated rules, consider using ACLs.
Solution 2
777
for C is decimal. You should use 0777
so that it is treated as octal.
Solution 3
Additional to others nice answers, you can set umask
to 000
before create folder, so you can have right permssion that you want:
#include <stdio.h>
#include <sys/stat.h>
int main(void) {
mode_t old_umask;
old_umask = umask(0);
mkdir("tests", 0777);
umask(old_umask);
}
Example:
$ g++ -Wall mkdir.cpp
$ ./a.out
$ ls -ld tests
drwxrwxrwx. 2 cuonglm cuonglm 4096 Jun 23 16:55 tests
Note
Instead of using octal mode, you can use POSIX macros in mkdir
:
mkdir("tests", S_IRWXU | S_IRWXG | S_IRWXO);
Solution 4
You probably want to use an octal number for mode, rather than a decimal one:
mkdir( "foldername", 0777 );
Secondly, you need to check your umask settings to make sure you actually can create an inode with these rights.
References:
Related videos on Youtube
kebs
Updated on September 18, 2022Comments
-
kebs almost 2 years
I have a strange issue with directory permissions.
From within a C++ app, I create a folder with:
mkdir( "foldername", 777 );
But I got into an issue when attempting to create a file in that folder,
fopen()
returnedNULL
, anderrno
told mePermission denied
. So I checked, and indeed, I had the following permissions on the folder that got created:dr----x--t
(The root folder has
drwxrwxr-x
)I checked, and this unusual
t
means "temporary", but I have no clue on what that means.chmod 777 foldername
from the shell does the job and sets attributes todrwxrwxrwx
, but I can't do it manually each time.Question: any clue on what is going on ? Why doesn't my app correctly sets the folders attributes ? What is the meaning of this 'temporary' attribute ?
(system is Ubuntu 12.04)
-
kebs about 10 yearsThank you all for your responses, this is so stupid I didn't realize. The fact is, it worked before, so I assume the
0
was in the code at some time, but somebody (me ?) probably removed it at one point in the SVN, and this part of the code never got checked since!
-
-
kebs about 10 years
-
kebs about 10 yearsOh, and sure, I agree that
777
isn't really a correct choice. But lets say, the context makes this acceptable, it is just some scientific computation and some log/data files that are written in that folder. Thanks for your detailed answer. -
kebs about 10 years
you can use POSIX macros in mkdir
: sure, but a lot harder to remember than octal flags ! ;-) -
cuonglm about 10 years@kebs: Maybe, but it prevents you from trouble like in your question.
-
user2071406 about 10 years@kebs That seems to be the manual for some kind of Windows port. It also mentions NTFS and various Windows versions a lot, and “system” and “hidden” flags in the mode…