1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
** DAV filesystem-based quota routines
*/
#include "apr.h"
#include "apr_strings.h"
#include "httpd.h"
#include "http_log.h"
#include "http_main.h"
#include "mod_dav.h"
#include "repos.h"
/*
* Just use a configure test? fields have been standardized for
* while: https://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstatvfs.h.html
*/
#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(OpenBSD) || \
defined(linux)
#include <sys/statvfs.h>
#define HAVE_STATVFS
#endif
#define DAV_TRUE 1
#define DAV_FALSE 0
/* Forwared declaration, since it calls itself */
static apr_status_t get_dir_used_bytes_walk(request_rec *r,
const char *path,
apr_off_t *used);
static apr_status_t get_dir_used_bytes_walk(request_rec *r,
const char *path,
apr_off_t *used)
{
apr_dir_t *dir = NULL;
apr_finfo_t finfo;
apr_status_t rv;
if ((rv = apr_dir_open(&dir, path, r->pool)) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"failed to open \"%s\"", path);
goto out;
}
do {
apr_int32_t wanted;
char *newpath;
wanted = APR_FINFO_DIRENT|APR_FINFO_TYPE|APR_FINFO_SIZE|APR_FINFO_NAME;
rv = apr_dir_read(&finfo, wanted, dir);
if (rv != APR_SUCCESS && rv != APR_INCOMPLETE)
break;
if ((finfo.valid & APR_FINFO_NAME) == 0) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Cannot get entry name in \"%s\"", path);
goto out;
}
if (!strcmp(finfo.name, ".") ||
!strcmp(finfo.name, "..") ||
!strcmp(finfo.name, DAV_FS_STATE_DIR) ||
!strncmp(finfo.name, DAV_FS_TMP_PREFIX, strlen(DAV_FS_TMP_PREFIX)))
continue;
if ((finfo.valid & APR_FINFO_TYPE) == 0) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Cannot get entry type in \"%s\"", path);
goto out;
}
switch (finfo.filetype) {
case APR_REG:
if ((finfo.valid & APR_FINFO_SIZE) == 0) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Cannot get entry size in \"%s\"", path);
goto out;
}
*used += finfo.size;
break;
case APR_DIR:
if ((finfo.valid & APR_FINFO_NAME) == 0) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Cannot get entry name in \"%s\"", path);
goto out;
}
rv = apr_filepath_merge(&newpath, path, finfo.name, 0, r->pool);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"apr_filepath_merge \"%s\" \"%s\" failed",
path, finfo.name);
goto out;
}
rv = get_dir_used_bytes_walk(r, newpath, used);
if (rv != APR_SUCCESS)
goto out;
break;
default:
/* skip other types */
break;
}
} while (1 /* CONSTCOND */);
if (rv == APR_ENOENT)
rv = APR_SUCCESS;
else
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"apr_dir_read failed on \"%s\"", path);
out:
if (dir)
(void)apr_dir_close(dir);
return rv;
}
static apr_off_t get_dir_used_bytes(request_rec *r, const char *path)
{
apr_off_t used_bytes = 0;
apr_status_t rv;
rv = get_dir_used_bytes_walk(r, path, &used_bytes);
return (rv == APR_SUCCESS) ? used_bytes : DAV_FS_BYTES_ERROR;
}
static apr_off_t get_fs_used_bytes(const char *path)
{
apr_off_t used_bytes = DAV_FS_BYTES_ERROR;
#ifdef HAVE_STATVFS
struct statvfs f;
if (statvfs(path, &f) != 0)
goto out;
#ifdef __NetBSD__
used_bytes = (f.f_blocks - f.f_bfree) * f.f_frsize;
#else
used_bytes = (f.f_blocks - f.f_bfree) * f.f_bsize;
#endif
out:
#endif
return used_bytes;
}
static apr_off_t get_fs_available_bytes(const char *path)
{
apr_off_t available_bytes = DAV_FS_BYTES_ERROR;
#ifdef HAVE_STATVFS
struct statvfs f;
if (statvfs(path, &f) != 0)
goto out;
#ifdef __NetBSD__
available_bytes = f.f_bavail * f.f_frsize;
#else
available_bytes = f.f_bavail * f.f_bsize;
#endif
out:
#endif
return available_bytes;
}
apr_off_t dav_fs_get_used_bytes(request_rec *r, const char *path)
{
apr_off_t quota;
apr_off_t used_bytes = DAV_FS_BYTES_ERROR;
if (dav_fs_get_quota(r, path, "a) != NULL)
goto out;
switch (quota) {
case DAV_FS_QUOTA_UNSET: /* FALLTHOTUGH */
case DAV_FS_QUOTA_OFF:
break;
case DAV_FS_QUOTA_NONE:;
used_bytes = get_fs_used_bytes(path);
break;
default:
used_bytes = get_dir_used_bytes(r, path);
break;
}
out:
return used_bytes;
}
apr_off_t dav_fs_get_available_bytes(request_rec *r,
const char *path, int *fs_low)
{
apr_off_t quota;
apr_off_t used_bytes;
apr_off_t fs_available_bytes;
apr_off_t available_bytes = DAV_FS_BYTES_ERROR;
int _fs_low = DAV_FALSE;
if (dav_fs_get_quota(r, path, "a) != NULL)
goto out;
switch (quota) {
case DAV_FS_QUOTA_UNSET: /* FALLTHROUGH */
case DAV_FS_QUOTA_OFF:
break;
case DAV_FS_QUOTA_NONE:
available_bytes = get_fs_available_bytes(path);
if (available_bytes != DAV_FS_BYTES_ERROR)
_fs_low = DAV_TRUE;
break;
default:
used_bytes = get_dir_used_bytes(r, path);
if (used_bytes != DAV_FS_BYTES_ERROR) {
if (used_bytes > quota)
available_bytes = 0;
else
available_bytes = quota - used_bytes;
/*
* Use available space from filesystem rather than quota
* if it is smaller
*/
fs_available_bytes = get_fs_available_bytes(path);
if (fs_available_bytes != DAV_FS_BYTES_ERROR) {
if (fs_available_bytes < available_bytes) {
available_bytes = fs_available_bytes;
_fs_low = DAV_TRUE;
}
}
}
break;
}
out:
if (available_bytes != DAV_FS_BYTES_ERROR && fs_low)
*fs_low = _fs_low;
return available_bytes;
}
int dav_fs_quota_precondition(request_rec *r,
dav_resource *src, const dav_resource *dst,
const apr_xml_doc *doc, dav_error **err)
{
apr_off_t quota;
apr_off_t available_bytes;
apr_off_t size;
const char *path;
const char *lenhdr;
const char *tag;
const char *msg;
int status = DECLINED;
int fs_low;
if (r->method_number == M_COPY || r->method_number == M_MOVE) {
/*
* dav_method_copymove() calls dav_run_method_precondition()
* twice, with dst NULL on first call and set on the second call.
*/
if (dst == NULL)
goto out;
path = dav_fs_fname(dst);
} else {
path = dav_fs_fname(src);
}
path = ap_make_dirstr_parent(r->pool, path);
if ((*err = dav_fs_get_quota(r, path, "a)) != NULL)
goto out;
if (quota == DAV_FS_QUOTA_OFF || quota == DAV_FS_QUOTA_UNSET)
goto out;
available_bytes = dav_fs_get_available_bytes(r, path, &fs_low);
if (available_bytes == DAV_FS_BYTES_ERROR) {
if (quota != DAV_FS_QUOTA_NONE) {
status = HTTP_INTERNAL_SERVER_ERROR;
*err = dav_new_error(r->pool, status, 0, 0,
"Quota enabled, but failed to compute "
"available space.");
}
goto out;
}
tag = fs_low ? "sufficient-disk-space" : "quota-not-exceeded";
msg = fs_low ? "Insufficient disk space" : "Quota exceeded";
/*
* For all operations, report overquota before the operation.
*/
if (available_bytes == 0) {
status = HTTP_INSUFFICIENT_STORAGE;
*err = dav_new_error_tag(r->pool, status, 0, 0,
msg, NULL, tag);
goto out;
}
switch (r->method_number) {
case M_PUT:
/*
* If PUT has Content-Length, we can forecast overquota
*/
if ((lenhdr = apr_table_get(r->headers_in, "Content-Length")) &&
(atol(lenhdr) > available_bytes)) {
status = HTTP_INSUFFICIENT_STORAGE;
*err = dav_new_error_tag(r->pool, status, 0, 0,
msg, NULL, tag);
goto out;
}
break;
case M_COPY: /* FALLTHROUGH */
case M_MOVE:
/*
* If source size is known, we can forecast ovequota
*/
if ((size = dav_fs_size(src) != DAV_FS_BYTES_ERROR) &&
(size > available_bytes)) {
status = HTTP_INSUFFICIENT_STORAGE;
*err = dav_new_error_tag(r->pool, status, 0, 0,
msg, "DAV:", tag);
goto out;
}
break;
default:
break;
}
out:
return status;
}
|