1 /* $Id$ */
3 /***
4 This file is part of fusedav.
6 fusedav is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 fusedav is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with fusedav; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ***/
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #define _XOPEN_SOURCE 500
27 #include <errno.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <assert.h>
34 #include <pthread.h>
36 #include <ne_props.h>
37 #include <ne_uri.h>
38 #include <ne_session.h>
39 #include <ne_utils.h>
40 #include <ne_socket.h>
41 #include <ne_auth.h>
42 #include <ne_dates.h>
43 #include <ne_basic.h>
45 #include "filecache.h"
46 #include "statcache.h"
47 #include "fusedav.h"
48 #include "session.h"
50 struct file_info {
51 char *filename;
52 int fd;
53 off_t server_length, length, present;
55 int readable;
56 int writable;
58 int modified;
60 int ref, dead;
62 pthread_mutex_t mutex;
64 /* This field is locked by files_mutex, not by file_info->mutex */
65 struct file_info *next;
66 };
68 static struct file_info *files = NULL;
69 static pthread_mutex_t files_mutex = PTHREAD_MUTEX_INITIALIZER;
71 static int file_cache_sync_unlocked(struct file_info *fi);
73 void* file_cache_get(const char *path) {
74 struct file_info *f, *r = NULL;
76 pthread_mutex_lock(&files_mutex);
78 for (f = files; f; f = f->next) {
80 pthread_mutex_lock(&f->mutex);
81 if (!f->dead && f->filename && !strcmp(path, f->filename)) {
82 f->ref++;
83 r = f;
84 }
85 pthread_mutex_unlock(&f->mutex);
87 if (r)
88 break;
89 }
91 pthread_mutex_unlock(&files_mutex);
92 return f;
93 }
95 static void file_cache_free_unlocked(struct file_info *fi) {
96 assert(fi && fi->dead && fi->ref == 0);
98 free(fi->filename);
100 if (fi->fd >= 0)
101 close(fi->fd);
103 pthread_mutex_destroy(&fi->mutex);
104 free(fi);
105 }
107 void file_cache_unref(void *f) {
108 struct file_info *fi = f;
109 assert(fi);
111 pthread_mutex_lock(&fi->mutex);
113 assert(fi->ref >= 1);
114 fi->ref--;
116 if (!fi->ref && fi->dead) {
117 file_cache_sync_unlocked(fi);
118 file_cache_free_unlocked(fi);
119 }
121 pthread_mutex_unlock(&fi->mutex);
122 }
124 static void file_cache_unlink(struct file_info *fi) {
125 struct file_info *s, *prev;
126 assert(fi);
128 pthread_mutex_lock(&files_mutex);
130 for (s = files, prev = NULL; s; s = s->next) {
131 if (s == fi) {
132 if (prev)
133 prev->next = s->next;
134 else
135 files = s->next;
137 break;
138 }
140 prev = s;
141 }
143 pthread_mutex_unlock(&files_mutex);
144 }
146 int file_cache_close(void *f) {
147 struct file_info *fi = f;
148 int r = 0;
149 assert(fi);
151 file_cache_unlink(f);
153 pthread_mutex_lock(&fi->mutex);
154 fi->dead = 1;
155 pthread_mutex_unlock(&fi->mutex);
157 return r;
158 }
160 void* file_cache_open(const char *path, int flags) {
161 struct file_info *fi;
162 char tempfile[PATH_MAX];
163 const char *length = NULL;
164 ne_request *req = NULL;
165 ne_session *session;
167 if (!(session = session_get(1))) {
168 errno = EIO;
169 goto fail;
170 }
172 if ((fi = file_cache_get(path))) {
173 if (flags & O_RDONLY || flags & O_RDWR) fi->readable = 1;
174 if (flags & O_WRONLY || flags & O_RDWR) fi->writable = 1;
175 return fi;
176 }
178 fi = malloc(sizeof(struct file_info));
179 memset(fi, 0, sizeof(struct file_info));
180 fi->fd = -1;
182 fi->filename = strdup(path);
184 snprintf(tempfile, sizeof(tempfile), "%s/fusedav-cache-XXXXXX", "/tmp");
185 if ((fi->fd = mkstemp(tempfile)) < 0)
186 goto fail;
187 unlink(tempfile);
189 req = ne_request_create(session, "HEAD", path);
190 assert(req);
192 if (ne_request_dispatch(req) != NE_OK) {
193 fprintf(stderr, "HEAD failed: %s\n", ne_get_error(session));
194 errno = ENOENT;
195 goto fail;
196 }
198 if (!(length = ne_get_response_header(req, "Content-Length")))
199 /* dirty hack, since Apache doesn't send the file size if the file is empty */
200 fi->server_length = fi->length = 0;
201 else
202 fi->server_length = fi->length = atoi(length);
204 ne_request_destroy(req);
206 if (flags & O_RDONLY || flags & O_RDWR) fi->readable = 1;
207 if (flags & O_WRONLY || flags & O_RDWR) fi->writable = 1;
209 pthread_mutex_init(&fi->mutex, NULL);
211 pthread_mutex_lock(&files_mutex);
212 fi->next = files;
213 files = fi;
214 pthread_mutex_unlock(&files_mutex);
216 fi->ref = 1;
218 return fi;
220 fail:
222 if (req)
223 ne_request_destroy(req);
225 if (fi) {
226 if (fi->fd >= 0)
227 close(fi->fd);
228 free(fi->filename);
229 free(fi);
230 }
232 return NULL;
233 }
235 static int load_up_to_unlocked(struct file_info *fi, off_t l) {
236 ne_content_range range;
237 ne_session *session;
239 assert(fi);
241 if (!(session = session_get(1))) {
242 errno = EIO;
243 return -1;
244 }
246 if (l > fi->server_length)
247 l = fi->server_length;
249 if (l <= fi->present)
250 return 0;
252 if (lseek(fi->fd, fi->present, SEEK_SET) != fi->present)
253 return -1;
255 range.start = fi->present;
256 range.end = l-1;
257 range.total = 0;
259 if (ne_get_range(session, fi->filename, &range, fi->fd) != NE_OK) {
260 fprintf(stderr, "GET failed: %s\n", ne_get_error(session));
261 errno = ENOENT;
262 return -1;
263 }
265 fi->present = l;
266 return 0;
267 }
269 int file_cache_read(void *f, char *buf, size_t size, off_t offset) {
270 struct file_info *fi = f;
271 ssize_t r = -1;
273 assert(fi && buf && size);
275 pthread_mutex_lock(&fi->mutex);
277 if (load_up_to_unlocked(fi, offset+size) < 0)
278 goto finish;
280 if ((r = pread(fi->fd, buf, size, offset)) < 0)
281 goto finish;
283 finish:
285 pthread_mutex_unlock(&fi->mutex);
287 return r;
288 }
290 int file_cache_write(void *f, const char *buf, size_t size, off_t offset) {
291 struct file_info *fi = f;
292 ssize_t r = -1;
294 assert (fi);
296 pthread_mutex_lock(&fi->mutex);
298 if (!fi->writable) {
299 errno = EBADF;
300 goto finish;
301 }
303 if (load_up_to_unlocked(fi, offset) < 0)
304 goto finish;
306 if ((r = pwrite(fi->fd, buf, size, offset)) < 0)
307 goto finish;
309 if (offset+size > fi->present)
310 fi->present = offset+size;
312 if (offset+size > fi->length)
313 fi->length = offset+size;
315 fi->modified = 1;
317 finish:
318 pthread_mutex_unlock(&fi->mutex);
320 return r;
321 }
323 int file_cache_truncate(void *f, off_t s) {
324 struct file_info *fi = f;
325 int r;
327 assert(fi);
329 pthread_mutex_lock(&fi->mutex);
331 fi->length = s;
332 r = ftruncate(fi->fd, fi->length);
334 pthread_mutex_unlock(&fi->mutex);
336 return r;
337 }
339 int file_cache_sync_unlocked(struct file_info *fi) {
340 int r = -1;
341 ne_session *session;
343 assert(fi);
345 if (!fi->writable) {
346 errno = EBADF;
347 goto finish;
348 }
350 if (!fi->modified) {
351 r = 0;
352 goto finish;
353 }
355 if (load_up_to_unlocked(fi, (off_t) -1) < 0)
356 goto finish;
358 if (lseek(fi->fd, 0, SEEK_SET) == (off_t)-1)
359 goto finish;
361 if (!(session = session_get(1))) {
362 errno = EIO;
363 goto finish;
364 }
366 if (ne_put(session, fi->filename, fi->fd)) {
367 fprintf(stderr, "PUT failed: %s\n", ne_get_error(session));
368 errno = ENOENT;
369 goto finish;
370 }
372 stat_cache_invalidate(fi->filename);
373 dir_cache_invalidate_parent(fi->filename);
375 r = 0;
377 finish:
379 return r;
380 }
382 int file_cache_sync(void *f) {
383 struct file_info *fi = f;
384 int r = -1;
385 assert(fi);
387 pthread_mutex_lock(&fi->mutex);
388 r = file_cache_sync_unlocked(fi);
389 pthread_mutex_unlock(&fi->mutex);
391 return r;
392 }
394 int file_cache_close_all(void) {
395 int r = 0;
397 pthread_mutex_lock(&files_mutex);
399 while (files) {
400 struct file_info *fi = files;
402 pthread_mutex_lock(&fi->mutex);
403 fi->ref++;
404 pthread_mutex_unlock(&fi->mutex);
406 pthread_mutex_unlock(&files_mutex);
407 file_cache_close(fi);
408 file_cache_unref(fi);
409 pthread_mutex_lock(&files_mutex);
410 }
412 pthread_mutex_unlock(&files_mutex);
414 return r;
415 }
417 off_t file_cache_get_size(void *f) {
418 struct file_info *fi = f;
420 assert(fi);
422 return fi->length;
423 }