2 * The Submit Daemon: Tasks
4 * (c) 2007 Martin Mares <mj@ucw.cz>
9 #include "lib/fastbuf.h"
10 #include "lib/stkstring.h"
11 #include "lib/simple-lists.h"
12 #include "lib/mempool.h"
13 #include "sherlock/object.h"
23 static clist extensions;
24 static clist open_data_extensions;
27 tasks_conf_commit(void *p UNUSED)
29 // We do not do any journaling here as we do not switch config files on the fly
30 CLIST_FOR_EACH(struct task *, t, task_list)
32 clist_init(&t->parts);
35 for (uns i=1; i<=t->open_data; i++)
36 simp_append(cf_pool, &t->parts)->s = mp_printf(cf_pool, "%d", i);
37 t->extensions = &open_data_extensions;
41 simp_append(cf_pool, &t->parts)->s = t->name;
42 t->extensions = &extensions;
48 static struct cf_section task_conf = {
51 CF_STRING("Name", PTR_TO(struct task, name)),
52 CF_UNS("OpenData", PTR_TO(struct task, open_data)),
53 CF_UNS("MaxSize", PTR_TO(struct task, max_size)),
58 struct cf_section tasks_conf = {
59 CF_COMMIT(tasks_conf_commit),
61 CF_LIST("Task", &task_list, &task_conf),
62 CF_LIST("Extension", &extensions, &cf_string_list_config),
63 CF_LIST("OpenDataExt", &open_data_extensions, &cf_string_list_config),
71 CLIST_FOR_EACH(struct task *, t, task_list)
72 if (!strcasecmp(t->name, name))
78 part_exists_p(struct task *t, byte *name)
80 CLIST_FOR_EACH(simp_node *, p, t->parts)
81 if (!strcmp(p->s, name))
87 ext_exists_p(struct task *t, byte *ext)
89 CLIST_FOR_EACH(simp_node *, x, *t->extensions)
90 if (!strcmp(x->s, ext))
96 user_exists_p(byte *user)
98 byte *fn = stk_printf("solutions/%s", user);
100 return !stat(fn, &st) && S_ISDIR(st.st_mode);
104 task_load_status(struct conn *c)
106 struct fastbuf *fb = bopen_try(stk_printf("solutions/%s/status", c->user), O_RDONLY, 4096);
107 c->task_status = obj_new(c->pool);
110 obj_read(fb, c->task_status);
116 task_lock_status(struct conn *c)
118 ASSERT(!c->task_lock_fd);
119 if ((c->task_lock_fd = open(stk_printf("solutions/%s/status.lock", c->user), O_RDWR | O_CREAT | O_TRUNC, 0666)) < 0)
120 die("Cannot create task lock: %m");
123 .l_whence = SEEK_SET,
127 if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
128 die("Cannot lock status file: %m");
133 task_unlock_status(struct conn *c, uns write_back)
135 ASSERT(c->task_lock_fd);
139 struct fastbuf *fb = bopen_tmp(4096);
140 obj_write(fb, c->task_status, BUCKET_TYPE_PLAIN);
142 bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
143 byte *name = stk_printf("solutions/%s/status", c->user);
144 if (rename(fb->name, name) < 0)
145 die("Unable to rename %s to %s: %m", fb->name, name);
151 .l_whence = SEEK_SET,
155 if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
156 die("Cannot unlock status file: %m");
161 task_status_find_task(struct conn *c, struct task *t, uns create)
163 for (struct oattr *a = obj_find_attr(c->task_status, 'T' + OBJ_ATTR_SON); a; a=a->same)
165 struct odes *o = a->son;
166 byte *name = obj_find_aval(o, 'T');
168 if (!strcmp(name, t->name))
173 struct odes *o = obj_add_son(c->task_status, 'T' + OBJ_ATTR_SON);
174 obj_set_attr(o, 'T', t->name);
179 task_status_find_part(struct odes *to, byte *part, uns create)
181 for (struct oattr *a = obj_find_attr(to, 'P' + OBJ_ATTR_SON); a; a=a->same)
183 struct odes *o = a->son;
184 byte *name = obj_find_aval(o, 'P');
186 if (!strcmp(name, part))
191 struct odes *o = obj_add_son(to, 'P' + OBJ_ATTR_SON);
192 obj_set_attr(o, 'P', part);
196 void task_submit_part(byte *user, byte *task, byte *part, byte *ext, uns version UNUSED, struct fastbuf *fb)
198 byte *dir = stk_printf("solutions/%s/%s", user, task);
199 byte *name = stk_printf("%s/%s.%s", dir, part, ext);
202 if (stat(dir, &st) < 0 && errno == ENOENT && mkdir(dir, 0777) < 0)
203 die("Cannot create %s: %m", dir);
205 bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
206 if (rename(fb->name, name) < 0)
207 die("Cannot rename %s to %s: %m", fb->name, name);
210 void task_delete_part(byte *user, byte *task, byte *part, byte *ext, uns version UNUSED)
212 byte *dir = stk_printf("solutions/%s/%s", user, task);
213 byte *name = stk_printf("%s/%s.%s", dir, part, ext);
214 if (unlink(name) < 0)
215 msg(L_ERROR, "Cannot delete %s: %m", name);