summaryrefslogtreecommitdiffstats
path: root/src/filecache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/filecache.c')
-rw-r--r--src/filecache.c50
1 files changed, 25 insertions, 25 deletions
diff --git a/src/filecache.c b/src/filecache.c
index 5bf7dfe..dfbbae8 100644
--- a/src/filecache.c
+++ b/src/filecache.c
@@ -57,7 +57,7 @@ struct file_info {
char *filename;
int fd;
off_t server_length, length, present;
-
+
int readable;
int writable;
@@ -80,9 +80,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++;
@@ -93,7 +93,7 @@ void* file_cache_get(const char *path) {
if (r)
break;
}
-
+
pthread_mutex_unlock(&files_mutex);
return f;
}
@@ -132,7 +132,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)
@@ -142,10 +142,10 @@ static void file_cache_unlink(struct file_info *fi) {
break;
}
-
+
prev = s;
}
-
+
pthread_mutex_unlock(&files_mutex);
}
@@ -203,24 +203,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:
@@ -234,7 +234,7 @@ fail:
free(fi->filename);
free(fi);
}
-
+
return NULL;
}
@@ -252,17 +252,17 @@ 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;
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_range64(session, fi->filename, &range, fi->fd) != NE_OK) {
fprintf(stderr, "GET failed: %s\n", ne_get_error(session));
errno = ENOENT;
@@ -276,7 +276,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);
@@ -288,7 +288,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;
@@ -309,7 +309,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;
@@ -323,7 +323,7 @@ int file_cache_write(void *f, const char *buf, size_t size, off_t offset) {
finish:
pthread_mutex_unlock(&fi->mutex);
-
+
return r;
}
@@ -348,7 +348,7 @@ int file_cache_sync_unlocked(struct file_info *fi) {
ne_session *session;
assert(fi);
-
+
if (!fi->writable) {
errno = EBADF;
goto finish;
@@ -358,7 +358,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;
@@ -369,7 +369,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;
@@ -382,7 +382,7 @@ int file_cache_sync_unlocked(struct file_info *fi) {
r = 0;
finish:
-
+
return r;
}
@@ -394,7 +394,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;
}
@@ -405,7 +405,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);