vsi_stat {gdalraster}R Documentation

Get filesystem object info

Description

vsi_stat() fetches status information about a filesystem object (file, directory, etc). This function goes through the GDAL VSIFileHandler virtualization and may work on unusual filesystems such as in memory. It is a wrapper for VSIStatExL() in the GDAL Common Portability Library. Analog of the POSIX stat() function.

Usage

vsi_stat(filename, info = "exists")

Arguments

filename

Character string. The path of the filesystem object to be queried.

info

Character string. The type of information to fetch, one of "exists" (the default), "type" or "size".

Value

If info = "exists", returns logical TRUE if the file system object exists, otherwise FALSE. If info = "type", returns a character string with one of "file" (regular file), "dir" (directory), "symlink" (symbolic link), or empty string (""). If info = "size", returns the file size in bytes, or -1 if an error occurs.

Note

For portability, vsi_stat() supports a subset of stat()-type information for filesystem objects. This function is primarily intended for use with GDAL virtual file systems (e.g., URLs, cloud storage systems, ZIP/GZip/7z/RAR archives, in-memory files). The base R function utils::file_test() could be used instead for file tests on regular local filesystems.

See Also

GDAL Virtual File Systems:
https://gdal.org/user/virtual_file_systems.html

Examples

data_dir <- system.file("extdata", package="gdalraster")
vsi_stat(data_dir)
vsi_stat(data_dir, "type")
# stat() on a directory doesn't return the sum of the file sizes in it,
# but rather how much space is used by the directory entry
vsi_stat(data_dir, "size")

elev_file <- file.path(data_dir, "storml_elev.tif")
vsi_stat(elev_file)
vsi_stat(elev_file, "type")
vsi_stat(elev_file, "size")

nonexistent <- file.path(data_dir, "nonexistent.tif")
vsi_stat(nonexistent)
vsi_stat(nonexistent, "type")
vsi_stat(nonexistent, "size")

# /vsicurl/ file system handler
base_url <- "https://raw.githubusercontent.com/usdaforestservice/"
f <- "gdalraster/main/sample-data/landsat_c2ard_sr_mt_hood_jul2022_utm.tif"
url_file <- paste0("/vsicurl/", base_url, f)

vsi_stat(url_file)
vsi_stat(url_file, "type")
vsi_stat(url_file, "size")

[Package gdalraster version 1.10.0 Index]