Code

use ne_get_range64 which is new in neon 0.26
[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 #include <errno.h>
26 #include <string.h>
27 #include <limits.h>
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <assert.h>
32 #include <pthread.h>
33 #include <inttypes.h>
34 #include <limits.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;
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 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);
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 = NULL;
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);
205     
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);
210     
211     pthread_mutex_lock(&files_mutex);
212     fi->next = files;
213     files = fi;
214     pthread_mutex_unlock(&files_mutex);
216     fi->ref = 1;
217     
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     }
231         
232     return NULL;
235 static int load_up_to_unlocked(struct file_info *fi, off_t l) {
237     ne_content_range64 range;
238     ne_session *session;
240     assert(fi);
242     if (!(session = session_get(1))) {
243         errno = EIO;
244         return -1;
245     }
247     if (l > fi->server_length)
248         l = fi->server_length;
249     
250     if (l <= fi->present)
251         return 0;
253     if (lseek(fi->fd, fi->present, SEEK_SET) != fi->present)
254         return -1;
255     
256     range.start = fi->present;
257     range.end = l-1;
258     range.total = 0;
259     
260     if (ne_get_range64(session, fi->filename, &range, fi->fd) != NE_OK) {
261         fprintf(stderr, "GET failed: %s\n", ne_get_error(session));
262         errno = ENOENT;
263         return -1;
264     }
266     fi->present = l;
267     return 0;
270 int file_cache_read(void *f, char *buf, size_t size, off_t offset) {
271     struct file_info *fi = f;
272     ssize_t r = -1;
273     
274     assert(fi && buf && size);
276     pthread_mutex_lock(&fi->mutex);
278     if (load_up_to_unlocked(fi, offset+size) < 0)
279         goto finish;
281     if ((r = pread(fi->fd, buf, size, offset)) < 0)
282         goto finish;
284 finish:
285     
286     pthread_mutex_unlock(&fi->mutex);
288     return r;
291 int file_cache_write(void *f, const char *buf, size_t size, off_t offset) {
292     struct file_info *fi = f;
293     ssize_t r = -1;
295     assert (fi);
297     pthread_mutex_lock(&fi->mutex);
299     if (!fi->writable) {
300         errno = EBADF;
301         goto finish;
302     }
304     if (load_up_to_unlocked(fi, offset) < 0)
305         goto finish;
306         
307     if ((r = pwrite(fi->fd, buf, size, offset)) < 0)
308         goto finish;
310     if (offset+size > fi->present)
311         fi->present = offset+size;
313     if (offset+size > fi->length)
314         fi->length = offset+size;
316     fi->modified = 1;
318 finish:
319     pthread_mutex_unlock(&fi->mutex);
320     
321     return r;
324 int file_cache_truncate(void *f, off_t s) {
325     struct file_info *fi = f;
326     int r;
328     assert(fi);
330     pthread_mutex_lock(&fi->mutex);
332     fi->length = s;
333     r = ftruncate(fi->fd, fi->length);
335     pthread_mutex_unlock(&fi->mutex);
337     return r;
340 int file_cache_sync_unlocked(struct file_info *fi) {
341     int r = -1;
342     ne_session *session;
344     assert(fi);
345     
346     if (!fi->writable) {
347         errno = EBADF;
348         goto finish;
349     }
351     if (!fi->modified) {
352         r = 0;
353         goto finish;
354     }
355     
356     if (load_up_to_unlocked(fi, (off_t) -1) < 0)
357         goto finish;
359     if (lseek(fi->fd, 0, SEEK_SET) == (off_t)-1)
360         goto finish;
362     if (!(session = session_get(1))) {
363         errno = EIO;
364         goto finish;
365     }
366     
367     if (ne_put(session, fi->filename, fi->fd)) {
368         fprintf(stderr, "PUT failed: %s\n", ne_get_error(session));
369         errno = ENOENT;
370         goto finish;
371     }
373     stat_cache_invalidate(fi->filename);
374     dir_cache_invalidate_parent(fi->filename);
376     r = 0;
378 finish:
379     
380     return r;
383 int file_cache_sync(void *f) {
384     struct file_info *fi = f;
385     int r = -1;
386     assert(fi);
388     pthread_mutex_lock(&fi->mutex);
389     r = file_cache_sync_unlocked(fi);
390     pthread_mutex_unlock(&fi->mutex);
391     
392     return r;
395 int file_cache_close_all(void) {
396     int r = 0;
398     pthread_mutex_lock(&files_mutex);
400     while (files) {
401         struct file_info *fi = files;
402         
403         pthread_mutex_lock(&fi->mutex);
404         fi->ref++;
405         pthread_mutex_unlock(&fi->mutex);
407         pthread_mutex_unlock(&files_mutex);
408         file_cache_close(fi);
409         file_cache_unref(fi);
410         pthread_mutex_lock(&files_mutex);
411     }
413     pthread_mutex_unlock(&files_mutex);
415     return r;
418 off_t file_cache_get_size(void *f) {
419     struct file_info *fi = f;
421     assert(fi);
423     return fi->length;