Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does tempdir() adds extra slash at end of directory tree on osx?

Tags:

file

path

macos

r

Somewhere along the way tempdir() prepends an extra forward slash before the directory at the end of the tree. Is there any reason for this and are there any cases in which it matters? According to this answer from another site, double slashes in a directory tree don't matter (but they do at the start of a path), so why does R on osx add an extra one?

Could this be considered a bug or is there a situation where it's necessary? In the example both paths resolve correctly:

tempdir()
  [1] "/var/folders/ck/1x5j5jvx5mq17clf5r32gg540000gn/T//Rtmpp6VKKK"
normalizePath( tempdir() )
  [1] "/private/var/folders/ck/1x5j5jvx5mq17clf5r32gg540000gn/T/Rtmpp6VKKK"

setwd( tempdir() )
write.table("" , file="This is a test.txt")

system( paste0( "cd " , tempdir() , "; ls -a" ) )
  .
  ..
  This is a test.txt

system( paste0( "cd " , normalizePath( tempdir() ) , "; ls -a" ) )
  .
  ..
  This is a test.txt

As an aside, I realise that it's not very relevant for this site, but perhaps someone with more osx experience could shed light on why normalizePath() changes /var to /private/var? Is this to do with aliases?

like image 692
Simon O'Hanlon Avatar asked Mar 12 '13 13:03

Simon O'Hanlon


1 Answers

This is most likely a bug in the implementation of tempdir. Per the function documentation[1]:

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.

TMPDIR, by default, contains a trailing slash which tempdir() seems to not notice when composing its result. A better implementation would remove the trailing slash first, if it exists.

And yes, on Mac OS X, /tmp is a symbolic link to /private/tmp.

[1] https://stat.ethz.ch/R-manual/R-devel/library/base/html/tempfile.html

like image 133
Ernest Friedman-Hill Avatar answered Oct 08 '22 09:10

Ernest Friedman-Hill