Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Create dir with user's default mask.

173 views
Skip to first unread message

MS

unread,
Jan 29, 2013, 10:51:18 AM1/29/13
to
I need to create a directory from my C program with the user's default
file mask.

Initially this was used, it works fine:

// Set the directory creation file mask to 755 (i.e. drwxr-xr-x).
mode_t dirMask = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
mkdir(filepath, dirMask)

It occurred to me that while 755 is the most common mask, it would be
better to use whatever the user's default mask is. So I tried this:

mode_t defMask = umask(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
umask(defMask);
mkdir(filepath, defMask)

The above code does something I did not anticipate!! It successfully
creates a dir (mkdir() returns 0) but with only the dir bit set.

i.e. 'd---------'. Not what I want at all.

How should I create a directory with the user's default file mask?

Thanks.

Rainer Weikusat

unread,
Jan 29, 2013, 10:54:39 AM1/29/13
to
MS <ms@no_spam_thanks.com> writes:
> I need to create a directory from my C program with the user's default
> file mask.
>
> Initially this was used, it works fine:
>
> // Set the directory creation file mask to 755 (i.e. drwxr-xr-x).
> mode_t dirMask = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
> mkdir(filepath, dirMask)
>
> It occurred to me that while 755 is the most common mask, it would be
> better to use whatever the user's default mask is.

The common way to do this would be to use 'maximially permissive'
settings (0777) and assume that the user has configued his umask
to mask out unwanted bits.

Casper H.S. Dik

unread,
Jan 29, 2013, 11:19:02 AM1/29/13
to
MS <ms@no_spam_thanks.com> writes:

>I need to create a directory from my C program with the user's default
>file mask.

>Initially this was used, it works fine:

>// Set the directory creation file mask to 755 (i.e. drwxr-xr-x).
>mode_t dirMask = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
>mkdir(filepath, dirMask)

>It occurred to me that while 755 is the most common mask, it would be
>better to use whatever the user's default mask is. So I tried this:

>mode_t defMask = umask(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
>umask(defMask);
>mkdir(filepath, defMask)

The umask is a *mask* of the permission given to creat/open/mkdir so
the resulting mask is

defmask & ~umask

Since umask == defmask, the result is 0 as you have discovered:

>The above code does something I did not anticipate!! It successfully
>creates a dir (mkdir() returns 0) but with only the dir bit set.

>i.e. 'd---------'. Not what I want at all.

>How should I create a directory with the user's default file mask?


mkdir(dir, 0777);

Create a directory with all bits set in the mode and masked by the
umask. With a tyical umask of 022 you get a a resulting mode of 755.


Casper

MS

unread,
Jan 29, 2013, 12:06:21 PM1/29/13
to
Thanks Rainer and Casper.

You're absolutely right. Using 0777 results in a dir created on my
system with 755 permissions, my default.


> The umask is a *mask* of the permission given to creat/open/mkdir so
> the resulting mask is
>
> defmask & ~umask
>
> Since umask == defmask, the result is 0 as you have discovered:

Ok thanks for the explanation, I did not understand that at all. Got it
now. Cheers.

Thanks again.

MS

unread,
Jan 29, 2013, 12:12:13 PM1/29/13
to

P.S. Just checking...

These 2 lines both do exactly the same thing right?

mkdir(filepath, 0777);

mkdir(filepath, S_IRWXU | S_IRWXG | S_IRWXO);

Thanks.

Jorgen Grahn

unread,
Jan 29, 2013, 5:14:49 PM1/29/13
to
Yes. They pass the same value to mkdir. I'm fairly sure no
Unix can map S_IRWXU and friends differently.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

MS

unread,
Jan 29, 2013, 5:36:50 PM1/29/13
to
On 29/01/13 22:14, Jorgen Grahn wrote:
> On Tue, 2013-01-29, MS wrote:
>>
>> P.S. Just checking...
>>
>> These 2 lines both do exactly the same thing right?
>>
>> mkdir(filepath, 0777);
>>
>> mkdir(filepath, S_IRWXU | S_IRWXG | S_IRWXO);
>
> Yes. They pass the same value to mkdir. I'm fairly sure no
> Unix can map S_IRWXU and friends differently.

Thanks Jorgen. :)

William Ahern

unread,
Jan 29, 2013, 6:27:53 PM1/29/13
to
Jorgen Grahn <grahn...@snipabacken.se> wrote:
> On Tue, 2013-01-29, MS wrote:
> >
> > P.S. Just checking...
> >
> > These 2 lines both do exactly the same thing right?
> >
> > mkdir(filepath, 0777);
> >
> > mkdir(filepath, S_IRWXU | S_IRWXG | S_IRWXO);

> Yes. They pass the same value to mkdir. I'm fairly sure no
> Unix can map S_IRWXU and friends differently.

POSIX specifies their numeric values, so there's no liberty for surprise.
0 new messages