Solved

umask

Posted on 2000-03-05
7
473 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

821 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