File: scan.c

package info (click to toggle)
libsfdo 0.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 736 kB
  • sloc: ansic: 6,491; python: 111; makefile: 4
file content (461 lines) | stat: -rw-r--r-- 12,669 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <dirent.h>
#include <errno.h>
#include <sfdo-icon.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include "common/api.h"
#include "common/grow.h"
#include "common/sized.h"
#include "common/strpool.h"
#include "sfdo-icon/internal.h"

#define ICON_THEME_CACHE_PATH "/icon-theme.cache"

struct sfdo_icon_scanner_image {
	struct sfdo_hashmap_entry base; // sfdo_icon_scanner.image_set
	int formats; // enum sfdo_icon_format_mask
};

static void check_dir_stats(struct sfdo_icon_state *state, size_t dir_i, const char *path) {
	struct stat statbuf;
	if (stat(path, &statbuf) == 0) {
		state->dir_exists[dir_i] = true;
		state->dir_mtimes[dir_i] = statbuf.st_mtime;
	} else {
		state->dir_exists[dir_i] = false;
		state->dir_mtimes[dir_i] = 0;
	}
}

static bool scanner_init(
		struct sfdo_icon_scanner *scanner, struct sfdo_logger *logger, size_t n_dirs) {
	if (!icon_state_init(&scanner->state, n_dirs)) {
		logger_write_oom(logger);
		return false;
	}

	scanner->logger = logger;

	scanner->images_len = scanner->images_cap = 0;
	sfdo_hashmap_init(&scanner->image_names, sizeof(struct sfdo_hashmap_entry));

	return true;
}

static void scanner_finish(struct sfdo_icon_scanner *scanner) {
	sfdo_hashmap_finish(&scanner->image_names);
}

static void scanner_discard_and_finish(struct sfdo_icon_scanner *scanner) {
	icon_state_finish(&scanner->state);
	scanner_finish(scanner);
}

static void scanner_commit_and_finish(
		struct sfdo_icon_scanner *scanner, struct sfdo_icon_state *out) {
	icon_state_finish(out);
	*out = scanner->state;
	scanner_finish(scanner);
}

const char *icon_scanner_intern_name(
		struct sfdo_icon_scanner *scanner, const char *name, size_t name_len) {
	struct sfdo_logger *logger = scanner->logger;

	struct sfdo_hashmap_entry *name_entry =
			sfdo_hashmap_get(&scanner->image_names, name, name_len, true);
	if (name_entry == NULL) {
		logger_write_oom(logger);
		return NULL;
	} else if (name_entry->key == NULL) {
		name_entry->key = sfdo_strpool_add(&scanner->state.names, name, name_len);
		if (name_entry->key == NULL) {
			logger_write_oom(logger);
			return NULL;
		}
	}
	return name_entry->key;
}

bool icon_scanner_add_image(struct sfdo_icon_scanner *scanner, const struct sfdo_string *basedir,
		const struct sfdo_icon_subdir *subdir, const char *name, size_t name_len, int formats) {
	struct sfdo_logger *logger = scanner->logger;

	struct sfdo_icon_state *state = &scanner->state;
	if (!sfdo_grow(&state->images, &scanner->images_cap, scanner->images_len,
				sizeof(*state->images))) {
		logger_write_oom(logger);
		return false;
	}

	struct sfdo_icon_image_list *image_list = sfdo_hashmap_get(&state->map, name, name_len, true);
	if (image_list == NULL) {
		logger_write_oom(logger);
		return false;
	} else if (image_list->base.key == NULL) {
		image_list->base.key = name;
		image_list->start_i = scanner->images_len;
	} else {
		state->images[image_list->end_i].next_i = scanner->images_len;
	}

	image_list->end_i = scanner->images_len;
	struct sfdo_icon_image *image = &state->images[scanner->images_len++];

	image->basedir = basedir;
	image->subdir = subdir;
	image->formats = formats;

	image->next_i = (size_t)-1;

	return true;
}

