vsi_stat: Get filesystem object info

View source: R/RcppExports.R

vsi_statR Documentation

Get filesystem object info

Description

These functions work on GDAL virtual file systems such as in-memory (/vsimem/), URLs (/vsicurl/), cloud storage services (e.g., /vsis3/, /vsigs/, /vsiaz/, etc.), compressed archives (e.g., /vsizip, /vsitar/, /vsi7z/, /vsigzip/, etc.), and others including "standard" file systems. See https://gdal.org/en/stable/user/virtual_file_systems.html.

Usage

vsi_stat(filename, info = "exists")

vsi_stat_exists(filenames)

vsi_stat_type(filenames)

vsi_stat_size(filenames)

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".

filenames

Character vector of filesystem objects to query.

Details

vsi_stat() fetches status information about a single filesystem object (file, directory, etc). It is a wrapper for VSIStatExL() in the GDAL Common Portability Library. Analog of the POSIX stat() function.

vsi_stat_exists(), vsi_stat_type() and vsi_stat_size() are specializations operating on a vector of potentially multiple file system object names, returning, respectfully, a logical vector, a character vector, and a numeric vector carrying the bit64::integer64 class attribute.

Value

If info = "exists", vsi_stat() 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 (as bit64::integer64 type), or -1 if an error occurs. vsi_stat_exists() returns a logical vector. vsi_stat_type() returns a character vector. vsi_stat_size() returns a numeric vector carrying the bit64::integer64 class attribute.

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), but can also be used on "standard" file systems (e.g., in the / hierarchy on Unix-like systems or in C:, D:, etc. drives on Windows).

See Also

GDAL Virtual File Systems:
https://gdal.org/en/stable/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")

fs_objects <- c(data_dir, elev_file, nonexistent)
vsi_stat_exists(fs_objects)
vsi_stat_type(fs_objects)
vsi_stat_size(fs_objects)

# /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)

# try to be CRAN-compliant for the example:
set_config_option("GDAL_HTTP_CONNECTTIMEOUT", "10")
set_config_option("GDAL_HTTP_TIMEOUT", "10")

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

gdalraster documentation built on Nov. 19, 2025, 9:07 a.m.