mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: implement os.cachedir() . Use it inside os.tmpdir() too
This commit is contained in:
parent
43895269ff
commit
67f397f888
@ -79,7 +79,7 @@ pub const (
|
|||||||
// create a new flag set for parsing command line arguments
|
// create a new flag set for parsing command line arguments
|
||||||
// TODO use INT_MAX some how
|
// TODO use INT_MAX some how
|
||||||
pub fn new_flag_parser(args []string) &FlagParser {
|
pub fn new_flag_parser(args []string) &FlagParser {
|
||||||
return &FlagParser{args:args, max_free_args: MAX_ARGS_NUMBER}
|
return &FlagParser{args: args.clone(), max_free_args: MAX_ARGS_NUMBER}
|
||||||
}
|
}
|
||||||
|
|
||||||
// change the application name to be used in 'usage' output
|
// change the application name to be used in 'usage' output
|
||||||
|
44
vlib/os/os.v
44
vlib/os/os.v
@ -91,6 +91,7 @@ pub fn (f mut File) read_bytes_at(size, pos int) []byte {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
pub fn read_bytes(path string) ?[]byte {
|
pub fn read_bytes(path string) ?[]byte {
|
||||||
mut fp := vfopen(path, 'rb')
|
mut fp := vfopen(path, 'rb')
|
||||||
if isnil(fp) {
|
if isnil(fp) {
|
||||||
@ -293,7 +294,6 @@ pub fn open_append(path string) ?File {
|
|||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
pub fn (f mut File) write_bytes_at(data voidptr, size, pos int) {
|
pub fn (f mut File) write_bytes_at(data voidptr, size, pos int) {
|
||||||
$if linux {
|
$if linux {
|
||||||
@ -529,7 +529,6 @@ pub fn rm(path string) {
|
|||||||
}
|
}
|
||||||
// C.unlink(path.cstr())
|
// C.unlink(path.cstr())
|
||||||
}
|
}
|
||||||
|
|
||||||
// rmdir removes a specified directory.
|
// rmdir removes a specified directory.
|
||||||
pub fn rmdir(path string) {
|
pub fn rmdir(path string) {
|
||||||
$if !windows {
|
$if !windows {
|
||||||
@ -540,18 +539,22 @@ pub fn rmdir(path string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn rmdir_recursive(path string) {
|
pub fn rmdir_recursive(path string) {
|
||||||
items := os.ls(path) or { panic(err)}
|
items := os.ls(path) or {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
for item in items {
|
for item in items {
|
||||||
if os.is_dir(filepath.join(path, item)) {
|
if os.is_dir(filepath.join(path,item)) {
|
||||||
rmdir_recursive(filepath.join(path, item))
|
rmdir_recursive(filepath.join(path,item))
|
||||||
}
|
}
|
||||||
os.rm(filepath.join(path, item))
|
os.rm(filepath.join(path,item))
|
||||||
}
|
}
|
||||||
os.rmdir(path)
|
os.rmdir(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_dir_empty(path string) bool {
|
pub fn is_dir_empty(path string) bool {
|
||||||
items := os.ls(path) or { panic(err)}
|
items := os.ls(path) or {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
return items.len == 0
|
return items.len == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1006,6 +1009,30 @@ pub fn join(base string, dirs ...string) string {
|
|||||||
return filepath.join(base,dirs)
|
return filepath.join(base,dirs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cachedir returns the path to a *writable* user specific folder, suitable for writing non-essential data.
|
||||||
|
pub fn cachedir() string {
|
||||||
|
// See: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||||
|
// There is a single base directory relative to which user-specific non-essential
|
||||||
|
// (cached) data should be written. This directory is defined by the environment
|
||||||
|
// variable $XDG_CACHE_HOME.
|
||||||
|
// $XDG_CACHE_HOME defines the base directory relative to which user specific
|
||||||
|
// non-essential data files should be stored. If $XDG_CACHE_HOME is either not set
|
||||||
|
// or empty, a default equal to $HOME/.cache should be used.
|
||||||
|
$if !windows {
|
||||||
|
xdg_cache_home := os.getenv('XDG_CACHE_HOME')
|
||||||
|
if xdg_cache_home != '' {
|
||||||
|
return xdg_cache_home
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cdir := os.home_dir() + '.cache'
|
||||||
|
if !os.is_dir(cdir) && !os.is_link(cdir) {
|
||||||
|
os.mkdir(cdir) or {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cdir
|
||||||
|
}
|
||||||
|
|
||||||
// tmpdir returns the path to a folder, that is suitable for storing temporary files
|
// tmpdir returns the path to a folder, that is suitable for storing temporary files
|
||||||
pub fn tmpdir() string {
|
pub fn tmpdir() string {
|
||||||
mut path := os.getenv('TMPDIR')
|
mut path := os.getenv('TMPDIR')
|
||||||
@ -1023,6 +1050,9 @@ pub fn tmpdir() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if path == '' {
|
||||||
|
path = os.cachedir()
|
||||||
|
}
|
||||||
if path == '' {
|
if path == '' {
|
||||||
path = '/tmp'
|
path = '/tmp'
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user