diff --git a/src/fusedav.c b/src/fusedav.c
index 11f7ae7796f45160f10a667f948b1f15fdf936c2..5620811e8f5f154bc61111316e9a71462ca7feff 100644 (file)
--- a/src/fusedav.c
+++ b/src/fusedav.c
-/* $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
#include "statcache.h"
#include "filecache.h"
#include "session.h"
-#include "openssl-thread.h"
#include "fusedav.h"
const ne_propname query_properties[] = {
int l;
pthread_once(&path_cvt_once, path_cvt_tsd_key_init);
-
+
if ((r = pthread_getspecific(path_cvt_tsd_key)))
free(r);
free(t);
pthread_setspecific(path_cvt_tsd_key, r);
-
+
return r;
}
void *userdata) {
int i, ret;
-
+
for (i = 0; i < MAX_REDIRECTS; i++) {
const ne_uri *u;
if (debug)
fprintf(stderr, "REDIRECT FROM '%s' to '%s'\n", path, u->path);
-
+
path = u->path;
}
ne_session *session,
const char *path,
const ne_proppatch_operation *ops) {
-
+
int i, ret;
-
+
for (i = 0; i < MAX_REDIRECTS; i++) {
const ne_uri *u;
if (debug)
fprintf(stderr, "REDIRECT FROM '%s' to '%s'\n", path, u->path);
-
+
path = u->path;
}
@@ -182,7 +185,7 @@ static void fill_stat(struct stat* st, const ne_prop_result_set *results, int is
const ne_propname getcontentlength = { "DAV:", "getcontentlength" };
const ne_propname getlastmodified = { "DAV:", "getlastmodified" };
const ne_propname creationdate = { "DAV:", "creationdate" };
-
+
assert(st && results);
rt = ne_propset_value(results, &resourcetype);
@@ -192,7 +195,7 @@ static void fill_stat(struct stat* st, const ne_prop_result_set *results, int is
cd = ne_propset_value(results, &creationdate);
memset(st, 0, sizeof(struct stat));
-
+
if (is_dir) {
st->st_mode = S_IFDIR | 0777;
st->st_nlink = 3; /* find will ignore this directory if nlin <= and st_size == 0 */
@@ -206,12 +209,12 @@ static void fill_stat(struct stat* st, const ne_prop_result_set *results, int is
st->st_atime = time(NULL);
st->st_mtime = glm ? ne_rfc1123_parse(glm) : 0;
st->st_ctime = cd ? ne_iso8601_parse(cd) : 0;
-
+
st->st_blocks = (st->st_size+511)/512;
/*fprintf(stderr, "a: %u; m: %u; c: %u\n", st->st_atime, st->st_mtime, st->st_ctime);*/
st->st_mode &= ~mask;
-
+
st->st_uid = getuid();
st->st_gid = getgid();
}
assert(fn);
assert(is_dir);
assert(l > 0);
-
+
if ((*is_dir = (fn[l-1] == '/')))
fn[l-1] = 0;
return fn;
}
-static void getdir_propfind_callback(void *userdata, const char *href, const ne_prop_result_set *results) {
+static void getdir_propfind_callback(void *userdata, const ne_uri *u, const ne_prop_result_set *results) {
struct fill_info *f = userdata;
struct stat st;
char fn[PATH_MAX], *t;
@@ -236,13 +239,13 @@ static void getdir_propfind_callback(void *userdata, const char *href, const ne_
assert(f);
- strncpy(fn, href, sizeof(fn));
+ strncpy(fn, u->path, sizeof(fn));
fn[sizeof(fn)-1] = 0;
strip_trailing_slash(fn, &is_dir);
if (strcmp(fn, f->root) && fn[0]) {
char *h;
-
+
if ((t = strrchr(fn, '/')))
t++;
else
const char *root,
const char *fn,
void *user) {
-
+
struct fill_info *f = user;
char path[PATH_MAX];
char *h;
assert(f);
-
+
snprintf(path, sizeof(path), "%s/%s", !strcmp(root, "/") ? "" : root, fn);
-
+
f->filler(f->buf, h = ne_path_unescape(fn), NULL, 0);
free(h);
}
fuse_fill_dir_t filler,
__unused off_t offset,
__unused struct fuse_file_info *fi) {
-
+
struct fill_info f;
ne_session *session;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
-
+
if (dir_cache_enumerate(path, getdir_cache_callback, &f) < 0) {
if (debug)
fprintf(stderr, "DIR-CACHE-MISS\n");
-
- if (!(session = session_get(1)))
+
+ if (!(session = session_get(1)))
return -EIO;
dir_cache_begin(path);
-
+
if (simple_propfind_with_redirect(session, path, NE_DEPTH_ONE, query_properties, getdir_propfind_callback, &f) != NE_OK) {
dir_cache_finish(path, 2);
fprintf(stderr, "PROPFIND failed: %s\n", ne_get_error(session));
return 0;
}
-static void getattr_propfind_callback(void *userdata, const char *href, const ne_prop_result_set *results) {
+static void getattr_propfind_callback(void *userdata, const ne_uri *u, const ne_prop_result_set *results) {
struct stat *st = (struct stat*) userdata;
char fn[PATH_MAX];
int is_dir;
assert(st);
- strncpy(fn, href, sizeof(fn));
+ strncpy(fn, u->path, sizeof(fn));
fn[sizeof(fn)-1] = 0;
strip_trailing_slash(fn, &is_dir);
-
+
fill_stat(st, results, is_dir);
stat_cache_set(fn, st);
}
@@ -336,7 +339,7 @@ static void getattr_propfind_callback(void *userdata, const char *href, const ne
static int get_stat(const char *path, struct stat *stbuf) {
ne_session *session;
- if (!(session = session_get(1)))
+ if (!(session = session_get(1)))
return -EIO;
if (stat_cache_get(path, stbuf) == 0) {
if (debug)
fprintf(stderr, "unlink(%s)\n", path);
- if (!(session = session_get(1)))
+ if (!(session = session_get(1)))
return -EIO;
if ((r = get_stat(path, &st)) < 0)
if (!S_ISREG(st.st_mode))
return -EISDIR;
-
+
if (ne_delete(session, path)) {
fprintf(stderr, "DELETE failed: %s\n", ne_get_error(session));
return -ENOENT;
stat_cache_invalidate(path);
dir_cache_invalidate_parent(path);
-
+
return 0;
}
if (debug)
fprintf(stderr, "rmdir(%s)\n", path);
- if (!(session = session_get(1)))
+ if (!(session = session_get(1)))
return -EIO;
if ((r = get_stat(path, &st)) < 0)
return -ENOTDIR;
snprintf(fn, sizeof(fn), "%s/", path);
-
+
if (ne_delete(session, fn)) {
fprintf(stderr, "DELETE failed: %s\n", ne_get_error(session));
return -ENOENT;
if (debug)
fprintf(stderr, "mkdir(%s)\n", path);
- if (!(session = session_get(1)))
+ if (!(session = session_get(1)))
return -EIO;
snprintf(fn, sizeof(fn), "%s/", path);
-
+
if (ne_mkcol(session, fn)) {
fprintf(stderr, "MKCOL failed: %s\n", ne_get_error(session));
return -ENOENT;
stat_cache_invalidate(path);
dir_cache_invalidate_parent(path);
-
+
return 0;
}
snprintf(fn, sizeof(fn), "%s/", from);
from = fn;
}
-
+
if (ne_move(session, 1, from, to)) {
fprintf(stderr, "MOVE failed: %s\n", ne_get_error(session));
r = -ENOENT;
goto finish;
}
-
+
stat_cache_invalidate(from);
stat_cache_invalidate(to);
finish:
free(_from);
-
+
return r;
}
@@ -509,7 +512,7 @@ static int dav_release(const char *path, __unused struct fuse_file_info *info) {
r = -EIO;
goto finish;
}
-
+
if (!(f = file_cache_get(path))) {
fprintf(stderr, "release() called for closed file\n");
r = -EFAULT;
@@ -524,7 +527,7 @@ static int dav_release(const char *path, __unused struct fuse_file_info *info) {
finish:
if (f)
file_cache_unref(f);
-
+
return r;
}
}
finish:
-
+
if (f)
file_cache_unref(f);
if (debug)
fprintf(stderr, "mknod(%s)\n", path);
- if (!(session = session_get(1)))
+ if (!(session = session_get(1)))
return -EIO;
if (!S_ISREG(mode))
snprintf(tempfile, sizeof(tempfile), "%s/fusedav-empty-XXXXXX", "/tmp");
if ((fd = mkstemp(tempfile)) < 0)
return -errno;
-
+
unlink(tempfile);
-
+
if (ne_put(session, path, fd)) {
fprintf(stderr, "mknod:PUT failed: %s\n", ne_get_error(session));
close(fd);
static int dav_read(const char *path, char *buf, size_t size, off_t offset, __unused struct fuse_file_info *info) {
void *f = NULL;
ssize_t r;
-
+
path = path_cvt(path);
-
+
if (debug)
fprintf(stderr, "read(%s, %lu+%lu)\n", path, (unsigned long) offset, (unsigned long) size);
-
+
if (!(f = file_cache_get(path))) {
fprintf(stderr, "read() called for closed file\n");
r = -EFAULT;
@@ -635,7 +638,7 @@ static int dav_read(const char *path, char *buf, size_t size, off_t offset, __un
finish:
if (f)
file_cache_unref(f);
-
+
return r;
}
@@ -658,11 +661,11 @@ static int dav_write(const char *path, const char *buf, size_t size, off_t offse
r = -errno;
goto finish;
}
-
+
finish:
if (f)
file_cache_unref(f);
-
+
return r;
}
void *f = NULL;
int r = 0;
ne_session *session;
-
+
path = path_cvt(path);
-
+
if (debug)
fprintf(stderr, "truncate(%s, %lu)\n", path, (unsigned long) size);
if (!(session = session_get(1)))
r = -EIO;
goto finish;
-
+
if (!(f = file_cache_get(path))) {
fprintf(stderr, "truncate() called for closed file\n");
r = -EFAULT;
finish:
if (f)
file_cache_unref(f);
-
+
return r;
}
ne_proppatch_operation ops[2];
int r = 0;
char *date;
-
+
assert(path);
assert(buf);
path = path_cvt(path);
-
+
if (debug)
fprintf(stderr, "utime(%s, %lu, %lu)\n", path, (unsigned long) buf->actime, (unsigned long) buf->modtime);
-
+
ops[0].name = &getlastmodified;
ops[0].type = ne_propset;
ops[0].value = date = ne_rfc1123_date(buf->modtime);
r = -ENOTSUP;
goto finish;
}
-
+
stat_cache_invalidate(path);
finish:
free(date);
-
+
return r;
}
struct listxattr_info *l = userdata;
int n;
-
+
assert(l);
if (!value || !pname)
if (l->list) {
n = snprintf(l->list, l->space, "user.webdav(%s;%s)", pname->nspace, pname->name) + 1;
-
+
if (n >= (int) l->space) {
l->size += l->space;
l->space = 0;
return 1;
-
+
} else {
l->size += n;
l->space -= n;
-
+
if (l->list)
l->list += n;
-
+
return 0;
}
} else {
/* Calculate space */
-
+
l->size += strlen(pname->nspace) + strlen(pname->name) + 15;
return 0;
}
}
-static void listxattr_propfind_callback(void *userdata, __unused const char *href, const ne_prop_result_set *results) {
+static void listxattr_propfind_callback(void *userdata, __unused const ne_uri *u, const ne_prop_result_set *results) {
struct listxattr_info *l = userdata;
ne_propset_iterate(results, listxattr_iterator, l);
}
const char *path,
char *list,
size_t size) {
-
+
ne_session *session;
struct listxattr_info l;
-
+
assert(path);
l.space -= sizeof(MIME_XATTR);
l.size += sizeof(MIME_XATTR);
}
-
+
} else {
l.list = NULL;
l.space = 0;
l.size = sizeof(MIME_XATTR);
}
-
- if (!(session = session_get(1)))
+
+ if (!(session = session_get(1)))
return -EIO;
if (simple_propfind_with_redirect(session, path, NE_DEPTH_ZERO, NULL, listxattr_propfind_callback, &l) != NE_OK) {
__unused const ne_status *status) {
struct getxattr_info *g = userdata;
-
+
assert(g);
if (!value || !pname)
g->size = l;
} else {
/* Calculate space */
-
+
g->size = strlen(value);
return 0;
}
-
+
return 0;
}
-static void getxattr_propfind_callback(void *userdata, __unused const char *href, const ne_prop_result_set *results) {
+static void getxattr_propfind_callback(void *userdata, __unused const ne_uri *u, const ne_prop_result_set *results) {
struct getxattr_info *g = userdata;
ne_propset_iterate(results, getxattr_iterator, g);
}
@@ -897,13 +900,13 @@ static void getxattr_propfind_callback(void *userdata, __unused const char *href
static int parse_xattr(const char *name, char *dnspace, size_t dnspace_length, char *dname, size_t dname_length) {
char *e;
size_t k;
-
+
assert(name);
assert(dnspace);
assert(dnspace_length);
assert(dname);
assert(dname_length);
-
+
if (strncmp(name, "user.webdav(", 12) ||
name[strlen(name)-1] != ')' ||
!(e = strchr(name+12, ';')))
@@ -916,7 +919,7 @@ static int parse_xattr(const char *name, char *dnspace, size_t dnspace_length, c
dnspace[k] = 0;
e++;
-
+
if ((k = strlen(e)) > dname_length-1)
return -1;
struct getxattr_info g;
ne_propname props[2];
char dnspace[128], dname[128];
-
+
assert(path);
path = path_cvt(path);
}
g.propname = props[0];
-
+
if (!(session = session_get(1)))
return -EIO;
path = path_cvt(path);
name = fix_xattr(name);
-
+
if (debug)
fprintf(stderr, "setxattr(%s, %s)\n", path, name);
r = ENOTSUP;
goto finish;
}
-
+
if (parse_xattr(name, dnspace, sizeof(dnspace), dname, sizeof(dname)) < 0) {
r = -ENOATTR;
goto finish;
propname.nspace = dnspace;
propname.name = dname;
-
+
/* Add trailing NUL byte if required */
if (!memchr(value, 0, size)) {
value_fixed = malloc(size+1);
assert(value_fixed);
-
+
memcpy(value_fixed, value, size);
value_fixed[size] = 0;
value = value_fixed;
}
-
+
ops[0].name = &propname;
ops[0].type = ne_propset;
ops[0].value = value;
-
+
ops[1].name = NULL;
if (!(session = session_get(1))) {
r = -EIO;
goto finish;
}
-
+
if (proppatch_with_redirect(session, path, ops)) {
fprintf(stderr, "PROPPATCH failed: %s\n", ne_get_error(session));
r = -ENOTSUP;
goto finish;
}
-
+
stat_cache_invalidate(path);
finish:
free(value_fixed);
-
+
return r;
}
path = path_cvt(path);
name = fix_xattr(name);
-
+
if (debug)
fprintf(stderr, "removexattr(%s, %s)\n", path, name);
propname.nspace = dnspace;
propname.name = dname;
-
+
ops[0].name = &propname;
ops[0].type = ne_propremove;
ops[0].value = NULL;
-
+
ops[1].name = NULL;
if (!(session = session_get(1))) {
r = -EIO;
goto finish;
}
-
+
if (proppatch_with_redirect(session, path, ops)) {
fprintf(stderr, "PROPPATCH failed: %s\n", ne_get_error(session));
r = -ENOTSUP;
goto finish;
}
-
+
stat_cache_invalidate(path);
finish:
-
+
return r;
}
const ne_propname executable = { "http://apache.org/dav/props/", "executable" };
ne_proppatch_operation ops[2];
int r = 0;
-
+
assert(path);
path = path_cvt(path);
-
+
if (debug)
fprintf(stderr, "chmod(%s, %04o)\n", path, mode);
-
+
ops[0].name = &executable;
ops[0].type = ne_propset;
ops[0].value = mode & 0111 ? "T" : "F";
r = -ENOTSUP;
goto finish;
}
-
+
stat_cache_invalidate(path);
finish:
-
+
return r;
}
e++;
else
e = argv0;
-
+
fprintf(stderr,
"%s [-hDL] [-t SECS] [-u USERNAME] [-p PASSWORD] [-o OPTIONS] URL MOUNTPOINT\n"
"\t-h Show this help\n"
static int setup_signal_handlers(void) {
struct sigaction sa;
sigset_t m;
-
+
sa.sa_handler = exit_handler;
sigemptyset(&(sa.sa_mask));
sa.sa_flags = 0;
-
+
if (sigaction(SIGHUP, &sa, NULL) == -1 ||
sigaction(SIGINT, &sa, NULL) == -1 ||
sigaction(SIGTERM, &sa, NULL) == -1) {
-
+
fprintf(stderr, "Cannot set exit signal handlers: %s\n", strerror(errno));
return -1;
}
-
+
sa.sa_handler = SIG_IGN;
-
+
if (sigaction(SIGPIPE, &sa, NULL) == -1) {
fprintf(stderr, "Cannot set ignored signals: %s\n", strerror(errno));
return -1;
sigaddset(&m, SIGPIPE);
sigaddset(&m, SIGUSR1);
pthread_sigmask(SIG_SETMASK, &m, NULL);
-
+
return 0;
}
char _owner[64], *owner;
int i;
int ret;
-
+
lock = ne_lock_create();
assert(lock);
if (!(session = session_get(0)))
return -1;
- if (!(owner = getenv("USER")))
- if (!(owner = getenv("LOGNAME"))) {
- snprintf(_owner, sizeof(_owner), "%lu", (unsigned long) getuid());
- owner = owner;
- }
+ if (!(owner = username))
+ if (!(owner = getenv("USER")))
+ if (!(owner = getenv("LOGNAME"))) {
+ snprintf(_owner, sizeof(_owner), "%lu", (unsigned long) getuid());
+ owner = owner;
+ }
ne_fill_server_uri(session, &lock->uri);
-
+
lock->uri.path = strdup(base_directory);
lock->depth = NE_DEPTH_INFINITE;
lock->timeout = lock_timeout;
if (debug)
fprintf(stderr, "Acquiring lock...\n");
-
+
for (i = 0; i < MAX_REDIRECTS; i++) {
const ne_uri *u;
lock_store = ne_lockstore_create();
assert(lock_store);
-
+
ne_lockstore_add(lock_store, lock);
-
+
return 0;
}
while (!lock_thread_exit) {
int r, t;
-
+
lock->timeout = lock_timeout;
pthread_sigmask(SIG_BLOCK, &block, NULL);
t = 1;
sleep(t);
}
-
+
if (debug)
fprintf(stderr, "lock_thread exiting\n");
pthread_t lock_thread;
int lock_thread_running = 0;
int enable_locking = 0;
-
+
static char *mount_args_strings[] = {
NULL, /* path*/
NULL, /* -o */
NULL,
NULL};
-
+
struct fuse_args mount_args = {
.argc = 1,
.argv = mount_args_strings,
.allocated = 0
};
-
+
if (ne_sock_init()) {
fprintf(stderr, "Failed to initialize libneon.\n");
goto finish;
}
- openssl_thread_setup();
+ if (!ne_has_support(NE_FEATURE_SSL) ||
+ !ne_has_support(NE_FEATURE_TS_SSL) ||
+ !ne_has_support(NE_FEATURE_LFS)) {
+ fprintf(stderr, "fusedav requires libneon built with SSL, SSL thread safety and LFS enabled.\n");
+ goto finish;
+ }
mask = umask(0);
umask(mask);
if (setup_signal_handlers() < 0)
goto finish;
-
+
while ((c = getopt(argc, argv, "hu:p:Do:Lt:")) != -1) {
switch(c) {
case 'u':
u = optarg;
break;
-
+
case 'p':
p = optarg;
break;
-
+
case 'D':
debug = !debug;
break;
goto finish;
}
break;
-
+
case 'h':
ret = 0;
mount_args_strings[2] = o;
mount_args.argc += 2;
}
-
+
if ((fuse_fd = fuse_mount(mountpoint, &mount_args)) < 0) {
fprintf(stderr, "Failed to mount FUSE file system.\n");
goto finish;
fprintf(stderr, "Failed to create FUSE object.\n");
goto finish;
}
-
+
if (enable_locking && create_lock() >= 0) {
int r;
if ((r = pthread_create(&lock_thread, NULL, lock_thread_func, NULL)) < 0) {
fprintf(stderr, "pthread_create(): %s\n", strerror(r));
goto finish;
}
-
+
lock_thread_running = 1;
}
-
+
fuse_loop_mt(fuse);
if (debug)
fprintf(stderr, "Exiting cleanly.\n");
-
+
ret = 0;
-
+
finish:
-
+
if (lock_thread_running) {
lock_thread_exit = 1;
pthread_kill(lock_thread, SIGUSR1);
if (fuse)
fuse_destroy(fuse);
-
+
if (fuse_fd >= 0)
fuse_unmount(mountpoint);
-
+
file_cache_close_all();
cache_free();
session_free();
- openssl_thread_cleanup();
-
+
return ret;
}