Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question file testing

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
I am having some trouble in file testing. Please see my sample code first:

Code:
[Linux|bash] 133 => cat /etc/issue
Red Hat Enterprise Linux Server release 5.3 (Tikanga)
Kernel \r on an \m

[Linux|bash] 134 => pwd
/test
[Linux|bash] 135 => ls -l
total 16
-r-------- 1 root root  28 Apr 28 18:23 log.txt
-rwxr-xr-x 1 root root 140 Apr 28 18:21 tt.pl*
[Linux|bash] 136 => cat tt.pl
#! /usr/local/bin/perl

my $f = "log.txt";
if(-w $f) {
        print "File '$f' is writable.\n";
}
else {
        print "File '$f' is NOT writable.\n";
}
[Linux|bash] 137 => ./tt.pl
File 'log.txt' is writable.

I don't understand why '-w $f' returns TRUE?

Thanks for the help.
 
Who are you logged in as? If you are root then any file is writable, regardless of the permissions.

perldoc -f -X says for -w: "File is writable by effective uid/gid."

If you want to check the file's permissions rather than whether the current UID/GID can write the file, use the stat function.

Annihilannic.
 
Thank you, Annihilannic.

That's what I thought (In the example above, I logged in as root) and I also tend to think that could be a potential problem.

Please take a look at following example.

1) I created two files in my home dir. I then mount my home to a remote host.

Code:
[Linux|bash] 21 => pwd
/corp/whn
[Linux|bash] 22 => whoami
whn
[Linux|bash] 23 => ls -l test?.txt
[b]-r-------- 1 whn ccasepp 26 Apr 29 11:24 test1.txt[/b]
-rw-rw-rw- 1 whn ccasepp 26 Apr 29 11:49 test2.txt

2) I then logged on as root.

Code:
[Linux|bash] 44 => pwd
/test
[Linux|bash] 45 => whoami
root
[Linux|bash] 46 => ./t2.pl /corp/whn/test1.txt
[b]File '/corp/whn/test1.txt' is writable.
Cannot open file '/corp/whn/test1.txt' for writing!![/b]

[Linux|bash] 47 => ./t2.pl /corp/whn/test2.txt
File '/corp/whn/test2.txt' is writable.
The contents of file /corp/whn/test2.txt.
============================
Can this file be written?
Append a line.

And lastly, here is my sample code of t2.pl

Code:
[Linux|bash] 48 => cat t2.pl
#! /usr/local/bin/perl

my $f = $ARGV[0];
if(!-e $f) {
  print "File '$f' does not exist.\n";
  exit;
}

if(-w $f) {
  print "File '$f' is writable.\n";
  if(open(WFH, ">>$f")) {
    print WFH "Append a line.\n";
    close(WFH);
    my $content = `cat $f`;
    print "The contents of file $f.\n";
    print "============================\n";
    print "$content";
  }
  else {
    print "Cannot open file '$f' for writing!!\n";
  }
}
else {
  print "File '$f' is NOT writable.\n";
}

What I expected was --
When /corp/whn/test1.txt is passed in, '-w $f' would return FALSE.

Again, thank you very much for your help.
 
Do you mean you logged on as root on the remote system, the one that has the filesystem mounted as remote?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Seems buggy to me! The shell isn't similarly fooled if you do a [[ -w filename ]] on an NFS-mounted filesystem.

Annihilannic.
 
It's a perl bug, right, Annihilannic?

I also tested using csh. It is not fooled as you already indicated.

This is a surprise, really.

But thank you very much for your time and help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top