src/server/daemon/vfs.c

Mon, 06 May 2013 14:54:40 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 06 May 2013 14:54:40 +0200
changeset 60
feb2f1e115c6
parent 59
ab25c0a231d0
child 63
66442f81f823
permissions
-rw-r--r--

improved logging

54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <unistd.h>
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <sys/types.h>
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "../util/pool.h"
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
35 #include "../ucx/map.h"
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "vfs.h"
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
38 static UcxMap *vfs_map;
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
39
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 static VFS_IO sys_file_io = {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 sys_file_read,
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 sys_file_write,
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 sys_file_close
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 };
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
46 static VFS_DIRIO sys_dir_io = {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
47 sys_dir_read,
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
48 sys_dir_close
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
49 };
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
50
59
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
51 int vfs_init() {
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
52 vfs_map = ucx_map_new(16);
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
53 if(!vfs_map) {
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
54 return -1;
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
55 }
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
56 return 0;
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
57 }
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
58
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
59 void vfs_add(char *name, VFS *vfs) {
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
60 if(!vfs_map) {
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
61 vfs_init();
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
62 }
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
63 ucx_map_cstr_put(vfs_map, name, vfs);
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
64 }
ab25c0a231d0 some fixes and new public APIs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 58
diff changeset
65
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 VFSContext* vfs_request_context(Session *sn, Request *rq) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 VFSContext *ctx = pool_malloc(sn->pool, sizeof(VFSContext));
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 ctx->sn = sn;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 ctx->rq = rq;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 ctx->vfs = rq->vfs;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 ctx->user = acllist_getuser(sn, rq, rq->acllist);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 ctx->acllist = rq->acllist;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 ctx->aclreqaccess = rq->aclreqaccess;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 ctx->pool = sn->pool;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 ctx->vfs_errno = 0;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 return ctx;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 SYS_FILE vfs_open(VFSContext *ctx, char *path, int oflags) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 Session *sn;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 Request *rq;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 pool_handle_t *pool;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 uint32_t access_mask;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 if(ctx) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 access_mask = ctx->aclreqaccess;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 access_mask |= acl_oflag2mask(oflags);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 if(!ctx->pool) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 // TODO: log warning
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 // broken VFSContext
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 if(ctx->vfs) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 // ctx->aclreqaccess should be the complete access mask
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 uint32_t m = ctx->aclreqaccess; // save original access mask
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 ctx->aclreqaccess = access_mask; // set mask for vfs->open call
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 SYS_FILE file = ctx->vfs->open(ctx, path, oflags);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 ctx->aclreqaccess = m; // restore original access mask
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 return file;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 } else {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 pool = ctx->pool;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 } else {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 sn = NULL;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 rq = NULL;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 pool = NULL;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 access_mask = acl_oflag2mask(oflags);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 // check ACLs
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 uid_t uid; // uid and gid will be initialized by sys_acl_check
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 gid_t gid;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 if(sys_acl_check(ctx, access_mask, &uid, &gid)) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 return NULL;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 // open file
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
117 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
118 int fd = open(path, oflags, mode);
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 if(fd == -1) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 if(ctx) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 ctx->vfs_errno = errno;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 sys_set_error_status(ctx);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 return NULL;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 VFSFile *file = pool ?
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 pool_malloc(pool, sizeof(VFSFile)) : malloc(sizeof(VFSFile));
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 if(!file) {
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
130 close(fd);
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 return NULL;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 file->ctx = ctx;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 file->data = NULL;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 file->fd = fd;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 file->io = &sys_file_io;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 return file;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 SYS_FILE vfs_openRO(VFSContext *ctx, char *path) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 return vfs_open(ctx, path, O_RDONLY);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 SYS_FILE vfs_openWO(VFSContext *ctx, char *path) {
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
145 return vfs_open(ctx, path, O_WRONLY | O_CREAT);
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 SYS_FILE vfs_openRW(VFSContext *ctx, char *path) {
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
149 return vfs_open(ctx, path, O_RDONLY | O_WRONLY | O_CREAT);
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 int vfs_stat(VFSContext *ctx, char *path, struct stat *buf) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 Session *sn;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 Request *rq;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 uint32_t access_mask;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 if(ctx) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 access_mask = ctx->aclreqaccess;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 access_mask |= ACL_READ_ATTRIBUTES;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 if(!ctx->pool) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 // TODO: log warning
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 // broken VFSContext
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 if(ctx->vfs) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 // ctx->aclreqaccess should be the complete access mask
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 uint32_t m = ctx->aclreqaccess; // save original access mask
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 ctx->aclreqaccess = access_mask; // set mask for vfs->fstat call
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 int ret = ctx->vfs->stat(ctx, path, buf);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 ctx->aclreqaccess = m; // restore original access mask
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 return ret;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 } else {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 sn = NULL;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 rq = NULL;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 access_mask = ACL_READ_ATTRIBUTES;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 // check ACLs
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 uid_t uid; // uid and gid will be initialized by sys_acl_check
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 gid_t gid;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 if(sys_acl_check(ctx, access_mask, &uid, &gid)) {
57
b3a89736b23e some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
182 return -1;
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 // stat
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 if(stat(path, buf)) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 if(ctx) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 ctx->vfs_errno = errno;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 sys_set_error_status(ctx);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 return -1;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 return 0;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 int vfs_fstat(VFSContext *ctx, SYS_FILE fd, struct stat *buf) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 if(ctx) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 if(!ctx->pool) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 // TODO: log warning
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 // broken VFSContext
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 if(ctx->vfs) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 return ctx->vfs->fstat(ctx, fd, buf);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 // stat
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 if(fstat(fd->fd, buf)) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 if(ctx) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 ctx->vfs_errno = errno;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 return -1;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 return 0;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 void vfs_close(SYS_FILE fd) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 fd->io->close(fd);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 if(fd->ctx) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 pool_free(fd->ctx->pool, fd);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 } else {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 free(fd);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
228 VFS_DIR vfs_opendir(VFSContext *ctx, char *path) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
229 Session *sn;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
230 Request *rq;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
231 pool_handle_t *pool;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
232 uint32_t access_mask;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
233
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
234 if(ctx) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
235 access_mask = ctx->aclreqaccess;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
236 access_mask |= ACL_LIST;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
237 if(!ctx->pool) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
238 // TODO: log warning
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
239 // broken VFSContext
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
240 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
241 if(ctx->vfs) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
242 // ctx->aclreqaccess should be the complete access mask
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
243 uint32_t m = ctx->aclreqaccess; // save original access mask
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
244 ctx->aclreqaccess = access_mask; // set mask for vfs->opendir call
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
245 VFS_DIR dir = ctx->vfs->opendir(ctx, path);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
246 ctx->aclreqaccess = m; // restore original access mask
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
247 return dir;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
248 } else {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
249 pool = ctx->pool;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
250 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
251 } else {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
252 sn = NULL;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
253 rq = NULL;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
254 pool = NULL;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
255 access_mask = ACL_LIST;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
256 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
257
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
258 // check ACLs
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
259 uid_t uid; // uid and gid will be initialized by sys_acl_check
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
260 gid_t gid;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
261 if(sys_acl_check(ctx, access_mask, &uid, &gid)) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
262 return NULL;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
263 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
264
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
265 // open file
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
266 int sys_fd = open(path, O_RDONLY);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
267 if(sys_fd == -1) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
268 if(ctx) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
269 ctx->vfs_errno = errno;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
270 sys_set_error_status(ctx);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
271 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
272 return NULL;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
273 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
274
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
275 DIR *sys_dir = fdopendir(sys_fd);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
276 if(!sys_dir) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
277 if(ctx) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
278 ctx->vfs_errno = errno;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
279 sys_set_error_status(ctx);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
280 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
281 return NULL;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
282 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
283
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
284 VFSDir *dir = pool ?
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
285 pool_malloc(pool, sizeof(VFSDir)) : malloc(sizeof(VFSDir));
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
286 if(!dir) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
287 closedir(sys_dir);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
288 return NULL;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
289 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
290 dir->ctx = ctx;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
291 dir->data = sys_dir;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
292 dir->fd = sys_fd;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
293 dir->io = &sys_dir_io;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
294 return dir;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
295 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
296
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
297 int vfs_readdir(VFS_DIR dir, VFS_ENTRY *entry) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
298 return dir->io->readdir(dir, entry, 0);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
299 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
300
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
301 int vfs_readdir_stat(VFS_DIR dir, VFS_ENTRY *entry) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
302 return dir->io->readdir(dir, entry, 1);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
303 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
304
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
305 void vfs_closedir(VFS_DIR dir) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
306 dir->io->close(dir);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
307 if(dir->ctx) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
308 pool_free(dir->ctx->pool, dir);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
309 } else {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
310 free(dir);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
311 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
312 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
313
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
314 int vfs_mkdir(VFSContext *ctx, char *path) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
315 if(ctx && ctx->vfs) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
316 return vfs_path_op(ctx, path, ctx->vfs->mkdir, ACL_ADD_FILE);
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
317 } else {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
318 return vfs_path_op(ctx, path, sys_mkdir, ACL_ADD_FILE);
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
319 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
320 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
321
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
322 int vfs_unlink(VFSContext *ctx, char *path) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
323 if(ctx && ctx->vfs) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
324 return vfs_path_op(ctx, path, ctx->vfs->unlink, ACL_DELETE);
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
325 } else {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
326 return vfs_path_op(ctx, path, sys_unlink, ACL_DELETE);
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
327 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
328 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
329
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 // private
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
332 int vfs_path_op(VFSContext *ctx, char *path, vfs_op_f op, uint32_t access) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
333 Session *sn;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
334 Request *rq;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
335 uint32_t access_mask;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
336
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
337 if(ctx) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
338 access_mask = ctx->aclreqaccess;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
339 access_mask |= access;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
340 if(!ctx->pool) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
341 // TODO: log warning
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
342 // broken VFSContext
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
343 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
344 if(ctx->vfs) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
345 // ctx->aclreqaccess should be the complete access mask
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
346 uint32_t m = ctx->aclreqaccess; // save original access mask
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
347 ctx->aclreqaccess = access_mask; // set mask for vfs->fstat call
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
348 int ret = op(ctx, path);
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
349 ctx->aclreqaccess = m; // restore original access mask
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
350 return ret;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
351 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
352 } else {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
353 sn = NULL;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
354 rq = NULL;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
355 access_mask = access;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
356 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
357
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
358 // check ACLs
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
359 uid_t uid; // uid and gid will be initialized by sys_acl_check
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
360 gid_t gid;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
361 if(sys_acl_check(ctx, access_mask, &uid, &gid)) {
57
b3a89736b23e some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 56
diff changeset
362 return -1;
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
363 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
364
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
365 // do path operation
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
366 if(op(ctx, path)) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
367 // error
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
368 if(ctx) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
369 ctx->vfs_errno = errno;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
370 sys_set_error_status(ctx);
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
371 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
372 return -1;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
373 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
374
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
375 return 0;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
376 }
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 int sys_acl_check(VFSContext *ctx, uint32_t acm, uid_t *uid, gid_t *gid) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 /*
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 * we don't allow remote root access, so a uid of 0 means that
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 * no file system acl check is needed
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 */
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 *uid = 0;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 *gid = 0;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 if(!ctx) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 return 0;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 ACLListHandle *acllist = ctx->acllist;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 if(acllist) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 ACLListElm *elm = acllist->listhead;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 while(elm) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 ACLList *acl = elm->acl;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 if(acl->isextern) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 // TODO
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 } else if(!acl->check(acl, ctx->user, acm)) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 // access denied
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 if(ctx->sn && ctx->rq) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 acl_set_error_status(ctx->sn, ctx->rq, acl, ctx->user);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 return 1;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 elm = elm->next;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 return 0;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 void sys_set_error_status(VFSContext *ctx) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 if(ctx->sn && ctx->rq) {
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
412 int status = util_errno2status(ctx->vfs_errno);
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 protocol_status(ctx->sn, ctx->rq, status, NULL);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 ssize_t sys_file_read(SYS_FILE fd, void *buf, size_t nbyte) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 return read(fd->fd, buf, nbyte);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 ssize_t sys_file_write(SYS_FILE fd, const void *buf, size_t nbyte) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 return write(fd->fd, buf, nbyte);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423 }
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 void sys_file_close(SYS_FILE fd) {
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 close(fd->fd);
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 }
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
428
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
429 int sys_dir_read(VFS_DIR dir, VFS_ENTRY *entry, int getstat) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
430 struct dirent *e = readdir(dir->data);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
431 if(e) {
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
432 char *name = e->d_name;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
433 if(!strcmp(name, ".") || !strcmp(name, "..")) {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
434 return sys_dir_read(dir, entry, getstat);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
435 } else {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
436 entry->name = name;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
437 if(getstat) {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
438 // TODO: check ACLs again for new path
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
439 if(fstatat(dir->fd, e->d_name, &entry->stat, 0)) {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
440 entry->stat_errno = errno;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
441 }
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
442 entry->stat_extra = NULL;
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
443 }
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
444 return 1;
55
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
445 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
446 } else {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
447 return 0;
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
448 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
449 }
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
450
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
451 void sys_dir_close(VFS_DIR dir) {
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
452 closedir(dir->data);
b7908bf38f9f vfs can read directories
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
453 }
56
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
454
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
455 int sys_mkdir(VFSContext *ctx, char *path) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
456 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
457 return mkdir(path, mode);
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
458 }
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
459
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
460 int sys_unlink(VFSContext *ctx, char *path) {
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
461 return unlink(path);
c6cf20b09043 added vfs_mkdir and vfs_unlink
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 55
diff changeset
462 }
58
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
463
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
464 /* public file api */
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
465
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
466 NSAPI_PUBLIC int system_fread(SYS_FILE fd, void *buf, int nbyte) {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
467 return fd->io->read(fd, buf, nbyte);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
468 }
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
469
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
470 NSAPI_PUBLIC int system_fwrite(SYS_FILE fd, const void *buf, int nbyte) {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
471 return fd->io->write(fd, buf, nbyte);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
472 }
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
473
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
474 NSAPI_PUBLIC int system_fclose(SYS_FILE fd) {
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
475 vfs_close(fd);
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
476 return 0;
66c22e54aa90 webdav uses the vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 57
diff changeset
477 }

mercurial