Using install.packages with custom temp dir
Asked Answered
S

2

7

I would like to install a package with a security profile that does not have access to /tmp , but has its own temporary directory, for example /tmp/jeroen. However even though I try to pass the TMPDIR environment variable, it still fails because it tries to use /tmp. Below a toy example using RAppArmor and unixtools (see here for the test profile)

> library(RAppArmor)
> library(unixtools)

> dir.create("/tmp/jeroen")
> set.tempdir("/tmp/jeroen")
> setwd(tempdir());

> aa_change_profile("r-test")
Switching profiles...

> print(tempdir());
[1] "/tmp/jeroen"

> install.packages("plyr", lib="/tmp/jeroen", configure.vars="TMPDIR=/tmp/jeroen")
trying URL 'http://cran.rstudio.com/src/contrib/plyr_1.8.tar.gz'
Content type 'application/x-gzip' length 384462 bytes (375 Kb)
opened URL
==================================================
downloaded 375 Kb

Fatal error: cannot create 'R_TempDir'

The downloaded source packages are in
    ‘/tmp/jeroen/downloaded_packages’

When looking at the kern.log file (which logs security messages), it turns out that the problem is that R CMD INSTALL still tried to use /tmp which was denied:

Jul 24 19:41:34 Jeroen-Antec kernel: [16270.696805] type=1400 audit(1374687694.097:599):
apparmor="DENIED" operation="mkdir" parent=5798 profile="r-test" name="/tmp/RtmpcUOJuQ/"
pid=5802 comm="R" requested_mask="c" denied_mask="c" fsuid=1000 ouid=1000

Is there any way I can tell R CMD INSTALL to use /tmp/jeroen instead?

Salpinx answered 24/7, 2013 at 17:53 Comment(7)
If you're going to set a TMPDIR environment variable, I believe you'd have to do it when starting R (or in a .Renviron file). e.g. you'd need to start R like this TMPDIR="/tmp/jeroen" REnterotomy
FWIW, install.packages makes 2 calls to tempdir(); it might help to examine when it's calledEnterotomy
@Enterotomy I was hoping the configure-vars option would take care of that. The process does not have permission to edit .RenvironSalpinx
While I don't know about this particular case, in the past I've had to go to great lengths to work around this (restarting R from within a script). Here's a related R-devel postEnterotomy
have you tried the destdir argument ?Faletti
@Karl Downloading of the packages works fine, destdir will use tempdir() which has been taken care of. The problem appears later in the process when install.packages calls out to R CMD INSTALL.Salpinx
@Jeroen set (then reset afterwards) in your script with Sys.setenv(). Environmental variables are inherited by child processes.Length
S
4

The correct answer was given by @hadley in the comments: in order to make sure install.packages uses the custom temporary directory, one needs to do

Sys.setenv(TMPDIR="/tmp/jeroen")

in addition to:

configure.vars="TMPDIR=/tmp/jeroen"

This way both the extraction and the installation of the package avoids the system or user default temporary directory.

Salpinx answered 26/7, 2013 at 12:20 Comment(0)
S
0

The documentation in help(tempdir) pretty clearly states that TMP, TMPDIR, ... are used:

By default, ‘tmpdir’ will be the directory given by ‘tempdir()’. This will be a subdirectory of the per-session temporary directory found by the following rule when the R session is started. The environment variables ‘TMPDIR’, ‘TMP’ and ‘TEMP’ are checked in turn and the first found which points to a writable directory is used: if none succeeds ‘/tmp’ is used.

So if setting one alone does not help, maybe you want to set several, and make sure the permissions on your 'replacement directory' are permissive enough etc pp.

Saintjust answered 24/7, 2013 at 21:3 Comment(2)
But where/how do I set these? The configure-vars doesn't help because it is only used when calling ./configure. The problem appears earlier while extracting the package.tar.gz.Salpinx
System-wide (/etc/bash.bashrc or /etc/profile) or application-wide ($RHOME/etc/Renviron), ditto for per-user. Windoze has similar system-wide settings. You can also try TEMPDIR=/foo TMP=/foo R CMD INSTALL ... etc pp. There is an entire manual devoted to this as you know.Saintjust

© 2022 - 2024 — McMap. All rights reserved.