Check for xattr support in configure script.
[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 #ifdef HAVE_XATTR
34 #include <attr/xattr.h>
35 #endif
36
37 struct trdata {
38     size_t startpos;
39 };
40
41 static char *fn = NULL;
42
43 void filelog(char *format, ...)
44 {
45     FILE *out;
46     va_list args;
47     char *b, *t, *p;
48     time_t now;
49     
50     if(fn == NULL)
51         return;
52     if((out = fopen(fn, "a")) == NULL) {
53         flog(LOG_WARNING, "could not open reqstat log file `%s': %s", fn, strerror(errno));
54         return;
55     }
56     now = time(NULL);
57     va_start(args, format);
58     b = vsprintf2(format, args);
59     va_end(args);
60     t = ctime(&now);
61     if((p = strchr(t, '\n')) != NULL)
62         *p = 0;
63     fprintf(out, "%s: %s\n", t, b);
64     free(b);
65     fclose(out);
66 }
67
68 #ifdef HAVE_XATTR
69 void xainc(wchar_t *file, char *an, off_t inc)
70 {
71     char buf[32];
72     ssize_t al;
73     off_t val;
74     char *fn;
75     
76     if((fn = icswcstombs(file, NULL, NULL)) == NULL) {
77         flog(LOG_WARNING, "could not convert filename %ls into local charset: %s", file, strerror(errno));
78         return;
79     }
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));
83             return;
84         }
85         val = 0;
86     } else {
87         buf[al] = 0;
88         val = strtoll(buf, NULL, 10);
89     }
90     val += inc;
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));
94 }
95 #endif
96
97 void request(struct transfer *transfer, struct trdata *data)
98 {
99     filelog("request %ls", transfer->path);
100 #ifdef HAVE_XATTR
101     if(confgetint("reqstat", "xa"))
102         xainc(transfer->path, "user.dc-req", 1);
103 #endif
104 }
105
106 void start(struct transfer *transfer, struct trdata *data)
107 {
108     filelog("start %ls at %zi", transfer->path, data->startpos);
109 #ifdef HAVE_XATTR
110     if(confgetint("reqstat", "xa"))
111         xainc(transfer->path, "user.dc-started", 1);
112 #endif
113 }
114
115 void finish(struct transfer *transfer, struct trdata *data)
116 {
117     filelog("finish %ls at %zi, total %zi", transfer->path, transfer->curpos, transfer->curpos - data->startpos);
118 #ifdef HAVE_XATTR
119     if(confgetint("reqstat", "xa"))
120         xainc(transfer->path, "user.dc-bytes", transfer->curpos - data->startpos);
121 #endif
122 }
123
124 static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data)
125 {
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);
134             free(data);
135         }
136         request(transfer, data);
137     }
138     return(0);
139 }
140
141 static int destroy(struct transfer *transfer, struct trdata *data)
142 {
143     if(transfer->curpos > data->startpos)
144         finish(transfer, data);
145     free(data);
146     return(0);
147 }
148
149 static int reg(struct transfer *transfer, void *uudata)
150 {
151     struct trdata *data;
152     
153     if(transfer->dir != TRNSD_UP)
154         return(0);
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);
158     return(0);
159 }
160
161 static int chfile(struct configvar *var, void *uudata)
162 {
163     if(fn != NULL)
164         free(fn);
165     if(var->val.str[0] == L'\0') {
166         fn = NULL;
167     } else {
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));
170     }
171     return(0);
172 }
173
174 static int init(int hup)
175 {
176     if(!hup) {
177         GCBREG(newtransfercb, reg, NULL);
178         chfile(confgetvar("reqstat", "file"), NULL);
179         CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL);
180     }
181     return(0);
182 }
183
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}},
194     {CONF_VAR_END}
195 };
196
197 static struct module me = {
198     .conf = {
199         .vars = myvars
200     },
201     .init = init,
202     .name = "reqstat"
203 };
204
205 MODULE(me)