Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2762 → Rev 2763

/trunk/uspace/app/tester/vfs/vfs1.c
95,11 → 95,17
printf("discovered node %s in /\n", dp->d_name);
closedir(dirp);
 
if (unlink("/mydir/myfile"))
if (rename("/mydir/myfile", "/mydir/yourfile"))
return "rename() failed.\n";
 
if (!quiet)
printf("renamed /mydir/myfile to /mydir/yourfile\n");
 
if (unlink("/mydir/yourfile"))
return "unlink() failed.\n";
if (!quiet)
printf("unlinked file /mydir/myfile\n");
printf("unlinked file /mydir/yourfile\n");
 
if (rmdir("/mydir"))
return "rmdir() failed.\n";
/trunk/uspace/lib/libfs/libfs.c
141,6 → 141,7
unsigned last = IPC_GET_ARG2(*request);
int dev_handle = IPC_GET_ARG3(*request);
int lflag = IPC_GET_ARG4(*request);
int index = IPC_GET_ARG5(*request); /* when L_LINK specified */
 
if (last < next)
last += PLB_SIZE;
180,21 → 181,34
 
/* handle miss: match amongst siblings */
if (!tmp) {
if ((next > last) && (lflag & L_CREATE)) {
/* no components left and L_CREATE specified */
if (next <= last) {
/* there are unprocessed components */
ipc_answer_0(rid, ENOENT);
return;
}
/* miss in the last component */
if (lflag & (L_CREATE | L_LINK)) {
/* request to create a new link */
if (!ops->is_directory(cur)) {
ipc_answer_0(rid, ENOTDIR);
return;
}
void *nodep = ops->create(lflag);
}
void *nodep;
if (lflag & L_CREATE)
nodep = ops->create(lflag);
else
nodep = ops->node_get(fs_handle,
dev_handle, index);
if (nodep) {
if (!ops->link(cur, nodep, component)) {
ops->destroy(nodep);
if (lflag & L_CREATE)
ops->destroy(nodep);
ipc_answer_0(rid, ENOSPC);
} else {
ipc_answer_5(rid, EOK,
fs_handle, dev_handle,
ops->index_get(nodep), 0,
ops->index_get(nodep),
ops->size_get(nodep),
ops->lnkcnt_get(nodep));
}
} else {
201,7 → 215,12
ipc_answer_0(rid, ENOSPC);
}
return;
}
} else if (lflag & L_PARENT) {
/* return parent */
ipc_answer_5(rid, EOK, fs_handle, dev_handle,
ops->index_get(cur), ops->size_get(cur),
ops->lnkcnt_get(cur));
}
ipc_answer_0(rid, ENOENT);
return;
}
214,7 → 233,7
 
