Fix bug in request logger.
[doldaconnect.git] / daemon / reqstat.c
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
33 struct trdata {
34     size_t startpos;
35 };
36
37 static char *fn = NULL;
38
39 void filelog(char *format, ...)
40 {
41     FILE *out;
42     va_list args;
43     char *b;
44     time_t now;
45     
46     if(fn == NULL)
47         return;
48     if((out = fopen(fn, "a")) == NULL) {
49         flog(LOG_WARNING, "could not open reqstat log file `%s': %s", fn, strerror(errno));
50         return;
51     }
52     now = time(NULL);
53     va_start(args, format);
54     b = vsprintf2(format, args);
55     va_end(args);
56     fprintf(out, "%s: %s\n", ctime(&now), b);
57     free(b);
58     fclose(out);
59 }
60
61 void request(struct transfer *transfer, struct trdata *data)
62 {
63     filelog("request %ls", transfer->path);
64 }
65
66 void start(struct transfer *transfer, struct trdata *data)
67 {
68     filelog("start %ls at %zi\n", transfer->path, data->startpos);
69 }
70
71 void finish(struct transfer *transfer, struct trdata *data)
72 {
73     filelog("finish %ls at %zi, total %zi\n", transfer->path, transfer->curpos, transfer->curpos - data->startpos);
74 }
75
76 static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data)
77 {
78     if(!wcscmp(attrib, L"state")) {
79         if(transfer->state == TRNS_MAIN)
80             data->startpos = transfer->curpos;
81         start(transfer, data);
82     } else if(!wcscmp(attrib, L"path")) {
83         if(transfer->path[0] != L'/') {
84             CBUNREG(transfer, trans_ac, data);
85             CBUNREG(transfer, trans_destroy, data);
86             free(data);
87         }
88         request(transfer, data);
89     }
90     return(0);
91 }
92
93 static int destroy(struct transfer *transfer, struct trdata *data)
94 {
95     finish(transfer, data);
96     free(data);
97     return(0);
98 }
99
100 static int reg(struct transfer *transfer, void *uudata)
101 {
102     struct trdata *data;
103     
104     if(transfer->dir != TRNSD_UP)
105         return(0);
106     data = memset(smalloc(sizeof(*data)), 0, sizeof(*data));
107     CBREG(transfer, trans_ac, (int (*)(struct transfer *, wchar_t *, void *))chattr, NULL, data);
108     CBREG(transfer, trans_destroy, (int (*)(struct transfer *, void *))destroy, NULL, data);
109     return(0);
110 }
111
112 static int chfile(struct configvar *var, void *uudata)
113 {
114     if(fn != NULL)
115         free(fn);
116     if(var->val.str[0] == L'0') {
117         fn = NULL;
118     } else {
119         if((fn = icwcstombs(var->val.str, NULL)) == NULL)
120             flog(LOG_WARNING, "could not convert reqstat filename `%ls' into local charset: %s", var->val.str, strerror(errno));
121     }
122     return(0);
123 }
124
125 static int init(int hup)
126 {
127     if(!hup) {
128         GCBREG(newtransfercb, reg, NULL);
129         CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL);
130     }
131     return(0);
132 }
133
134 static struct configvar myvars[] = {
135     /** The name of a file to log upload request information to. If
136      * unspecified, upload requests will not be logged. */
137     {CONF_VAR_STRING, "file", {.str = L""}},
138     {CONF_VAR_END}
139 };
140
141 static struct module me = {
142     .conf = {
143         .vars = myvars
144     },
145     .init = init,
146     .name = "reqstat"
147 };
148
149 MODULE(me)