HenrikBengtsson / R.utils

🔧 R package: R.utils (this is *not* the utils package that comes with R itself)
https://henrikbengtsson.github.io/R.utils/
62 stars 14 forks source link

mkdirs(path): Add check if `path` is a link to broken file/directory #38

Closed HenrikBengtsson closed 8 years ago

HenrikBengtsson commented 8 years ago

Add check to mkdirs(path) to see if path is a link to broken file/directory, e.g.

> mkdirs.default
function (pathname, ...)
{
    if (length(pathname) == 0L)
        return(TRUE)
    pathname <- as.character(pathname)
    if (isFile(pathname) || isDirectory(pathname))
        return(FALSE)

    ## If neither a file nor a directory, then it could be a
    ## link to a broken file/directory.  If so, fail.
    if (isLink(pathname)) return(FALSE)

    parent <- getParent(pathname)
    if (identical(parent, pathname))
        throw("Could not get parent directory: ", pathname)
    if (isFile(parent))
        return(FALSE)
    if (!isDirectory(parent)) {
        if (!mkdirs(parent))
            return(FALSE)
    }
    if (!isDirectory(pathname)) {
        res <- dir.create(pathname)
        if (!res) {
            pathname <- getRelativePath(pathname)
            res <- dir.create(pathname)
        }
        return(res)
    }
    TRUE
}

where

isLink <- function(pathname) {
  nzchar(Sys.readlink2(pathname))
}
HenrikBengtsson commented 8 years ago

Done in commit 5051c2b.