38,8 → 38,8 |
#include <unistd.h> |
#include <dirent.h> |
#include <fcntl.h> |
#include <stdio.h> |
#include <sys/stat.h> |
#include <stdio.h> |
#include <sys/types.h> |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
48,16 → 48,17 |
#include <futex.h> |
#include <errno.h> |
#include <string.h> |
#include <devmap.h> |
#include <ipc/vfs.h> |
#include <ipc/devmap.h> |
#include "../../srv/vfs/vfs.h" |
|
int vfs_phone = -1; |
futex_t vfs_phone_futex = FUTEX_INITIALIZER; |
static int vfs_phone = -1; |
static futex_t vfs_phone_futex = FUTEX_INITIALIZER; |
static futex_t cwd_futex = FUTEX_INITIALIZER; |
|
futex_t cwd_futex = FUTEX_INITIALIZER; |
DIR *cwd_dir = NULL; |
char *cwd_path = NULL; |
size_t cwd_len = 0; |
size_t cwd_size = 0; |
|
char *absolutize(const char *path, size_t *retlen) |
{ |
65,22 → 66,22 |
char *ncwd_path_nc; |
|
futex_down(&cwd_futex); |
size_t len = strlen(path); |
size_t size = str_size(path); |
if (*path != '/') { |
if (!cwd_path) { |
futex_up(&cwd_futex); |
return NULL; |
} |
ncwd_path_nc = malloc(cwd_len + 1 + len + 1); |
ncwd_path_nc = malloc(cwd_size + 1 + size + 1); |
if (!ncwd_path_nc) { |
futex_up(&cwd_futex); |
return NULL; |
} |
strcpy(ncwd_path_nc, cwd_path); |
ncwd_path_nc[cwd_len] = '/'; |
ncwd_path_nc[cwd_len + 1] = '\0'; |
str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path); |
ncwd_path_nc[cwd_size] = '/'; |
ncwd_path_nc[cwd_size + 1] = '\0'; |
} else { |
ncwd_path_nc = malloc(len + 1); |
ncwd_path_nc = malloc(size + 1); |
if (!ncwd_path_nc) { |
futex_up(&cwd_futex); |
return NULL; |
87,7 → 88,7 |
} |
ncwd_path_nc[0] = '\0'; |
} |
strcat(ncwd_path_nc, path); |
str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path); |
ncwd_path = canonify(ncwd_path_nc, retlen); |
if (!ncwd_path) { |
futex_up(&cwd_futex); |
99,7 → 100,7 |
* the address in ncwd_path need not be the same as ncwd_path_nc, even |
* though they both point into the same dynamically allocated buffer. |
*/ |
ncwd_path = strdup(ncwd_path); |
ncwd_path = str_dup(ncwd_path); |
free(ncwd_path_nc); |
if (!ncwd_path) { |
futex_up(&cwd_futex); |
109,75 → 110,44 |
return ncwd_path; |
} |
|
static int vfs_connect(void) |
static void vfs_connect(void) |
{ |
if (vfs_phone < 0) |
vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0); |
return vfs_phone; |
while (vfs_phone < 0) |
vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); |
} |
|
static int device_get_handle(char *name, dev_handle_t *handle) |
int mount(const char *fs_name, const char *mp, const char *dev, |
const char *opts, unsigned int flags) |
{ |
int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CLIENT, |
0); |
if (phone < 0) |
return phone; |
|
ipc_call_t answer; |
aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, 0, 0, |
&answer); |
|
ipcarg_t retval = ipc_data_write_start(phone, name, strlen(name) + 1); |
|
if (retval != EOK) { |
async_wait_for(req, NULL); |
ipc_hangup(phone); |
return retval; |
} |
|
async_wait_for(req, &retval); |
|
if (handle != NULL) |
*handle = -1; |
|
if (retval == EOK) { |
if (handle != NULL) |
*handle = (dev_handle_t) IPC_GET_ARG1(answer); |
} |
|
ipc_hangup(phone); |
return retval; |
} |
|
int mount(const char *fs_name, const char *mp, const char *dev) |
{ |
int res; |
ipcarg_t rc; |
aid_t req; |
dev_handle_t dev_handle; |
|
res = device_get_handle(dev, &dev_handle); |
res = devmap_device_get_handle(dev, &dev_handle, flags); |
if (res != EOK) |
return res; |
|
size_t mpa_len; |
char *mpa = absolutize(mp, &mpa_len); |
size_t mpa_size; |
char *mpa = absolutize(mp, &mpa_size); |
if (!mpa) |
return ENOMEM; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
vfs_connect(); |
|
req = async_send_2(vfs_phone, VFS_IN_MOUNT, dev_handle, flags, NULL); |
rc = ipc_data_write_start(vfs_phone, (void *) mpa, mpa_size); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
free(mpa); |
return res; |
return (int) rc; |
} |
} |
req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL); |
rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name)); |
|
rc = ipc_data_write_start(vfs_phone, (void *) opts, str_size(opts)); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
185,8 → 155,8 |
free(mpa); |
return (int) rc; |
} |
/* Ask VFS whether it likes fs_name. */ |
rc = async_req_0_0(vfs_phone, IPC_M_PING); |
|
rc = ipc_data_write_start(vfs_phone, (void *) fs_name, str_size(fs_name)); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
194,7 → 164,9 |
free(mpa); |
return (int) rc; |
} |
rc = ipc_data_write_start(vfs_phone, (void *)mpa, mpa_len); |
|
/* Ask VFS whether it likes fs_name. */ |
rc = async_req_0_0(vfs_phone, IPC_M_PING); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
202,38 → 174,32 |
free(mpa); |
return (int) rc; |
} |
|
async_wait_for(req, &rc); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
free(mpa); |
|
return (int) rc; |
} |
|
static int _open(const char *path, int lflag, int oflag, ...) |
{ |
int res; |
ipcarg_t rc; |
ipc_call_t answer; |
aid_t req; |
|
size_t pa_len; |
char *pa = absolutize(path, &pa_len); |
size_t pa_size; |
char *pa = absolutize(path, &pa_size); |
if (!pa) |
return ENOMEM; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
free(pa); |
return res; |
} |
} |
req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer); |
rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
vfs_connect(); |
|
req = async_send_3(vfs_phone, VFS_IN_OPEN, lflag, oflag, 0, &answer); |
rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
248,6 → 214,7 |
|
if (rc != EOK) |
return (int) rc; |
|
return (int) IPC_GET_ARG1(answer); |
} |
|
256,24 → 223,37 |
return _open(path, L_FILE, oflag); |
} |
|
int close(int fildes) |
int open_node(fdi_node_t *node, int oflag) |
{ |
int res; |
ipcarg_t rc; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
vfs_connect(); |
|
ipc_call_t answer; |
aid_t req = async_send_4(vfs_phone, VFS_IN_OPEN_NODE, node->fs_handle, |
node->dev_handle, node->index, oflag, &answer); |
|
ipcarg_t rc; |
async_wait_for(req, &rc); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
return res; |
|
if (rc != EOK) |
return (int) rc; |
|
return (int) IPC_GET_ARG1(answer); |
} |
} |
|
rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes); |
int close(int fildes) |
{ |
ipcarg_t rc; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
vfs_connect(); |
|
rc = async_req_1_0(vfs_phone, VFS_IN_CLOSE, fildes); |
|
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
|
282,7 → 262,6 |
|
ssize_t read(int fildes, void *buf, size_t nbyte) |
{ |
int res; |
ipcarg_t rc; |
ipc_call_t answer; |
aid_t req; |
289,15 → 268,9 |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
return res; |
} |
} |
req = async_send_1(vfs_phone, VFS_READ, fildes, &answer); |
vfs_connect(); |
|
req = async_send_1(vfs_phone, VFS_IN_READ, fildes, &answer); |
rc = ipc_data_read_start(vfs_phone, (void *)buf, nbyte); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
316,7 → 289,6 |
|
ssize_t write(int fildes, const void *buf, size_t nbyte) |
{ |
int res; |
ipcarg_t rc; |
ipc_call_t answer; |
aid_t req; |
323,15 → 295,9 |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
return res; |
} |
} |
req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer); |
vfs_connect(); |
|
req = async_send_1(vfs_phone, VFS_IN_WRITE, fildes, &answer); |
rc = ipc_data_write_start(vfs_phone, (void *)buf, nbyte); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
348,26 → 314,32 |
return -1; |
} |
|
off_t lseek(int fildes, off_t offset, int whence) |
int fsync(int fildes) |
{ |
int res; |
ipcarg_t rc; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
vfs_connect(); |
|
ipcarg_t rc = async_req_1_0(vfs_phone, VFS_IN_SYNC, fildes); |
|
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
return res; |
|
return (int) rc; |
} |
} |
|
off_t newoffs; |
rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence, |
(ipcarg_t)&newoffs); |
off_t lseek(int fildes, off_t offset, int whence) |
{ |
ipcarg_t rc; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
vfs_connect(); |
|
ipcarg_t newoffs; |
rc = async_req_3_1(vfs_phone, VFS_IN_SEEK, fildes, offset, whence, |
&newoffs); |
|
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
|
374,29 → 346,85 |
if (rc != EOK) |
return (off_t) -1; |
|
return newoffs; |
return (off_t) newoffs; |
} |
|
int ftruncate(int fildes, off_t length) |
{ |
int res; |
ipcarg_t rc; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
vfs_connect(); |
|
rc = async_req_2_0(vfs_phone, VFS_IN_TRUNCATE, fildes, length); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
return res; |
return (int) rc; |
} |
|
int fstat(int fildes, struct stat *stat) |
{ |
ipcarg_t rc; |
ipc_call_t answer; |
aid_t req; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
vfs_connect(); |
|
req = async_send_1(vfs_phone, VFS_IN_FSTAT, fildes, NULL); |
rc = ipc_data_read_start(vfs_phone, (void *)stat, sizeof(struct stat)); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
return (ssize_t) rc; |
} |
rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length); |
async_wait_for(req, &rc); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
|
return rc; |
} |
|
int stat(const char *path, struct stat *stat) |
{ |
ipcarg_t rc; |
aid_t req; |
|
size_t pa_size; |
char *pa = absolutize(path, &pa_size); |
if (!pa) |
return ENOMEM; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
vfs_connect(); |
|
req = async_send_0(vfs_phone, VFS_IN_STAT, NULL); |
rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
free(pa); |
return (int) rc; |
} |
rc = ipc_data_read_start(vfs_phone, stat, sizeof(struct stat)); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
free(pa); |
return (int) rc; |
} |
async_wait_for(req, &rc); |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
free(pa); |
return rc; |
} |
|
DIR *opendir(const char *dirname) |
{ |
433,28 → 461,20 |
|
int mkdir(const char *path, mode_t mode) |
{ |
int res; |
ipcarg_t rc; |
aid_t req; |
|
size_t pa_len; |
char *pa = absolutize(path, &pa_len); |
size_t pa_size; |
char *pa = absolutize(path, &pa_size); |
if (!pa) |
return ENOMEM; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
free(pa); |
return res; |
} |
} |
req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL); |
rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
vfs_connect(); |
|
req = async_send_1(vfs_phone, VFS_IN_MKDIR, mode, NULL); |
rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
471,28 → 491,20 |
|
static int _unlink(const char *path, int lflag) |
{ |
int res; |
ipcarg_t rc; |
aid_t req; |
|
size_t pa_len; |
char *pa = absolutize(path, &pa_len); |
size_t pa_size; |
char *pa = absolutize(path, &pa_size); |
if (!pa) |
return ENOMEM; |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
free(pa); |
return res; |
} |
} |
req = async_send_0(vfs_phone, VFS_UNLINK, NULL); |
rc = ipc_data_write_start(vfs_phone, pa, pa_len); |
vfs_connect(); |
|
req = async_send_0(vfs_phone, VFS_IN_UNLINK, NULL); |
rc = ipc_data_write_start(vfs_phone, pa, pa_size); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
519,17 → 531,16 |
|
int rename(const char *old, const char *new) |
{ |
int res; |
ipcarg_t rc; |
aid_t req; |
|
size_t olda_len; |
char *olda = absolutize(old, &olda_len); |
size_t olda_size; |
char *olda = absolutize(old, &olda_size); |
if (!olda) |
return ENOMEM; |
|
size_t newa_len; |
char *newa = absolutize(new, &newa_len); |
size_t newa_size; |
char *newa = absolutize(new, &newa_size); |
if (!newa) { |
free(olda); |
return ENOMEM; |
537,18 → 548,10 |
|
futex_down(&vfs_phone_futex); |
async_serialize_start(); |
if (vfs_phone < 0) { |
res = vfs_connect(); |
if (res < 0) { |
async_serialize_end(); |
futex_up(&vfs_phone_futex); |
free(olda); |
free(newa); |
return res; |
} |
} |
req = async_send_0(vfs_phone, VFS_RENAME, NULL); |
rc = ipc_data_write_start(vfs_phone, olda, olda_len); |
vfs_connect(); |
|
req = async_send_0(vfs_phone, VFS_IN_RENAME, NULL); |
rc = ipc_data_write_start(vfs_phone, olda, olda_size); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
557,7 → 560,7 |
free(newa); |
return (int) rc; |
} |
rc = ipc_data_write_start(vfs_phone, newa, newa_len); |
rc = ipc_data_write_start(vfs_phone, newa, newa_size); |
if (rc != EOK) { |
async_wait_for(req, NULL); |
async_serialize_end(); |
576,8 → 579,8 |
|
int chdir(const char *path) |
{ |
size_t pa_len; |
char *pa = absolutize(path, &pa_len); |
size_t pa_size; |
char *pa = absolutize(path, &pa_size); |
if (!pa) |
return ENOMEM; |
|
593,11 → 596,11 |
cwd_dir = NULL; |
free(cwd_path); |
cwd_path = NULL; |
cwd_len = 0; |
cwd_size = 0; |
} |
cwd_dir = d; |
cwd_path = pa; |
cwd_len = pa_len; |
cwd_size = pa_size; |
futex_up(&cwd_futex); |
return EOK; |
} |
607,14 → 610,43 |
if (!size) |
return NULL; |
futex_down(&cwd_futex); |
if (size < cwd_len + 1) { |
if (size < cwd_size + 1) { |
futex_up(&cwd_futex); |
return NULL; |
} |
strcpy(buf, cwd_path); |
str_cpy(buf, size, cwd_path); |
futex_up(&cwd_futex); |
return buf; |
} |
|
int fd_phone(int fildes) |
{ |
struct stat stat; |
int rc; |
|
rc = fstat(fildes, &stat); |
|
if (!stat.devfs_stat.device) |
return -1; |
|
return devmap_device_connect(stat.devfs_stat.device, 0); |
} |
|
int fd_node(int fildes, fdi_node_t *node) |
{ |
struct stat stat; |
int rc; |
|
rc = fstat(fildes, &stat); |
|
if (rc == EOK) { |
node->fs_handle = stat.fs_handle; |
node->dev_handle = stat.dev_handle; |
node->index = stat.index; |
} |
|
return rc; |
} |
|
/** @} |
*/ |