]> mj.ucw.cz Git - eval.git/blob - submit/tasks.c
1ca515a86588e1711be1ab97506417992c5036ae
[eval.git] / submit / tasks.c
1 /*
2  *  The Submit Daemon: Tasks
3  *
4  *  (c) 2007 Martin Mares <mj@ucw.cz>
5  */
6
7 #include "lib/lib.h"
8 #include "lib/conf.h"
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"
14
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <errno.h>
19
20 #include "submitd.h"
21
22 clist task_list;
23 static clist extensions;
24 static clist open_data_extensions;
25
26 static byte *
27 tasks_conf_commit(void *p UNUSED)
28 {
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)
31     {
32       clist_init(&t->parts);
33       if (t->open_data)
34         {
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;
38         }
39       else
40         {
41           simp_append(cf_pool, &t->parts)->s = t->name;
42           t->extensions = &extensions;
43         }
44     }
45   return NULL;
46 }
47
48 static struct cf_section task_conf = {
49   CF_TYPE(struct task),
50   CF_ITEMS {
51     CF_STRING("Name", PTR_TO(struct task, name)),
52     CF_UNS("OpenData", PTR_TO(struct task, open_data)),
53     CF_END
54   }
55 };
56
57 struct cf_section tasks_conf = {
58   CF_COMMIT(tasks_conf_commit),
59   CF_ITEMS {
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),
63     CF_END
64   }
65 };
66
67 struct task *
68 task_find(byte *name)
69 {
70   CLIST_FOR_EACH(struct task *, t, task_list)
71     if (!strcasecmp(t->name, name))
72       return t;
73   return NULL;
74 }
75
76 int
77 part_exists_p(struct task *t, byte *name)
78 {
79   CLIST_FOR_EACH(simp_node *, p, t->parts)
80     if (!strcmp(p->s, name))
81       return 1;
82   return 0;
83 }
84
85 int
86 ext_exists_p(struct task *t, byte *ext)
87 {
88   CLIST_FOR_EACH(simp_node *, x, *t->extensions)
89     if (!strcmp(x->s, ext))
90       return 1;
91   return 0;
92 }
93
94 int
95 user_exists_p(byte *user)
96 {
97   byte *fn = stk_printf("solutions/%s", user);
98   struct stat st;
99   return !stat(fn, &st) && S_ISDIR(st.st_mode);
100 }
101
102 void
103 task_load_status(struct conn *c)
104 {
105   struct fastbuf *fb = bopen_try(stk_printf("solutions/%s/status", c->user), O_RDONLY, 4096);
106   c->task_status = obj_new(c->pool);
107   if (fb)
108     {
109       obj_read(fb, c->task_status);
110       bclose(fb);
111     }
112 }
113
114 void
115 task_lock_status(struct conn *c)
116 {
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");
120   struct flock fl = {
121     .l_type = F_WRLCK,
122     .l_whence = SEEK_SET,
123     .l_start = 0,
124     .l_len = 1
125   };
126   if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
127     die("Cannot lock status file: %m");
128   task_load_status(c);
129 }
130
131 void
132 task_unlock_status(struct conn *c, uns write_back)
133 {
134   ASSERT(c->task_lock_fd);
135
136   if (write_back)
137     {
138       struct fastbuf *fb = bopen_tmp(4096);
139       obj_write(fb, c->task_status, BUCKET_TYPE_PLAIN);
140       brewind(fb);
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);
145       bclose(fb);
146     }
147
148   struct flock fl = {
149     .l_type = F_UNLCK,
150     .l_whence = SEEK_SET,
151     .l_start = 0,
152     .l_len = 1
153   };
154   if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
155     die("Cannot unlock status file: %m");
156   c->task_lock_fd = 0;
157 }
158
159 struct odes *
160 task_status_find_task(struct conn *c, struct task *t, uns create)
161 {
162   for (struct oattr *a = obj_find_attr(c->task_status, 'T' + OBJ_ATTR_SON); a; a=a->same)
163     {
164       struct odes *o = a->son;
165       byte *name = obj_find_aval(o, 'T');
166       ASSERT(name);
167       if (!strcmp(name, t->name))
168         return o;
169     }
170   if (!create)
171     return NULL;
172   struct odes *o = obj_add_son(c->task_status, 'T' + OBJ_ATTR_SON);
173   obj_set_attr(o, 'T', t->name);
174   return o;
175 }
176
177 struct odes *
178 task_status_find_part(struct odes *to, byte *part, uns create)
179 {
180   for (struct oattr *a = obj_find_attr(to, 'P' + OBJ_ATTR_SON); a; a=a->same)
181     {
182       struct odes *o = a->son;
183       byte *name = obj_find_aval(o, 'P');
184       ASSERT(name);
185       if (!strcmp(name, part))
186         return o;
187     }
188   if (!create)
189     return NULL;
190   struct odes *o = obj_add_son(to, 'P' + OBJ_ATTR_SON);
191   obj_set_attr(o, 'P', part);
192   return o;
193 }
194
195 void task_submit_part(byte *user, byte *task, byte *part, byte *ext, uns version UNUSED, struct fastbuf *fb)
196 {
197   byte *dir = stk_printf("solutions/%s/%s", user, task);
198   byte *name = stk_printf("%s/%s.%s", dir, part, ext);
199
200   struct stat st;
201   if (stat(dir, &st) < 0 && errno == ENOENT && mkdir(dir, 0777) < 0)
202     die("Cannot create %s: %m", dir);
203
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);
207 }
208
209 void task_delete_part(byte *user, byte *task, byte *part, byte *ext, uns version UNUSED)
210 {
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);
215 }