File::NCopy question

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

I'm trying to use File::NCopy::copy to preserve the modification time
of a file, but I can't seem to get it to work. I've got, basically,

use File::NCopy qw(copy);
$fobj=File::NCopy->new(
'force_write' => 1,
'preserve' => 1,
);
$fobj->copy( "file1", "file2" );

file2, however, is not keeping file1's modification time. I'm using
ActiveState Perl version 5.8.7 on WinXP. Do I need to change
'set_times'? Is there some other WinDoze quirk I need to be aware of
to get this to work?
 
R

Robert Gamble

Christopher said:
I'm trying to use File::NCopy::copy to preserve the modification time
of a file, but I can't seem to get it to work. I've got, basically,

use File::NCopy qw(copy);
$fobj=File::NCopy->new(
'force_write' => 1,
'preserve' => 1,
);
$fobj->copy( "file1", "file2" );

file2, however, is not keeping file1's modification time. I'm using
ActiveState Perl version 5.8.7 on WinXP. Do I need to change
'set_times'? Is there some other WinDoze quirk I need to be aware of
to get this to work?

Is the file that you are copying marked read-only? If so, the copy
will also be read-only and the utime function that is called by NCopy
to change the times ater the file is copied will fail (utime doesn't
work on read-only files on Windows). If this is the case, remove the
read-only attribute or override the utime function with the one
provided in Win32API::File::Time that will work on read-only files.

Robert Gamble
 
C

Christopher Benson-Manica

Robert Gamble said:
Is the file that you are copying marked read-only?

No, but I imagine that I will have to make use of your suggestion once
I do get whatever the underlying problem is resolved.
If so, the copy
will also be read-only and the utime function that is called by NCopy
to change the times ater the file is copied will fail (utime doesn't
work on read-only files on Windows). If this is the case, remove the
read-only attribute or override the utime function with the one
provided in Win32API::File::Time that will work on read-only files.

So are those set_time and friends to copy just the Perl equivalent of
function pointers? The documentation for ncopy really isn't clear on
what those parameters are.
 
R

Robert Gamble

Christopher said:
No, but I imagine that I will have to make use of your suggestion once
I do get whatever the underlying problem is resolved.

I couldn't duplicate your problem on WinXP Pro with ActiveState 5.8.7
using NCopy versions 0.32 and 0.34. Could you confirm your version?
Are you using something besides NTFS? You might want try using the
perl utime function on your platform and see if it works on the files
you are copying. There might also be a problem with the stat function
on your system, check that too.
So are those set_time and friends to copy just the Perl equivalent of
function pointers? The documentation for ncopy really isn't clear on
what those parameters are.

Pretty much, we call them subroutine references in Perl though ;). If
you take a look at the NCopy.pm file you will see how set_times is
initialized to a reference to the File::NCopy::s_times subroutine.
This sub stats the file being copied, gets the modification times, and
then uses utime to update the copy. You can create your own subroutine
and use the set_times option to use your sub instead. You could
probably get away with just adding "use Win32API::File::Time
qw(utime);" to the top of the NCopy.pm file.

Robert Gamble
 
C

Christopher Benson-Manica

Robert Gamble said:
I couldn't duplicate your problem on WinXP Pro with ActiveState 5.8.7
using NCopy versions 0.32 and 0.34. Could you confirm your version?

ppm>
Querying target 1 (ActivePerl 5.8.7.813)
1. File-NCopy [0.32] Copy file, file Copy file | dir, dir
Are you using something besides NTFS? You might want try using the
perl utime function on your platform and see if it works on the files
you are copying. There might also be a problem with the stat function
on your system, check that too.

I'll try that.
This sub stats the file being copied, gets the modification times, and
then uses utime to update the copy. You can create your own subroutine
and use the set_times option to use your sub instead. You could
probably get away with just adding "use Win32API::File::Time
qw(utime);" to the top of the NCopy.pm file.

I added it, and then got

Can't locate Win32API/File/Time.pm in @INC (@INC contains: c:\bin\modules C:/Per l/lib C:/Perl/site/lib .) at C:/Perl/site/lib/File/NCopy.pm line 4.
BEGIN failed--compilation aborted at C:/Perl/site/lib/File/NCopy.pm line 4.

Is my installation broken?
 
A

A. Sinan Unur

Can't locate Win32API/File/Time.pm in @INC (@INC contains:
c:\bin\modules C:/Per l/lib C:/Perl/site/lib .) at
C:/Perl/site/lib/File/NCopy.pm line 4. BEGIN failed--compilation
aborted at C:/Perl/site/lib/File/NCopy.pm line 4.

Is my installation broken?

What makes you think so?

