Added XATTR logging facility to the reqstat module.
[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>
5dce4f6c 25#include <attr/xattr.h>
07dd1674
FT
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include "transfer.h"
31#include "module.h"
32#include "log.h"
33
34struct trdata {
35 size_t startpos;
36};
37
38static char *fn = NULL;
39
40void filelog(char *format, ...)
41{
42 FILE *out;
43 va_list args;
ce93a8cb 44 char *b, *t, *p;
07dd1674
FT
45 time_t now;
46
47 if(fn == NULL)
48 return;
49 if((out = fopen(fn, "a")) == NULL) {
50 flog(LOG_WARNING, "could not open reqstat log file `%s': %s", fn, strerror(errno));
51 return;
52 }
53 now = time(NULL);
54 va_start(args, format);
55 b = vsprintf2(format, args);
56 va_end(args);
ce93a8cb
FT
57 t = ctime(&now);
58 if((p = strchr(t, '\n')) != NULL)
59 *p = 0;
713581e5 60 fprintf(out, "%s: %s\n", t, b);
07dd1674
FT
61 free(b);
62 fclose(out);
63}
64
5dce4f6c
FT
65void xainc(wchar_t *file, char *an, off_t inc)
66{
67 char buf[32];
68 ssize_t al;
69 off_t val;
70 char *fn;
71
72 if((fn = icswcstombs(file, NULL, NULL)) == NULL) {
73 flog(LOG_WARNING, "could not convert filename %ls into local charset: %s", file, strerror(errno));
74 return;
75 }
76 if((al = getxattr(fn, an, buf, sizeof(buf) - 1)) < 0) {
77 if(errno != ENOATTR) {
78 flog(LOG_WARNING, "could not get xattr %s on %s: %s", an, fn, strerror(errno));
79 return;
80 }
81 val = 0;
82 } else {
83 buf[al] = 0;
84 val = strtoll(buf, NULL, 10);
85 }
86 val += inc;
87 al = snprintf(buf, sizeof(buf), "%ji", (intmax_t)val);
88 if(setxattr(fn, an, buf, al, 0) < 0)
89 flog(LOG_WARNING, "could not set xattr %s on %s: %s", an, fn, strerror(errno));
90}
91
07dd1674
FT
92void request(struct transfer *transfer, struct trdata *data)
93{
94 filelog("request %ls", transfer->path);
5dce4f6c
FT
95 if(confgetint("reqstat", "xa"))
96 xainc(transfer->path, "user.dc-req", 1);
07dd1674
FT
97}
98
99void start(struct transfer *transfer, struct trdata *data)
100{
ce93a8cb 101 filelog("start %ls at %zi", transfer->path, data->startpos);
5dce4f6c
FT
102 if(confgetint("reqstat", "xa"))
103 xainc(transfer->path, "user.dc-started", 1);
07dd1674
FT
104}
105
106void finish(struct transfer *transfer, struct trdata *data)
107{
ce93a8cb 108 filelog("finish %ls at %zi, total %zi", transfer->path, transfer->curpos, transfer->curpos - data->startpos);
5dce4f6c
FT
109 if(confgetint("reqstat", "xa"))
110 xainc(transfer->path, "user.dc-bytes", transfer->curpos - data->startpos);
07dd1674
FT
111}
112
113static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data)
114{
115 if(!wcscmp(attrib, L"state")) {
116 if(transfer->state == TRNS_MAIN)
117 data->startpos = transfer->curpos;
118 start(transfer, data);
119 } else if(!wcscmp(attrib, L"path")) {
120 if(transfer->path[0] != L'/') {
121 CBUNREG(transfer, trans_ac, data);
122 CBUNREG(transfer, trans_destroy, data);
123 free(data);
124 }
125 request(transfer, data);
126 }
127 return(0);
128}
129
130static int destroy(struct transfer *transfer, struct trdata *data)
131{
ce93a8cb
FT
132 if(transfer->curpos > data->startpos)
133 finish(transfer, data);
07dd1674
FT
134 free(data);
135 return(0);
136}
137
138static int reg(struct transfer *transfer, void *uudata)
139{
140 struct trdata *data;
141
142 if(transfer->dir != TRNSD_UP)
143 return(0);
144 data = memset(smalloc(sizeof(*data)), 0, sizeof(*data));
145 CBREG(transfer, trans_ac, (int (*)(struct transfer *, wchar_t *, void *))chattr, NULL, data);
146 CBREG(transfer, trans_destroy, (int (*)(struct transfer *, void *))destroy, NULL, data);
147 return(0);
148}
149
150static int chfile(struct configvar *var, void *uudata)
151{
152 if(fn != NULL)
153 free(fn);
56c91062 154 if(var->val.str[0] == L'\0') {
07dd1674
FT
155 fn = NULL;
156 } else {
157 if((fn = icwcstombs(var->val.str, NULL)) == NULL)
158 flog(LOG_WARNING, "could not convert reqstat filename `%ls' into local charset: %s", var->val.str, strerror(errno));
159 }
160 return(0);
161}
162
38b22d1a 163static int init(int hup)
07dd1674
FT
164{
165 if(!hup) {
166 GCBREG(newtransfercb, reg, NULL);
ca63c1a6 167 chfile(confgetvar("reqstat", "file"), NULL);
07dd1674
FT
168 CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL);
169 }
38b22d1a 170 return(0);
07dd1674
FT
171}
172
173static struct configvar myvars[] = {
174 /** The name of a file to log upload request information to. If
175 * unspecified, upload requests will not be logged. */
176 {CONF_VAR_STRING, "file", {.str = L""}},
5dce4f6c
FT
177 /** If set to true, upload statistics of files will be accumulated
178 * in counters stored in extends attributes (see attr(5)) on the
179 * files themselves. The data accumulated is the number of
180 * requests, the number of successfully started uploads and the
181 * number of uploaded bytes. */
182 {CONF_VAR_BOOL, "xa", {.num = 0}},
07dd1674
FT
183 {CONF_VAR_END}
184};
185
186static struct module me = {
187 .conf = {
188 .vars = myvars
189 },
38b22d1a 190 .init = init,
07dd1674
FT
191 .name = "reqstat"
192};
193
38b22d1a 194MODULE(me)