Solved

umask

Posted on 2000-03-05
7
468 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now