File: basedir.c

package info (click to toggle)
libsfdo 0.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 740 kB
  • sloc: ansic: 6,491; python: 111; makefile: 4
file content (289 lines) | stat: -rw-r--r-- 7,778 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <assert.h>
#include <sfdo-basedir.h>
#include <stdlib.h>
#include <string.h>

#include "common/api.h"
#include "common/membuild.h"
#include "common/path.h"
#include "common/sized.h"
#include "common/striter.h"

#define DATA_HOME_FALLBACK "/.local/share/"
#define CONFIG_HOME_FALLBACK "/.config/"
#define STATE_HOME_FALLBACK "/.local/state/"
#define CACHE_HOME_FALLBACK "/.cache/"

#define DATA_DIRS_FALLBACK "/usr/local/share/:/usr/share/"
#define CONFIG_DIRS_FALLBACK "/etc/xdg/"

// Lists in ctx include home directories

struct sfdo_basedir_ctx {
	char *data_dirs_mem;
	struct sfdo_string *data_dirs;
	size_t n_data_dirs;

	char *config_dirs_mem;
	struct sfdo_string *config_dirs;
	size_t n_config_dirs;

	char *state_home_mem;
	struct sfdo_string state_home;

	char *cache_home_mem;
	struct sfdo_string cache_home;

	char *runtime_dir_mem;
	struct sfdo_string runtime_dir;
};

static inline bool is_unset_or_empty(const char *var) {
	return var == NULL || var[0] == '\0';
}

static inline bool is_absolute(const char *path) {
	return path[0] == '/';
}

static bool init_dir_list(struct sfdo_string **ptr, char **mem_ptr, size_t *n_dirs_ptr,
		const char *home, size_t home_len, const char *home_var_name, const char *home_fallback,
		size_t home_fallback_len, const char *list_var_name, const char *list_fallback) {
	const char *list = getenv(list_var_name);
	if (is_unset_or_empty(list)) {
		list = list_fallback;
	}

	const char *home_var_path = getenv(home_var_name);
	bool home_var_path_valid = !is_unset_or_empty(home_var_path) && is_absolute(home_var_path);

	size_t home_var_path_len;
	size_t mem_size = 0;
	if (home_var_path_valid) {
		home_var_path_len = strlen(home_var_path);
		mem_size += sfdo_path_compute_mem_size(home_var_path, home_var_path_len);
	} else {
		mem_size += home_len + home_fallback_len + 1;
	}

	size_t n_dirs = 1;

	size_t path_start, path_len;
	size_t iter = 0;

	while (sfdo_striter(list, ':', &iter, &path_start, &path_len)) {
		const char *path = list + path_start;
		if (path_len > 0 && is_absolute(path)) {
			++n_dirs;
			mem_size += sfdo_path_compute_mem_size(path, path_len);
		}
	}

	struct sfdo_string *dirs = calloc(n_dirs, sizeof(*dirs));
	if (dirs == NULL) {
		return false;
	}

	struct sfdo_membuild mem_buf;
	if (!sfdo_membuild_setup(&mem_buf, mem_size)) {
		free(dirs);
		return false;
	}

	// Home directory
	size_t dir_i = 0;
	struct sfdo_string *dir = &dirs[dir_i++];
	dir->data = sfdo_membuild_curr(&mem_buf);

	if (home_var_path_valid) {
		sfdo_membuild_add_path_segment(&mem_buf, home_var_path, home_var_path_len);
	} else {
		SFDO_MEMBUILD_ADD(&mem_buf, {home, home_len}, {home_fallback, home_fallback_len});
	}
	dir->len = sfdo_membuild_nulterm(&mem_buf);

	iter = 0;
	while (sfdo_striter(list, ':', &iter, &path_start, &path_len)) {
		const char *path = list + path_start;
		if (path_len > 0 && is_absolute(path)) {
			dir = &dirs[dir_i++];
			dir->data = sfdo_membuild_curr(&mem_buf);
			sfdo_membuild_add_path_segment(&mem_buf, path, path_len);
			dir->len = sfdo_membuild_nulterm(&mem_buf);
		}
	}

	sfdo_membuild_validate(&mem_buf);
	assert(dir_i == n_dirs);

	*ptr = dirs;
	*mem_ptr = mem_buf.data;
	*n_dirs_ptr = n_dirs;
	return true;
}

static bool init_dir(struct sfdo_string *ptr, char **mem_ptr, const char *home, size_t home_len,
		const char *var_name, const char *home_fallback, size_t home_fallback_len) {
	const char *var_path = getenv(var_name);
	bool var_path_valid = !is_unset_or_empty(var_path) && is_absolute(var_path);

	size_t var_path_len;
	size_t mem_size = 0;
	if (var_path_valid) {
		var_path_len = strlen(var_path);
		mem_size += sfdo_path_compute_mem_size(var_path, var_path_len);
	} else {
		if (home_fallback == NULL) {
			return true;
		}
		mem_size += home_len + home_fallback_len + 1;
	}

	struct sfdo_membuild mem_buf;
	if (!sfdo_membuild_setup(&mem_buf, mem_size)) {
		return false;
	}
	ptr->data = mem_buf.data;

	if (var_path_valid) {
		sfdo_membuild_add_path_segment(&mem_buf, var_path, var_path_len);
	} else {
		SFDO_MEMBUILD_ADD(&mem_buf, {home, home_len}, {home_fallback, home_fallback_len});
	}
	ptr->len = sfdo_membuild_nulterm(&mem_buf);

	sfdo_membuild_validate(&mem_buf);

	*mem_ptr = mem_buf.data;
	return true;
}

