From 07dd1674df5d34bda138c2727f157d0e76fa8f02 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Thu, 4 Oct 2007 04:20:16 +0200 Subject: [PATCH] Added upload request logging. --- daemon/Makefile.am | 3 +- daemon/reqstat.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 daemon/reqstat.c diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 99b77f2..9d8f77c 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -24,7 +24,8 @@ doldacond_SOURCES= main.c \ log.c \ ui.c \ conf.c \ - conf.h + conf.h \ + reqstat.c EXTRA_DIST=emacs-local doldacond_LDADD=$(top_srcdir)/common/libcommon.a \ diff --git a/daemon/reqstat.c b/daemon/reqstat.c new file mode 100644 index 0000000..41ca1c6 --- /dev/null +++ b/daemon/reqstat.c @@ -0,0 +1,148 @@ +/* + * Dolda Connect - Modular multiuser Direct Connect-style client + * Copyright (C) 2007 Fredrik Tolf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif +#include "transfer.h" +#include "module.h" +#include "log.h" + +struct trdata { + size_t startpos; +}; + +static char *fn = NULL; + +void filelog(char *format, ...) +{ + FILE *out; + va_list args; + char *b; + time_t now; + + if(fn == NULL) + return; + if((out = fopen(fn, "a")) == NULL) { + flog(LOG_WARNING, "could not open reqstat log file `%s': %s", fn, strerror(errno)); + return; + } + now = time(NULL); + va_start(args, format); + b = vsprintf2(format, args); + va_end(args); + fprintf(out, "%s: %s\n", ctime(&now), b); + free(b); + fclose(out); +} + +void request(struct transfer *transfer, struct trdata *data) +{ + filelog("request %ls", transfer->path); +} + +void start(struct transfer *transfer, struct trdata *data) +{ + filelog("start %ls at %zi\n", transfer->path, data->startpos); +} + +void finish(struct transfer *transfer, struct trdata *data) +{ + filelog("finish %ls at %zi, total %zi\n", transfer->path, transfer->curpos, transfer->curpos - data->startpos); +} + +static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data) +{ + if(!wcscmp(attrib, L"state")) { + if(transfer->state == TRNS_MAIN) + data->startpos = transfer->curpos; + start(transfer, data); + } else if(!wcscmp(attrib, L"path")) { + if(transfer->path[0] != L'/') { + CBUNREG(transfer, trans_ac, data); + CBUNREG(transfer, trans_destroy, data); + free(data); + } + request(transfer, data); + } + return(0); +} + +static int destroy(struct transfer *transfer, struct trdata *data) +{ + finish(transfer, data); + free(data); + return(0); +} + +static int reg(struct transfer *transfer, void *uudata) +{ + struct trdata *data; + + if(transfer->dir != TRNSD_UP) + return(0); + data = memset(smalloc(sizeof(*data)), 0, sizeof(*data)); + CBREG(transfer, trans_ac, (int (*)(struct transfer *, wchar_t *, void *))chattr, NULL, data); + CBREG(transfer, trans_destroy, (int (*)(struct transfer *, void *))destroy, NULL, data); + return(0); +} + +static int chfile(struct configvar *var, void *uudata) +{ + if(fn != NULL) + free(fn); + if(var->val.str[0] == L'0') { + fn = NULL; + } else { + if((fn = icwcstombs(var->val.str, NULL)) == NULL) + flog(LOG_WARNING, "could not convert reqstat filename `%ls' into local charset: %s", var->val.str, strerror(errno)); + } + return(0); +} + +static void preinit(int hup) +{ + if(!hup) { + GCBREG(newtransfercb, reg, NULL); + CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL); + } +} + +static struct configvar myvars[] = { + /** The name of a file to log upload request information to. If + * unspecified, upload requests will not be logged. */ + {CONF_VAR_STRING, "file", {.str = L""}}, + {CONF_VAR_END} +}; + +static struct module me = { + .conf = { + .vars = myvars + }, + .preinit = preinit, + .name = "reqstat" +}; + +MODULE(me); -- 2.11.0