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

View file

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