7

Say I have a file /var/tmp/filename.

In Perl, I write the following snippet:

#!/usr/bin/env perl
use strict;
use warnings;

if (-W /var/tmp/filename)
{
...
}

exit;

Will the -W and -w functions (http://perldoc.perl.org/functions/-X.html) check that the file can actually be written to, or only check that the file permissions allow writing?

The reason I ask is that I am writing a script which retrieves a cookie.

This script needs to be able to write the cookie file to the filesystem, but if the filesystem path to the cookie file is read only, the permissions on the file won't matter. So I want to test if the cookiefile is actually writable as opposed to just checking its permissions.

8
  • I don't have any system where I can intentionally make the filesystem read only, so I haven't been able to test it. Hence the question of how does -w and -W work.
    – Speeddymon
    Dec 25, 2016 at 23:19
  • You could try with a cd or dvd. They are read only filesystems. Dec 25, 2016 at 23:29
  • No physical access to the server unfortunately as I'm writing this on a cloud server hosted by Rackspace. Also not my server, so I don't have root.
    – Speeddymon
    Dec 25, 2016 at 23:30
  • With linux you can easily make a filesystem and mount it with whatever read/write or read-only option you want. You could do this on a physical linux install, a VM, or a rented VPS.
    – Mort
    Dec 26, 2016 at 2:35
  • 3
    The easiest and best way to test if a file is writable is to open it with write or append access and try to write to it.
    – ysth
    Dec 26, 2016 at 3:48

4 Answers 4

4

In general, the -X operators only test the file modes, not whether the file is actually writeable. So you are going to have to test actually performing the operation, and catch any failure.

From http://perldoc.perl.org/functions/-X.html :

The interpretation of the file permission operators -r , -R , -w , -W , -x , and -X is by default based solely on the mode of the file and the uids and gids of the user. There may be other reasons you can't actually read, write, or execute the file: for example network filesystem access controls, ACLs (access control lists), read-only filesystems, and unrecognized executable formats. Note that the use of these six specific operators to verify if some operation is possible is usually a mistake, because it may be open to race conditions.

I have just verified this with a read-only loopback mount of a filesystem-in-a-file on my test system (Ubuntu 16.04) and the -W flag does indeed incorrectly report that the file can be written to.

You could use the touch command:

$ touch /media/MountPoint/testfile
touch: cannot touch '/media/MountPoint/testfile': Read-only file system

This returns a zero-length string on success when run using backtics:

unless (length `touch $filename`) 
{
    print "Can write\n";
}
2
  • 2
    Doesn't it also generate a return code? That would seem a better test to my mind.
    – Sobrique
    Dec 26, 2016 at 13:12
  • If you want the exit value, you can run the same thing with system, or look in the $? variable (perlvar). Jan 24 at 18:27
3

If your concern is the filesystem, you can check whether it is mounted read-only. If so, its line in /proc/mounts will have ro, so open the file and grep for lines with ro and your filesystem.

This will work only on filesystems that use /proc but yours do.

Note that querying with mount is less reliable.

A sure-fire way is to attempt to write a file, and I'd use File::Temp for that.

-1

Yes. You could also do something like below:

if (-r $file && -w $file) {
    # file exists, I can read and write it
}
2
  • Sorry but that is already addressed in the question. I'm wanting to find out if the -w function actually will do an open() call with the '>' operator to test that the file is writable, or if the -w function simply checks the file's permissions with stat().
    – Speeddymon
    Dec 26, 2016 at 5:57
  • 1
    It checks permission with stat. If you are using ACLs, there is a pragma called filetest that may produce more accurate results than the bare stat mode bits. Dec 26, 2016 at 6:01
-1

In contrast to perl, the bash/sh shell test -w command does check for the filesystem mount, so you can use that, e.g.

perl -e 'if (`[ -w . ] || echo NO`) {print "unwritable\n"}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.