[R-pkg-devel] creating indelible file during unit test
b.rowlingson at lancaster.ac.uk
Tue Dec 12 15:47:06 CET 2017
On Tue, Dec 12, 2017 at 12:24 PM, Thierry Onkelinx <thierry.onkelinx at inbo.be
> Dear all,
> Some function I wrote deletes a bunch of files. It is crucial that all
> files get deleted. Hence it should return an error when one or more
> files couldn't be deleted.
> I'm writing a unit test for this function. I fail to create a file
> that can't be deleted by the function. I've tried Sys.chmod(file,
> "000") which didn't work.
On a linux system the ability to remove a file is controlled by the
permissions on the directory that the file is in, so setting a file to any
permission doesn't affect your process's ability to delete it:
$ mkdir foo
$ touch foo/1
$ touch foo/2
$ rm foo/1
$ chmod a-w foo # remove write permission on the folder
$ rm foo/2
rm: cannot remove ‘foo/2’: Permission denied
It might cause `rm` on the command line to warn you though:
$ touch 000 ; chmod 000 000
$ rm 000
rm: remove write-protected regular empty file ‘000’? y
- and its gone.
Depending on how your code is trying to delete things, it might be very
hard to create a file owned by you that it can't delete, since it might try
and change the permissions on non-writable directories.
Even things *not owned by you* can be deleted if you have permission on the
$ touch foo
$ ls -l
-rw-r--r-- 1 rowlings rowlings 0 Dec 12 14:38 foo
$ sudo chown root.root foo
$ ls -l
-rw-r--r-- 1 root root 0 Dec 12 14:38 foo
$ rm foo
rm: remove write-protected regular empty file ‘foo’? y
$ ls -l
I think unix makes it impossible for you to create something that even you
can't delete. You may need higher powers ("root") to change the ownership
of the thing you've created.
There may be some other possibilities involving mount points or pipes that
can't get deleted until their processes die...
[[alternative HTML version deleted]]
More information about the R-package-devel