简体   繁体   English

为什么 tempdir() 在 osx 上的目录树末尾添加额外的斜杠?

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

Somewhere along the way tempdir() prepends an extra forward slash before the directory at the end of the tree.tempdir()某个地方,在树末尾的目录之前添加了一个额外的正斜杠。 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?根据另一个站点的这个答案,目录树中的双斜杠无关紧要(但它们在路径的开头),那么为什么 osx 上的R添加一个额外的斜杠?

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 ?顺便说一句,我意识到它与这个站点不太相关,但也许有更多osx经验的人可以解释为什么normalizePath()/var更改为/private/var Is this to do with aliases?这与别名有关吗?

This is most likely a bug in the implementation of tempdir .这很可能是tempdir实现中的一个错误。 Per the function documentation [1] :根据功能文档[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.依次检查环境变量TMPDIRTMPTEMP并使用第一个找到的指向可写目录的变量:如果没有成功,则使用'/tmp'

TMPDIR , by default, contains a trailing slash which tempdir() seems to not notice when composing its result.默认情况下, TMPDIR包含一个尾部斜杠, tempdir()在组合其结果时似乎没有注意到。 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 .是的,在 Mac OS X 上, /tmp/private/tmp的符号链接。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM