Code

add boilerplate licenses in source files
[fusedav.git] / src / filecache.c
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.
10   
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.
15   
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 "fusedav.h"
47 #include "session.h"
48 #include "statcache.h"
50 struct file_info {
51     char *filename;
52     int fd;
53     off_t server_length, length, present;
54     
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 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);
77     
78     for (f = files; f; f = f->next) {
79         
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     }
90     
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);
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);
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);
129     
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         }
139         
140         prev = s;
141     }
142     
143     pthread_mutex_unlock(&files_mutex);
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;
160 void* file_cache_open(const char *path, int flags) {
161     struct file_info *fi;
162     char tempfile[PATH_MAX];
163     char *length = NULL;
164     ne_request *req = NULL;
165     ne_session *session;
167     if ((fi = file_cache_get(path))) {
168         if (flags & O_RDONLY || flags & O_RDWR) fi->readable = 1;
169         if (flags & O_WRONLY || flags & O_RDWR) fi->writable = 1;
170         return fi;
171     }
173     if (!(session = session_get())) {
174         errno = -EIO;
175         return NULL;
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     ne_add_response_header_handler(req, "Content-Length", ne_duplicate_header, &length);
193     
194     if (ne_request_dispatch(req) != NE_OK) {
195         fprintf(stderr, "HEAD failed: %s\n", ne_get_error(session));
196         errno = ENOENT;
197         goto fail;
198     }
200     if (!length) {
201         fprintf(stderr, "HEAD did not return content length.\n");
202         errno = EPROTO;
203         goto fail;
204     }
206     fi->server_length = fi->length = atoi(length);
208     ne_request_destroy(req);
209     free(length);
210     
211     if (flags & O_RDONLY || flags & O_RDWR) fi->readable = 1;
212     if (flags & O_WRONLY || flags & O_RDWR) fi->writable = 1;
214     pthread_mutex_init(&fi->mutex, NULL);
215     
216     pthread_mutex_lock(&files_mutex);
217     fi->next = files;
218     files = fi;
219     pthread_mutex_unlock(&files_mutex);
221     fi->ref = 1;
222     
223     return fi;
225 fail:
227     if (req)
228         ne_request_destroy(req);
230     if (length)
231         free(length);
233     if (fi) {
234         if (fi->fd >= 0)
235             close(fi->fd);
236         free(fi->filename);
237         free(fi);
238     }
239         
240     return NULL;
243 static int load_up_to_unlocked(struct file_info *fi, off_t l) {
244     ne_content_range range;
245     assert(fi);
246     ne_session *session;
248     if (!(session = session_get())) {
249         errno = EIO;
250         return -1;
251     }
253     if (l > fi->server_length)
254         l = fi->server_length;
255     
256     if (l <= fi->present)
257         return 0;
259     if (lseek(fi->fd, fi->present, SEEK_SET) != fi->present)
260         return -1;
262     range.start = fi->present;
263     range.end = l-1;
264     
265     if (ne_get_range(session, fi->filename, &range, fi->fd)) {
266         fprintf(stderr, "GET failed: %s\n", ne_get_error(session));
267         errno = ENOENT;
268         return -1;
269     }
271     fi->present = l;
272     return 0;
275 int file_cache_read(void *f, char *buf, size_t size, off_t offset) {
276     struct file_info *fi = f;
277     ssize_t r = -1;
278     
279     assert(fi && buf && size);
281     pthread_mutex_lock(&fi->mutex);
283     if (load_up_to_unlocked(fi, offset+size) < 0)
284         goto finish;
286     if ((r = pread(fi->fd, buf, size, offset)) < 0)
287         goto finish;
289 finish:
290     
291     pthread_mutex_unlock(&fi->mutex);
293     return r;
296 int file_cache_write(void *f, const char *buf, size_t size, off_t offset) {
297     struct file_info *fi = f;
298     ssize_t r = -1;
300     assert (fi);
302     pthread_mutex_lock(&fi->mutex);
304     if (!fi->writable) {
305         errno = EBADF;
306         goto finish;
307     }
309     if (load_up_to_unlocked(fi, offset) < 0)
310         goto finish;
311         
312     if ((r = pwrite(fi->fd, buf, size, offset)) < 0)
313         goto finish;
315     if (offset+size > fi->present)
316         fi->present = offset+size;
318     if (offset+size > fi->length)
319         fi->length = offset+size;
321     fi->modified = 1;
323     r = 0;
325 finish:
326     pthread_mutex_unlock(&fi->mutex);
327     
328     return r;
331 int file_cache_truncate(void *f, off_t s) {
332     struct file_info *fi = f;
333     assert(fi);
334     int r;
336     pthread_mutex_lock(&fi->mutex);
338     fi->length = s;
339     r = ftruncate(fi->fd, fi->length);
341     pthread_mutex_unlock(&fi->mutex);
343     return r;
346 int file_cache_sync_unlocked(struct file_info *fi) {
347     int r = -1;
348     ne_session *session;
349     assert(fi);
351     if (!(session = session_get())) {
352         errno = EIO;
353         goto finish;
354     }
356     if (!fi->writable) {
357         errno = EBADF;
358         goto finish;
359     }
361     if (!fi->modified) {
362         r = 0;
363         goto finish;
364     }
365     
366     if (load_up_to_unlocked(fi, (off_t) -1) < 0)
367         goto finish;
369     if (lseek(fi->fd, 0, SEEK_SET) == (off_t)-1)
370         goto finish;
372     
373     if (ne_put(session, fi->filename, fi->fd)) {
374         fprintf(stderr, "PUT failed: %s\n", ne_get_error(session));
375         errno = ENOENT;
376         goto finish;
377     }
379     stat_cache_invalidate(fi->filename);
380     dir_cache_invalidate_parent(fi->filename);
382     r = 0;
384 finish:
385     
386     return r;
389 int file_cache_sync(void *f) {
390     struct file_info *fi = f;
391     int r = -1;
392     assert(fi);
394     pthread_mutex_lock(&fi->mutex);
395     r = file_cache_sync_unlocked(fi);
396     pthread_mutex_unlock(&fi->mutex);
397     
398     return r;
401 int file_cache_close_all(void) {
402     int r = 0;
404     pthread_mutex_lock(&files_mutex);
406     while (files) {
407         struct file_info *fi = files;
408         
409         pthread_mutex_lock(&fi->mutex);
410         fi->ref++;
411         pthread_mutex_unlock(&fi->mutex);
413         pthread_mutex_unlock(&files_mutex);
414         file_cache_close(fi);
415         file_cache_unref(fi);
416         pthread_mutex_lock(&files_mutex);
417     }
419     pthread_mutex_unlock(&files_mutex);
421     return r;