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->dirty = false;
|
---|
82 | d_info->index = index;
|
---|
83 | d_info->node = mnode;
|
---|
84 | return d_info;
|
---|
85 |
|
---|
86 | out_err:
|
---|
87 | free(d_info);
|
---|
88 | return NULL;
|
---|
89 | }
|
---|
90 |
|
---|
91 | int
|
---|
92 | write_dentry(struct mfs_dentry_info *d_info)
|
---|
93 | {
|
---|
94 | struct mfs_node *mnode = d_info->node;
|
---|
95 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
96 | const unsigned d_off_bytes = d_info->index * sbi->dirsize;
|
---|
97 | const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
|
---|
98 | block_t *b;
|
---|
99 | uint32_t block;
|
---|
100 | int r;
|
---|
101 |
|
---|
102 | r = read_map(&block, mnode, d_off_bytes);
|
---|
103 | if (r != EOK)
|
---|
104 | goto out;
|
---|
105 |
|
---|
106 | r = block_get(&b, mnode->instance->handle, block, BLOCK_FLAGS_NONE);
|
---|
107 | if (r != EOK)
|
---|
108 | goto out;
|
---|
109 |
|
---|
110 | const size_t name_len = str_size(d_info->d_name);
|
---|
111 | uint8_t *ptr = b->data;
|
---|
112 | ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
|
---|
113 |
|
---|
114 | if (sbi->fs_version == MFS_VERSION_V3) {
|
---|
115 | struct mfs3_dentry *dentry;
|
---|
116 | dentry = (struct mfs3_dentry *) ptr;
|
---|
117 |
|
---|
118 | dentry->d_inum = conv32(sbi->native, d_info->d_inum);
|
---|
119 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
---|
120 | } else {
|
---|
121 | struct mfs_dentry *dentry;
|
---|
122 | dentry = (struct mfs_dentry *) ptr;
|
---|
123 |
|
---|
124 | dentry->d_inum = conv16(sbi->native, d_info->d_inum);
|
---|
125 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
---|
126 | }
|
---|
127 |
|
---|
128 | b->dirty = true;
|
---|
129 | block_put(b);
|
---|
130 |
|
---|
131 | out:
|
---|
132 | return r;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * @}
|
---|
137 | */
|
---|
138 |
|
---|