Skip to content

Commit

Permalink
Normalize paths for filesystem API on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
nealrichardson committed Sep 24, 2019
1 parent 2c7fb24 commit f03815e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
48 changes: 32 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(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(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(path), isTRUE(recursive))
},

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

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

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

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

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

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

OpenInputStream = function(path) {
shared_ptr(InputStream, fs___FileSystem__OpenInputStream(self, path))
shared_ptr(InputStream, fs___FileSystem__OpenInputStream(self, clean_path(path)))
},
OpenInputFile = function(path) {
shared_ptr(InputStream, fs___FileSystem__OpenInputFile(self, path))
shared_ptr(InputStream, fs___FileSystem__OpenInputFile(self, clean_path(path)))
},
OpenOutputStream = function(path) {
shared_ptr(OutputStream, fs___FileSystem__OpenOutputStream(self, path))
shared_ptr(OutputStream, fs___FileSystem__OpenOutputStream(self, clean_path(path)))
},
OpenAppendStream = function(path) {
shared_ptr(OutputStream, fs___FileSystem__OpenAppendStream(self, path))
shared_ptr(OutputStream, fs___FileSystem__OpenAppendStream(self, clean_path(path)))
}
)
)
Expand All @@ -232,6 +243,11 @@ 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(base_path), base_fs)
shared_ptr(SubTreeFileSystem, xp)
}

clean_path <- function(path) {
# Make sure we have a valid, forward-slashed path for passing to Arrow
normalizePath(path, winslash = "/", mustWork = FALSE)
}
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(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(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(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(path)
shared_ptr(MemoryMappedFile, io___MemoryMappedFile__Open(path, mode))
}

Expand Down
1 change: 0 additions & 1 deletion r/tests/testthat/test-filesystem.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ test_that("SubTreeFilesystem", {
expect_is(st_fs, "FileSystem")
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

0 comments on commit f03815e

Please sign in to comment.