delta: rework how enumerate files
This commit is contained in:
+49
-56
@@ -186,11 +186,43 @@ DEFINE_PRIVATE_HASH_OPS_FULL(
|
||||
char, string_hash_func, string_compare_func, free,
|
||||
OrderedHashmap, ordered_hashmap_free);
|
||||
|
||||
static int path_put(OrderedHashmap **h, const char *dir_path, const char *filename, bool override) {
|
||||
int r;
|
||||
|
||||
assert(h);
|
||||
assert(dir_path);
|
||||
assert(filename);
|
||||
|
||||
if (override) {
|
||||
_unused_ _cleanup_free_ char *old = NULL;
|
||||
free(ordered_hashmap_remove2(*h, filename, (void**) &old));
|
||||
} else if (ordered_hashmap_contains(*h, filename))
|
||||
return 0;
|
||||
|
||||
_cleanup_free_ char *p = path_join(dir_path, filename);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
_cleanup_free_ char *f = strdup(filename);
|
||||
if (!f)
|
||||
return -ENOMEM;
|
||||
|
||||
r = ordered_hashmap_ensure_put(h, &path_hash_ops_free_free, f, p);
|
||||
if (r < 0)
|
||||
return r;
|
||||
assert(r > 0);
|
||||
|
||||
TAKE_PTR(f);
|
||||
TAKE_PTR(p);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int enumerate_dir_d(
|
||||
OrderedHashmap **top,
|
||||
OrderedHashmap **bottom,
|
||||
OrderedHashmap **drops,
|
||||
const char *toppath, const char *drop) {
|
||||
const char *toppath,
|
||||
const char *drop) {
|
||||
|
||||
_cleanup_free_ char *unit = NULL;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
@@ -222,41 +254,23 @@ static int enumerate_dir_d(
|
||||
strv_sort(list);
|
||||
|
||||
STRV_FOREACH(file, list) {
|
||||
OrderedHashmap *h;
|
||||
char *p;
|
||||
char *d;
|
||||
|
||||
if (!endswith(*file, ".conf"))
|
||||
continue;
|
||||
|
||||
p = path_join(path, *file);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
d = p + strlen(toppath) + 1;
|
||||
|
||||
log_debug("Adding at top: %s %s %s", d, glyph(GLYPH_ARROW_RIGHT), p);
|
||||
r = ordered_hashmap_ensure_put(top, &string_hash_ops_value_free, d, p);
|
||||
if (r >= 0) {
|
||||
p = strdup(p);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
d = p + strlen(toppath) + 1;
|
||||
} else if (r != -EEXIST) {
|
||||
free(p);
|
||||
log_debug("Adding at top: %s %s %s/%s", *file, glyph(GLYPH_ARROW_RIGHT), path, *file);
|
||||
r = path_put(top, path, *file, /* override = */ false);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
log_debug("Adding at bottom: %s %s %s", d, glyph(GLYPH_ARROW_RIGHT), p);
|
||||
free(ordered_hashmap_remove(*bottom, d));
|
||||
r = ordered_hashmap_ensure_put(bottom, &string_hash_ops_value_free, d, p);
|
||||
if (r < 0) {
|
||||
free(p);
|
||||
log_debug("Adding at bottom: %s %s %s/%s", *file, glyph(GLYPH_ARROW_RIGHT), path, *file);
|
||||
r = path_put(bottom, path, *file, /* override = */ true);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
h = ordered_hashmap_get(*drops, unit);
|
||||
OrderedHashmap *h = ordered_hashmap_get(*drops, unit);
|
||||
if (!h) {
|
||||
h = ordered_hashmap_new(&string_hash_ops_value_free);
|
||||
h = ordered_hashmap_new(&path_hash_ops_free_free);
|
||||
if (!h)
|
||||
return -ENOMEM;
|
||||
r = ordered_hashmap_ensure_put(drops, &drop_hash_ops, unit, h);
|
||||
@@ -269,18 +283,9 @@ static int enumerate_dir_d(
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
p = strdup(p);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
log_debug("Adding to drops: %s %s %s %s %s",
|
||||
unit, glyph(GLYPH_ARROW_RIGHT), basename(p), glyph(GLYPH_ARROW_RIGHT), p);
|
||||
r = ordered_hashmap_put(h, basename(p), p);
|
||||
if (r < 0) {
|
||||
free(p);
|
||||
if (r != -EEXIST)
|
||||
return r;
|
||||
}
|
||||
log_debug("Adding to drops: %s %s %s %s %s/%s",
|
||||
unit, glyph(GLYPH_ARROW_RIGHT), *file, glyph(GLYPH_ARROW_RIGHT), path, *file);
|
||||
r = path_put(&h, path, *file, /* override = */ false);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -344,27 +349,15 @@ static int enumerate_dir(
|
||||
}
|
||||
|
||||
STRV_FOREACH(t, files) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
|
||||
p = path_join(path, *t);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
log_debug("Adding at top: %s %s %s", basename(p), glyph(GLYPH_ARROW_RIGHT), p);
|
||||
r = ordered_hashmap_ensure_put(top, &string_hash_ops_value_free, basename(p), p);
|
||||
if (r >= 0) {
|
||||
p = strdup(p);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
} else if (r != -EEXIST)
|
||||
log_debug("Adding at top: %s %s %s/%s", *t, glyph(GLYPH_ARROW_RIGHT), path, *t);
|
||||
r = path_put(top, path, *t, /* override = */ false);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
log_debug("Adding at bottom: %s %s %s", basename(p), glyph(GLYPH_ARROW_RIGHT), p);
|
||||
free(ordered_hashmap_remove(*bottom, basename(p)));
|
||||
r = ordered_hashmap_ensure_put(bottom, &string_hash_ops_value_free, basename(p), p);
|
||||
log_debug("Adding at bottom: %s %s %s/%s", *t, glyph(GLYPH_ARROW_RIGHT), path, *t);
|
||||
r = path_put(bottom, path, *t, /* override = */ true);
|
||||
if (r < 0)
|
||||
return r;
|
||||
p = NULL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user