static bool scan_dir(struct sfdo_icon_scanner *scanner, const char *path,
		const struct sfdo_string *basedir, const struct sfdo_icon_subdir *subdir) {
	struct sfdo_logger *logger = scanner->logger;

	DIR *dirp = opendir(path);
	if (dirp == NULL) {
		logger_write(logger, SFDO_LOG_LEVEL_ERROR, "Failed to open directory %s: %s", path,
				strerror(errno));
		return false;
	}

	struct sfdo_hashmap image_set;
	sfdo_hashmap_init(&image_set, sizeof(struct sfdo_icon_scanner_image));

	bool ok = false;

	struct dirent *dirent;
	while ((dirent = readdir(dirp)) != NULL) {
		char *name = dirent->d_name;
		size_t name_len = strlen(dirent->d_name);
		if (name_len < 5) {
			continue;
		}
		size_t icon_name_len = name_len - 4;
		if (name[icon_name_len] != '.') {
			continue;
		}

		char *ext = &name[icon_name_len + 1];
		int format = 0;
		if (strcmp(ext, "png") == 0) {
			format = SFDO_ICON_FORMAT_MASK_PNG;
		} else if (strcmp(ext, "svg") == 0) {
			format = SFDO_ICON_FORMAT_MASK_SVG;
		} else if (strcmp(ext, "xpm") == 0) {
			format = SFDO_ICON_FORMAT_MASK_XPM;
		} else {
			continue;
		}

		struct sfdo_icon_scanner_image *entry =
				sfdo_hashmap_get(&image_set, name, icon_name_len, true);
		if (entry == NULL) {
			logger_write_oom(logger);
			return false;
		} else if (entry->base.key == NULL) {
			entry->base.key = icon_scanner_intern_name(scanner, name, icon_name_len);
			if (entry->base.key == NULL) {
				goto end;
			}
			entry->formats = 0;
		}
		entry->formats |= format;
	}

	for (size_t i = 0; i < image_set.cap; i++) {
		struct sfdo_icon_scanner_image *entry =
				&((struct sfdo_icon_scanner_image *)image_set.mem)[i];
		if (entry->base.key != NULL) {
			if (!icon_scanner_add_image(scanner, basedir, subdir, entry->base.key,
						entry->base.key_len, entry->formats)) {
				goto end;
			}
		}
	}

	ok = true;
	if (image_set.len > 0) {
		logger_write(
				logger, SFDO_LOG_LEVEL_DEBUG, "Added %zu image(s) from %s", image_set.len, path);
	}

end:
	sfdo_hashmap_finish(&image_set);
	closedir(dirp);
	return ok;
}

