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. Any suggestions? Best regards, ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkelinx at inbo.be Kliniekstraat 25, B-1070 Brussel www.inbo.be /////////////////////////////////////////////////////////////////////////////////////////// To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey /////////////////////////////////////////////////////////////////////////////////////////// Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging in Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis. Vanaf dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000 Brussel.
[R-pkg-devel] creating indelible file during unit test
6 messages · Thierry Onkelinx, Iñaki Ucar, Barry Rowlingson +1 more
2017-12-12 13:24 GMT+01:00 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. Any suggestions?
Supposing you call file.remove at some point, what about the following:
file.remove <- function(...) { invisible() }
# test your function here, should issue an error
rm("file.remove")
I?aki
On Tue, Dec 12, 2017 at 12:24 PM, Thierry Onkelinx <thierry.onkelinx at inbo.be
wrote:
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
containing directory:
$ touch foo
$ ls -l
total 0
-rw-r--r-- 1 rowlings rowlings 0 Dec 12 14:38 foo
$ sudo chown root.root foo
$ ls -l
total 0
-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
total 0
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...
2017-12-12 15:47 GMT+01:00 Barry Rowlingson <b.rowlingson at lancaster.ac.uk>:
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.
In a Unix system with administrator rights, you can set the immutable flag: $ touch foo $ chmod a-w foo $ sudo chattr +i foo $ rm foo Operation not permitted Otherwise, you can always remove your own files. Moreover, Thierry may want the test to be cross-platform... So I think the easiest way to go is to bypass the R function used to remove the files (file.remove, unlink, whatever), run the test and then restore it. I?aki
In *nix systems, permission to delete is at the directory level, not the file level, so you might try changing the mode of the parent directory, rather than the file itself, as in:
dir.create('test')
file.create(file.path("test",'file1'))
[1] TRUE
Sys.chmod("test", mode="0000")
file.remove(file.path("test",'file1'))
[1] FALSE
Warning message:
In file.remove(file.path("test", "file1")) :
cannot remove file 'test/file1', reason 'Permission denied'
Sys.chmod("test", mode="0777")
file.remove(file.path("test",'file1'))
[1] TRUE
Not sure if this will work in Windows, though. Regards, Tom Wainwright On Tue, Dec 12, 2017 at 4:24 AM, Thierry Onkelinx <thierry.onkelinx at inbo.be> wrote:
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. Any suggestions? Best regards, ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkelinx at inbo.be Kliniekstraat 25, B-1070 Brussel www.inbo.be //////////////////////////////////////////////////////////// /////////////////////////////// To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey //////////////////////////////////////////////////////////// /////////////////////////////// Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging in Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis. Vanaf dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000 Brussel.
______________________________________________ R-package-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
2 days later
Thanks for the replies to my question.
My function uses first list.files() to select the relevant files and
then file.remove() to delete them.
Redefining file.remove() doesn't work in case of a package due to
lexical scoping. Otherwise this would be a simple and portable
solution.
Having a Linux only solution is better than no solution at all. And it
should be sufficient. Note that I had to use Sys.chmod("test",
mode="0400") instead of Sys.chmod("test", mode="0000") because
list.files() needs at least read permission.
The Linux only unit test can be bypassed on other systems with
testthat::skip_on_os().
Best regards,
ir. Thierry Onkelinx
Statisticus / Statistician
Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE
AND FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry.onkelinx at inbo.be
Kliniekstraat 25, B-1070 Brussel
www.inbo.be
///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no
more than asking him to perform a post-mortem examination: he may be
able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does
not ensure that a reasonable answer can be extracted from a given body
of data. ~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////
Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging in
Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis.
Vanaf dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000 Brussel.
///////////////////////////////////////////////////////////////////////////////////////////
2017-12-12 18:42 GMT+01:00 Tom Wainwright <tcwainw at gmail.com>:
In *nix systems, permission to delete is at the directory level, not the file level, so you might try changing the mode of the parent directory, rather than the file itself, as in:
dir.create('test')
file.create(file.path("test",'file1'))
[1] TRUE
Sys.chmod("test", mode="0000")
file.remove(file.path("test",'file1'))
[1] FALSE
Warning message:
In file.remove(file.path("test", "file1")) :
cannot remove file 'test/file1', reason 'Permission denied'
Sys.chmod("test", mode="0777")
file.remove(file.path("test",'file1'))
[1] TRUE
Not sure if this will work in Windows, though. Regards, Tom Wainwright On Tue, Dec 12, 2017 at 4:24 AM, Thierry Onkelinx <thierry.onkelinx at inbo.be> wrote:
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. Any suggestions? Best regards, ir. Thierry Onkelinx Statisticus / Statistician Vlaamse Overheid / Government of Flanders INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND FOREST Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance thierry.onkelinx at inbo.be Kliniekstraat 25, B-1070 Brussel www.inbo.be /////////////////////////////////////////////////////////////////////////////////////////// To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey /////////////////////////////////////////////////////////////////////////////////////////// Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging in Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis. Vanaf dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000 Brussel.
______________________________________________ R-package-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel