Subversion Repositories HelenOS

Rev

Rev 2694 | Rev 2700 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2671 jermar 1
/*
2684 jermar 2
 * Copyright (c) 2008 Jakub Jermar
2671 jermar 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup libc
30
 * @{
31
 */
32
/** @file
33
 */
34
 
35
#include <vfs.h>
2694 jermar 36
#include <stdlib.h>
2674 jermar 37
#include <unistd.h>
2694 jermar 38
#include <dirent.h>
2674 jermar 39
#include <fcntl.h>
2671 jermar 40
#include <ipc/ipc.h>
41
#include <ipc/services.h>
42
#include <async.h>
43
#include <atomic.h>
44
#include <futex.h>
45
#include <errno.h>
46
#include <string.h>
47
#include "../../srv/vfs/vfs.h"
48
 
49
int vfs_phone = -1;
50
atomic_t vfs_phone_futex = FUTEX_INITIALIZER;
51
 
52
static int vfs_connect(void)
53
{
54
    if (vfs_phone < 0)
55
        vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
56
    return vfs_phone;
57
}
58
 
59
int mount(const char *fs_name, const char *mp, const char *dev)
60
{
61
    int res;
62
    ipcarg_t rc;
63
    aid_t req;
64
 
65
    int dev_handle = 0; /* TODO */
66
 
67
    futex_down(&vfs_phone_futex);
68
    async_serialize_start();
69
    if (vfs_phone < 0) {
70
        res = vfs_connect();
71
        if (res < 0) {
72
            async_serialize_end();
73
            futex_up(&vfs_phone_futex);
74
            return res;
75
        }
76
    }
77
    req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
2678 jermar 78
    rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name));
2671 jermar 79
    if (rc != EOK) {
80
        async_wait_for(req, NULL);
81
        async_serialize_end();
82
        futex_up(&vfs_phone_futex);
83
        return (int) rc;
84
    }
2678 jermar 85
    rc = ipc_data_write_start(vfs_phone, (void *)mp, strlen(mp));
2671 jermar 86
    if (rc != EOK) {
87
        async_wait_for(req, NULL);
88
        async_serialize_end();
89
        futex_up(&vfs_phone_futex);
90
        return (int) rc;
91
    }
92
    async_wait_for(req, &rc);
93
    async_serialize_end();
94
    futex_up(&vfs_phone_futex);
95
    return (int) rc;
96
}
97
 
98
 
2693 jermar 99
int open(const char *path, int oflag, ...)
2671 jermar 100
{
101
    int res;
102
    ipcarg_t rc;
103
    ipc_call_t answer;
104
    aid_t req;
105
 
106
    futex_down(&vfs_phone_futex);
107
    async_serialize_start();
108
    if (vfs_phone < 0) {
109
        res = vfs_connect();
110
        if (res < 0) {
111
            async_serialize_end();
112
            futex_up(&vfs_phone_futex);
113
            return res;
114
        }
115
    }
116
    req = async_send_2(vfs_phone, VFS_OPEN, oflag, 0, &answer);
2693 jermar 117
    rc = ipc_data_write_start(vfs_phone, path, strlen(path));
2671 jermar 118
    if (rc != EOK) {
119
        async_wait_for(req, NULL);
120
        async_serialize_end();
121
        futex_up(&vfs_phone_futex);
122
        return (int) rc;
123
    }
124
    async_wait_for(req, &rc);
125
    async_serialize_end();
126
    futex_up(&vfs_phone_futex);
127
    return (int) IPC_GET_ARG1(answer);
128
}
129
 
130
ssize_t read(int fildes, void *buf, size_t nbyte)
131
{
132
    int res;
133
    ipcarg_t rc;
134
    ipc_call_t answer;
135
    aid_t req;
136
 
137
    futex_down(&vfs_phone_futex);
138
    async_serialize_start();
139
    if (vfs_phone < 0) {
140
        res = vfs_connect();
141
        if (res < 0) {
142
            async_serialize_end();
143
            futex_up(&vfs_phone_futex);
144
            return res;
145
        }
146
    }
2674 jermar 147
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
2678 jermar 148
    if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) {
2671 jermar 149
        async_wait_for(req, NULL);
150
        async_serialize_end();
151
        futex_up(&vfs_phone_futex);
152
        return (ssize_t) rc;
153
    }
