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)),
57 struct cf_section tasks_conf = {
58 CF_COMMIT(tasks_conf_commit),
60 CF_LIST("Task", &task_list, &task_conf),
61 CF_LIST("Extension", &extensions, &cf_string_list_config),
62 CF_LIST("OpenDataExt", &open_data_extensions, &cf_string_list_config),
70 CLIST_FOR_EACH(struct task *, t, task_list)
71 if (!strcasecmp(t->name, name))
77 part_exists_p(struct task *t, byte *name)
79 CLIST_FOR_EACH(simp_node *, p, t->parts)
80 if (!strcmp(p->s, name))
86 ext_exists_p(struct task *t, byte *ext)
88 CLIST_FOR_EACH(simp_node *, x, *t->extensions)
89 if (!strcmp(x->s, ext))
95 user_exists_p(byte *user)
97 byte *fn = stk_printf("solutions/%s", user);
99 return !stat(fn, &st) && S_ISDIR(st.st_mode);
103 task_load_status(struct conn *c)
105 struct fastbuf *fb = bopen_try(stk_printf("solutions/%s/status", c->user), O_RDONLY, 4096);
106 c->task_status = obj_new(c->pool);
109 obj_read(fb, c->task_status);
115 task_lock_status(struct conn *c)
117 ASSERT(!c->task_lock_fd);
118 if ((c->task_lock_fd = open(stk_printf("solutions/%s/status.lock", c->user), O_RDWR | O_CREAT | O_TRUNC, 0666)) < 0)
119 die("Cannot create task lock: %m");
122 .l_whence = SEEK_SET,
126 if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
127 die("Cannot lock status file: %m");
132 task_unlock_status(struct conn *c, uns write_back)
134 ASSERT(c->task_lock_fd);
138 struct fastbuf *fb = bopen_tmp(4096);
139 obj_write(fb, c->task_status, BUCKET_TYPE_PLAIN);
141 bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
142 byte *name = stk_printf("solutions/%s/status", c->user);
143 if (rename(fb->name, name) < 0)
144 die("Unable to rename %s to %s: %m", fb->name, name);
150 .l_whence = SEEK_SET,
154 if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
155 die("Cannot unlock status file: %m");
160 task_status_find_task(struct conn *c, struct task *t, uns create)
162 for (struct oattr *a = obj_find_attr(c->task_status, 'T' + OBJ_ATTR_SON); a; a=a->same)
164 struct odes *o = a->son;
165 byte *name = obj_find_aval(o, 'T');
167 if (!strcmp(name, t->name))
172 struct odes *o = obj_add_son(c->task_status, 'T' + OBJ_ATTR_SON);
173 obj_set_attr(o, 'T', t->name);
178 task_status_find_part(struct odes *to, byte *part, uns create)
180 for (struct oattr *a = obj_find_attr(to, 'P' + OBJ_ATTR_SON); a; a=a->same)
182 struct odes *o = a->son;
183 byte *name = obj_find_aval(o, 'P');
185 if (!strcmp(name, part))
190 struct odes *o = obj_add_son(to, 'P' + OBJ_ATTR_SON);
191 obj_set_attr(o, 'P', part);
195 void task_submit_part(byte *user, byte *task, byte *part, byte *ext, uns version UNUSED, struct fastbuf *fb)
197 byte *dir = stk_printf("solutions/%s/%s", user, task);
198 byte *name = stk_printf("%s/%s.%s", dir, part, ext);
201 if (stat(dir, &st) < 0 && errno == ENOENT && mkdir(dir, 0777) < 0)
202 die("Cannot create %s: %m", dir);
204 bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
205 if (rename(fb->name, name) < 0)
206 die("Cannot rename %s to %s: %m", fb->name, name);
209 void task_delete_part(byte *user, byte *task, byte *part, byte *ext, uns version UNUSED)
211 byte *dir = stk_printf("solutions/%s/%s", user, task);
212 byte *name = stk_printf("%s/%s.%s", dir, part, ext);
213 if (unlink(name) < 0)
214 log(L_ERROR, "Cannot delete %s: %m", name);