New constant MD5_HEX_SIZE.

This commit is contained in:
default 2024-07-23 09:59:48 +02:00
parent 5921ec57f6
commit c06132579c
2 changed files with 22 additions and 20 deletions

36
data.c
View file

@ -373,7 +373,7 @@ double f_ctime(const char *fn)
int is_md5_hex(const char *md5) int is_md5_hex(const char *md5)
{ {
return xs_is_hex(md5) && strlen(md5) == 32; return xs_is_hex(md5) && strlen(md5) == MD5_HEX_SIZE - 1;
} }
@ -433,13 +433,13 @@ int index_del_md5(const char *fn, const char *md5)
char line[256]; char line[256];
while (fgets(line, sizeof(line), f) != NULL) { while (fgets(line, sizeof(line), f) != NULL) {
line[32] = '\0'; line[MD5_HEX_SIZE - 1] = '\0';
if (strcmp(line, md5) == 0) { if (strcmp(line, md5) == 0) {
/* found! just rewind, overwrite it with garbage /* found! just rewind, overwrite it with garbage
and an eventual call to index_gc() will clean it and an eventual call to index_gc() will clean it
[yes: this breaks index_len()] */ [yes: this breaks index_len()] */
fseek(f, -33, SEEK_CUR); fseek(f, -MD5_HEX_SIZE, SEEK_CUR);
fwrite("-", 1, 1, f); fwrite("-", 1, 1, f);
status = HTTP_STATUS_OK; status = HTTP_STATUS_OK;
@ -482,7 +482,7 @@ int index_gc(const char *fn)
gc = 0; gc = 0;
while (fgets(line, sizeof(line), i) != NULL) { while (fgets(line, sizeof(line), i) != NULL) {
line[32] = '\0'; line[MD5_HEX_SIZE - 1] = '\0';
if (line[0] != '-' && object_here_by_md5(line)) if (line[0] != '-' && object_here_by_md5(line))
fprintf(o, "%s\n", line); fprintf(o, "%s\n", line);
@ -520,7 +520,7 @@ int index_in_md5(const char *fn, const char *md5)
char line[256]; char line[256];
while (!ret && fgets(line, sizeof(line), f) != NULL) { while (!ret && fgets(line, sizeof(line), f) != NULL) {
line[32] = '\0'; line[MD5_HEX_SIZE - 1] = '\0';
if (strcmp(line, md5) == 0) if (strcmp(line, md5) == 0)
ret = 1; ret = 1;
@ -551,7 +551,7 @@ int index_first(const char *fn, char *line, int size)
flock(fileno(f), LOCK_SH); flock(fileno(f), LOCK_SH);
if (fgets(line, size, f) != NULL) { if (fgets(line, size, f) != NULL) {
line[32] = '\0'; line[MD5_HEX_SIZE - 1] = '\0';
ret = 1; ret = 1;
} }
@ -569,7 +569,7 @@ int index_len(const char *fn)
int len = 0; int len = 0;
if (stat(fn, &st) != -1) if (stat(fn, &st) != -1)
len = st.st_size / 33; len = st.st_size / MD5_HEX_SIZE;
return len; return len;
} }
@ -589,7 +589,7 @@ xs_list *index_list(const char *fn, int max)
while (n < max && fgets(line, sizeof(line), f) != NULL) { while (n < max && fgets(line, sizeof(line), f) != NULL) {
if (line[0] != '-') { if (line[0] != '-') {
line[32] = '\0'; line[MD5_HEX_SIZE - 1] = '\0';
list = xs_list_append(list, line); list = xs_list_append(list, line);
n++; n++;
} }
@ -602,41 +602,41 @@ xs_list *index_list(const char *fn, int max)
} }
int index_desc_next(FILE *f, char md5[33]) int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE])
/* reads the next entry of a desc index */ /* reads the next entry of a desc index */
{ {
for (;;) { for (;;) {
/* move backwards 2 entries */ /* move backwards 2 entries */
if (fseek(f, -66, SEEK_CUR) == -1) if (fseek(f, MD5_HEX_SIZE * -2, SEEK_CUR) == -1)
return 0; return 0;
/* read and md5 */ /* read and md5 */
if (!fread(md5, 33, 1, f)) if (!fread(md5, MD5_HEX_SIZE, 1, f))
return 0; return 0;
if (md5[0] != '-') if (md5[0] != '-')
break; break;
} }
md5[32] = '\0'; md5[MD5_HEX_SIZE - 1] = '\0';
return 1; return 1;
} }
int index_desc_first(FILE *f, char md5[33], int skip) int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip)
/* reads the first entry of a desc index */ /* reads the first entry of a desc index */
{ {
/* try to position at the end and then back to the first element */ /* try to position at the end and then back to the first element */
if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -33, SEEK_CUR)) if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -MD5_HEX_SIZE, SEEK_CUR))
return 0; return 0;
/* try to read an md5 */ /* try to read an md5 */
if (!fread(md5, 33, 1, f)) if (!fread(md5, MD5_HEX_SIZE, 1, f))
return 0; return 0;
/* null-terminate */ /* null-terminate */
md5[32] = '\0'; md5[MD5_HEX_SIZE - 1] = '\0';
/* deleted? retry next */ /* deleted? retry next */
if (md5[0] == '-') if (md5[0] == '-')
@ -653,7 +653,7 @@ xs_list *index_list_desc(const char *fn, int skip, int show)
FILE *f; FILE *f;
if ((f = fopen(fn, "r")) != NULL) { if ((f = fopen(fn, "r")) != NULL) {
char md5[33]; char md5[MD5_HEX_SIZE];
if (index_desc_first(f, md5, skip)) { if (index_desc_first(f, md5, skip)) {
int n = 1; int n = 1;
@ -1145,7 +1145,7 @@ xs_str *timeline_fn_by_md5(snac *snac, const char *md5)
{ {
xs_str *fn = NULL; xs_str *fn = NULL;
if (xs_is_hex(md5) && strlen(md5) == 32) { if (is_md5_hex(md5)) {
fn = xs_fmt("%s/private/%s.json", snac->basedir, md5); fn = xs_fmt("%s/private/%s.json", snac->basedir, md5);
if (mtime(fn) == 0.0) { if (mtime(fn) == 0.0) {

6
snac.h
View file

@ -20,6 +20,8 @@
#define MAX_CONVERSATION_LEVELS 48 #define MAX_CONVERSATION_LEVELS 48
#endif #endif
#define MD5_HEX_SIZE 33
extern double disk_layout; extern double disk_layout;
extern xs_str *srv_basedir; extern xs_str *srv_basedir;
extern xs_dict *srv_config; extern xs_dict *srv_config;
@ -101,8 +103,8 @@ int index_gc(const char *fn);
int index_first(const char *fn, char *buf, int size); int index_first(const char *fn, char *buf, int size);
int index_len(const char *fn); int index_len(const char *fn);
xs_list *index_list(const char *fn, int max); xs_list *index_list(const char *fn, int max);
int index_desc_next(FILE *f, char md5[33]); int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE]);
int index_desc_first(FILE *f, char md5[33], int skip); int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip);
xs_list *index_list_desc(const char *fn, int skip, int show); xs_list *index_list_desc(const char *fn, int skip, int show);
int object_add(const char *id, const xs_dict *obj); int object_add(const char *id, const xs_dict *obj);