static bool rescan_node(struct sfdo_icon_theme_node *node, struct sfdo_icon_theme *theme) {
	struct sfdo_logger *logger = &theme->ctx->logger;

	logger_write(logger, SFDO_LOG_LEVEL_DEBUG, "Scanning %s", node->name);

	struct sfdo_icon_cache **cache_files =
			calloc(theme->n_basedirs, sizeof(struct sfdo_icon_cache *));
	if (cache_files == NULL) {
		logger_write_oom(logger);
		return false;
	}

	struct sfdo_icon_scanner scanner;
	if (!scanner_init(&scanner, logger, theme->n_basedirs * (node->n_subdirs + 1))) {
		free(cache_files);
		return false;
	}

	bool ok = false;
	struct sfdo_strbuild *pb = &theme->path_buf;

	size_t node_dir_i = 0;
	for (size_t basedir_i = 0; basedir_i < theme->n_basedirs; basedir_i++) {
		struct sfdo_string *basedir = &theme->basedirs[basedir_i];

		sfdo_strbuild_reset(pb);
		if (!SFDO_STRBUILD_ADD(pb, {basedir->data, basedir->len}, {node->name, node->name_len})) {
			logger_write_oom(logger);
			goto end;
		}

		check_dir_stats(&scanner.state, node_dir_i++, pb->data);
		if (!scanner.state.dir_exists[basedir_i]) {
			continue;
		}

		if (!SFDO_STRBUILD_ADD(pb, SFDO_SIZED_LIT(ICON_THEME_CACHE_PATH))) {
			goto end;
		}
		cache_files[basedir_i] =
				icon_cache_create(pb->data, scanner.state.dir_mtimes[basedir_i], logger);
	}

	for (size_t subdir_i = 0; subdir_i < node->n_subdirs; subdir_i++) {
		struct sfdo_icon_subdir *subdir = &node->subdirs[subdir_i];
		for (size_t basedir_i = 0; basedir_i < theme->n_basedirs; basedir_i++) {
			size_t dir_i = node_dir_i++;

			if (!scanner.state.dir_exists[basedir_i]) {
				// If the /<basedir>/<theme>/ doesn't exist, its subdirs don't exist either
				scanner.state.dir_exists[dir_i] = false;
				scanner.state.dir_mtimes[dir_i] = 0;
				continue;
			}

			struct sfdo_string *basedir = &theme->basedirs[basedir_i];

			sfdo_strbuild_reset(pb);
			if (!SFDO_STRBUILD_ADD(pb, {basedir->data, basedir->len}, {node->name, node->name_len},
						SFDO_SIZED_SLASH, {subdir->path.data, subdir->path.len})) {
				logger_write_oom(logger);
				goto end;
			}

			check_dir_stats(&scanner.state, dir_i, pb->data);
			if (!scanner.state.dir_exists[dir_i]) {
				continue;
			}

			struct sfdo_icon_cache *cache_file = cache_files[basedir_i];
			if (cache_file != NULL) {
				if (!icon_cache_scan_dir(cache_file, &scanner, basedir, subdir)) {
					goto end;
				}
				continue;
			}

			if (!scan_dir(&scanner, pb->data, basedir, subdir)) {
				goto end;
			}
		}
	}

	ok = true;

end:
	for (size_t basedir_i = 0; basedir_i < theme->n_basedirs; basedir_i++) {
		struct sfdo_icon_cache *cache_file = cache_files[basedir_i];
		icon_cache_destroy(cache_file);
	}
	free(cache_files);

	if (ok) {
		logger_write(logger, SFDO_LOG_LEVEL_INFO, "Found %zu image(s) in %s", scanner.images_len,
				node->name);
		scanner_commit_and_finish(&scanner, &node->state);
		return true;
	} else {
		scanner_discard_and_finish(&scanner);
		return false;
	}
}

static bool rescan_fallback(struct sfdo_icon_theme *theme) {
	struct sfdo_logger *logger = &theme->ctx->logger;

	logger_write(logger, SFDO_LOG_LEVEL_DEBUG, "Scanning fallback icon directories");

	struct sfdo_icon_scanner scanner;
	if (!scanner_init(&scanner, &theme->ctx->logger, theme->n_basedirs)) {
		return false;
	}

	bool ok = false;

	for (size_t basedir_i = 0; basedir_i < theme->n_basedirs; basedir_i++) {
		struct sfdo_string *basedir = &theme->basedirs[basedir_i];
		check_dir_stats(&scanner.state, basedir_i, basedir->data);
		if (!scanner.state.dir_exists[basedir_i]) {
			continue;
		}
		if (!scan_dir(&scanner, basedir->data, basedir, NULL)) {
			goto end;
		}
	}

	ok = true;

end:
	if (ok) {
		logger_write(
				logger, SFDO_LOG_LEVEL_INFO, "Found %zu fallback image(s)", scanner.images_len);
		scanner_commit_and_finish(&scanner, &theme->state);
		return true;
	} else {
		scanner_discard_and_finish(&scanner);
		return false;
	}
}

static bool rescan_theme(struct sfdo_icon_theme *theme) {
	for (struct sfdo_icon_theme_node *node = theme->nodes; node != NULL; node = node->next) {
		if (!rescan_node(node, theme)) {
			return false;
		}
	}

	if (!rescan_fallback(theme)) {
		return false;
	}

	return true;
}

