aboutsummaryrefslogtreecommitdiff
path: root/src/os.cpp
diff options
context:
space:
mode:
authorMichael Dusan <michael.dusan@gmail.com>2019-11-02 17:01:43 -0400
committerAndrew Kelley <andrew@ziglang.org>2019-11-02 18:30:07 -0400
commit10046f9a52c089c656bfd4578e4ef17671ff1f75 (patch)
tree4b63ae0994061631a051a18895da03aa28a80ae7 /src/os.cpp
parent2e0dd5733f9aec168f72167ca0a0e306b2810ae2 (diff)
downloadzig-10046f9a52c089c656bfd4578e4ef17671ff1f75.tar.gz
zig-10046f9a52c089c656bfd4578e4ef17671ff1f75.zip
stage1: add linux XDG Base Directory support
- define zig global cache based on XDG spec: if env XDG_CACHE_HOME { "$XDG_CACHE_HOME/zig" } else { "$HOME/.cache/zig" } - old definition "$HOME/.local/share/zig" is retired - closes #3573
Diffstat (limited to 'src/os.cpp')
-rw-r--r--src/os.cpp39
1 files changed, 25 insertions, 14 deletions
diff --git a/src/os.cpp b/src/os.cpp
index 908c3b47d9..da1f4ecfb0 100644
--- a/src/os.cpp
+++ b/src/os.cpp
@@ -1748,7 +1748,7 @@ static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le) {
#endif
// Ported from std.os.getAppDataDir
-Error os_get_app_data_dir(Buf *out_path, const char *appname) {
+Error os_get_cache_dir(Buf *out_path, const char *appname) {
#if defined(ZIG_OS_WINDOWS)
WCHAR *dir_path_ptr;
switch (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &dir_path_ptr)) {
@@ -1774,24 +1774,35 @@ Error os_get_app_data_dir(Buf *out_path, const char *appname) {
buf_appendf(out_path, "%s/Library/Application Support/%s", home_dir, appname);
return ErrorNone;
#elif defined(ZIG_OS_POSIX)
- const char *home_dir = getenv("HOME");
- if (home_dir == nullptr) {
- // TODO use /etc/passwd
- return ErrorFileNotFound;
- }
- if (home_dir[0] == 0) {
- return ErrorFileNotFound;
- }
- buf_init_from_str(out_path, home_dir);
- if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') {
- buf_append_char(out_path, '/');
+ const char *cache_dir = getenv("XDG_CACHE_HOME");
+ if (cache_dir == nullptr) {
+ cache_dir = getenv("HOME");
+ if (cache_dir == nullptr) {
+ // TODO use /etc/passwd
+ return ErrorFileNotFound;
+ }
+ if (cache_dir[0] == 0) {
+ return ErrorFileNotFound;
+ }
+ buf_init_from_str(out_path, cache_dir);
+ if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') {
+ buf_append_char(out_path, '/');
+ }
+ buf_appendf(out_path, ".cache/%s", appname);
+ } else {
+ if (cache_dir[0] == 0) {
+ return ErrorFileNotFound;
+ }
+ buf_init_from_str(out_path, cache_dir);
+ if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') {
+ buf_append_char(out_path, '/');
+ }
+ buf_appendf(out_path, "%s", appname);
}
- buf_appendf(out_path, ".local/share/%s", appname);
return ErrorNone;
#endif
}
-
#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) || defined(ZIG_OS_DRAGONFLY)
static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, void *data) {
ZigList<Buf *> *libs = reinterpret_cast< ZigList<Buf *> *>(data);