ppm> s file-time
Searching in Active Repositories
1. Win32API-File-Time [0.005] Get and set file times in Windows -

ppm> i 1

Sinan
 
C

Christopher Benson-Manica

Robert Gamble said:
I couldn't duplicate your problem on WinXP Pro with ActiveState 5.8.7
using NCopy versions 0.32 and 0.34. Could you confirm your version?
Are you using something besides NTFS? You might want try using the
perl utime function on your platform and see if it works on the files
you are copying. There might also be a problem with the stat function
on your system, check that too.

stat and utime seem to be fine - the following script works just fine:

my( $mtime1 )=( stat "a.txt" )[9];
my( $mtime2 )=( stat "b.txt" )[9];
print( "mtime of a.txt=$mtime1\n" );
print( "mtime of b.txt=$mtime2\n" );
utime $mtime1,$mtime1,"b.txt";
print( "after:\n" );
my( $mtime1 )=( stat "a.txt" )[9];
my( $mtime2 )=( stat "b.txt" )[9];
print( "mtime of a.txt=$mtime1\n" );
print( "mtime of b.txt=$mtime2\n" );

I added some debug directly to s_times, and when it is finished the
copied file has the correct modification time. However, when I check
again immediately after copy() returns, it's wrong again, and
different than what it was at the beginning of s_times. Does copy()
do something else after preserving the file status that would cause
the file to be "modified"?
 
R

Robert Gamble

Christopher said:
Robert Gamble said:
I couldn't duplicate your problem on WinXP Pro with ActiveState 5.8.7
using NCopy versions 0.32 and 0.34. Could you confirm your version?
Are you using something besides NTFS? You might want try using the
perl utime function on your platform and see if it works on the files
you are copying. There might also be a problem with the stat function
on your system, check that too.

stat and utime seem to be fine - the following script works just fine:

my( $mtime1 )=( stat "a.txt" )[9];
my( $mtime2 )=( stat "b.txt" )[9];
print( "mtime of a.txt=$mtime1\n" );
print( "mtime of b.txt=$mtime2\n" );
utime $mtime1,$mtime1,"b.txt";
print( "after:\n" );
my( $mtime1 )=( stat "a.txt" )[9];
my( $mtime2 )=( stat "b.txt" )[9];
print( "mtime of a.txt=$mtime1\n" );
print( "mtime of b.txt=$mtime2\n" );

I added some debug directly to s_times, and when it is finished the
copied file has the correct modification time. However, when I check
again immediately after copy() returns, it's wrong again, and
different than what it was at the beginning of s_times. Does copy()
do something else after preserving the file status that would cause
the file to be "modified"?

It shouldn't. Try running the following script on your machine:

use File::NCopy qw(copy);
$fobj=File::NCopy->new(
'force_write' => 1,
'preserve' => 1,
);
open(FILE, ">testfile1");
close(FILE);
my $mod_time1 = (stat "testfile1")[9];
sleep 5;
$fobj->copy( "testfile1", "testfile2" );
my $mod_time2 = (stat "testfile2")[9];
if ($mod_time1 == $mod_time2) {
print "PASS\n";
} else {
print "FAIL\n";
}

On my WinXP box with AS Perl 5.8.7 build 813 with NCopy 0.32 is PASSes.
If it FAILs on your machine, go to cpan.org, search for File::NCopy,
download either version 0.32 or 0.34, extract the file called NCopy.pm
and replace your current NCopy.pm file with it. If after doing this it
still fails I'm at a loss (can you confirm you are using NTFS?). If
this program prints PASS then you are doing something else in your
program to modify the file.

Robert Gamble
 
C

Christopher Benson-Manica

Robert Gamble said:
On my WinXP box with AS Perl 5.8.7 build 813 with NCopy 0.32 is PASSes.
If it FAILs on your machine, go to cpan.org, search for File::NCopy,
download either version 0.32 or 0.34, extract the file called NCopy.pm
and replace your current NCopy.pm file with it. If after doing this it
still fails I'm at a loss (can you confirm you are using NTFS?). If
this program prints PASS then you are doing something else in your
program to modify the file.

Well, it passed, but I have no idea what the problem could be. I am
settling for manually changing the file times as appropriate after
copy(), since I've wasted too much time on this problem already :-|
Thanks a lot for your very detailed help though!
 
A

A. Sinan Unur

Well, it passed, but I have no idea what the problem could be. I am
settling for manually changing the file times as appropriate after
copy(), since I've wasted too much time on this problem already :-|
Thanks a lot for your very detailed help though!

If the module is not working out for you, and you have resort to those
kinds of measures, have you considered using xcopy?

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top