Python: Getting umask without change


Hi, I recently found myself in the need of getting the umask of a process. After a few searches there seems to be no other way of getting the desired umask then using the umask() system call located within the python os module. The following code seems to be the universally accepted solution to this particular problem:

def getUMask():
    current_umask = os.umask(0)
    os.umask(current_umask)

    return current_umask 

So the solution is to make a first call to umask() and get a hold of the current umask (Line 2). The argument you pass to the umask() call during the first invocation doesn’t matter since you are going to change it back to its original value. After that you make a second call to umask() (Line 3) with the umask you acquired during the first call (Line 2). So this is the point where you change the umask back to its original value. The call to umask is not atomic though, so you have to take this fact into account if you are using multiple threads, this could lead to race conditions.

Even though this is certainly not the holy grail of programming I thought I share this piece of code anyway…

If this post was useful to you, don’t forget to share it with the world…till next time…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s