struct sfdo_basedir_ctx;

SFDO_API struct sfdo_basedir_ctx *sfdo_basedir_ctx_create(void) {
	struct sfdo_basedir_ctx *ctx = calloc(1, sizeof(*ctx));
	if (ctx == NULL) {
		return NULL;
	}

	const char *home = getenv("HOME");
	if (home == NULL) {
		// All home fallbacks start with "/" so results will be absolute paths
		home = "";
	}

	size_t home_len = strlen(home);
	if (home_len >= 1 && home[home_len - 1] == '/') {
		// Avoid duplicate slashes
		--home_len;
	}

	if (!init_dir_list(&ctx->data_dirs, &ctx->data_dirs_mem, &ctx->n_data_dirs, home, home_len,
				"XDG_DATA_HOME", DATA_HOME_FALLBACK, SFDO_SIZED_LEN(DATA_HOME_FALLBACK),
				"XDG_DATA_DIRS", DATA_DIRS_FALLBACK)) {
		goto err;
	}
	if (!init_dir_list(&ctx->config_dirs, &ctx->config_dirs_mem, &ctx->n_config_dirs, home,
				home_len, "XDG_CONFIG_HOME", CONFIG_HOME_FALLBACK,
				SFDO_SIZED_LEN(CONFIG_HOME_FALLBACK), "XDG_CONFIG_DIRS", CONFIG_DIRS_FALLBACK)) {
		goto err;
	}

	if (!init_dir(&ctx->state_home, &ctx->state_home_mem, home, home_len, "XDG_STATE_HOME",
				STATE_HOME_FALLBACK, SFDO_SIZED_LEN(STATE_HOME_FALLBACK))) {
		goto err;
	}
	if (!init_dir(&ctx->cache_home, &ctx->cache_home_mem, home, home_len, "XDG_CACHE_HOME",
				CACHE_HOME_FALLBACK, SFDO_SIZED_LEN(CACHE_HOME_FALLBACK))) {
		goto err;
	}

	if (!init_dir(&ctx->runtime_dir, &ctx->runtime_dir_mem, home, home_len, "XDG_RUNTIME_DIR", NULL,
				0)) {
		goto err;
	}

	return ctx;

err:
	sfdo_basedir_ctx_destroy(ctx);
	return NULL;
}

SFDO_API void sfdo_basedir_ctx_destroy(struct sfdo_basedir_ctx *ctx) {
	if (ctx == NULL) {
		return;
	}

	free(ctx->data_dirs);
	free(ctx->config_dirs);

	free(ctx->data_dirs_mem);
	free(ctx->config_dirs_mem);
	free(ctx->state_home_mem);
	free(ctx->cache_home_mem);
	free(ctx->runtime_dir_mem);

	free(ctx);
}

SFDO_API const struct sfdo_string *sfdo_basedir_get_data_dirs(
		struct sfdo_basedir_ctx *ctx, size_t *n_directories) {
	*n_directories = ctx->n_data_dirs;
	return ctx->data_dirs;
}

SFDO_API const char *sfdo_basedir_get_data_home(struct sfdo_basedir_ctx *ctx, size_t *len) {
	struct sfdo_string *data_home = ctx->data_dirs;
	if (len != NULL) {
		*len = data_home->len;
	}
	return data_home->data;
}

SFDO_API const struct sfdo_string *sfdo_basedir_get_data_system_dirs(
		struct sfdo_basedir_ctx *ctx, size_t *n_directories) {
	*n_directories = ctx->n_data_dirs - 1;
	return ctx->data_dirs + 1;
}

SFDO_API const struct sfdo_string *sfdo_basedir_get_config_dirs(
		struct sfdo_basedir_ctx *ctx, size_t *n_directories) {
	*n_directories = ctx->n_config_dirs;
	return ctx->config_dirs;
}

SFDO_API const char *sfdo_basedir_get_config_home(struct sfdo_basedir_ctx *ctx, size_t *len) {
	struct sfdo_string *config_home = ctx->config_dirs;
	if (len != NULL) {
		*len = config_home->len;
	}
	return config_home->data;
}

SFDO_API const struct sfdo_string *sfdo_basedir_get_config_system_dirs(
		struct sfdo_basedir_ctx *ctx, size_t *n_directories) {
	*n_directories = ctx->n_config_dirs - 1;
	return ctx->config_dirs + 1;
}

SFDO_API const char *sfdo_basedir_get_state_home(struct sfdo_basedir_ctx *ctx, size_t *len) {
	if (len != NULL) {
		*len = ctx->state_home.len;
	}
	return ctx->state_home.data;
}

SFDO_API const char *sfdo_basedir_get_cache_home(struct sfdo_basedir_ctx *ctx, size_t *len) {
	if (len != NULL) {
		*len = ctx->cache_home.len;
	}
	return ctx->cache_home.data;
}

SFDO_API const char *sfdo_basedir_get_runtime_dir(struct sfdo_basedir_ctx *ctx, size_t *len) {
	if (len != NULL) {
		*len = ctx->runtime_dir.len;
	}
	return ctx->runtime_dir.data;
}