Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

umask

Posted on 2000-03-05
7
Medium Priority
?
484 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 40 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

618 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