commit c061325

default  ·  2024-07-23 07:59:48 +0000 UTC
parent 5921ec5
New constant MD5_HEX_SIZE.
2 files changed,  +22, -20
M data.c
M snac.h
M data.c
+18, -18
  1@@ -373,7 +373,7 @@ double f_ctime(const char *fn)
  2 
  3 int is_md5_hex(const char *md5)
  4 {
  5-    return xs_is_hex(md5) && strlen(md5) == 32;
  6+    return xs_is_hex(md5) && strlen(md5) == MD5_HEX_SIZE - 1;
  7 }
  8 
  9 
 10@@ -433,13 +433,13 @@ int index_del_md5(const char *fn, const char *md5)
 11         char line[256];
 12 
 13         while (fgets(line, sizeof(line), f) != NULL) {
 14-            line[32] = '\0';
 15+            line[MD5_HEX_SIZE - 1] = '\0';
 16 
 17             if (strcmp(line, md5) == 0) {
 18                 /* found! just rewind, overwrite it with garbage
 19                    and an eventual call to index_gc() will clean it
 20                    [yes: this breaks index_len()] */
 21-                fseek(f, -33, SEEK_CUR);
 22+                fseek(f, -MD5_HEX_SIZE, SEEK_CUR);
 23                 fwrite("-", 1, 1, f);
 24                 status = HTTP_STATUS_OK;
 25 
 26@@ -482,7 +482,7 @@ int index_gc(const char *fn)
 27             gc = 0;
 28 
 29             while (fgets(line, sizeof(line), i) != NULL) {
 30-                line[32] = '\0';
 31+                line[MD5_HEX_SIZE - 1] = '\0';
 32 
 33                 if (line[0] != '-' && object_here_by_md5(line))
 34                     fprintf(o, "%s\n", line);
 35@@ -520,7 +520,7 @@ int index_in_md5(const char *fn, const char *md5)
 36         char line[256];
 37 
 38         while (!ret && fgets(line, sizeof(line), f) != NULL) {
 39-            line[32] = '\0';
 40+            line[MD5_HEX_SIZE - 1] = '\0';
 41 
 42             if (strcmp(line, md5) == 0)
 43                 ret = 1;
 44@@ -551,7 +551,7 @@ int index_first(const char *fn, char *line, int size)
 45         flock(fileno(f), LOCK_SH);
 46 
 47         if (fgets(line, size, f) != NULL) {
 48-            line[32] = '\0';
 49+            line[MD5_HEX_SIZE - 1] = '\0';
 50             ret = 1;
 51         }
 52 
 53@@ -569,7 +569,7 @@ int index_len(const char *fn)
 54     int len = 0;
 55 
 56     if (stat(fn, &st) != -1)
 57-        len = st.st_size / 33;
 58+        len = st.st_size / MD5_HEX_SIZE;
 59 
 60     return len;
 61 }
 62@@ -589,7 +589,7 @@ xs_list *index_list(const char *fn, int max)
 63 
 64         while (n < max && fgets(line, sizeof(line), f) != NULL) {
 65             if (line[0] != '-') {
 66-                line[32] = '\0';
 67+                line[MD5_HEX_SIZE - 1] = '\0';
 68                 list = xs_list_append(list, line);
 69                 n++;
 70             }
 71@@ -602,41 +602,41 @@ xs_list *index_list(const char *fn, int max)
 72 }
 73 
 74 
 75-int index_desc_next(FILE *f, char md5[33])
 76+int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE])
 77 /* reads the next entry of a desc index */
 78 {
 79     for (;;) {
 80         /* move backwards 2 entries */
 81-        if (fseek(f, -66, SEEK_CUR) == -1)
 82+        if (fseek(f, MD5_HEX_SIZE * -2, SEEK_CUR) == -1)
 83             return 0;
 84 
 85         /* read and md5 */
 86-        if (!fread(md5, 33, 1, f))
 87+        if (!fread(md5, MD5_HEX_SIZE, 1, f))
 88             return 0;
 89 
 90         if (md5[0] != '-')
 91             break;
 92     }
 93 
 94-    md5[32] = '\0';
 95+    md5[MD5_HEX_SIZE - 1] = '\0';
 96 
 97     return 1;
 98 }
 99 
100 
101-int index_desc_first(FILE *f, char md5[33], int skip)
102+int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip)
103 /* reads the first entry of a desc index */
104 {
105     /* try to position at the end and then back to the first element */
106-    if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -33, SEEK_CUR))
107+    if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -MD5_HEX_SIZE, SEEK_CUR))
108         return 0;
109 
110     /* try to read an md5 */
111-    if (!fread(md5, 33, 1, f))
112+    if (!fread(md5, MD5_HEX_SIZE, 1, f))
113         return 0;
114 
115     /* null-terminate */
116-    md5[32] = '\0';
117+    md5[MD5_HEX_SIZE - 1] = '\0';
118 
119     /* deleted? retry next */
120     if (md5[0] == '-')
121@@ -653,7 +653,7 @@ xs_list *index_list_desc(const char *fn, int skip, int show)
122     FILE *f;
123 
124     if ((f = fopen(fn, "r")) != NULL) {
125-        char md5[33];
126+        char md5[MD5_HEX_SIZE];
127 
128         if (index_desc_first(f, md5, skip)) {
129             int n = 1;
130@@ -1145,7 +1145,7 @@ xs_str *timeline_fn_by_md5(snac *snac, const char *md5)
131 {
132     xs_str *fn = NULL;
133 
134-    if (xs_is_hex(md5) && strlen(md5) == 32) {
135+    if (is_md5_hex(md5)) {
136         fn = xs_fmt("%s/private/%s.json", snac->basedir, md5);
137 
138         if (mtime(fn) == 0.0) {
M snac.h
+4, -2
 1@@ -20,6 +20,8 @@
 2 #define MAX_CONVERSATION_LEVELS 48
 3 #endif
 4 
 5+#define MD5_HEX_SIZE 33
 6+
 7 extern double disk_layout;
 8 extern xs_str *srv_basedir;
 9 extern xs_dict *srv_config;
10@@ -101,8 +103,8 @@ int index_gc(const char *fn);
11 int index_first(const char *fn, char *buf, int size);
12 int index_len(const char *fn);
13 xs_list *index_list(const char *fn, int max);
14-int index_desc_next(FILE *f, char md5[33]);
15-int index_desc_first(FILE *f, char md5[33], int skip);
16+int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE]);
17+int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip);
18 xs_list *index_list_desc(const char *fn, int skip, int show);
19 
20 int object_add(const char *id, const xs_dict *obj);