]> pd.if.org Git - zpackage/blob - lib/foreach_path.c
remove stray debug fprintf
[zpackage] / lib / foreach_path.c
1 #define _POSIX_C_SOURCE 200809L
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/wait.h>
6
7 #include <string.h>
8
9 #include "sqlite3.h"
10 #include "zpm.h"
11
12 void zpm_file_init(struct zpm_file *file) {
13         file->package = 0;
14         file->version = 0;
15         file->release = 0;
16         file->path = 0;
17         file->owner = 0;
18         file->group = 0;
19         file->target = 0;
20         file->status = 0;
21         file->hash[0] = 0;
22         file->confhash[0] = 0;
23         file->configuration = 0;
24         file->type = 0;
25         file->mode = 0;
26         file->device = 0;
27 }
28
29 void zpm_file_free(struct zpm_file *file) {
30         free(file->package);
31         free(file->version);
32         free(file->path);
33         free(file->owner);
34         free(file->group);
35         free(file->target);
36         free(file->status);
37         zpm_file_init(file);
38 }
39
40 #define getstring(x,n) ((const char *)sqlite3_column_text(x,n))
41
42 int zpm_foreach_path_ds(struct zpm *zpm, char *pkgid, char *where,
43 int (*callback)(struct zpm *, struct zpm_file *, void *),
44 void *data) {
45         char *sql;
46         sqlite3_str *s;
47         sqlite3_stmt *st;
48         struct zpm_file file = { 0 };
49         int rv;
50
51         zpm_file_init(&file);
52
53         if (!zpm || zpm->error || !callback) return 0;
54
55         s = sqlite3_str_new(zpm->db);
56         sqlite3_str_appendall(s, "select package,version,release,path,filetype,mode,username,groupname,configuration,target,device,hash,confhash,status from packagefiles_status where ");
57
58         if (where) {
59                 sqlite3_str_appendf(s, "%s", where);
60         } else {
61                 sqlite3_str_appendall(s, "true");
62         }
63
64         if (pkgid) {
65                 sqlite3_str_appendf(s, " and printf('%%s-%%s-%%s', package, version, release) = %Q", pkgid);
66         }
67
68         sql = sqlite3_str_value(s);
69         if (!sql) {
70                 sqlite3_str_finish(s);
71                 zpm->error = 1;
72                 return 0;
73         }
74
75         st = zpm_dbquery(zpm, sql);
76         sqlite3_str_finish(s);
77
78         while (sqlite3_step(st) == SQLITE_ROW) {
79                 const char *val;
80
81                 val = getstring(st, 0);
82                 file.package = val ? strdup(val) : 0;
83
84                 val = getstring(st, 1);
85                 file.version = val ? strdup(val) : 0;
86
87                 file.release = sqlite3_column_int(st, 2);
88
89                 val = getstring(st, 3);
90                 file.path = val ? strdup(val) : 0;
91
92                 val = getstring(st, 4);
93                 file.type = val ? *val : 0;
94
95                 val = getstring(st, 5);
96                 file.mode = val ? strtol(val, NULL, 8) : 0;
97
98                 val = getstring(st, 6);
99                 file.owner = val ? strdup(val) : 0;
100
101                 val = getstring(st, 7);
102                 file.group = val ? strdup(val) : 0;
103
104                 file.configuration  = sqlite3_column_int(st, 8);
105
106                 val = getstring(st, 9);
107                 file.target = val ? strdup(val) : 0;
108
109                 val = getstring(st, 10);
110                 file.device = val ? strtol(val, NULL, 10) : 0;
111
112                 val = getstring(st, 11);
113                 if (val) {
114                         strncpy(file.hash, val, ZPM_HASH_STRLEN);
115                         file.hash[ZPM_HASH_STRLEN] = 0;
116                 }
117
118                 val = getstring(st, 12);
119                 if (val) {
120                         strncpy(file.hash, val, ZPM_HASH_STRLEN);
121                         file.confhash[ZPM_HASH_STRLEN] = 0;
122                 }
123
124                 val = getstring(st, 13);
125                 file.status = val ? strdup(val) : 0;
126
127                 rv = callback(zpm, &file, data);
128                 zpm_file_free(&file);
129
130                 if (rv != 0) {
131                         break;
132                 }
133         }
134         
135         zpm->dbresult = sqlite3_finalize(st);
136
137         if (rv != 0) {
138                 zpm->error = rv;
139                 return 0;
140         }
141
142         if (zpm->dbresult != SQLITE_OK) {
143                 free(zpm->dberrmsg);
144                 zpm->dberrmsg = strdup(sqlite3_errmsg(zpm->db));
145                 return 0;
146         }
147
148         zpm->error = 0;
149
150         return 1;
151 }
152
153 int zpm_foreach_path(struct zpm *zpm, char *pkgid, char *where,
154 int (*callback)(void *f, int ncols, char **vals, char **cols),
155 void *data, char **errmsg) {
156         char *sql;
157         sqlite3_str *s;
158
159         if (!zpm || zpm->error || !callback) return 0;
160
161         s = sqlite3_str_new(zpm->db);
162         sqlite3_str_appendall(s, "select * from packagefiles_status where ");
163
164         if (where) {
165                 sqlite3_str_appendf(s, "%s", where);
166         } else {
167                 sqlite3_str_appendall(s, "true");
168         }
169
170         if (pkgid) {
171                 sqlite3_str_appendf(s, " and printf('%%s-%%s-%%s', package, version, release) = %Q", pkgid);
172         }
173
174         sql = sqlite3_str_value(s);
175         if (!sql) {
176                 sqlite3_str_finish(s);
177                 zpm->error = 1;
178                 return 0;
179         }
180
181         zpm_exec(zpm, sql, callback, data, errmsg);
182         sqlite3_str_finish(s);
183         if (*errmsg) {
184                 fprintf(stderr, "errmsg: %s\n", *errmsg);
185                 zpm->error = 2;
186                 return 0;
187         }
188
189         return 1;
190 }
191
192 int zpm_foreach_package(struct zpm *zpm, char *where,
193 int (*callback)(void *cbdata, int ncols, char **vals, char **cols),
194 void *data, char **errmsg) {
195         char *sql;
196         sqlite3_str *s;
197
198         if (!zpm || zpm->error || !callback) return 0;
199
200         s = sqlite3_str_new(zpm->db);
201         sqlite3_str_appendall(s, "select * from packages_pkgid");
202
203         if (where) {
204                 sqlite3_str_appendf(s, " where %s", where);
205         }
206
207         sql = sqlite3_str_value(s);
208         if (!sql) {
209                 sqlite3_str_finish(s);
210                 zpm->error = 1;
211                 return 0;
212         }
213
214         zpm_exec(zpm, sql, callback, data, errmsg);
215         sqlite3_str_finish(s);
216         if (*errmsg) {
217                 fprintf(stderr, "errmsg: %s\n", *errmsg);
218                 zpm->error = 2;
219                 return 0;
220         }
221
222         return 1;
223 }