2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2007 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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
34 #include <attr/xattr.h>
41 static char *fn = NULL;
43 void filelog(char *format, ...)
52 if((out = fopen(fn, "a")) == NULL) {
53 flog(LOG_WARNING, "could not open reqstat log file `%s': %s", fn, strerror(errno));
57 va_start(args, format);
58 b = vsprintf2(format, args);
61 if((p = strchr(t, '\n')) != NULL)
63 fprintf(out, "%s: %s\n", t, b);
69 void xainc(wchar_t *file, char *an, off_t inc)
78 if((fn = icswcstombs(file, NULL, NULL)) == NULL) {
79 flog(LOG_WARNING, "could not convert filename %ls into local charset: %s", file, strerror(errno));
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));
90 val = strtoll(buf, NULL, 10);
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));
99 void request(struct transfer *transfer, struct trdata *data)
101 filelog("request %ls", transfer->path);
103 if(confgetint("reqstat", "xa"))
104 xainc(transfer->path, "user.dc-req", 1);
108 void start(struct transfer *transfer, struct trdata *data)
110 filelog("start %ls at %zi", transfer->path, data->startpos);
112 if(confgetint("reqstat", "xa"))
113 xainc(transfer->path, "user.dc-started", 1);
117 void finish(struct transfer *transfer, struct trdata *data)
119 filelog("finish %ls at %zi, total %zi", transfer->path, transfer->curpos, transfer->curpos - data->startpos);
121 if(confgetint("reqstat", "xa"))
122 xainc(transfer->path, "user.dc-bytes", transfer->curpos - data->startpos);
126 static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data)
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);
138 request(transfer, data);
143 static int destroy(struct transfer *transfer, struct trdata *data)
145 if(transfer->curpos > data->startpos)
146 finish(transfer, data);
151 static int reg(struct transfer *transfer, void *uudata)
155 if(transfer->dir != TRNSD_UP)
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);
163 static int chfile(struct configvar *var, void *uudata)
167 if(var->val.str[0] == L'\0') {
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));
176 static int init(int hup)
179 GCBREG(newtransfercb, reg, NULL);
180 chfile(confgetvar("reqstat", "file"), NULL);
181 CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL);
186 static 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""}},
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}},
199 static struct module me = {