154
    async_wait_for(req, &rc);
155
    async_serialize_end();
156
    futex_up(&vfs_phone_futex);
157
    return (ssize_t) IPC_GET_ARG1(answer);
158
}
159
 
2674 jermar 160
ssize_t write(int fildes, const void *buf, size_t nbyte)
161
{
162
    int res;
163
    ipcarg_t rc;
164
    ipc_call_t answer;
165
    aid_t req;
166
 
167
    futex_down(&vfs_phone_futex);
168
    async_serialize_start();
169
    if (vfs_phone < 0) {
170
        res = vfs_connect();
171
        if (res < 0) {
172
            async_serialize_end();
173
            futex_up(&vfs_phone_futex);
174
            return res;
175
        }
176
    }
177
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
2678 jermar 178
    if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) {
2674 jermar 179
        async_wait_for(req, NULL);
180
        async_serialize_end();
181
        futex_up(&vfs_phone_futex);
182
        return (ssize_t) rc;
183
    }
184
    async_wait_for(req, &rc);
185
    async_serialize_end();
186
    futex_up(&vfs_phone_futex);
187
    return (ssize_t) IPC_GET_ARG1(answer);
188
}
2684 jermar 189
 
190
off_t lseek(int fildes, off_t offset, int whence)
191
{
192
    int res;
193
    ipcarg_t rc;
194
 
195
    futex_down(&vfs_phone_futex);
196
    async_serialize_start();
197
    if (vfs_phone < 0) {
198
        res = vfs_connect();
199
        if (res < 0) {
200
            async_serialize_end();
201
            futex_up(&vfs_phone_futex);
202
            return res;
203
        }
204
    }
205
 
206
    off_t newoffs;
207
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
208
        &newoffs);
209
 
210
    async_serialize_end();
211
    futex_up(&vfs_phone_futex);
212
 
213
    if (rc != EOK)
214
        return (off_t) -1;
215
 
216
    return newoffs;
217
}
218
 
2693 jermar 219
int ftruncate(int fildes, off_t length)
220
{
221
    int res;
222
    ipcarg_t rc;
223
 
224
    futex_down(&vfs_phone_futex);
225
    async_serialize_start();
226
    if (vfs_phone < 0) {
227
        res = vfs_connect();
228
        if (res < 0) {
229
            async_serialize_end();
230
            futex_up(&vfs_phone_futex);
231
            return res;
232
        }
233
    }
234
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
235
    async_serialize_end();
236
    futex_up(&vfs_phone_futex);
237
    return (int) rc;
238
}
239
 
2694 jermar 240
DIR *opendir(const char *dirname)
241
{
242
    DIR *dirp = malloc(sizeof(DIR));
243
    if (!dirp)
244
        return NULL;
245
    dirp->fd = open(dirname, 0);    /* TODO: must be a directory */
2699 jermar 246
    if (dirp->fd < 0) {
2694 jermar 247
        free(dirp);
248
        return NULL;
249
    }
250
    return dirp;
251
}
252
 
253
struct dirent *readdir(DIR *dirp)
254
{
2699 jermar 255
    ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
256
    if (len <= 0)
257
        return NULL;
258
    return &dirp->res;
2694 jermar 259
}
260
 
261
void rewinddir(DIR *dirp)
262
{
2699 jermar 263
    (void) lseek(dirp->fd, 0, SEEK_SET);
2694 jermar 264
}
265
 
266
int closedir(DIR *dirp)
267
{
268
    (void) close(dirp->fd);
269
    free(dirp);
270
    return 0;
271
}
272
 
273
int close(int fildes)
274
{
275
    return 0;   /* TODO */
276
}
277
 
2671 jermar 278
/** @}
279
 */