1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/* $Id: filecache.c 25 2006-04-02 14:03:08Z lennart $ */
/***
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
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <inttypes.h>
#include <limits.h>
#include <ne_props.h>
#include <ne_uri.h>
#include <ne_session.h>
#include <ne_utils.h>
#include <ne_socket.h>
#include <ne_auth.h>
#include <ne_dates.h>
#include <ne_basic.h>
#include "filecache.h"
#include "statcache.h"
#include "fusedav.h"
#include "session.h"
struct file_info {
char *filename;
int fd;
off_t server_length, length, present;
int readable;
int writable;
int modified;
int ref, dead;
pthread_mutex_t mutex;
/* This field is locked by files_mutex, not by file_info->mutex */
struct file_info *next;
};
static struct file_info *files = NULL;
static pthread_mutex_t files_mutex = PTHREAD_MUTEX_INITIALIZER;
static int file_cache_sync_unlocked(struct file_info *fi);
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++;
r = f;
}
pthread_mutex_unlock(&f->mutex);
if (r)
break;
}
pthread_mutex_unlock(&files_mutex);
return f;
}
static void file_cache_free_unlocked(struct file_info *fi) {
assert(fi && fi->dead && fi->ref == 0);
free(fi->filename);
if (fi->fd >= 0)
close(fi->fd);
pthread_mutex_destroy(&fi->mutex);
free(fi);
}
void file_cache_unref(void *f) {
struct file_info *fi = f;
assert(fi);
pthread_mutex_lock(&fi->mutex);
assert(fi->ref >= 1);
fi->ref--;
if (!fi->ref && fi->dead) {
file_cache_sync_unlocked(fi);
file_cache_free_unlocked(fi);
}
pthread_mutex_unlock(&fi->mutex);
}
static void file_cache_unlink(struct file_info *fi) {
struct file_info *s, *prev;
assert(fi);
pthread_mutex_lock(&files_mutex);
for (s = files, prev = NULL; s; s = s->next) {
if (s == fi) {
if (prev)
prev->next = s->next;
else
files = s->next;
break;
}
prev = s;
}
pthread_mutex_unlock(&files_mutex);
}
int file_cache_close(void *f) {
struct file_info *fi = f;
int r = 0;
assert(fi);
file_cache_unlink(f);
pthread_mutex_lock(&fi->mutex);
fi->dead = 1;
pthread_mutex_unlock(&fi->mutex);
return r;
}
void* file_cache_open(const char *path, int flags) {
struct file_info *fi = NULL;
char tempfile[PATH_MAX];
const char *length = NULL;
ne_request *req = NULL;
ne_session *session;
if (!(session = session_get(1))) {
errno = EIO;
goto fail;
}
if ((fi = file_cache_get(path))) {
if (flags & O_RDONLY || flags & O_RDWR) fi->readable = 1;
if (flags & O_WRONLY || flags & O_RDWR) fi->writable = 1;
return fi;
}
fi = malloc(sizeof(struct file_info));
memset(fi, 0, sizeof(struct file_info));
fi->fd = -1;
fi->filename = strdup(path);
snprintf(tempfile, sizeof(tempfile), "%s/fusedav-cache-XXXXXX", "/tmp");
if ((fi->fd = mkstemp(tempfile)) < 0)
goto fail;
unlink(tempfile);
req = ne_request_create(session, "HEAD", path);
assert(req);
if (ne_request_dispatch(req) != NE_OK) {
fprintf(stderr, "HEAD failed: %s\n", ne_get_error(session));
errno = ENOENT;
goto fail;
}
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;
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:
if (req)
ne_request_destroy(req);
if (fi) {
if (fi->fd >= 0)
close(fi->fd);
free(fi->filename);
free(fi);
}
return NULL;
}
static int load_up_to_unlocked(struct file_info *fi, off_t l) {
ne_content_range64 range;
ne_session *session;
assert(fi);
if (!(session = session_get(1))) {
errno = EIO;
return -1;
}
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;
return -1;
}
fi->present = l;
return 0;
}
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);
if (load_up_to_unlocked(fi, offset+size) < 0)
goto finish;
if ((r = pread(fi->fd, buf, size, offset)) < 0)
goto finish;
finish:
pthread_mutex_unlock(&fi->mutex);
return r;
}
int file_cache_write(void *f, const char *buf, size_t size, off_t offset) {
struct file_info *fi = f;
ssize_t r = -1;
assert (fi);
pthread_mutex_lock(&fi->mutex);
if (!fi->writable) {
errno = EBADF;
goto finish;
}
if (load_up_to_unlocked(fi, offset) < 0)
goto finish;
if ((r = pwrite(fi->fd, buf, size, offset)) < 0)
goto finish;
if (offset+size > fi->present)
fi->present = offset+size;
if (offset+size > fi->length)
fi->length = offset+size;
fi->modified = 1;
finish:
pthread_mutex_unlock(&fi->mutex);
return r;
}
int file_cache_truncate(void *f, off_t s) {
struct file_info *fi = f;
int r;
assert(fi);
pthread_mutex_lock(&fi->mutex);
fi->length = s;
r = ftruncate(fi->fd, fi->length);
pthread_mutex_unlock(&fi->mutex);
return r;
}
int file_cache_sync_unlocked(struct file_info *fi) {
int r = -1;
ne_session *session;
assert(fi);
if (!fi->writable) {
errno = EBADF;
goto finish;
}
if (!fi->modified) {
r = 0;
goto finish;
}
if (load_up_to_unlocked(fi, (off_t) -1) < 0)
goto finish;
if (lseek(fi->fd, 0, SEEK_SET) == (off_t)-1)
goto finish;
if (!(session = session_get(1))) {
errno = EIO;
goto finish;
}
if (ne_put(session, fi->filename, fi->fd)) {
fprintf(stderr, "PUT failed: %s\n", ne_get_error(session));
errno = ENOENT;
goto finish;
}
stat_cache_invalidate(fi->filename);
dir_cache_invalidate_parent(fi->filename);
r = 0;
finish:
return r;
}
int file_cache_sync(void *f) {
struct file_info *fi = f;
int r = -1;
assert(fi);
pthread_mutex_lock(&fi->mutex);
r = file_cache_sync_unlocked(fi);
pthread_mutex_unlock(&fi->mutex);
return r;
}
int file_cache_close_all(void) {
int r = 0;
pthread_mutex_lock(&files_mutex);
while (files) {
struct file_info *fi = files;
pthread_mutex_lock(&fi->mutex);
fi->ref++;
pthread_mutex_unlock(&fi->mutex);
pthread_mutex_unlock(&files_mutex);
file_cache_close(fi);
file_cache_unref(fi);
pthread_mutex_lock(&files_mutex);
}
pthread_mutex_unlock(&files_mutex);
return r;
}
off_t file_cache_get_size(void *f) {
struct file_info *fi = f;
assert(fi);
return fi->length;
}
|