]> mj.ucw.cz Git - moe.git/blob - submit/tasks.c
4a6930319799983dae873420da09c8ed5d879174
[moe.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 "sherlock/object.h"
12
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16
17 #include "submitd.h"
18
19 clist task_list;
20
21 static byte *
22 tasks_conf_init(void)
23 {
24   clist_init(&task_list);
25   return NULL;
26 }
27
28 static struct cf_section task_conf = {
29   CF_TYPE(struct task),
30   CF_ITEMS {
31     CF_STRING("Name", PTR_TO(struct task, name)),
32     CF_END
33   }
34 };
35
36 struct cf_section tasks_conf = {
37   CF_INIT(tasks_conf_init),
38   CF_ITEMS {
39     CF_LIST("Task", &task_list, &task_conf),
40     CF_END
41   }
42 };
43
44 struct task *
45 task_find(byte *name)
46 {
47   CLIST_FOR_EACH(struct task *, t, task_list)
48     if (!strcasecmp(t->name, name))
49       return t;
50   return NULL;
51 }
52
53 int
54 user_exists_p(byte *user)
55 {
56   byte *fn = stk_printf("solutions/%s/status", user);
57   struct stat st;
58   return !stat(fn, &st) && S_ISREG(st.st_mode);
59 }
60
61 void
62 task_lock_status(struct conn *c)
63 {
64   ASSERT(!c->task_lock_fd);
65   if ((c->task_lock_fd = open(stk_printf("solutions/%s/status.lock", c->user), O_RDWR | O_CREAT | O_TRUNC, 0666)) < 0)
66     die("Cannot create task lock: %m");
67   struct flock fl = {
68     .l_type = F_WRLCK,
69     .l_whence = SEEK_SET,
70     .l_start = 0,
71     .l_len = 1
72   };
73   if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
74     die("Cannot lock status file: %m");
75
76   struct fastbuf *fb = bopen_try(stk_printf("solutions/%s/status", c->user), O_RDONLY, 4096);
77   c->task_status = obj_new(c->pool);
78   if (fb)
79     {
80       obj_read(fb, c->task_status);
81       bclose(fb);
82     }
83 }
84
85 void
86 task_unlock_status(struct conn *c, uns write_back)
87 {
88   ASSERT(c->task_lock_fd);
89   ASSERT(c->task_status);
90
91   if (write_back)
92     {
93       struct fastbuf *fb = bopen_tmp(4096);
94       obj_write(fb, c->task_status, BUCKET_TYPE_PLAIN);
95       brewind(fb);
96       bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
97       byte *name = stk_printf("solutions/%s/status", c->user);
98       if (rename(fb->name, name) < 0)
99         die("Unable to rename %s to %s: %m", fb->name, name);
100       bclose(fb);
101     }
102
103   struct flock fl = {
104     .l_type = F_UNLCK,
105     .l_whence = SEEK_SET,
106     .l_start = 0,
107     .l_len = 1
108   };
109   if (fcntl(c->task_lock_fd, F_SETLKW, &fl) < 0)
110     die("Cannot unlock status file: %m");
111   c->task_lock_fd = 0;
112   c->task_status = NULL;
113 }
114
115 struct odes *
116 task_status_find_task(struct conn *c, struct task *t)
117 {
118   for (struct oattr *a = obj_find_attr(c->task_status, 'T' + OBJ_ATTR_SON); a; a=a->same)
119     {
120       struct odes *o = a->son;
121       byte *name = obj_find_aval(o, 'T');
122       if (!strcmp(name, t->name))
123         return o;
124     }
125   struct odes *o = obj_add_son(c->task_status, 'T' + OBJ_ATTR_SON);
126   obj_set_attr(o, 'T', t->name);
127   return o;
128 }
129
130 void
131 task_submit(struct conn *c, struct task *t, struct fastbuf *fb, byte *filename)
132 {
133   byte *dir = stk_printf("solutions/%s/%s", c->user, t->name);
134   byte *name = stk_printf("%s/%s", dir, filename);
135 }