1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>
#include "jobs.h"
#include "util.h"
static void output_free(struct output *output);
static int job_output_insert(struct job *j, char *name, char *store_path);
static int job_read_inputdrvs(struct job *job, cJSON *input_drvs);
static int job_read_outputs(struct job *job, cJSON *outputs);
static void output_free(struct output *output)
{
if (output == NULL)
return;
free(output->name);
free(output->store_path);
free(output);
}
static int job_output_insert(struct job *j, char *name, char *store_path)
{
struct output *o;
o = malloc(sizeof(*o));
if (o == NULL) {
print_err("%s", strerror(errno));
return -errno;
}
o->name = name;
o->store_path = store_path;
LIST_INSERT_HEAD(&j->outputs, o, dlist);
return 0;
}
static int job_read_inputdrvs(struct job *job, cJSON *input_drvs)
{
cJSON *array;
int ret = 0;
struct job *dep_job = NULL;
char *drv_path = NULL;
char *out_name = NULL;
for (cJSON *i = input_drvs; i != NULL; i = i->next) {
array = cJSON_GetObjectItemCaseSensitive(i, i->string);
if (!cJSON_IsArray(array)) {
ret = -EPERM;
goto out_free;
}
drv_path = strdup(i->string);
if (drv_path == NULL) {
ret = -EPERM;
goto out_free;
}
ret = job_new(&dep_job, NULL, drv_path);
if (ret < 0) {
ret = -EPERM;
goto out_free;
}
for (; array != NULL; array = array->next) {
out_name = strdup(array->string);
if (out_name == NULL) {
ret = -EPERM;
goto out_free;
}
ret = job_output_insert(dep_job, out_name, NULL);
if (ret < 0) {
job_free(dep_job);
goto out_free;
}
}
drv_path = NULL;
out_name = NULL;
dep_job = NULL;
LIST_INSERT_HEAD(&job->deps, dep_job, dlist);
}
out_free:
if (ret < 0) {
job_free(dep_job);
free(drv_path);
free(out_name);
}
return ret;
}
static int job_read_outputs(struct job *job, cJSON *outputs)
{
int ret = 0;
char *out_name = NULL;
char *out_path = NULL;
for (cJSON *i = outputs; i != NULL; i = i->next) {
out_name = strdup(i->string);
if (out_name == NULL) {
ret = -EPERM;
goto out_free;
}
out_path = strdup(i->valuestring);
if (out_path == NULL) {
ret = -EPERM;
goto out_free;
}
ret = job_output_insert(job, out_name, out_path);
if (ret < 0) {
ret = -EPERM;
goto out_free;
}
out_path = NULL;
out_name = NULL;
}
out_free:
if (ret < 0) {
free(out_name);
free(out_path);
}
return 0;
}
int job_read(FILE *stream, struct job **job)
{
cJSON *temp;
int ret = 0;
cJSON *root = NULL;
char *name = NULL;
char *drv_path = NULL;
ret = json_streaming_read(stream, &root);
if (ret < 0)
return ret;
temp = cJSON_GetObjectItemCaseSensitive(root, "name");
if (!cJSON_IsString(temp)) {
ret = -EPERM;
goto out_free;
}
name = strdup(temp->valuestring);
if (name == NULL) {
ret = -EPERM;
goto out_free;
}
temp = cJSON_GetObjectItemCaseSensitive(root, "drvPath");
if (!cJSON_IsString(temp)) {
ret = -EPERM;
goto out_free;
}
drv_path = strdup(temp->valuestring);
if (drv_path == NULL) {
ret = -EPERM;
goto out_free;
}
ret = job_new(job, name, drv_path);
if (ret < 0)
goto out_free;
temp = cJSON_GetObjectItemCaseSensitive(root, "inputDrvs");
if (!cJSON_IsObject(temp)) {
ret = -EPERM;
goto out_free;
}
ret = job_read_inputdrvs(*job, temp);
if (ret < 0)
goto out_free;
temp = cJSON_GetObjectItemCaseSensitive(root, "outputs");
if (!cJSON_IsObject(temp)) {
ret = -EPERM;
goto out_free;
}
ret = job_read_outputs(*job, temp);
if (ret < 0)
goto out_free;
out_free:
cJSON_Delete(root);
if (ret < 0) {
print_err("%s", "Invalid JSON");
free(name);
free(drv_path);
}
return ret;
}
void job_free(struct job *job)
{
struct job *j;
struct output *o;
if (job == NULL)
return;
free(job->name);
free(job->drv_path);
LIST_FOREACH (o, &job->outputs, dlist)
output_free(o);
LIST_FOREACH (j, &job->deps, dlist)
job_free(j);
free(job);
}
int job_new(struct job **j, char *name, char *drv_path)
{
struct job *job;
job = malloc(sizeof(*job));
if (job == NULL) {
print_err("%s", strerror(errno));
return -errno;
}
job->name = name;
job->drv_path = drv_path;
LIST_INIT(&job->deps);
LIST_INIT(&job->outputs);
*j = job;
return 0;
}
int jobs_init(FILE **stream)
{
int ret;
/* TODO: proproperly handle args */
char *const args[] = {
"--flake",
"github:sinanmohd/evanix#packages.x86_64-linux.evanix",
};
/* the package is wrapProgram-ed with nix-eval-jobs */
ret = vpopen(stream, "nix-eval-jobs", args);
return ret;
}
|