1
0
Fork 0

07: C: Don’t needlessly store file entries

This commit is contained in:
Mia Herkt 2022-12-07 22:51:26 +01:00
parent 5a1a8bb571
commit 3144a5f528
Signed by: mia
GPG Key ID: 72E154B8622EC191
1 changed files with 13 additions and 21 deletions

View File

@ -14,7 +14,6 @@ uint32_t fnv1a_32(const char *buf, size_t len)
typedef struct entry { typedef struct entry {
uint32_t id; uint32_t id;
int isdir;
size_t size; size_t size;
size_t n_entries; size_t n_entries;
size_t n_alloc; size_t n_alloc;
@ -22,18 +21,17 @@ typedef struct entry {
struct entry *parent; struct entry *parent;
} entry; } entry;
entry *findent(entry *e, int isdir, const char *name) { entry *findent(entry *e, const char *name) {
uint32_t id = fnv1a_32(name, strlen(name)); uint32_t id = fnv1a_32(name, strlen(name));
for (size_t i = 0; i < e->n_entries; i++) { for (size_t i = 0; i < e->n_entries; i++) {
if (e->entries[i]->isdir == isdir) if (e->entries[i]->id == id)
if (e->entries[i]->id == id) return e->entries[i];
return e->entries[i];
} }
return NULL; return NULL;
} }
entry *addent(entry *parent, int isdir, const char *name) { entry *addent(entry *parent, const char *name) {
if (parent->n_entries >= parent->n_alloc) { if (parent->n_entries >= parent->n_alloc) {
parent->n_alloc += 10; parent->n_alloc += 10;
if (parent->n_entries) if (parent->n_entries)
@ -45,7 +43,6 @@ entry *addent(entry *parent, int isdir, const char *name) {
entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry)); entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry));
e->id = fnv1a_32(name, strlen(name)); e->id = fnv1a_32(name, strlen(name));
e->isdir = isdir;
e->parent = parent; e->parent = parent;
return e; return e;
@ -54,7 +51,6 @@ entry *addent(entry *parent, int isdir, const char *name) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
char l[64]; char l[64];
entry *root = calloc(1, sizeof(entry)); entry *root = calloc(1, sizeof(entry));
root->isdir++;
entry *current = root; entry *current = root;
size_t S = 0, G = 0, required = 0; size_t S = 0, G = 0, required = 0;
@ -67,16 +63,14 @@ int main(int argc, char **argv) {
else if (l[5] == '.') else if (l[5] == '.')
current = current->parent; current = current->parent;
else else
current = findent(current, 1, l + 5); current = findent(current, l + 5);
} }
} else if (l[0] == 'd') { } else if (l[0] == 'd') {
addent(current, 1, l + 4); addent(current, l + 4);
} else { } else {
char *p; size_t sz = strtoul(l, NULL, 10);
size_t sz = strtoul(l, &p, 10); entry *f = current;
entry *f = addent(current, 0, p + 1); for (; f->parent; f = f->parent)
f->size = sz;
for (f = current; f->parent; f = f->parent)
f->size += sz; f->size += sz;
f->size += sz; f->size += sz;
} }
@ -92,12 +86,10 @@ int main(int argc, char **argv) {
current->n_entries--; current->n_entries--;
current = (current->entries++)[0]; current = (current->entries++)[0];
} else { } else {
if (current->isdir) { if (current->size < 100000)
if (current->size < 100000) S += current->size;
S += current->size; else if (current->size >= required && (current->size < G || !G))
else if (current->size >= required && (current->size < G || !G)) G = current->size;
G = current->size;
}
current = current->parent; current = current->parent;
} }