/* handle miss: excessive components */
if (!tmp && next <= last) {
if (lflag & L_CREATE) {
if (lflag & (L_CREATE | L_LINK)) {
if (!ops->is_directory(cur)) {
ipc_answer_0(rid, ENOTDIR);
return;
239,15 → 258,22
component[len] = '\0';
len = 0;
void *nodep = ops->create(lflag);
void *nodep;
if (lflag & L_CREATE)
nodep = ops->create(lflag);
else
nodep = ops->node_get(fs_handle, dev_handle,
index);
if (nodep) {
if (!ops->link(cur, nodep, component)) {
ops->destroy(nodep);
if (lflag & L_CREATE)
ops->destroy(nodep);
ipc_answer_0(rid, ENOSPC);
} else {
ipc_answer_5(rid, EOK,
fs_handle, dev_handle,
ops->index_get(nodep), 0,
ops->index_get(nodep),
ops->size_get(nodep),
ops->lnkcnt_get(nodep));
}
} else {
260,7 → 286,14
}
 
/* handle hit */
if (lflag & L_DESTROY) {
if (lflag & L_PARENT) {
cur = par;
if (!cur) {
ipc_answer_0(rid, ENOENT);
return;
}
}
if (lflag & L_UNLINK) {
unsigned old_lnkcnt = ops->lnkcnt_get(cur);
int res = ops->unlink(par, cur);
ipc_answer_5(rid, (ipcarg_t)res, fs_handle, dev_handle,
267,7 → 300,8
ops->index_get(cur), ops->size_get(cur), old_lnkcnt);
return;
}
if ((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) {
if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
(lflag & L_LINK)) {
ipc_answer_0(rid, EEXIST);
return;
}
/trunk/uspace/lib/libfs/libfs.h
43,6 → 43,7
 
typedef struct {
bool (* match)(void *, void *, const char *);
void * (* node_get)(int, int, unsigned long);
void * (* create)(int);
void (* destroy)(void *);
bool (* link)(void *, void *, const char *);
/trunk/uspace/lib/libc/include/stdio.h
67,6 → 67,8
 
#define fprintf(f, fmt, ...) printf(fmt, ##__VA_ARGS__)
 
extern int rename(const char *, const char *);
 
#endif
 
/** @}
/trunk/uspace/lib/libc/generic/vfs/vfs.c
468,6 → 468,73
return _unlink(path, L_DIRECTORY);
}
 
int rename(const char *old, const char *new)
{
int res;
ipcarg_t rc;
aid_t req;
char *olda = absolutize(old);
if (!olda)
return ENOMEM;
size_t oldc_len;
char *oldc = canonify(olda, &oldc_len);
if (!oldc) {
free(olda);
return EINVAL;
}
char *newa = absolutize(new);
if (!newa) {
free(olda);
return ENOMEM;
}
size_t newc_len;
char *newc = canonify(newa, &newc_len);
if (!newc) {
free(olda);
free(newa);
return EINVAL;
}
 
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, oldc, oldc_len);
if (rc != EOK) {
async_wait_for(req, NULL);
async_serialize_end();
futex_up(&vfs_phone_futex);
free(olda);
free(newa);
return (int) rc;
}
rc = ipc_data_write_start(vfs_phone, newc, newc_len);
if (rc != EOK) {
async_wait_for(req, NULL);
async_serialize_end();
futex_up(&vfs_phone_futex);
free(olda);
free(newa);
return (int) rc;
}
async_wait_for(req, &rc);
async_serialize_end();
futex_up(&vfs_phone_futex);
free(olda);
free(newa);
return rc;
}
 
int chdir(const char *path)
{
char *pa = absolutize(path);
/trunk/uspace/srv/fs/tmpfs/tmpfs_ops.c
70,6 → 70,7
 
/* Forward declarations of static functions. */
static bool tmpfs_match(void *, void *, const char *);
static void *tmpfs_node_get(int, int, unsigned long);
static void *tmpfs_create_node(int);
static bool tmpfs_link_node(void *, void *, const char *);
static int tmpfs_unlink_node(void *, void *);
124,6 → 125,7
/** libfs operations */
libfs_ops_t tmpfs_libfs_ops = {
.match = tmpfs_match,
.node_get = tmpfs_node_get,
.create = tmpfs_create_node,
.destroy = tmpfs_destroy_node,
.link = tmpfs_link_node,
260,6 → 262,14
return !strcmp(namep->name, component);
}
 
void *tmpfs_node_get(int fs_handle, int dev_handle, unsigned long index)
{
link_t *lnk = hash_table_find(&dentries, &index);
if (!lnk)
return NULL;
return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
}
 
void *tmpfs_create_node(int lflag)
{
assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
/trunk/uspace/srv/vfs/vfs.c
114,6 → 114,9
case VFS_UNLINK:
vfs_unlink(callid, &call);
break;
case VFS_RENAME:
vfs_rename(callid, &call);
break;
default:
ipc_answer_0(callid, ENOTSUP);
break;
/trunk/uspace/srv/vfs/vfs_ops.c
35,6 → 35,7
* @brief Operations that VFS offers to its clients.
*/
 
#include "vfs.h"
#include <ipc/ipc.h>
#include <async.h>
#include <errno.h>
50,7 → 51,7
#include <fcntl.h>
#include <assert.h>
#include <atomic.h>
#include "vfs.h"
#include <vfs/canonify.h>
 
/* Forward declarations of static functions. */
static int vfs_truncate_internal(int, int, unsigned long, size_t);
306,21 → 307,12
ipc_answer_0(rid, EINVAL);
return;
}
 
/*
* Now we are on the verge of accepting the path.
*
* There is one optimization we could do in the future: copy the path
* directly into the PLB using some kind of a callback.
*/
char *path = malloc(len + 1);
if (!path) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
return;
}
 
int rc;
if ((rc = ipc_data_write_finalize(callid, path, len))) {
ipc_answer_0(rid, rc);
629,21 → 621,12
ipc_answer_0(rid, EINVAL);
return;
}
 
/*
* Now we are on the verge of accepting the path.
*
* There is one optimization we could do in the future: copy the path
* directly into the PLB using some kind of a callback.
*/
char *path = malloc(len + 1);
if (!path) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
return;
}
 
int rc;
if ((rc = ipc_data_write_finalize(callid, path, len))) {
ipc_answer_0(rid, rc);
672,21 → 655,12
ipc_answer_0(rid, EINVAL);
return;
}
 
/*
* Now we are on the verge of accepting the path.
*
* There is one optimization we could do in the future: copy the path
* directly into the PLB using some kind of a callback.
*/
char *path = malloc(len + 1);
if (!path) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
return;
}
 
int rc;
if ((rc = ipc_data_write_finalize(callid, path, len))) {
ipc_answer_0(rid, rc);
698,7 → 672,7
rwlock_write_lock(&namespace_rwlock);
lflag &= L_DIRECTORY; /* sanitize lflag */
vfs_lookup_res_t lr;
rc = vfs_lookup_internal(path, lflag | L_DESTROY, &lr, NULL);
rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, NULL);
free(path);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
718,6 → 692,167
ipc_answer_0(rid, EOK);
}
 
void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
{
size_t len;
ipc_callid_t callid;
int rc;
 
/* Retrieve the old path. */
if (!ipc_data_write_receive(&callid, &len)) {
ipc_answer_0(callid, EINVAL);
ipc_answer_0(rid, EINVAL);
return;
}
char *old = malloc(len + 1);
if (!old) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
return;
}
if ((rc = ipc_data_write_finalize(callid, old, len))) {
ipc_answer_0(rid, rc);
free(old);
return;
}
old[len] = '\0';
/* Retrieve the new path. */
if (!ipc_data_write_receive(&callid, &len)) {
ipc_answer_0(callid, EINVAL);
ipc_answer_0(rid, EINVAL);
free(old);
return;
}
char *new = malloc(len + 1);
if (!new) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
free(old);
return;
}
if ((rc = ipc_data_write_finalize(callid, new, len))) {
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
new[len] = '\0';
 
char *oldc = canonify(old, &len);
char *newc = canonify(new, NULL);
if (!oldc || !newc) {
ipc_answer_0(rid, EINVAL);
free(old);
free(new);
return;
}
if (!strncmp(newc, oldc, len)) {
/* oldc is a prefix of newc */
ipc_answer_0(rid, EINVAL);
free(old);
free(new);
return;
}
vfs_lookup_res_t old_lr;
vfs_lookup_res_t new_lr;
vfs_lookup_res_t new_par_lr;
rwlock_write_lock(&namespace_rwlock);
/* Lookup the node belonging to the old file name. */
rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
vfs_node_t *old_node = vfs_node_get(&old_lr);
if (!old_node) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, ENOMEM);
free(old);
free(new);
return;
}
/* Lookup parent of the new file name. */
rc = vfs_lookup_internal(newc, L_PARENT, &new_par_lr, NULL);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
/* Check whether linking to the same file system instance. */
if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
(old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, EXDEV); /* different file systems */
free(old);
free(new);
return;
}
/* Destroy the old link for the new name. */
vfs_node_t *new_node = NULL;
rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
switch (rc) {
case ENOENT:
/* simply not in our way */
break;
case EOK:
new_node = vfs_node_get(&new_lr);
if (!new_node) {
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, ENOMEM);
free(old);
free(new);
return;
}
new_node->lnkcnt--;
break;
default:
rwlock_write_unlock(&namespace_rwlock);
ipc_answer_0(rid, ENOTEMPTY);
free(old);
free(new);
return;
}
/* Create the new link for the new name. */
rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
if (new_node)
vfs_node_put(new_node);
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
old_node->lnkcnt++;
/* Destroy the link for the old name. */
rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
if (rc != EOK) {
rwlock_write_unlock(&namespace_rwlock);
vfs_node_put(old_node);
if (new_node)
vfs_node_put(new_node);
ipc_answer_0(rid, rc);
free(old);
free(new);
return;
}
old_node->lnkcnt--;
rwlock_write_unlock(&namespace_rwlock);
vfs_node_put(old_node);
if (new_node)
vfs_node_put(new_node);
free(old);
free(new);
ipc_answer_0(rid, EOK);
}
 
