commit 7f458f4
default
·
2022-11-23 18:32:26 +0000 UTC
parent 0ff3b32
More listfile functions.
1 files changed,
+62,
-4
M
data.c
M
data.c
+62,
-4
1@@ -295,7 +295,7 @@ int object_del(const char *id)
2 }
3
4
5-int list_add_md5(const char *fn, const char *md5)
6+int listfile_add_md5(const char *fn, const char *md5)
7 /* adds an md5 to a list */
8 {
9 int status = 200;
10@@ -314,7 +314,7 @@ int list_add_md5(const char *fn, const char *md5)
11 }
12
13
14-int list_del_md5(const char *fn, const char *md5)
15+int listfile_del_md5(const char *fn, const char *md5)
16 /* deletes an md5 from a list */
17 {
18 int status = 404;
19@@ -324,10 +324,9 @@ int list_del_md5(const char *fn, const char *md5)
20 flock(fileno(i), LOCK_EX);
21
22 xs *nfn = xs_fmt("%s.new", fn);
23+ char line[32 + 1];
24
25 if ((o = fopen(nfn, "w")) != NULL) {
26- char line[32 + 1];
27-
28 while (fgets(line, sizeof(line), i) != NULL) {
29 if (memcmp(line, md5, 32) != 0)
30 fwrite(line, sizeof(line), 1, o);
31@@ -352,6 +351,65 @@ int list_del_md5(const char *fn, const char *md5)
32 }
33
34
35+d_char *listfile_get_n(const char *fn, int max)
36+/* returns a list */
37+{
38+ xs *list = NULL;
39+ FILE *f;
40+ int n = 0;
41+
42+ if ((f = fopen(fn, "r")) != NULL) {
43+ flock(fileno(f), LOCK_SH);
44+
45+ char line[32 + 1];
46+ list = xs_list_new();
47+
48+ while (n < max && fgets(line, sizeof(line), f) != NULL) {
49+ line[32] = '\0';
50+ list = xs_list_append(list, line);
51+ n++;
52+ }
53+
54+ fclose(f);
55+ }
56+
57+ return list;
58+}
59+
60+
61+d_char *listfile_get_inv_n(const char *fn, int max)
62+/* returns a list, inversely */
63+{
64+ xs *list = NULL;
65+ FILE *f;
66+ int n = 0;
67+
68+ if ((f = fopen(fn, "r")) != NULL) {
69+ flock(fileno(f), LOCK_SH);
70+
71+ char line[32 + 1];
72+ list = xs_list_new();
73+
74+ /* move to the end minus one entry */
75+ if (!fseek(f, 0, SEEK_END) && !fseek(f, -sizeof(line), SEEK_SET)) {
76+ while (n < max && fgets(line, sizeof(line), f) != NULL) {
77+ line[32] = '\0';
78+ list = xs_list_append(list, line);
79+ n++;
80+
81+ /* move backwards 2 entries */
82+ if (fseek(f, -sizeof(line) * 2, SEEK_SET) == -1)
83+ break;
84+ }
85+ }
86+
87+ fclose(f);
88+ }
89+
90+ return list;
91+}
92+
93+
94 d_char *_follower_fn(snac *snac, char *actor)
95 {
96 xs *md5 = xs_md5_hex(actor, strlen(actor));