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
25 #include <attr/xattr.h>
38 static char *fn = NULL;
40 void filelog(char *format, ...)
49 if((out = fopen(fn, "a")) == NULL) {
50 flog(LOG_WARNING, "could not open reqstat log file `%s': %s", fn, strerror(errno));
54 va_start(args, format);
55 b = vsprintf2(format, args);
58 if((p = strchr(t, '\n')) != NULL)
60 fprintf(out, "%s: %s\n", t, b);
65 void xainc(wchar_t *file, char *an, off_t inc)
72 if((fn = icswcstombs(file, NULL, NULL)) == NULL) {
73 flog(LOG_WARNING, "could not convert filename %ls into local charset: %s", file, strerror(errno));
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));
84 val = strtoll(buf, NULL, 10);
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));
92 void request(struct transfer *transfer, struct trdata *data)
94 filelog("request %ls", transfer->path);
95 if(confgetint("reqstat", "xa"))
96 xainc(transfer->path, "user.dc-req", 1);
99 void start(struct transfer *transfer, struct trdata *data)
101 filelog("start %ls at %zi", transfer->path, data->startpos);
102 if(confgetint("reqstat", "xa"))
103 xainc(transfer->path, "user.dc-started", 1);
106 void finish(struct transfer *transfer, struct trdata *data)
108 filelog("finish %ls at %zi, total %zi", transfer->path, transfer->curpos, transfer->curpos - data->startpos);
109 if(confgetint("reqstat", "xa"))
110 xainc(transfer->path, "user.dc-bytes", transfer->curpos - data->startpos);
113 static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data)
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);
125 request(transfer, data);
130 static int destroy(struct transfer *transfer, struct trdata *data)
132 if(transfer->curpos > data->startpos)
133 finish(transfer, data);
138 static int reg(struct transfer *transfer, void *uudata)
142 if(transfer->dir != TRNSD_UP)
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);
150 static int chfile(struct configvar *var, void *uudata)
154 if(var->val.str[0] == L'\0') {
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));
163 static int init(int hup)
166 GCBREG(newtransfercb, reg, NULL);
167 chfile(confgetvar("reqstat", "file"), NULL);
168 CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL);
173 static 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""}},
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}},
186 static struct module me = {