Fixed HTTP-client query-string handling bug.
[doldaconnect.git] / daemon / reqstat.c
CommitLineData
07dd1674
FT
1/*
2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2007 Fredrik Tolf <fredrik@dolda2000.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <unistd.h>
23#include <errno.h>
24#include <time.h>
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29#include "transfer.h"
30#include "module.h"
31#include "log.h"
32
a77b52c8
FT
33#ifdef HAVE_XATTR
34#include <attr/xattr.h>
35#endif
36
07dd1674
FT
37struct trdata {
38 size_t startpos;
39};
40
41static char *fn = NULL;
42
43void filelog(char *format, ...)
44{
45 FILE *out;
46 va_list args;
ce93a8cb 47 char *b, *t, *p;
07dd1674
FT
48 time_t now;
49
50 if(fn == NULL)
51 return;
52 if((out = fopen(fn, "a")) == NULL) {
53 flog(LOG_WARNING, "could not open reqstat log file `%s': %s", fn, strerror(errno));
54 return;
55 }
56 now = time(NULL);
57 va_start(args, format);
58 b = vsprintf2(format, args);
59 va_end(args);
ce93a8cb
FT
60 t = ctime(&now);
61 if((p = strchr(t, '\n')) != NULL)
62 *p = 0;
713581e5 63 fprintf(out, "%s: %s\n", t, b);
07dd1674
FT
64 free(b);
65 fclose(out);
66}
67
a77b52c8 68#ifdef HAVE_XATTR
5dce4f6c
FT
69void xainc(wchar_t *file, char *an, off_t inc)
70{
71 char buf[32];
72 ssize_t al;
73 off_t val;
74 char *fn;
75
d337b23f
FT
76 if(file[0] != L'/')
77 return;
5dce4f6c
FT
78 if((fn = icswcstombs(file, NULL, NULL)) == NULL) {
79 flog(LOG_WARNING, "could not convert filename %ls into local charset: %s", file, strerror(errno));
80 return;
81 }
82 if((al = getxattr(fn, an, buf, sizeof(buf) - 1)) < 0) {
83 if(errno != ENOATTR) {
84 flog(LOG_WARNING, "could not get xattr %s on %s: %s", an, fn, strerror(errno));
85 return;
86 }
87 val = 0;
88 } else {
89 buf[al] = 0;
90 val = strtoll(buf, NULL, 10);
91 }
92 val += inc;
93 al = snprintf(buf, sizeof(buf), "%ji", (intmax_t)val);
94 if(setxattr(fn, an, buf, al, 0) < 0)
95 flog(LOG_WARNING, "could not set xattr %s on %s: %s", an, fn, strerror(errno));
96}
a77b52c8 97#endif
5dce4f6c 98
07dd1674
FT
99void request(struct transfer *transfer, struct trdata *data)
100{
101 filelog("request %ls", transfer->path);
a77b52c8 102#ifdef HAVE_XATTR
5dce4f6c
FT
103 if(confgetint("reqstat", "xa"))
104 xainc(transfer->path, "user.dc-req", 1);
a77b52c8 105#endif
07dd1674
FT
106}
107
108void start(struct transfer *transfer, struct trdata *data)
109{
ce93a8cb 110 filelog("start %ls at %zi", transfer->path, data->startpos);
a77b52c8 111#ifdef HAVE_XATTR
5dce4f6c
FT
112 if(confgetint("reqstat", "xa"))
113 xainc(transfer->path, "user.dc-started", 1);
a77b52c8 114#endif
07dd1674
FT
115}
116
117void finish(struct transfer *transfer, struct trdata *data)
118{
ce93a8cb 119 filelog("finish %ls at %zi, total %zi", transfer->path, transfer->curpos, transfer->curpos - data->startpos);
a77b52c8 120#ifdef HAVE_XATTR
5dce4f6c
FT
121 if(confgetint("reqstat", "xa"))
122 xainc(transfer->path, "user.dc-bytes", transfer->curpos - data->startpos);
a77b52c8 123#endif
07dd1674
FT
124}
125
126static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data)
127{
128 if(!wcscmp(attrib, L"state")) {
129 if(transfer->state == TRNS_MAIN)
130 data->startpos = transfer->curpos;
131 start(transfer, data);
132 } else if(!wcscmp(attrib, L"path")) {
133 if(transfer->path[0] != L'/') {
134 CBUNREG(transfer, trans_ac, data);
135 CBUNREG(transfer, trans_destroy, data);
136 free(data);
137 }
138 request(transfer, data);
139 }
140 return(0);
141}
142
143static int destroy(struct transfer *transfer, struct trdata *data)
144{
ce93a8cb
FT
145 if(transfer->curpos > data->startpos)
146 finish(transfer, data);
07dd1674
FT
147 free(data);
148 return(0);
149}
150
151static int reg(struct transfer *transfer, void *uudata)
152{
153 struct trdata *data;
154
155 if(transfer->dir != TRNSD_UP)
156 return(0);
157 data = memset(smalloc(sizeof(*data)), 0, sizeof(*data));
158 CBREG(transfer, trans_ac, (int (*)(struct transfer *, wchar_t *, void *))chattr, NULL, data);
159 CBREG(transfer, trans_destroy, (int (*)(struct transfer *, void *))destroy, NULL, data);
160 return(0);
161}
162
163static int chfile(struct configvar *var, void *uudata)
164{
165 if(fn != NULL)
166 free(fn);
56c91062 167 if(var->val.str[0] == L'\0') {
07dd1674
FT
168 fn = NULL;
169 } else {
170 if((fn = icwcstombs(var->val.str, NULL)) == NULL)
171 flog(LOG_WARNING, "could not convert reqstat filename `%ls' into local charset: %s", var->val.str, strerror(errno));
172 }
173 return(0);
174}
175
38b22d1a 176static int init(int hup)
07dd1674
FT
177{
178 if(!hup) {
179 GCBREG(newtransfercb, reg, NULL);
ca63c1a6 180 chfile(confgetvar("reqstat", "file"), NULL);
07dd1674
FT
181 CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL);
182 }
38b22d1a 183 return(0);
07dd1674
FT
184}
185
186static struct configvar myvars[] = {
187 /** The name of a file to log upload request information to. If
188 * unspecified, upload requests will not be logged. */
189 {CONF_VAR_STRING, "file", {.str = L""}},
5dce4f6c
FT
190 /** If set to true, upload statistics of files will be accumulated
191 * in counters stored in extends attributes (see attr(5)) on the
192 * files themselves. The data accumulated is the number of
193 * requests, the number of successfully started uploads and the
194 * number of uploaded bytes. */
195 {CONF_VAR_BOOL, "xa", {.num = 0}},
07dd1674
FT
196 {CONF_VAR_END}
197};
198
199static struct module me = {
200 .conf = {
201 .vars = myvars
202 },
38b22d1a 203 .init = init,
07dd1674
FT
204 .name = "reqstat"
205};
206
38b22d1a 207MODULE(me)