Code

git: remove $Id$ svn cruft
[fusedav.git] / src / filecache.c
index f64f69e7e516b82d272fa24e2120ed25e1878960..3b1e90728a4ccd8fc7b6317198e4102bd6113a39 100644 (file)
@@ -1,21 +1,25 @@
-/* $Id$ */
-
 /***
-  This file is part of fusedav.
-
-  fusedav is free software; you can redistribute it and/or modify it
-  under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
-  (at your option) any later version.
-  
-  fusedav is distributed in the hope that it will be useful, but WITHOUT
-  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
-  License for more details.
-  
-  You should have received a copy of the GNU General Public License
-  along with fusedav; if not, write to the Free Software Foundation,
-  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+  Copyright (c) 2004-2006 Lennart Poettering
+
+  Permission is hereby granted, free of charge, to any person
+  obtaining a copy of this software and associated documentation files
+  (the "Software"), to deal in the Software without restriction,
+  including without limitation the rights to use, copy, modify, merge,
+  publish, distribute, sublicense, and/or sell copies of the Software,
+  and to permit persons to whom the Software is furnished to do so,
+  subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be
+  included in all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+  SOFTWARE.
 ***/
 
 #ifdef HAVE_CONFIG_H
@@ -51,7 +55,7 @@ struct file_info {
     char *filename;
     int fd;
     off_t server_length, length, present;
-    
+
     int readable;
     int writable;
 
@@ -74,9 +78,9 @@ void* file_cache_get(const char *path) {
     struct file_info *f, *r = NULL;
 
     pthread_mutex_lock(&files_mutex);
-    
+
     for (f = files; f; f = f->next) {
-        
+
         pthread_mutex_lock(&f->mutex);
         if (!f->dead && f->filename && !strcmp(path, f->filename)) {
             f->ref++;
@@ -87,7 +91,7 @@ void* file_cache_get(const char *path) {
         if (r)
             break;
     }
-    
+
     pthread_mutex_unlock(&files_mutex);
     return f;
 }
@@ -126,7 +130,7 @@ static void file_cache_unlink(struct file_info *fi) {
     assert(fi);
 
     pthread_mutex_lock(&files_mutex);
-    
+
     for (s = files, prev = NULL; s; s = s->next) {
         if (s == fi) {
             if (prev)
@@ -136,10 +140,10 @@ static void file_cache_unlink(struct file_info *fi) {
 
             break;
         }
-        
+
         prev = s;
     }
-    
+
     pthread_mutex_unlock(&files_mutex);
 }
 
@@ -197,24 +201,24 @@ void* file_cache_open(const char *path, int flags) {
 
     if (!(length = ne_get_response_header(req, "Content-Length")))
         /* dirty hack, since Apache doesn't send the file size if the file is empty */
-        fi->server_length = fi->length = 0; 
+        fi->server_length = fi->length = 0;
     else
         fi->server_length = fi->length = atoi(length);
 
     ne_request_destroy(req);
-    
+
     if (flags & O_RDONLY || flags & O_RDWR) fi->readable = 1;
     if (flags & O_WRONLY || flags & O_RDWR) fi->writable = 1;
 
     pthread_mutex_init(&fi->mutex, NULL);
-    
+
     pthread_mutex_lock(&files_mutex);
     fi->next = files;
     files = fi;
     pthread_mutex_unlock(&files_mutex);
 
     fi->ref = 1;
-    
+
     return fi;
 
 fail:
@@ -228,28 +232,13 @@ fail:
         free(fi->filename);
         free(fi);
     }
-        
+
     return NULL;
 }
 
-#ifdef FIXNEON64
-
-/* We assume that off_t is 64 bit, neon assumes it is 32 bit wide. we
- * need to work around this somehow */
-
-typedef struct {
-    uint32_t start, end, total;
-} ne_content_range64;
-
-#endif
-
 static int load_up_to_unlocked(struct file_info *fi, off_t l) {
 
-#ifdef FIXNEON64
     ne_content_range64 range;
-#else
-    ne_content_range range;
-#endif
     ne_session *session;
 
     assert(fi);
@@ -261,26 +250,18 @@ static int load_up_to_unlocked(struct file_info *fi, off_t l) {
 
     if (l > fi->server_length)
         l = fi->server_length;
-    
+
     if (l <= fi->present)
         return 0;
 
-#ifdef FIXNEON64
-    if (l > UINT_MAX) {
-        /* neon doesn't support 64bit ne_get_range right now */
-        errno = EIO;
-        return -1;
-    }
-#endif
-
     if (lseek(fi->fd, fi->present, SEEK_SET) != fi->present)
         return -1;
-    
+
     range.start = fi->present;
     range.end = l-1;
     range.total = 0;
-    
-    if (ne_get_range(session, fi->filename, (ne_content_range*) &range, fi->fd) != NE_OK) {
+
+    if (ne_get_range64(session, fi->filename, &range, fi->fd) != NE_OK) {
         fprintf(stderr, "GET failed: %s\n", ne_get_error(session));
         errno = ENOENT;
         return -1;
@@ -293,7 +274,7 @@ static int load_up_to_unlocked(struct file_info *fi, off_t l) {
 int file_cache_read(void *f, char *buf, size_t size, off_t offset) {
     struct file_info *fi = f;
     ssize_t r = -1;
-    
+
     assert(fi && buf && size);
 
     pthread_mutex_lock(&fi->mutex);
@@ -305,7 +286,7 @@ int file_cache_read(void *f, char *buf, size_t size, off_t offset) {
         goto finish;
 
 finish:
-    
+
     pthread_mutex_unlock(&fi->mutex);
 
     return r;
@@ -326,7 +307,7 @@ int file_cache_write(void *f, const char *buf, size_t size, off_t offset) {
 
     if (load_up_to_unlocked(fi, offset) < 0)
         goto finish;
-        
+
     if ((r = pwrite(fi->fd, buf, size, offset)) < 0)
         goto finish;
 
@@ -340,7 +321,7 @@ int file_cache_write(void *f, const char *buf, size_t size, off_t offset) {
 
 finish:
     pthread_mutex_unlock(&fi->mutex);
-    
+
     return r;
 }
 
@@ -365,7 +346,7 @@ int file_cache_sync_unlocked(struct file_info *fi) {
     ne_session *session;
 
     assert(fi);
-    
+
     if (!fi->writable) {
         errno = EBADF;
         goto finish;
@@ -375,7 +356,7 @@ int file_cache_sync_unlocked(struct file_info *fi) {
         r = 0;
         goto finish;
     }
-    
+
     if (load_up_to_unlocked(fi, (off_t) -1) < 0)
         goto finish;
 
@@ -386,7 +367,7 @@ int file_cache_sync_unlocked(struct file_info *fi) {
         errno = EIO;
         goto finish;
     }
-    
+
     if (ne_put(session, fi->filename, fi->fd)) {
         fprintf(stderr, "PUT failed: %s\n", ne_get_error(session));
         errno = ENOENT;
@@ -399,7 +380,7 @@ int file_cache_sync_unlocked(struct file_info *fi) {
     r = 0;
 
 finish:
-    
+
     return r;
 }
 
@@ -411,7 +392,7 @@ int file_cache_sync(void *f) {
     pthread_mutex_lock(&fi->mutex);
     r = file_cache_sync_unlocked(fi);
     pthread_mutex_unlock(&fi->mutex);
-    
+
     return r;
 }
 
@@ -422,7 +403,7 @@ int file_cache_close_all(void) {
 
     while (files) {
         struct file_info *fi = files;
-        
+
         pthread_mutex_lock(&fi->mutex);
         fi->ref++;
         pthread_mutex_unlock(&fi->mutex);