Skip to content

Commit 822f955

Browse files
improve error handling in fread() for cmd exe and decompression
1 parent e32e553 commit 822f955

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

R/fread.R

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC")
7070
}
7171
}
7272
if (!is.null(cmd)) {
73-
(if (.Platform$OS.type == "unix") system else shell)(paste0('(', cmd, ') > ', tmpFile<-tempfile(tmpdir=tmpdir)))
73+
tmpFile = tempfile(tmpdir=tmpdir)
74+
on.exit(unlink(tmpFile), add=TRUE)
75+
status = suppressWarnings((if (.Platform$OS.type == "unix") system else shell)(paste0('(', cmd, ') > ', tmpFile)))
76+
if (status != 0) {
77+
stopf("External command failed with exit code %d. This can happen when the disk is full in the temporary directory ('%s'). See ?fread for the tmpdir argument.", status, tmpdir)
78+
}
7479
file = tmpFile
75-
on.exit(unlink(tmpFile), add=TRUE)
7680
}
7781
if (!is.null(file)) {
7882
if (!is.character(file) || length(file)!=1L)
@@ -116,9 +120,14 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC")
116120
if (!requireNamespace("R.utils", quietly = TRUE))
117121
stopf("To read %s files directly, fread() requires 'R.utils' package which cannot be found. Please install 'R.utils' using 'install.packages('R.utils')'.", if (w<=2L || gzsig) "gz" else "bz2") # nocov
118122
FUN = if (w<=2L || gzsig) gzfile else bzfile
119-
R.utils::decompressFile(file, decompFile<-tempfile(tmpdir=tmpdir), ext=NULL, FUN=FUN, remove=FALSE) # ext is not used by decompressFile when destname is supplied, but isn't optional
120-
file = decompFile # don't use 'tmpFile' symbol again, as tmpFile might be the http://domain.org/file.csv.gz download
121-
on.exit(unlink(decompFile), add=TRUE)
123+
decompFile = tempfile(tmpdir=tmpdir)
124+
on.exit(unlink(decompFile), add=TRUE)
125+
tryCatch({
126+
R.utils::decompressFile(file, decompFile, ext=NULL, FUN=FUN, remove=FALSE)
127+
}, error = function(e) {
128+
stopf("Failed to decompress file '%s'. This can happen when the disk is full in the temporary directory ('%s'). See ?fread for the tmpdir argument.", file, tmpdir)
129+
})
130+
file = decompFile
122131
}
123132
file = enc2native(file) # CfreadR cannot handle UTF-8 if that is not the native encoding, see #3078.
124133

0 commit comments

Comments
 (0)