Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-6622: [R] Normalize paths for filesystem API on Windows #5445

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions r/R/filesystem.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ Selector <- R6Class("Selector",
)

Selector$create <- function(base_dir, allow_non_existent = FALSE, recursive = FALSE) {
shared_ptr(Selector, fs___Selector__create(base_dir, allow_non_existent, recursive))
shared_ptr(
Selector,
fs___Selector__create(clean_path_rel(base_dir), allow_non_existent, recursive)
)
}

#' @title FileSystem classes
Expand Down Expand Up @@ -165,53 +168,61 @@ FileSystem <- R6Class("FileSystem", inherit = Object,
public = list(
GetTargetStats = function(x) {
if (inherits(x, "Selector")) {
map(fs___FileSystem__GetTargetStats_Selector(self, x), shared_ptr, class = FileStats)
map(
fs___FileSystem__GetTargetStats_Selector(self, x),
shared_ptr,
class = FileStats
)
} else if (is.character(x)){
map(fs___FileSystem__GetTargetStats_Paths(self, x), shared_ptr, class = FileStats)
map(
fs___FileSystem__GetTargetStats_Paths(self, clean_path_rel(x)),
shared_ptr,
class = FileStats
)
} else {
abort("incompatible type for FileSystem$GetTargetStarts()")
abort("incompatible type for FileSystem$GetTargetStats()")
}
},

CreateDir = function(path, recursive = TRUE) {
fs___FileSystem__CreateDir(self, path, isTRUE(recursive))
fs___FileSystem__CreateDir(self, clean_path_rel(path), isTRUE(recursive))
},

DeleteDir = function(path) {
fs___FileSystem__DeleteDir(self, path)
fs___FileSystem__DeleteDir(self, clean_path_rel(path))
},

DeleteDirContents = function(path) {
fs___FileSystem__DeleteDirContents(self, path)
fs___FileSystem__DeleteDirContents(self, clean_path_rel(path))
},

DeleteFile = function(path) {
fs___FileSystem__DeleteFile(self, path)
fs___FileSystem__DeleteFile(self, clean_path_rel(path))
},

DeleteFiles = function(paths) {
fs___FileSystem__DeleteFiles(self, paths)
fs___FileSystem__DeleteFiles(self, clean_path_rel(paths))
},

Move = function(src, dest) {
fs___FileSystem__Move(self, src, dest)
fs___FileSystem__Move(self, clean_path_rel(src), clean_path_rel(dest))
},

CopyFile = function(src, dest) {
fs___FileSystem__CopyFile(self, src, dest)
fs___FileSystem__CopyFile(self, clean_path_rel(src), clean_path_rel(dest))
},

OpenInputStream = function(path) {
shared_ptr(InputStream, fs___FileSystem__OpenInputStream(self, path))
shared_ptr(InputStream, fs___FileSystem__OpenInputStream(self, clean_path_rel(path)))
},
OpenInputFile = function(path) {
shared_ptr(InputStream, fs___FileSystem__OpenInputFile(self, path))
shared_ptr(InputStream, fs___FileSystem__OpenInputFile(self, clean_path_rel(path)))
},
OpenOutputStream = function(path) {
shared_ptr(OutputStream, fs___FileSystem__OpenOutputStream(self, path))
shared_ptr(OutputStream, fs___FileSystem__OpenOutputStream(self, clean_path_rel(path)))
},
OpenAppendStream = function(path) {
shared_ptr(OutputStream, fs___FileSystem__OpenAppendStream(self, path))
shared_ptr(OutputStream, fs___FileSystem__OpenAppendStream(self, clean_path_rel(path)))
}
)
)
Expand All @@ -232,6 +243,17 @@ LocalFileSystem$create <- function() {
#' @export
SubTreeFileSystem <- R6Class("SubTreeFileSystem", inherit = FileSystem)
SubTreeFileSystem$create <- function(base_path, base_fs) {
xp <- fs___SubTreeFileSystem__create(base_path, base_fs)
xp <- fs___SubTreeFileSystem__create(clean_path_rel(base_path), base_fs)
shared_ptr(SubTreeFileSystem, xp)
}

clean_path_abs <- function(path) {
# Make sure we have a valid, absolute, forward-slashed path for passing to Arrow
normalizePath(path, winslash = "/", mustWork = FALSE)
}

clean_path_rel <- function(path) {
# Make sure all path separators are "/", not "\" as on Windows
path_sep <- ifelse(tolower(Sys.info()[["sysname"]]) == "windows", "\\\\", "/")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably use .Platform$file.sep

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not sure about the name of this function, it is not really doing anything to compute a relative path, you could probably just use normalizePath(mustWork = FALSE), assuming path doesn't actually have to exist on the filesystem.

Copy link
Member Author

@nealrichardson nealrichardson Sep 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: .Platform$file.sep, I intended to but then on my Windows VM, .Platform$file.sep reported "/" while tempfile() still created paths with "\" 🤷‍♂

I originally did normalizePath but unfortunately for this use case, normalizePath does path expansion, even with mustWork=FALSE. We need to keep the path relative (to whatever the FileSystem object holds as its cwd) and just need Windows \ to be converted to /. Regular expressions were my last resort and I'm happy to use something better or more robust if it exists.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks a bit weird to do a no-op replacement on Unix, though I suppose it doesn't work.

gsub(path_sep, "/", path)
}
11 changes: 5 additions & 6 deletions r/R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ OutputStream <- R6Class("OutputStream", inherit = Writable,
#' @export
FileOutputStream <- R6Class("FileOutputStream", inherit = OutputStream)
FileOutputStream$create <- function(path) {
path <- normalizePath(path, mustWork = FALSE)
shared_ptr(FileOutputStream, io___FileOutputStream__Open(path))
shared_ptr(FileOutputStream, io___FileOutputStream__Open(clean_path_abs(path)))
}

#' @usage NULL
Expand Down Expand Up @@ -148,7 +147,7 @@ Readable <- R6Class("Readable", inherit = Object,
#'
#' @section Methods:
#'
#' - `$GetSize()`:
#' - `$GetSize()`:
#' - `$supports_zero_copy()`: Logical
#' - `$seek(position)`: go to that position in the stream
#' - `$tell()`: return the position in the stream
Expand Down Expand Up @@ -210,7 +209,7 @@ MemoryMappedFile <- R6Class("MemoryMappedFile", inherit = RandomAccessFile,
#' @export
ReadableFile <- R6Class("ReadableFile", inherit = RandomAccessFile)
ReadableFile$create <- function(path) {
shared_ptr(ReadableFile, io___ReadableFile__Open(normalizePath(path)))
shared_ptr(ReadableFile, io___ReadableFile__Open(clean_path_abs(path)))
}

#' @usage NULL
Expand All @@ -232,7 +231,7 @@ BufferReader$create <- function(x) {
#'
#' @export
mmap_create <- function(path, size) {
path <- normalizePath(path, mustWork = FALSE)
path <- clean_path_abs(path)
shared_ptr(MemoryMappedFile, io___MemoryMappedFile__Create(path, size))
}

Expand All @@ -244,7 +243,7 @@ mmap_create <- function(path, size) {
#' @export
mmap_open <- function(path, mode = c("read", "write", "readwrite")) {
mode <- match(match.arg(mode), c("read", "write", "readwrite")) - 1L
path <- normalizePath(path)
path <- clean_path_abs(path)
shared_ptr(MemoryMappedFile, io___MemoryMappedFile__Open(path, mode))
}

Expand Down
3 changes: 1 addition & 2 deletions r/tests/testthat/test-filesystem.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

context("test-type")
context("File system")

test_that("LocalFilesystem", {
fs <- LocalFileSystem$create()
Expand Down Expand Up @@ -83,7 +83,6 @@ test_that("SubTreeFilesystem", {
expect_is(st_fs, "FileSystem")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you should simply /-normalize td above. Should be easy :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At worse you simply string-replace \ with / (but only when on Windows :-)).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, then you shouldn't call it fs_relative_path...

st_fs$CreateDir("test")
st_fs$CopyFile("DESCRIPTION", "DESC.txt")
skip_on_os("windows") # See ARROW-6622
stats <- st_fs$GetTargetStats(c("DESCRIPTION", "test", "nope", "DESC.txt"))
expect_equal(stats[[1L]]$type, FileType$File)
expect_equal(stats[[2L]]$type, FileType$Directory)
Expand Down