Advertisement

09.10.2004 at 08:19AM PDT, ID: 21126323
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.4

Windows File Locking Anomaly

Asked by Reddgum in Perl Programming Language

Tags:

Hello,

I've run into a little quirk in dealing with Windows file locking -- or to be more precise, an apparent misunderstanding on how to determine when a file is being written to and when it's not.

A call made to determine if a file arriving in an FTP drop-box is free and clear of being written to is apparently failing to indicate that the file under test is indeed still being written to.

I wrote a test script to open a file for write access, then write a byte every second to that file for 5 minutes. The code below was then executed in another process against the test-write file. The results were that the code shown below returned a correct indication that it could not get an exlcusive lock on the file being written to in the other process. (An expected result.)

However, the FLOCK routine fails to indicate that a file is being written to when an FTP process has a file open for writing. Apparently, my subroutine CAN get an exclusive lock on the file.

Additionally, the file is checked for the Read-Only attribute using a Win32::GetFileAttribute call. (And if read-Only, the program attempts to reset the read-only bit.)

What happens next is that the partially completed file is copied to a destination. When the attempt is made to delete the file, only then does the OS return an error:

"The process cannot access the file because it is being used by another process"

Well excuse me, but I thought the FLOCK would have hinted at that fact, but it apparently does not.

So the question is, how do you tell for certain that any given file is NOT being used by another process and can be fully accessed? (i.e. READ/WRITE/MODIFY/DELETE)


=========
Code synopsis:
=========

use Fcntl ':flock';
use IO::File;
...

-->Get a list of files arriving into a directory from FTP process...
-->Send each path+filename string to a validation function that checks we're only handling the right type of file -- ( placed into $pathname variable )
-->Before returning a file type value (1-9) check that we can get an exclusive lock on the file (ensure it's not presently being written to by FTP process.)
...

$Ftype = validate($pathname);

if($Ftype < 9){

...
copy($pathname, $destination);
...
delete($pathname);   <--- FAILS here (with message quoted above)

}

########### SUBS ###########

########
sub validate {
if( test_lock($pathname)){   # See if the file is locked for writing
   error($ERRLOG,"Locked file ->[ $pathname ]");
   $retval = 9;                    # retval = 9 tells caller to leave the file alone this cycle
}
# end sub validate()


#########
sub test_lock {
       
my $File                   = $_[0];
my $mode       = ( -e $File ) ? '<' : '>';    
my $fh             = new IO::File $mode.$File;
my $status       = flock($fh, LOCK_NB | LOCK_EX );
       
   if(0 == $status){      # Status will be zero if file is locked
      print DBG "\nFound locked File =>".$File if($DEBUG);
      return($TRUE);          #TRUE = File IS locked
   }
      
flock($fh, LOCK_UN);       # Release the lock - we don't need it
return($FALSE);               # $FALSE = File is NOT locked
                         
} # end sub test_lockStart Free Trial
[+][-]09.11.2004 at 12:29PM PDT, ID: 12035497

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.12.2004 at 01:36PM PDT, ID: 12039835

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 03:09AM PDT, ID: 12042550

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 07:10AM PDT, ID: 12044136

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 07:46AM PDT, ID: 12044486

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 07:56AM PDT, ID: 12044594

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 08:05AM PDT, ID: 12044694

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 08:13AM PDT, ID: 12044776

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]09.13.2004 at 08:31AM PDT, ID: 12044950

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 08:41AM PDT, ID: 12045068

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 08:53AM PDT, ID: 12045209

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 09:55AM PDT, ID: 12045843

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.13.2004 at 10:00AM PDT, ID: 12045888

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.14.2004 at 11:01AM PDT, ID: 12057645

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.14.2004 at 11:21AM PDT, ID: 12057864

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.14.2004 at 12:25PM PDT, ID: 12058510

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.14.2004 at 12:34PM PDT, ID: 12058594

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.14.2004 at 02:22PM PDT, ID: 12059491

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: getfileattribute
Sign Up Now!
Solution Provided By: ahoffmann
Participating Experts: 4
Solution Grade: B
 
 
[+][-]09.14.2004 at 03:07PM PDT, ID: 12059803

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.14.2004 at 11:59PM PDT, ID: 12061879

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32