0

I want to check if the output directory given to my R script is writable. But to check that, I also need to check the groups the current effective user belongs to, and I do not seem to be able to find out how to do that. The query does not google well either.

This is what I have so far

is.writable <- function(dir)
    {
        if(file.info(outputDir)['size'] == NA || file.info(outputDir)['isdir'] == FALSE)
            return FALSE
        mode <- file.info['mode']
        owner <- file.info['uname']
        fileGroup <- file.info['grname']
        user <- Sys.info()[["effective_user"]]

        if(bitwAnd(mode,2) != 0) # every can access
            return TRUE
        #if(bitwAnd(mode,16) != 0) # 16=0020 in octal, some group has access
        if(bitwAnd(mode,128) != 0) # 128 = 0200 in octal, the owner has write access
            return user == owner
    }

I used this to implement it. I would be able to do it on my own once I figure out how to get a vector of the groups the effective user belongs to.

Thank you,

Community
  • 1
  • 1
Mohammad Alaggan
  • 3,749
  • 1
  • 28
  • 20

2 Answers2

1

You can always shell out and use the groups command:

> groups = strsplit(system("groups",intern=TRUE)," ")[[1]]
> groups
[1] "rowlings"   "adm"        "sudo"       "lpadmin"    "sambashare"

Alternatively an Rcpp wrapper round the relevant part of the Unix API will do the job.

(Obviously this mostly doesn't work on Windows)

Spacedman
  • 92,590
  • 12
  • 140
  • 224
0

I appear to have found a solution for checking whether a directory (or a file) is writable. However, I do not know yet how to get the groups of the user, hence though I am posting this as an answer, I will not accept it since it does not address the question in the main title.

To check whether a director outputDir is writable, just check whether this condition is true:

file.access(outputDir,2) == 0
Mohammad Alaggan
  • 3,749
  • 1
  • 28
  • 20