source: mainline/uspace/srv/fs/minixfs/mfs_dentry.c@ 936ece7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 936ece7 was b89281b, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

Return the error code in the read_directory_entry() function

  • Property mode set to 100644
File size: 5.2 KB
Line 
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 "mfs.h"
34#include "mfs_utils.h"
35
36int
37read_directory_entry(struct mfs_node *mnode,
38 struct mfs_dentry_info *d_info, unsigned index)
39{
40 const struct mfs_instance *inst = mnode->instance;
41 const struct mfs_sb_info *sbi = inst->sbi;
42 const bool longnames = sbi->long_names;
43 uint32_t block;
44 block_t *b;
45
46 int r = read_map(&block, mnode, index * sbi->dirsize);
47 on_error(r, goto out_err);
48
49 if (block == 0) {
50 /*End of the dentries list*/
51 r = EOK;
52 goto out_err;
53 }
54
55 r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NONE);
56 on_error(r, goto out_err);
57
58 unsigned dentries_per_zone = sbi->block_size / sbi->dirsize;
59 unsigned dentry_off = index % dentries_per_zone;
60
61 if (sbi->fs_version == MFS_VERSION_V3) {
62 struct mfs3_dentry *d3;
63
64 d3 = b->data + (dentry_off * MFS3_DIRSIZE);
65
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 + dentry_off * (longnames ? MFSL_DIRSIZE :
75 MFS_DIRSIZE);
76 d_info->d_inum = conv16(sbi->native, d->d_inum);
77 memcpy(d_info->d_name, d->d_name, namelen);
78 }
79
80 block_put(b);
81
82 d_info->index = index;
83 d_info->node = mnode;
84
85out_err:
86 return r;
87}
88
89int
90write_dentry(struct mfs_dentry_info *d_info)
91{
92 struct mfs_node *mnode = d_info->node;
93 struct mfs_sb_info *sbi = mnode->instance->sbi;
94 const unsigned d_off_bytes = d_info->index * sbi->dirsize;
95 const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
96 block_t *b;
97 uint32_t block;
98 int r;
99
100 r = read_map(&block, mnode, d_off_bytes);
101 on_error(r, goto out);
102
103 r = block_get(&b, mnode->instance->handle, block, BLOCK_FLAGS_NONE);
104 on_error(r, goto out);
105
106 const size_t name_len = sbi->max_name_len;
107 uint8_t *ptr = b->data;
108 ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
109
110 if (sbi->fs_version == MFS_VERSION_V3) {
111 struct mfs3_dentry *dentry;
112 dentry = (struct mfs3_dentry *) ptr;
113
114 dentry->d_inum = conv32(sbi->native, d_info->d_inum);
115 memcpy(dentry->d_name, d_info->d_name, name_len);
116 } else {
117 struct mfs_dentry *dentry;
118 dentry = (struct mfs_dentry *) ptr;
119
120 dentry->d_inum = conv16(sbi->native, d_info->d_inum);
121 memcpy(dentry->d_name, d_info->d_name, name_len);
122 }
123
124 b->dirty = true;
125 block_put(b);
126
127out:
128 return r;
129}
130
131int
132remove_dentry(struct mfs_node *mnode, const char *d_name)
133{
134 struct mfs_sb_info *sbi = mnode->instance->sbi;
135 struct mfs_dentry_info d_info;
136 int r;
137
138 const size_t name_len = str_size(d_name);
139
140 if (name_len > sbi->max_name_len)
141 return ENAMETOOLONG;
142
143 /*Search the directory entry to be removed*/
144 unsigned i;
145 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize ; ++i) {
146 r = read_directory_entry(mnode, &d_info, i);
147 on_error(r, return r);
148
149 if (!bcmp(d_info.d_name, d_name, name_len)) {
150 d_info.d_inum = 0;
151 r = write_dentry(&d_info);
152 return r;
153 }
154 }
155
156 return ENOENT;
157}
158
159int
160insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum)
161{
162 int r;
163 struct mfs_sb_info *sbi = mnode->instance->sbi;
164 struct mfs_dentry_info d_info;
165 bool empty_dentry_found = false;
166
167 const size_t name_len = str_size(d_name);
168
169 if (name_len > sbi->max_name_len)
170 return ENAMETOOLONG;
171
172 /*Search for an empty dentry*/
173 unsigned i;
174 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
175 r = read_directory_entry(mnode, &d_info, i);
176 on_error(r, return r);
177
178 if (d_info.d_inum == 0) {
179 /*This entry is not used*/
180 empty_dentry_found = true;
181 break;
182 }
183 }
184
185 if (!empty_dentry_found) {
186 r = inode_grow(mnode, sbi->dirsize);
187 on_error(r, goto out);
188
189 r = read_directory_entry(mnode, &d_info, i);
190 on_error(r, goto out);
191 }
192
193 d_info.d_inum = d_inum;
194 memcpy(d_info.d_name, d_name, name_len);
195 d_info.d_name[name_len] = 0;
196
197 r = write_dentry(&d_info);
198out:
199 return r;
200}
201
202
203/**
204 * @}
205 */
206
Note: See TracBrowser for help on using the repository browser.