about summary refs log tree commit diff
path: root/pkgs/build-support
diff options
context:
space:
mode:
authoraszlig <aszlig@nix.build>2017-11-30 09:16:49 +0100
committeraszlig <aszlig@nix.build>2017-11-30 13:22:09 +0100
commit2df7436fb32cf7ac871b1143a8270f8cfa16b7e1 (patch)
tree406a7a9cc5a8f5e5192d1f732bac95d05fa7b858 /pkgs/build-support
parent947cd56bd8872d8e19bc196d925a1e0886555ec3 (diff)
pkgs/sandbox: Add handling for XDG_CACHE_HOME
We only handle XDG_DATA_HOME and XDG_CONFIG_HOME, but we've missed
XDG_CACHE_HOME. While the latter is used very rarely as it doesn't
matter a lot if it ends up within a tmpfs anyway. However if the cache
directory gets pretty large we might run out of space.

Not only do we now have proper fallbacks but this also adds tests for
all of the XDG environment variables we're using.

Signed-off-by: aszlig <aszlig@nix.build>
Diffstat (limited to 'pkgs/build-support')
-rw-r--r--pkgs/build-support/build-sandbox/src/setup.c34
1 files changed, 15 insertions, 19 deletions
diff --git a/pkgs/build-support/build-sandbox/src/setup.c b/pkgs/build-support/build-sandbox/src/setup.c
index 83876b2a..dfe550e0 100644
--- a/pkgs/build-support/build-sandbox/src/setup.c
+++ b/pkgs/build-support/build-sandbox/src/setup.c
@@ -268,6 +268,18 @@ static void free_offsets(struct envar_offset *base)
         free_offsets(next);
 }
 
+#define MK_XDG_EXPAND(varname, fallback) \
+    if (strcmp(xdg_var, varname) == 0) { \
+        result = malloc(homelen + sizeof fallback); \
+        if (result == NULL) { \
+            perror("malloc " varname); \
+            return NULL; \
+        } \
+        memcpy(result, home, homelen); \
+        memcpy(result + homelen, fallback, sizeof fallback); \
+        return result; \
+    }
+
 static char *expand_xdg_fallback(const char *xdg_var)
 {
     static char *home = NULL;
@@ -282,25 +294,9 @@ static char *expand_xdg_fallback(const char *xdg_var)
         homelen = strlen(home);
     }
 
-    if (strcmp(xdg_var, "XDG_DATA_HOME") == 0) {
-        result = malloc(homelen + 14);
-        if (result == NULL) {
-            perror("malloc XDG_DATA_HOME");
-            return NULL;
-        }
-        memcpy(result, home, homelen);
-        memcpy(result + homelen, "/.local/share", 14);
-        return result;
-    } else if (strcmp(xdg_var, "XDG_CONFIG_HOME") == 0) {
-        result = malloc(homelen + 9);
-        if (result == NULL) {
-            perror("malloc XDG_CONFIG_HOME");
-            return NULL;
-        }
-        memcpy(result, home, homelen);
-        memcpy(result + homelen, "/.config", 9);
-        return result;
-    }
+    MK_XDG_EXPAND("XDG_DATA_HOME", "/.local/share");
+    MK_XDG_EXPAND("XDG_CONFIG_HOME", "/.config");
+    MK_XDG_EXPAND("XDG_CACHE_HOME", "/.cache");
 
     return NULL;
 }