static bool dir_is_stale(const struct sfdo_icon_state *state, struct sfdo_icon_theme *theme,
		const char *path, size_t dir_i) {
	struct sfdo_logger *logger = &theme->ctx->logger;

	bool exists = false;
	time_t mtime = 0;
	struct stat statbuf;
	if (stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
		exists = true;
		mtime = statbuf.st_mtime;
	}
	bool c_exists = state->dir_exists[dir_i];
	time_t c_mtime = state->dir_mtimes[dir_i];

	if (exists != c_exists || (exists && mtime != c_mtime)) {
		logger_write(logger, SFDO_LOG_LEVEL_DEBUG, "%s is stale: old=%s,%lld new=%s,%lld", path,
				c_exists ? "yes" : "no", (long long)c_mtime, exists ? "yes" : "no",
				(long long)mtime);
		return true;
	}

	return false;
}

static bool node_check_stale(
		struct sfdo_icon_theme_node *node, struct sfdo_icon_theme *theme, bool *out) {
	struct sfdo_logger *logger = &theme->ctx->logger;

	struct sfdo_strbuild *pb = &theme->path_buf;

	size_t node_dir_i = 0;
	for (size_t basedir_i = 0; basedir_i < theme->n_basedirs; basedir_i++) {
		size_t dir_i = node_dir_i++;

		struct sfdo_string *basedir = &theme->basedirs[basedir_i];

		sfdo_strbuild_reset(pb);
		if (!SFDO_STRBUILD_ADD(pb, {basedir->data, basedir->len}, {node->name, node->name_len})) {
			logger_write_oom(logger);
			return false;
		}

		if (dir_is_stale(&node->state, theme, pb->data, dir_i)) {
			*out = true;
			return true;
		}
	}

	for (size_t subdir_i = 0; subdir_i < node->n_subdirs; subdir_i++) {
		struct sfdo_icon_subdir *subdir = &node->subdirs[subdir_i];
		for (size_t basedir_i = 0; basedir_i < theme->n_basedirs; basedir_i++) {
			size_t dir_i = node_dir_i++;

			if (!node->state.dir_exists[basedir_i]) {
				// If the /<basedir>/<theme>/ doesn't exist, its subdirs don't exist either
				continue;
			}

			struct sfdo_string *basedir = &theme->basedirs[basedir_i];

			sfdo_strbuild_reset(pb);
			if (!SFDO_STRBUILD_ADD(pb, {basedir->data, basedir->len}, {node->name, node->name_len},
						SFDO_SIZED_SLASH, {subdir->path.data, subdir->path.len})) {
				logger_write_oom(logger);
				return false;
			}

			if (dir_is_stale(&node->state, theme, pb->data, dir_i)) {
				*out = true;
				return true;
			}
		}
	}

	*out = false;
	return true;
}

// Returns true on success, false otherwise
bool icon_theme_maybe_rescan(struct sfdo_icon_theme *theme) {
	struct timespec now;
	clock_gettime(CLOCK_MONOTONIC, &now);

	const struct timespec *then = &theme->scan_time;

	// At least 5 seconds between automatic rescans
	if (now.tv_sec - then->tv_sec - (now.tv_nsec < then->tv_sec) < 5) {
		return true;
	}
	theme->scan_time = now;

	for (size_t basedir_i = 0; basedir_i < theme->n_basedirs; basedir_i++) {
		struct sfdo_string *basedir = &theme->basedirs[basedir_i];
		if (dir_is_stale(&theme->state, theme, basedir->data, basedir_i)) {
			return rescan_theme(theme);
		}
	}

	for (struct sfdo_icon_theme_node *node = theme->nodes; node != NULL; node = node->next) {
		bool stale;
		if (!node_check_stale(node, theme, &stale)) {
			return false;
		} else if (stale && !rescan_node(node, theme)) {
			return false;
		}
	}
	return true;
}

SFDO_API bool sfdo_icon_theme_rescan(struct sfdo_icon_theme *theme) {
	clock_gettime(CLOCK_MONOTONIC, &theme->scan_time);
	return rescan_theme(theme);
}