Check for xattr support in configure script.
[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
76 if((fn = icswcstombs(file, NULL, NULL)) == NULL) {
77 flog(LOG_WARNING, "could not convert filename %ls into local charset: %s", file, strerror(errno));
78 return;
79 }
80 if((al = getxattr(fn, an, buf, sizeof(buf) - 1)) < 0) {
81 if(errno != ENOATTR) {
82 flog(LOG_WARNING, "could not get xattr %s on %s: %s", an, fn, strerror(errno));
83 return;
84 }
85 val = 0;
86 } else {
87 buf[al] = 0;
88 val = strtoll(buf, NULL, 10);
89 }
90 val += inc;
91 al = snprintf(buf, sizeof(buf), "%ji", (intmax_t)val);
92 if(setxattr(fn, an, buf, al, 0) < 0)
93 flog(LOG_WARNING, "could not set xattr %s on %s: %s", an, fn, strerror(errno));
94}
a77b52c8 95#endif
5dce4f6c 96
07dd1674
FT
97void request(struct transfer *transfer, struct trdata *data)
98{
99 filelog("request %ls", transfer->path);
a77b52c8 100#ifdef HAVE_XATTR
5dce4f6c
FT
101 if(confgetint("reqstat", "xa"))
102 xainc(transfer->path, "user.dc-req", 1);
a77b52c8 103#endif
07dd1674
FT
104}
105
106void start(struct transfer *transfer, struct trdata *data)
107{
ce93a8cb 108 filelog("start %ls at %zi", transfer->path, data->startpos);
a77b52c8 109#ifdef HAVE_XATTR
5dce4f6c
FT
110 if(confgetint("reqstat", "xa"))
111 xainc(transfer->path, "user.dc-started", 1);
a77b52c8 112#endif
07dd1674
FT
113}
114
115void finish(struct transfer *transfer, struct trdata *data)
116{
ce93a8cb 117 filelog("finish %ls at %zi, total %zi", transfer->path, transfer->curpos, transfer->curpos - data->startpos);
a77b52c8 118#ifdef HAVE_XATTR
5dce4f6c
FT
119 if(confgetint("reqstat", "xa"))
120 xainc(transfer->path, "user.dc-bytes", transfer->curpos - data->startpos);
a77b52c8 121#endif
07dd1674
FT
122}
123
124static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data)
125{
126 if(!wcscmp(attrib, L"state")) {
127 if(transfer->state == TRNS_MAIN)
128 data->startpos = transfer->curpos;
129 start(transfer, data);
130 } else if(!wcscmp(attrib, L"path")) {
131 if(transfer->path[0] != L'/') {
132 CBUNREG(transfer, trans_ac, data);
133 CBUNREG(transfer, trans_destroy, data);
134 free(data);
135 }
136 request(transfer, data);
137 }
138 return(0);
139}
140
141static int destroy(struct transfer *transfer, struct trdata *data)
142{
ce93a8cb
FT
143 if(transfer->curpos > data->startpos)
144 finish(transfer, data);
07dd1674
FT
145 free(data);
146 return(0);
147}
148
149static int reg(struct transfer *transfer, void *uudata)
150{
151 struct trdata *data;
152
153 if(transfer->dir != TRNSD_UP)
154 return(0);
155 data = memset(smalloc(sizeof(*data)), 0, sizeof(*data));
156 CBREG(transfer, trans_ac, (int (*)(struct transfer *, wchar_t *, void *))chattr, NULL, data);
157 CBREG(transfer, trans_destroy, (int (*)(struct transfer *, void *))destroy, NULL, data);
158 return(0);
159}
160
161static int chfile(struct configvar *var, void *uudata)
162{
163 if(fn != NULL)
164 free(fn);
56c91062 165 if(var->val.str[0] == L'\0') {
07dd1674
FT
166 fn = NULL;
167 } else {
168 if((fn = icwcstombs(var->val.str, NULL)) == NULL)
169 flog(LOG_WARNING, "could not convert reqstat filename `%ls' into local charset: %s", var->val.str, strerror(errno));
170 }
171 return(0);
172}
173
38b22d1a 174static int init(int hup)
07dd1674
FT
175{
176 if(!hup) {
177 GCBREG(newtransfercb, reg, NULL);
ca63c1a6 178 chfile(confgetvar("reqstat", "file"), NULL);
07dd1674
FT
179 CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL);
180 }
38b22d1a 181 return(0);
07dd1674
FT
182}
183
184static struct configvar myvars[] = {
185 /** The name of a file to log upload request information to. If
186 * unspecified, upload requests will not be logged. */
187 {CONF_VAR_STRING, "file", {.str = L""}},
5dce4f6c
FT
188 /** If set to true, upload statistics of files will be accumulated
189 * in counters stored in extends attributes (see attr(5)) on the
190 * files themselves. The data accumulated is the number of
191 * requests, the number of successfully started uploads and the
192 * number of uploaded bytes. */
193 {CONF_VAR_BOOL, "xa", {.num = 0}},
07dd1674
FT
194 {CONF_VAR_END}
195};
196
197static struct module me = {
198 .conf = {
199 .vars = myvars
200 },
38b22d1a 201 .init = init,
07dd1674
FT
202 .name = "reqstat"
203};
204
38b22d1a 205MODULE(me)