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)
76 if((fn = icswcstombs(file, NULL, NULL)) == NULL) {
77 flog(LOG_WARNING, "could not convert filename %ls into local charset: %s", file, strerror(errno));
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));
88 val = strtoll(buf, NULL, 10);
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));
97 void request(struct transfer *transfer, struct trdata *data)
99 filelog("request %ls", transfer->path);
101 if(confgetint("reqstat", "xa"))
102 xainc(transfer->path, "user.dc-req", 1);
106 void start(struct transfer *transfer, struct trdata *data)
108 filelog("start %ls at %zi", transfer->path, data->startpos);
110 if(confgetint("reqstat", "xa"))
111 xainc(transfer->path, "user.dc-started", 1);
115 void finish(struct transfer *transfer, struct trdata *data)
117 filelog("finish %ls at %zi, total %zi", transfer->path, transfer->curpos, transfer->curpos - data->startpos);
119 if(confgetint("reqstat", "xa"))
120 xainc(transfer->path, "user.dc-bytes", transfer->curpos - data->startpos);
124 static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data)
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);
136 request(transfer, data);
141 static int destroy(struct transfer *transfer, struct trdata *data)
143 if(transfer->curpos > data->startpos)
144 finish(transfer, data);
149 static int reg(struct transfer *transfer, void *uudata)
153 if(transfer->dir != TRNSD_UP)
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);
161 static int chfile(struct configvar *var, void *uudata)
165 if(var->val.str[0] == L'\0') {
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));
174 static int init(int hup)
177 GCBREG(newtransfercb, reg, NULL);
178 chfile(confgetvar("reqstat", "file"), NULL);
179 CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL);
184 static 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""}},
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}},
197 static struct module me = {