Solved

umask

Posted on 2000-03-05
7
478 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
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 dynamically set the form action using jQuery.

688 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