| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Maurizio Lombardi
|
|---|
| 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 fs
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | #include <assert.h>
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include "mfs.h"
|
|---|
| 36 | #include "mfs_utils.h"
|
|---|
| 37 |
|
|---|
| 38 | struct mfs_dentry_info *
|
|---|
| 39 | read_directory_entry(struct mfs_node *mnode, unsigned index)
|
|---|
| 40 | {
|
|---|
| 41 | const struct mfs_instance *inst = mnode->instance;
|
|---|
| 42 | const struct mfs_sb_info *sbi = inst->sbi;
|
|---|
| 43 | const bool longnames = sbi->long_names;
|
|---|
| 44 | uint32_t block;
|
|---|
| 45 | block_t *b;
|
|---|
| 46 |
|
|---|
| 47 | struct mfs_dentry_info *d_info = malloc(sizeof *d_info);
|
|---|
| 48 |
|
|---|
| 49 | if (!d_info)
|
|---|
| 50 | return NULL;
|
|---|
| 51 |
|
|---|
| 52 | int r = read_map(&block, mnode, index * sbi->dirsize);
|
|---|
| 53 |
|
|---|
| 54 | if (r != EOK || block == 0)
|
|---|
| 55 | goto out_err;
|
|---|
| 56 |
|
|---|
| 57 | r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NONE);
|
|---|
| 58 |
|
|---|
| 59 | if (r != EOK)
|
|---|
| 60 | goto out_err;
|
|---|
| 61 |
|
|---|
| 62 | if (sbi->fs_version == MFS_VERSION_V3) {
|
|---|
| 63 | struct mfs3_dentry *d3;
|
|---|
| 64 |
|
|---|
| 65 | d3 = b->data;
|
|---|
| 66 | d_info->d_inum = conv32(sbi->native, d3->d_inum);
|
|---|
| 67 | memcpy(d_info->d_name, d3->d_name, MFS3_MAX_NAME_LEN);
|
|---|
| 68 | } else {
|
|---|
| 69 | const int namelen = longnames ? MFS_L_MAX_NAME_LEN :
|
|---|
| 70 | MFS_MAX_NAME_LEN;
|
|---|
| 71 |
|
|---|
| 72 | struct mfs_dentry *d;
|
|---|
| 73 |
|
|---|
| 74 | d = b->data;
|
|---|
| 75 | d_info->d_inum = conv16(sbi->native, d->d_inum);
|
|---|
| 76 | memcpy(d_info->d_name, d->d_name, namelen);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | block_put(b);
|
|---|
| 80 |
|
|---|
| 81 | d_info->index = index;
|
|---|
| 82 | d_info->node = mnode;
|
|---|
| 83 | return d_info;
|
|---|
| 84 |
|
|---|
| 85 | out_err:
|
|---|
| 86 | free(d_info);
|
|---|
| 87 | return NULL;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | int
|
|---|
| 91 | write_dentry(struct mfs_dentry_info *d_info)
|
|---|
| 92 | {
|
|---|
| 93 | struct mfs_node *mnode = d_info->node;
|
|---|
| 94 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 95 | const unsigned d_off_bytes = d_info->index * sbi->dirsize;
|
|---|
| 96 | const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
|
|---|
| 97 | block_t *b;
|
|---|
| 98 | uint32_t block;
|
|---|
| 99 | int r;
|
|---|
| 100 |
|
|---|
| 101 | r = read_map(&block, mnode, d_off_bytes);
|
|---|
| 102 | if (r != EOK)
|
|---|
| 103 | goto out;
|
|---|
| 104 |
|
|---|
| 105 | r = block_get(&b, mnode->instance->handle, block, BLOCK_FLAGS_NONE);
|
|---|
| 106 | if (r != EOK)
|
|---|
| 107 | goto out;
|
|---|
| 108 |
|
|---|
| 109 | const size_t name_len = str_size(d_info->d_name);
|
|---|
| 110 | uint8_t *ptr = b->data;
|
|---|
| 111 | ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
|
|---|
| 112 |
|
|---|
| 113 | if (sbi->fs_version == MFS_VERSION_V3) {
|
|---|
| 114 | struct mfs3_dentry *dentry;
|
|---|
| 115 | dentry = (struct mfs3_dentry *) ptr;
|
|---|
| 116 |
|
|---|
| 117 | dentry->d_inum = conv32(sbi->native, d_info->d_inum);
|
|---|
| 118 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
|---|
| 119 | } else {
|
|---|
| 120 | struct mfs_dentry *dentry;
|
|---|
| 121 | dentry = (struct mfs_dentry *) ptr;
|
|---|
| 122 |
|
|---|
| 123 | dentry->d_inum = conv16(sbi->native, d_info->d_inum);
|
|---|
| 124 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | b->dirty = true;
|
|---|
| 128 | block_put(b);
|
|---|
| 129 |
|
|---|
| 130 | out:
|
|---|
| 131 | return r;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * @}
|
|---|
| 136 | */
|
|---|
| 137 |
|
|---|