Skip to content

Commit e2bed7e

Browse files
committed
Handle fopen() errors with drun cache.
1 parent b32c995 commit e2bed7e

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/drun.c

+23-4
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ struct desktop_vec drun_generate_cached()
233233
free(cache_path);
234234
return apps;
235235
}
236+
errno = 0;
236237
FILE *cache = fopen(cache_path, "wb");
238+
if (cache == NULL) {
239+
log_error("Error creating drun cache: %s.\n", strerror(errno));
240+
free(cache_path);
241+
return apps;
242+
}
237243
desktop_vec_save(&apps, cache);
238244
fclose(cache);
239245
free(cache_path);
@@ -265,14 +271,27 @@ struct desktop_vec drun_generate_cached()
265271
log_indent();
266272
apps = drun_generate();
267273
log_unindent();
274+
errno = 0;
268275
FILE *cache = fopen(cache_path, "wb");
269-
desktop_vec_save(&apps, cache);
270-
fclose(cache);
276+
if (cache == NULL) {
277+
log_error("Failed to update cache: %s.\n", strerror(errno));
278+
} else {
279+
desktop_vec_save(&apps, cache);
280+
fclose(cache);
281+
}
271282
} else {
272283
log_debug("Cache up to date, loading.\n");
284+
errno = 0;
273285
FILE *cache = fopen(cache_path, "rb");
274-
apps = desktop_vec_load(cache);
275-
fclose(cache);
286+
if (cache == NULL) {
287+
log_error("Failed to load cache: %s.\n", strerror(errno));
288+
log_indent();
289+
apps = drun_generate();
290+
log_unindent();
291+
} else {
292+
apps = desktop_vec_load(cache);
293+
fclose(cache);
294+
}
276295
}
277296
free(cache_path);
278297
return apps;

0 commit comments

Comments
 (0)