/**
* @}
*/
/trunk/uspace/srv/vfs/vfs.h
166,11 → 166,20
*/
#define L_CREATE 8
/**
* L_DESTROY is used to remove leaves from the file system namespace. This flag
* L_LINK is used for linking to an already existing nodes.
*/
#define L_LINK 16
/**
* L_UNLINK is used to remove leaves from the file system namespace. This flag
* cannot be passed directly by the client, but will be set by VFS during
* VFS_UNLINK.
*/
#define L_DESTROY 16
#define L_UNLINK 32
/**
* L_PARENT performs a lookup but returns the triplet of the parent node.
* This flag may not be combined with any other lookup flag.
*/
#define L_PARENT 64
 
typedef struct {
vfs_triplet_t triplet;
250,7 → 259,8
 
extern int fs_name_to_handle(char *, bool);
 
extern int vfs_lookup_internal(char *, int, vfs_lookup_res_t *, vfs_pair_t *);
extern int vfs_lookup_internal(char *, int, vfs_lookup_res_t *, vfs_pair_t *,
...);
 
extern bool vfs_nodes_init(void);
extern vfs_node_t *vfs_node_get(vfs_lookup_res_t *);
279,6 → 289,7
extern void vfs_truncate(ipc_callid_t, ipc_call_t *);
extern void vfs_mkdir(ipc_callid_t, ipc_call_t *);
extern void vfs_unlink(ipc_callid_t, ipc_call_t *);
extern void vfs_rename(ipc_callid_t, ipc_call_t *);
 
#endif
 
/trunk/uspace/srv/vfs/vfs_lookup.c
40,6 → 40,7
#include <async.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <bool.h>
#include <futex.h>
#include <libadt/list.h>
65,7 → 66,7
* @return EOK on success or an error code from errno.h.
*/
int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result,
vfs_pair_t *altroot)
vfs_pair_t *altroot, ...)
{
vfs_pair_t *root;
 
82,6 → 83,15
if (!path)
return EINVAL;
unsigned long index = 0;
if (lflag & L_LINK) {
va_list ap;
 
va_start(ap, altroot);
index = va_arg(ap, unsigned long);
va_end(ap);
}
futex_down(&plb_futex);
 
plb_entry_t entry;
149,9 → 159,10
 
ipc_call_t answer;
int phone = vfs_grab_phone(root->fs_handle);
aid_t req = async_send_4(phone, VFS_LOOKUP, (ipcarg_t) first,
aid_t req = async_send_5(phone, VFS_LOOKUP, (ipcarg_t) first,
(ipcarg_t) (first + len - 1) % PLB_SIZE,
(ipcarg_t) root->dev_handle, (ipcarg_t) lflag, &answer);
(ipcarg_t) root->dev_handle, (ipcarg_t) lflag, (ipcarg_t) index,
&answer);
vfs_release_phone(phone);
 
ipcarg_t rc;