Win32::File::SetAttribute does not work

S

Shea Martin

Using build 809, on winxp pro.

Win32::File::GetAttributes("somefile.txt", \$attrib);
Win32::File::SetAttributes("somefile.txt", $attrib|READONLY)
|| die "failed to set attribute";

But it does not work. If I manually set the file to readonly, then run
this code on it, it actually removes the readonly attribute, when I
would expect it to do nothing.

Is this a known bug, or am I doing something wrong.
chmod(0444, "somefile.txt) does work.

~S
 
P

Paul Lalli

Shea said:
Using build 809, on winxp pro.

Win32::File::GetAttributes("somefile.txt", \$attrib);
^^

The second argument to GetAttributes shouldn't be a reference. The
function directly modifies its argument.
Win32::File::SetAttributes("somefile.txt", $attrib|READONLY)
|| die "failed to set attribute";

But it does not work. If I manually set the file to readonly, then run
this code on it, it actually removes the readonly attribute, when I
would expect it to do nothing.

Is this a known bug, or am I doing something wrong.
chmod(0444, "somefile.txt) does work.

The following script works correctly for me, on both:
This is perl, v5.8.4 built for MSWin32-x86-multi-thread
Binary build 810 provided by ActiveState Corp.
http://www.ActiveState.com
and
This is perl, v5.8.6 built for cygwin

#!/usr/bin/perl
use strict;
use warnings;
use Win32::File;

my $file = 'somefile.txt';
open my $fh, '>', $file or die "Couldn't create $file: $!\n";
close $fh;
my $attrib;

Win32::File::GetAttributes($file, $attrib)
or die "Couldn't get attribs of somefile.txt: $!";
printf "Attributes: 0x%04x\n", $attrib;

Win32::File::SetAttributes($file, $attrib|READONLY)
|| die "failed to set attribute";

Win32::File::GetAttributes($file, $attrib)
or die "Couldn't get attribs of somefile.txt: $!";

printf "Attributes: 0x%04x\n", $attrib;

__END__

Output:
Attributes: 0x0020
Attributes: 0x0021

Paul Lalli
 
A

A. Sinan Unur

Using build 809, on winxp pro.

Win32::File::GetAttributes("somefile.txt", \$attrib);
Win32::File::SetAttributes("somefile.txt", $attrib|READONLY)
|| die "failed to set attribute";

But it does not work. If I manually set the file to readonly, then
run this code on it, it actually removes the readonly attribute, when
I would expect it to do nothing.

Well, you should always use

use strict;

in your scripts, and post a short but complete program that still
exhibits the problem you are experiencing.

Looking at the documentation for Win32::File, I see

GetAttributes(filename, returnedAttributes)

admittedly, this is not great, but I see no indication that you need to
pass a reference to GetAttributes.

D:\Home\asu1\UseNet\clpmisc> cat tt.pl
#!/usr/bin/perl

use strict;
use warnings;

use Win32::File;

my $attr;

Win32::File::GetAttributes($ARGV[0], $attr)
or die "Cannot get attributes for $ARGV[0]: $!\n";

print "Attributes for $ARGV[0]: $attr\n";

Win32::File::SetAttributes($ARGV[0], $attr | READONLY)
or die "Cannot set READONLY attribute for $ARGV[0]: $!\n";


__END__


D:\Home\asu1\UseNet\clpmisc> attrib test.txt
A D:\Home\asu1\UseNet\clpmisc\test.txt

D:\Home\asu1\UseNet\clpmisc> tt test.txt
Attributes for test.txt: 32

D:\Home\asu1\UseNet\clpmisc> attrib test.txt
A R D:\Home\asu1\UseNet\clpmisc\test.txt

D:\Home\asu1\UseNet\clpmisc> tt test.txt
Attributes for test.txt: 33

D:\Home\asu1\UseNet\clpmisc> attrib test.txt
A R D:\Home\asu1\UseNet\clpmisc\test.txt

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top