diff options
| author | 2024-07-23 09:59:48 +0200 | |
|---|---|---|
| committer | 2024-07-23 09:59:48 +0200 | |
| commit | c06132579cdfa1ffa41d73f438b7e2999c4c8e62 (patch) | |
| tree | 3c6633a956b3600122c878b1effdf840f198a7a3 | |
| parent | Reimplemented index_list_desc() by way of index_desc_first() and index_desc_n... (diff) | |
| download | snac2-c06132579cdfa1ffa41d73f438b7e2999c4c8e62.tar.gz snac2-c06132579cdfa1ffa41d73f438b7e2999c4c8e62.tar.xz snac2-c06132579cdfa1ffa41d73f438b7e2999c4c8e62.zip | |
New constant MD5_HEX_SIZE.
| -rw-r--r-- | data.c | 36 | ||||
| -rw-r--r-- | snac.h | 6 |
2 files changed, 22 insertions, 20 deletions
| @@ -373,7 +373,7 @@ double f_ctime(const char *fn) | |||
| 373 | 373 | ||
| 374 | int is_md5_hex(const char *md5) | 374 | int is_md5_hex(const char *md5) |
| 375 | { | 375 | { |
| 376 | return xs_is_hex(md5) && strlen(md5) == 32; | 376 | return xs_is_hex(md5) && strlen(md5) == MD5_HEX_SIZE - 1; |
| 377 | } | 377 | } |
| 378 | 378 | ||
| 379 | 379 | ||
| @@ -433,13 +433,13 @@ int index_del_md5(const char *fn, const char *md5) | |||
| 433 | char line[256]; | 433 | char line[256]; |
| 434 | 434 | ||
| 435 | while (fgets(line, sizeof(line), f) != NULL) { | 435 | while (fgets(line, sizeof(line), f) != NULL) { |
| 436 | line[32] = '\0'; | 436 | line[MD5_HEX_SIZE - 1] = '\0'; |
| 437 | 437 | ||
| 438 | if (strcmp(line, md5) == 0) { | 438 | if (strcmp(line, md5) == 0) { |
| 439 | /* found! just rewind, overwrite it with garbage | 439 | /* found! just rewind, overwrite it with garbage |
| 440 | and an eventual call to index_gc() will clean it | 440 | and an eventual call to index_gc() will clean it |
| 441 | [yes: this breaks index_len()] */ | 441 | [yes: this breaks index_len()] */ |
| 442 | fseek(f, -33, SEEK_CUR); | 442 | fseek(f, -MD5_HEX_SIZE, SEEK_CUR); |
| 443 | fwrite("-", 1, 1, f); | 443 | fwrite("-", 1, 1, f); |
| 444 | status = HTTP_STATUS_OK; | 444 | status = HTTP_STATUS_OK; |
| 445 | 445 | ||
| @@ -482,7 +482,7 @@ int index_gc(const char *fn) | |||
| 482 | gc = 0; | 482 | gc = 0; |
| 483 | 483 | ||
| 484 | while (fgets(line, sizeof(line), i) != NULL) { | 484 | while (fgets(line, sizeof(line), i) != NULL) { |
| 485 | line[32] = '\0'; | 485 | line[MD5_HEX_SIZE - 1] = '\0'; |
| 486 | 486 | ||
| 487 | if (line[0] != '-' && object_here_by_md5(line)) | 487 | if (line[0] != '-' && object_here_by_md5(line)) |
| 488 | fprintf(o, "%s\n", line); | 488 | fprintf(o, "%s\n", line); |
| @@ -520,7 +520,7 @@ int index_in_md5(const char *fn, const char *md5) | |||
| 520 | char line[256]; | 520 | char line[256]; |
| 521 | 521 | ||
| 522 | while (!ret && fgets(line, sizeof(line), f) != NULL) { | 522 | while (!ret && fgets(line, sizeof(line), f) != NULL) { |
| 523 | line[32] = '\0'; | 523 | line[MD5_HEX_SIZE - 1] = '\0'; |
| 524 | 524 | ||
| 525 | if (strcmp(line, md5) == 0) | 525 | if (strcmp(line, md5) == 0) |
| 526 | ret = 1; | 526 | ret = 1; |
| @@ -551,7 +551,7 @@ int index_first(const char *fn, char *line, int size) | |||
| 551 | flock(fileno(f), LOCK_SH); | 551 | flock(fileno(f), LOCK_SH); |
| 552 | 552 | ||
| 553 | if (fgets(line, size, f) != NULL) { | 553 | if (fgets(line, size, f) != NULL) { |
| 554 | line[32] = '\0'; | 554 | line[MD5_HEX_SIZE - 1] = '\0'; |
| 555 | ret = 1; | 555 | ret = 1; |
| 556 | } | 556 | } |
| 557 | 557 | ||
| @@ -569,7 +569,7 @@ int index_len(const char *fn) | |||
| 569 | int len = 0; | 569 | int len = 0; |
| 570 | 570 | ||
| 571 | if (stat(fn, &st) != -1) | 571 | if (stat(fn, &st) != -1) |
| 572 | len = st.st_size / 33; | 572 | len = st.st_size / MD5_HEX_SIZE; |
| 573 | 573 | ||
| 574 | return len; | 574 | return len; |
| 575 | } | 575 | } |
| @@ -589,7 +589,7 @@ xs_list *index_list(const char *fn, int max) | |||
| 589 | 589 | ||
| 590 | while (n < max && fgets(line, sizeof(line), f) != NULL) { | 590 | while (n < max && fgets(line, sizeof(line), f) != NULL) { |
| 591 | if (line[0] != '-') { | 591 | if (line[0] != '-') { |
| 592 | line[32] = '\0'; | 592 | line[MD5_HEX_SIZE - 1] = '\0'; |
| 593 | list = xs_list_append(list, line); | 593 | list = xs_list_append(list, line); |
| 594 | n++; | 594 | n++; |
| 595 | } | 595 | } |
| @@ -602,41 +602,41 @@ xs_list *index_list(const char *fn, int max) | |||
| 602 | } | 602 | } |
| 603 | 603 | ||
| 604 | 604 | ||
| 605 | int index_desc_next(FILE *f, char md5[33]) | 605 | int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE]) |
| 606 | /* reads the next entry of a desc index */ | 606 | /* reads the next entry of a desc index */ |
| 607 | { | 607 | { |
| 608 | for (;;) { | 608 | for (;;) { |
| 609 | /* move backwards 2 entries */ | 609 | /* move backwards 2 entries */ |
| 610 | if (fseek(f, -66, SEEK_CUR) == -1) | 610 | if (fseek(f, MD5_HEX_SIZE * -2, SEEK_CUR) == -1) |
| 611 | return 0; | 611 | return 0; |
| 612 | 612 | ||
| 613 | /* read and md5 */ | 613 | /* read and md5 */ |
| 614 | if (!fread(md5, 33, 1, f)) | 614 | if (!fread(md5, MD5_HEX_SIZE, 1, f)) |
| 615 | return 0; | 615 | return 0; |
| 616 | 616 | ||
| 617 | if (md5[0] != '-') | 617 | if (md5[0] != '-') |
| 618 | break; | 618 | break; |
| 619 | } | 619 | } |
| 620 | 620 | ||
| 621 | md5[32] = '\0'; | 621 | md5[MD5_HEX_SIZE - 1] = '\0'; |
| 622 | 622 | ||
| 623 | return 1; | 623 | return 1; |
| 624 | } | 624 | } |
| 625 | 625 | ||
| 626 | 626 | ||
| 627 | int index_desc_first(FILE *f, char md5[33], int skip) | 627 | int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip) |
| 628 | /* reads the first entry of a desc index */ | 628 | /* reads the first entry of a desc index */ |
| 629 | { | 629 | { |
| 630 | /* try to position at the end and then back to the first element */ | 630 | /* try to position at the end and then back to the first element */ |
| 631 | if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -33, SEEK_CUR)) | 631 | if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -MD5_HEX_SIZE, SEEK_CUR)) |
| 632 | return 0; | 632 | return 0; |
| 633 | 633 | ||
| 634 | /* try to read an md5 */ | 634 | /* try to read an md5 */ |
| 635 | if (!fread(md5, 33, 1, f)) | 635 | if (!fread(md5, MD5_HEX_SIZE, 1, f)) |
| 636 | return 0; | 636 | return 0; |
| 637 | 637 | ||
| 638 | /* null-terminate */ | 638 | /* null-terminate */ |
| 639 | md5[32] = '\0'; | 639 | md5[MD5_HEX_SIZE - 1] = '\0'; |
| 640 | 640 | ||
| 641 | /* deleted? retry next */ | 641 | /* deleted? retry next */ |
| 642 | if (md5[0] == '-') | 642 | if (md5[0] == '-') |
| @@ -653,7 +653,7 @@ xs_list *index_list_desc(const char *fn, int skip, int show) | |||
| 653 | FILE *f; | 653 | FILE *f; |
| 654 | 654 | ||
| 655 | if ((f = fopen(fn, "r")) != NULL) { | 655 | if ((f = fopen(fn, "r")) != NULL) { |
| 656 | char md5[33]; | 656 | char md5[MD5_HEX_SIZE]; |
| 657 | 657 | ||
| 658 | if (index_desc_first(f, md5, skip)) { | 658 | if (index_desc_first(f, md5, skip)) { |
| 659 | int n = 1; | 659 | int n = 1; |
| @@ -1145,7 +1145,7 @@ xs_str *timeline_fn_by_md5(snac *snac, const char *md5) | |||
| 1145 | { | 1145 | { |
| 1146 | xs_str *fn = NULL; | 1146 | xs_str *fn = NULL; |
| 1147 | 1147 | ||
| 1148 | if (xs_is_hex(md5) && strlen(md5) == 32) { | 1148 | if (is_md5_hex(md5)) { |
| 1149 | fn = xs_fmt("%s/private/%s.json", snac->basedir, md5); | 1149 | fn = xs_fmt("%s/private/%s.json", snac->basedir, md5); |
| 1150 | 1150 | ||
| 1151 | if (mtime(fn) == 0.0) { | 1151 | if (mtime(fn) == 0.0) { |
| @@ -20,6 +20,8 @@ | |||
| 20 | #define MAX_CONVERSATION_LEVELS 48 | 20 | #define MAX_CONVERSATION_LEVELS 48 |
| 21 | #endif | 21 | #endif |
| 22 | 22 | ||
| 23 | #define MD5_HEX_SIZE 33 | ||
| 24 | |||
| 23 | extern double disk_layout; | 25 | extern double disk_layout; |
| 24 | extern xs_str *srv_basedir; | 26 | extern xs_str *srv_basedir; |
| 25 | extern xs_dict *srv_config; | 27 | extern xs_dict *srv_config; |
| @@ -101,8 +103,8 @@ int index_gc(const char *fn); | |||
| 101 | int index_first(const char *fn, char *buf, int size); | 103 | int index_first(const char *fn, char *buf, int size); |
| 102 | int index_len(const char *fn); | 104 | int index_len(const char *fn); |
| 103 | xs_list *index_list(const char *fn, int max); | 105 | xs_list *index_list(const char *fn, int max); |
| 104 | int index_desc_next(FILE *f, char md5[33]); | 106 | int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE]); |
| 105 | int index_desc_first(FILE *f, char md5[33], int skip); | 107 | int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip); |
| 106 | xs_list *index_list_desc(const char *fn, int skip, int show); | 108 | xs_list *index_list_desc(const char *fn, int skip, int show); |
| 107 | 109 | ||
| 108 | int object_add(const char *id, const xs_dict *obj); | 110 | int object_add(const char *id, const xs_dict *obj); |