int drop_in_file(const char *dir, const char *unit, unsigned level, const char *name, char **_p, char **_q) { char prefix[DECIMAL_STR_MAX(unsigned)]; _cleanup_free_ char *b = NULL; char *p, *q; assert(unit); assert(name); assert(_p); assert(_q); sprintf(prefix, "%u", level); b = xescape(name, "/."); if (!b) return -ENOMEM; if (!filename_is_valid(b)) return -EINVAL; p = strjoin(dir, "/", unit, ".d"); if (!p) return -ENOMEM; q = strjoin(p, "/", prefix, "-", b, ".conf"); if (!q) { free(p); return -ENOMEM; } *_p = p; *_q = q; return 0; }
int pull_find_old_etags( const char *url, const char *image_root, int dt, const char *prefix, const char *suffix, char ***etags) { _cleanup_free_ char *escaped_url = NULL; _cleanup_closedir_ DIR *d = NULL; _cleanup_strv_free_ char **l = NULL; struct dirent *de; int r; assert(url); assert(etags); if (!image_root) image_root = "/var/lib/machines"; escaped_url = xescape(url, FILENAME_ESCAPE); if (!escaped_url) return -ENOMEM; d = opendir(image_root); if (!d) { if (errno == ENOENT) { *etags = NULL; return 0; } return -errno; } FOREACH_DIRENT_ALL(de, d, return -errno) { const char *a, *b; char *u; if (de->d_type != DT_UNKNOWN && de->d_type != dt) continue; if (prefix) { a = startswith(de->d_name, prefix); if (!a) continue; } else a = de->d_name; a = startswith(a, escaped_url); if (!a) continue; a = startswith(a, "."); if (!a) continue; if (suffix) { b = endswith(de->d_name, suffix); if (!b) continue; } else b = strchr(de->d_name, 0); if (a >= b) continue; r = cunescape_length(a, b - a, 0, &u); if (r < 0) return r; if (!http_etag_is_valid(u)) { free(u); continue; } r = strv_consume(&l, u); if (r < 0) return r; } *etags = l; l = NULL; return 0; }