HelenOS sources
This source file includes following definitions.
- mfs_read_dentry
- mfs_write_dentry
- mfs_remove_dentry
- mfs_insert_dentry
#include <str.h>
#include "mfs.h"
errno_t
mfs_read_dentry(struct mfs_node *mnode,
struct mfs_dentry_info *d_info, unsigned index)
{
const struct mfs_instance *inst = mnode->instance;
const struct mfs_sb_info *sbi = inst->sbi;
const bool longnames = sbi->long_names;
uint32_t block;
block_t *b;
errno_t r = mfs_read_map(&block, mnode, index * sbi->dirsize);
if (r != EOK)
goto out_err;
if (block == 0) {
r = EOK;
goto out_err;
}
r = block_get(&b, inst->service_id, block, BLOCK_FLAGS_NONE);
if (r != EOK)
goto out_err;
unsigned dentries_per_zone = sbi->block_size / sbi->dirsize;
unsigned dentry_off = index % dentries_per_zone;
if (sbi->fs_version == MFS_VERSION_V3) {
struct mfs3_dentry *d3;
d3 = b->data + (dentry_off * MFS3_DIRSIZE);
d_info->d_inum = conv32(sbi->native, d3->d_inum);
memcpy(d_info->d_name, d3->d_name, MFS3_MAX_NAME_LEN);
d_info->d_name[MFS3_MAX_NAME_LEN] = 0;
} else {
const int namelen = longnames ? MFS_L_MAX_NAME_LEN :
MFS_MAX_NAME_LEN;
struct mfs_dentry *d;
d = b->data + dentry_off * (longnames ? MFSL_DIRSIZE :
MFS_DIRSIZE);
d_info->d_inum = conv16(sbi->native, d->d_inum);
memcpy(d_info->d_name, d->d_name, namelen);
d_info->d_name[namelen] = 0;
}
r = block_put(b);
d_info->index = index;
d_info->node = mnode;
out_err:
return r;
}
errno_t
mfs_write_dentry(struct mfs_dentry_info *d_info)
{
struct mfs_node *mnode = d_info->node;
struct mfs_sb_info *sbi = mnode->instance->sbi;
const unsigned d_off_bytes = d_info->index * sbi->dirsize;
const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
block_t *b;
uint32_t block;
errno_t r;
r = mfs_read_map(&block, mnode, d_off_bytes);
if (r != EOK)
goto out;
r = block_get(&b, mnode->instance->service_id, block, BLOCK_FLAGS_NONE);
if (r != EOK)
goto out;
const size_t name_len = sbi->max_name_len;
uint8_t *ptr = b->data;
ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
if (sbi->fs_version == MFS_VERSION_V3) {
struct mfs3_dentry *dentry;
dentry = (struct mfs3_dentry *) ptr;
dentry->d_inum = conv32(sbi->native, d_info->d_inum);
memcpy(dentry->d_name, d_info->d_name, name_len);
} else {
struct mfs_dentry *dentry;
dentry = (struct mfs_dentry *) ptr;
dentry->d_inum = conv16(sbi->native, d_info->d_inum);
memcpy(dentry->d_name, d_info->d_name, name_len);
}
b->dirty = true;
r = block_put(b);
out:
return r;
}
errno_t
mfs_remove_dentry(struct mfs_node *mnode, const char *d_name)
{
struct mfs_sb_info *sbi = mnode->instance->sbi;
struct mfs_dentry_info d_info;
errno_t r;
const size_t name_len = str_size(d_name);
if (name_len > sbi->max_name_len)
return ENAMETOOLONG;
unsigned i;
for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
r = mfs_read_dentry(mnode, &d_info, i);
if (r != EOK)
return r;
const size_t d_name_len = str_size(d_info.d_name);
if (name_len == d_name_len &&
memcmp(d_info.d_name, d_name, name_len) == 0) {
d_info.d_inum = 0;
r = mfs_write_dentry(&d_info);
return r;
}
}
return ENOENT;
}
errno_t
mfs_insert_dentry(struct mfs_node *mnode, const char *d_name,
fs_index_t d_inum)
{
errno_t r;
struct mfs_sb_info *sbi = mnode->instance->sbi;
struct mfs_dentry_info d_info;
bool empty_dentry_found = false;
const size_t name_len = str_size(d_name);
if (name_len > sbi->max_name_len)
return ENAMETOOLONG;
unsigned i;
for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
r = mfs_read_dentry(mnode, &d_info, i);
if (r != EOK)
return r;
if (d_info.d_inum == 0) {
empty_dentry_found = true;
break;
}
}
if (!empty_dentry_found) {
uint32_t b, pos;
pos = mnode->ino_i->i_size;
r = mfs_read_map(&b, mnode, pos);
if (r != EOK)
goto out;
if (b == 0) {
uint32_t dummy;
r = mfs_alloc_zone(mnode->instance, &b);
if (r != EOK)
goto out;
r = mfs_write_map(mnode, pos, b, &dummy);
if (r != EOK) {
mfs_free_zone(mnode->instance, b);
goto out;
}
}
mnode->ino_i->i_size += sbi->dirsize;
mnode->ino_i->dirty = true;
d_info.index = i;
d_info.node = mnode;
}
d_info.d_inum = d_inum;
memcpy(d_info.d_name, d_name, name_len);
if (name_len < sbi->max_name_len)
d_info.d_name[name_len] = 0;
r = mfs_write_dentry(&d_info);
out:
return r;
}
HelenOS homepage, sources at GitHub