v7fs_file_util.c revision 1.3 1 1.3 apb /* $NetBSD: v7fs_file_util.c,v 1.3 2011/07/18 21:51:49 apb Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch *
19 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
30 1.1 uch */
31 1.1 uch
32 1.3 apb #if HAVE_NBTOOL_CONFIG_H
33 1.3 apb #include "nbtool_config.h"
34 1.3 apb #endif
35 1.3 apb
36 1.1 uch #include <sys/cdefs.h>
37 1.3 apb __KERNEL_RCSID(0, "$NetBSD: v7fs_file_util.c,v 1.3 2011/07/18 21:51:49 apb Exp $");
38 1.1 uch #ifdef _KERNEL
39 1.1 uch #include <sys/systm.h>
40 1.1 uch #include <sys/param.h>
41 1.1 uch #else
42 1.1 uch #include <stdio.h>
43 1.1 uch #include <string.h>
44 1.1 uch #include <errno.h>
45 1.1 uch #endif
46 1.1 uch
47 1.1 uch #include "v7fs.h"
48 1.1 uch #include "v7fs_impl.h"
49 1.1 uch #include "v7fs_endian.h"
50 1.1 uch #include "v7fs_inode.h"
51 1.1 uch #include "v7fs_dirent.h"
52 1.1 uch #include "v7fs_file.h"
53 1.1 uch #include "v7fs_datablock.h"
54 1.1 uch
55 1.1 uch #ifdef V7FS_FILE_DEBUG
56 1.1 uch #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
57 1.1 uch #else
58 1.1 uch #define DPRINTF(fmt, args...) ((void)0)
59 1.1 uch #endif
60 1.1 uch
61 1.1 uch static int replace_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
62 1.1 uch static int lookup_by_number_subr(struct v7fs_self *, void *, v7fs_daddr_t,
63 1.1 uch size_t);
64 1.1 uch
65 1.1 uch int
66 1.1 uch v7fs_file_link(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
67 1.1 uch struct v7fs_inode *p, const char *name)
68 1.1 uch {
69 1.1 uch int error = 0;
70 1.1 uch
71 1.1 uch DPRINTF("%d %d %s\n", parent_dir->inode_number, p->inode_number, name);
72 1.1 uch if ((error = v7fs_directory_add_entry(fs, parent_dir, p->inode_number,
73 1.1 uch name))) {
74 1.1 uch DPRINTF("can't add entry");
75 1.1 uch return error;
76 1.1 uch }
77 1.1 uch p->nlink++;
78 1.1 uch v7fs_inode_writeback(fs, p);
79 1.1 uch
80 1.1 uch return 0;
81 1.1 uch }
82 1.1 uch
83 1.1 uch int
84 1.2 uch v7fs_file_symlink(struct v7fs_self *fs, struct v7fs_inode *p,
85 1.2 uch const char *target)
86 1.2 uch {
87 1.2 uch int error;
88 1.2 uch size_t len = strlen(target) + 1;
89 1.2 uch
90 1.2 uch if (len > V7FSBSD_MAXSYMLINKLEN) {/* limited target 512byte pathname */
91 1.2 uch DPRINTF("too long pathname.");
92 1.2 uch return ENAMETOOLONG;
93 1.2 uch }
94 1.2 uch
95 1.2 uch if ((error = v7fs_datablock_expand(fs, p, len))) {
96 1.2 uch return error;
97 1.2 uch }
98 1.2 uch
99 1.2 uch v7fs_daddr_t blk = p->addr[0]; /* 1block only. */
100 1.2 uch void *buf;
101 1.2 uch if (!(buf = scratch_read(fs, blk))) {
102 1.2 uch return EIO;
103 1.2 uch }
104 1.2 uch
105 1.2 uch strncpy(buf, target, V7FS_BSIZE);
106 1.2 uch if (!fs->io.write(fs->io.cookie, buf, blk)) {
107 1.2 uch scratch_free(fs, buf);
108 1.2 uch return EIO;
109 1.2 uch }
110 1.2 uch scratch_free(fs, buf);
111 1.2 uch v7fs_inode_writeback(fs, p);
112 1.2 uch
113 1.2 uch return 0;
114 1.2 uch }
115 1.2 uch
116 1.2 uch int
117 1.1 uch v7fs_file_rename(struct v7fs_self *fs, struct v7fs_inode *parent_from,
118 1.1 uch const char *from, struct v7fs_inode *parent_to, const char *to)
119 1.1 uch {
120 1.1 uch v7fs_ino_t from_ino, to_ino;
121 1.1 uch int error;
122 1.1 uch
123 1.1 uch if ((error = v7fs_file_lookup_by_name(fs, parent_from, from,
124 1.1 uch &from_ino))) {
125 1.1 uch DPRINTF("%s don't exists\n", from);
126 1.1 uch return error;
127 1.1 uch }
128 1.1 uch
129 1.1 uch /* If target file exists, remove. */
130 1.1 uch error = v7fs_file_lookup_by_name(fs, parent_to, to, &to_ino);
131 1.1 uch if (error == 0) {
132 1.1 uch DPRINTF("%s already exists\n", to);
133 1.1 uch if ((error = v7fs_file_deallocate(fs, parent_to, to))) {
134 1.1 uch DPRINTF("%s can't remove\n", to);
135 1.1 uch return error;
136 1.1 uch }
137 1.1 uch } else if (error != ENOENT) {
138 1.1 uch DPRINTF("error=%d\n", error);
139 1.1 uch return error;
140 1.1 uch }
141 1.1 uch
142 1.1 uch if ((error = v7fs_directory_add_entry(fs, parent_to, from_ino, to))) {
143 1.1 uch DPRINTF("can't add entry");
144 1.1 uch return error;
145 1.1 uch }
146 1.1 uch
147 1.1 uch if ((error = v7fs_directory_remove_entry(fs, parent_from, from))) {
148 1.1 uch DPRINTF("can't remove entry");
149 1.1 uch return error;
150 1.1 uch }
151 1.1 uch
152 1.1 uch if (parent_from != parent_to) {
153 1.1 uch /* If directory move, update ".." */
154 1.1 uch struct v7fs_inode inode;
155 1.1 uch v7fs_inode_load(fs, &inode, from_ino);
156 1.1 uch if (v7fs_inode_isdir(&inode)) {
157 1.1 uch if ((error = v7fs_directory_replace_entry(fs, &inode,
158 1.1 uch "..", parent_to->inode_number))) {
159 1.1 uch DPRINTF("can't replace parent dir");
160 1.1 uch return error;
161 1.1 uch }
162 1.1 uch v7fs_inode_writeback(fs, &inode);
163 1.1 uch }
164 1.1 uch }
165 1.1 uch
166 1.1 uch return 0;
167 1.1 uch }
168 1.1 uch
169 1.1 uch
170 1.1 uch int
171 1.1 uch v7fs_directory_replace_entry(struct v7fs_self *fs, struct v7fs_inode *self_dir,
172 1.1 uch const char *name, v7fs_ino_t ino)
173 1.1 uch {
174 1.1 uch int error;
175 1.1 uch
176 1.1 uch /* Search entry that replaced. replace it to new inode number. */
177 1.1 uch struct v7fs_lookup_arg lookup_arg = { .name = name,
178 1.1 uch .inode_number = ino };
179 1.1 uch if ((error = v7fs_datablock_foreach(fs, self_dir, replace_subr,
180 1.1 uch &lookup_arg)) != V7FS_ITERATOR_BREAK)
181 1.1 uch return ENOENT;
182 1.1 uch
183 1.1 uch return 0;
184 1.1 uch }
185 1.1 uch
186 1.1 uch static int
187 1.1 uch replace_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
188 1.1 uch {
189 1.1 uch struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
190 1.1 uch struct v7fs_dirent *dir;
191 1.1 uch void *buf;
192 1.1 uch size_t i, n;
193 1.1 uch int ret = 0;
194 1.1 uch
195 1.1 uch DPRINTF("match start blk=%x\n", blk);
196 1.1 uch if (!(buf = scratch_read(fs, blk)))
197 1.1 uch return EIO;
198 1.1 uch
199 1.1 uch dir = (struct v7fs_dirent *)buf;
200 1.1 uch n = sz / sizeof(*dir);
201 1.1 uch
202 1.1 uch for (i = 0; i < n; i++, dir++) { /*disk endian */
203 1.1 uch if (strncmp(p->name, (const char *)dir->name, V7FS_NAME_MAX)
204 1.1 uch == 0) {
205 1.1 uch /* Replace inode# */
206 1.1 uch dir->inode_number = V7FS_VAL16(fs, p->inode_number);
207 1.1 uch /* Write back. */
208 1.1 uch if (!fs->io.write(fs->io.cookie, buf, blk))
209 1.1 uch ret = EIO;
210 1.1 uch else
211 1.1 uch ret = V7FS_ITERATOR_BREAK;
212 1.1 uch break;
213 1.1 uch }
214 1.1 uch }
215 1.1 uch scratch_free(fs, buf);
216 1.1 uch
217 1.1 uch return ret;
218 1.1 uch }
219 1.1 uch
220 1.1 uch bool
221 1.1 uch v7fs_file_lookup_by_number(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
222 1.1 uch v7fs_ino_t ino, char *buf)
223 1.1 uch {
224 1.1 uch int ret;
225 1.1 uch
226 1.1 uch ret = v7fs_datablock_foreach(fs, parent_dir, lookup_by_number_subr,
227 1.1 uch &(struct v7fs_lookup_arg){ .inode_number = ino, .buf = buf });
228 1.1 uch
229 1.1 uch return ret == V7FS_ITERATOR_BREAK;
230 1.1 uch }
231 1.1 uch
232 1.1 uch static int
233 1.1 uch lookup_by_number_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk,
234 1.1 uch size_t sz)
235 1.1 uch {
236 1.1 uch struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
237 1.1 uch struct v7fs_dirent *dir;
238 1.1 uch void *buf;
239 1.1 uch size_t i, n;
240 1.1 uch int ret = 0;
241 1.1 uch
242 1.1 uch if (!(buf = scratch_read(fs, blk)))
243 1.1 uch return EIO;
244 1.1 uch
245 1.1 uch dir = (struct v7fs_dirent *)buf;
246 1.1 uch n = sz / sizeof(*dir);
247 1.1 uch v7fs_dirent_endian_convert(fs, dir, n);
248 1.1 uch
249 1.1 uch for (i = 0; i < n; i++, dir++) {
250 1.1 uch if (dir->inode_number == p->inode_number) {
251 1.1 uch if (p->buf)
252 1.1 uch v7fs_dirent_filename(p->buf, dir->name);
253 1.1 uch ret = V7FS_ITERATOR_BREAK;
254 1.1 uch break;
255 1.1 uch }
256 1.1 uch }
257 1.1 uch scratch_free(fs, buf);
258 1.1 uch
259 1.1 uch return ret;
260 1.1 uch }
261