adokter / bioRad

R package for analysis and visualisation of biological signals in weather radar data
http://adokter.github.io/bioRad
Other
29 stars 16 forks source link

read error when path contains a space #162

Closed adokter closed 2 years ago

adokter commented 6 years ago

Docker dependent functions (read_pvolfile for NEXRAD, nexrad_to_odim, calculate_profile) give an error when the file path contains a space

> read_pvolfile("/my/path/with a space/polar_volume_file")
Converting using Docker...
docker: invalid reference format.
See 'docker run --help'.
 Show Traceback

 Rerun with Debug
 Error in nexrad_to_odim_tempfile(file, verbose = verbose, mount = mount) : 
  Failed to start vol2bird Docker container. In addition: Warning message:
In mount_docker_container(normalizePath(mount, winslash = "/")) :
  failed to mount /my/path/with a space/polar_volume_file ... Go to 'Docker -> preferences -> File Sharing' and add this directory (or its root directory) as a bind mounted directory
adokter commented 6 years ago

The docker run command in mount_docker_container expects special characters and spaces to have a leading escape, i.e. this correctly mounts the container:

 mount_docker_container("~/Dropbox/Eleonora\\'s\\ falcon")

however the normalize_Path function does not expect added escapes, therefore I get a warning when passing this path string

> normalizePath("~/Dropbox/Eleonora\\'s\\ falcon")
[1] "/Users/amd427/Dropbox/Eleonora\\'s\\ falcon"
Warning message:
In normalizePath("~/Dropbox/Eleonora\\'s\\ falcon") :
  path[1]="/Users/amd427/Dropbox/Eleonora\'s\ falcon": No such file or directory

Solution is therefore on Mac/Linux to 1) apply normalizePath first on path without escape characters. 2) to the output of 1) add leading \\ to all special characters and spaces (find out how)

Likely different solution needed on Windows

adokter commented 5 years ago

On Mac this seems to work, but only when the space is at the end of the path:

library(stringr)
mydir="~/Dropbox/Eleonora's falcon"
escape=function(string) {
  str_replace_all(string, "(\\W)", "\\\\\\1")
}
paste(dirname(mydir),"/",escape(basename(mydir)),sep="")

[1] "/Users/amd427/Dropbox/Eleonora\\'s\\ falcon"

Correction: does not work, this also incorrectly escapes dots (.)

adokter commented 5 years ago

@stijnvanhoey maybe you know this from the top of your head:

I need a function that escapes the white space and special characters in a path, exactly as you would need in a shell

On Mac/Linux the path string ~/Dropbox/Eleonora's falcon/dir with whitespace/ should be translated into /Users/adriaan/Eleonora\\'s\\ falcon/dir\\ with\\ whitespace/ (need double backslashes in the string, in order to have a real backslash when I execute it with a system() command).

On Windows (I think) c:/Dropbox/Eleonora's falcon/dir with whitespace/ should be translated into "c:\Dropbox\Eleonora 's falcon\dir with whitespace" (so with explicit quotes " in the string).

I've checked functionality of normalizePath and shQuote but can't get the escaping quite right. I'm also unsure which characters need escaping and which not, which is a hurdle to programming something myself.

adokter commented 5 years ago

This seems promising:

system(command="ls",input="/Users/amd427/Dropbox/Eleonora's falcon")

Correctly lists the directory contents without the need to escape anything

Update: doesn't seem to work when the call consists of multiple elements, like docker run ...

stijnvanhoey commented 5 years ago

Just a guess: Would using 'file.path' to create the path name not properly parse this?

adokter commented 5 years ago

No unfortunately not - to interact with the Docker daemon I have to parse a path via a system() call in R, as in

result <- system(
      paste("docker run -v ",
        "CORRECTLY_ESCAPED_PATH_GOES_HERE",
        ":/data -d --name vol2bird adokter/vol2bird sleep infinity",
        sep = ""),ignore.stdout = TRUE)

file.path doesn't add the escapes that you need in a shell that is opened by the system() command ...

adokter commented 5 years ago

Closed by PR #174

adokter commented 2 years ago

This issue persists when using a local installation

adokter commented 2 years ago

nexrad_to_odim() fixed in d123b94. Combined with fix for #502, this issue can be closed