Solved

umask

Posted on 2000-03-05
7
477 Views
Last Modified: 2013-12-25
I am new to PERL
I am learning CGI with source codes.
I don't understand what is umask even after reading some books.


sub lock
{
local ($oumask);

# create the lock file world-writable
$oumask = umask(0);    
for($i = 0; !open(LOCK, ">$lockfile"); $i++) {
  # wait a sec and try again
  sleep 1;
  # after 30 seconds, just unlock it
  &unlock if ($i > 30);
}

close(LOCK);
umask($oumask);
}
                     
why it uses umask?what is it doing?
umask(0) = ???
umask($oumask) ???

please help!!!
                     
0
Comment
Question by:mwhuen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2585360
Q) What is umask?
A) You can set the default file permissions for the news files that you create using the command umask.
This mask affects the initial value  of
the  file  permission of subsequently created files

Q) what does umask(0) do?
A) Sets the umask for the process to EXPR (0 in this case) and returns the previous value

Here, $oumask=umask(0);, the previous value returned by umask is stored in the $oumask variable.

After the processing is done it just reset the umask back to its original value - umask($oumask).
0
 

Author Comment

by:mwhuen
ID: 2586142
I still can't understand it all,
can you explain every lines of the above code?
umask(0) makes the file permission to 777?

0
 

Author Comment

by:mwhuen
ID: 2586520
what meant by !open()???
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 84

Expert Comment

by:ozo
ID: 2586920
open returns true if it succeds, and false if it fails.
so !open would be true if it fails, and false if it succeeds.
But the above routine does not seem to be a reliable way of obtaining a lock
0
 
LVL 16

Accepted Solution

by:
maneshr earned 10 total points
ID: 2588273
here you go...


sub lock
                   {
                   local ($oumask);

                   # create the lock file world-writable
##this line store the Current umask setting.
##once the current setting is stored, then you can change the
## umask value to something else
                   $oumask = umask(0);    


##To understand this better try this simple program
================mask_test.pl
#!/usr/local/bin/perl
 
  open(TMP,">/tmp/test_before") || die $!;
  print TMP "Before\n";
  close(TMP);

  $oumask = umask(0);    

  open(TMP,">/tmp/test_after") || die $!;
  print TMP "Before\n";
  close(TMP);
==========================

now at the UNIX prompt do ls -l /tmp/test_* & see the permissions
remove the /tmp/test_* files.

Next change mask_test.pl to....

================mask_test.pl
#!/usr/local/bin/perl
 
  open(TMP,">/tmp/test_before") || die $!;
  print TMP "Before\n";
  close(TMP);

  ##NOTE the 0 is removed here!!
  $oumask = umask();    

  open(TMP,">/tmp/test_after") || die $!;
  print TMP "Before\n";
  close(TMP);
==========================

now at the UNIX prompt do ls -l /tmp/test_* & see the permissions
Compare the results against the 1st case and you will have a much better idea.

Remove the /tmp/test_* files.
=========================================='

## here the open command is used as the condition check.
## Here open tries to open the given file in write mode.
## if open is successfull it returns 1, if not the loop is executed again.
## The loop will be executed again and again till the open is successfull and returns 1.
## you can read !open(LOCK, ">$lockfile") as " if cannot open file"

for($i = 0; !open(LOCK, ">$lockfile"); $i++) {


## Here is another example you can try.

===============open_test.pl
#!/usr/local/bin/perl

  $first=open(TMP,">/tmp/test_before")
  print TMP "Before\n";
  close(TMP);

print $first,"\n";

## See the value printed, assuming you have write access to /tmp directory.
## Next change open_test.pl to the following

===============open_test.pl
#!/usr/local/bin/perl
  $first=open(TMP,">/usr/local/bin/test_before")
  print TMP "Before\n";
  close(TMP);

print $first,"\n";

## See the value now.
##Compare the 2 results.

Hope that helps.

0
 

Author Comment

by:mwhuen
ID: 2612299
can you provide me a piece of  good/typical  locking source code?
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2612431
please explain that is it that you are trying to do using file locking.

maybe then i could provide you wil a more precise code.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question