]>
Commit | Line | Data |
---|---|---|
0d4deba2 RY |
1 | #include "create_inode.h" |
2 | ||
052064b9 RY |
3 | #if __STDC_VERSION__ < 199901L |
4 | # if __GNUC__ >= 2 | |
5 | # define __func__ __FUNCTION__ | |
6 | # else | |
7 | # define __func__ "<unknown>" | |
8 | # endif | |
9 | #endif | |
10 | ||
ce600dc4 RY |
11 | /* 64KiB is the minimium blksize to best minimize system call overhead. */ |
12 | #ifndef IO_BUFSIZE | |
13 | #define IO_BUFSIZE 64*1024 | |
14 | #endif | |
15 | ||
16 | /* Block size for `st_blocks' */ | |
17 | #ifndef S_BLKSIZE | |
18 | #define S_BLKSIZE 512 | |
19 | #endif | |
20 | ||
0d4deba2 RY |
21 | /* Make a special file which is block, character and fifo */ |
22 | errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st) | |
23 | { | |
b2346118 RY |
24 | ext2_ino_t ino; |
25 | errcode_t retval; | |
26 | struct ext2_inode inode; | |
27 | unsigned long major, minor, mode; | |
28 | int filetype; | |
29 | ||
30 | switch(st->st_mode & S_IFMT) { | |
31 | case S_IFCHR: | |
32 | mode = LINUX_S_IFCHR; | |
33 | filetype = EXT2_FT_CHRDEV; | |
34 | break; | |
35 | case S_IFBLK: | |
36 | mode = LINUX_S_IFBLK; | |
37 | filetype = EXT2_FT_BLKDEV; | |
38 | break; | |
39 | case S_IFIFO: | |
40 | mode = LINUX_S_IFIFO; | |
41 | filetype = EXT2_FT_FIFO; | |
42 | break; | |
43 | } | |
44 | ||
45 | if (!(current_fs->flags & EXT2_FLAG_RW)) { | |
46 | com_err(__func__, 0, "Filesystem opened read/only"); | |
47 | return -1; | |
48 | } | |
49 | retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino); | |
50 | if (retval) { | |
51 | com_err(__func__, retval, 0); | |
52 | return retval; | |
53 | } | |
54 | ||
55 | #ifdef DEBUGFS | |
56 | printf("Allocated inode: %u\n", ino); | |
57 | #endif | |
58 | retval = ext2fs_link(current_fs, cwd, name, ino, filetype); | |
59 | if (retval == EXT2_ET_DIR_NO_SPACE) { | |
60 | retval = ext2fs_expand_dir(current_fs, cwd); | |
61 | if (retval) { | |
62 | com_err(__func__, retval, "while expanding directory"); | |
63 | return retval; | |
64 | } | |
65 | retval = ext2fs_link(current_fs, cwd, name, ino, filetype); | |
66 | } | |
67 | if (retval) { | |
68 | com_err(name, retval, 0); | |
69 | return -1; | |
70 | } | |
71 | if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) | |
72 | com_err(__func__, 0, "Warning: inode already set"); | |
73 | ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0); | |
74 | memset(&inode, 0, sizeof(inode)); | |
75 | inode.i_mode = mode; | |
76 | inode.i_atime = inode.i_ctime = inode.i_mtime = | |
77 | current_fs->now ? current_fs->now : time(0); | |
78 | ||
79 | major = major(st->st_rdev); | |
80 | minor = minor(st->st_rdev); | |
81 | ||
82 | if ((major < 256) && (minor < 256)) { | |
83 | inode.i_block[0] = major * 256 + minor; | |
84 | inode.i_block[1] = 0; | |
85 | } else { | |
86 | inode.i_block[0] = 0; | |
87 | inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12); | |
88 | } | |
89 | inode.i_links_count = 1; | |
90 | ||
91 | retval = ext2fs_write_new_inode(current_fs, ino, &inode); | |
92 | if (retval) | |
93 | com_err(__func__, retval, "while creating inode %u", ino); | |
94 | ||
95 | return retval; | |
0d4deba2 RY |
96 | } |
97 | ||
98 | /* Make a symlink name -> target */ | |
99 | errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target) | |
100 | { | |
b25ddc5e RY |
101 | char *cp; |
102 | ext2_ino_t parent_ino; | |
103 | errcode_t retval; | |
104 | struct ext2_inode inode; | |
105 | struct stat st; | |
106 | ||
107 | cp = strrchr(name, '/'); | |
108 | if (cp) { | |
109 | *cp = 0; | |
110 | if ((retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino))){ | |
111 | com_err(name, retval, 0); | |
112 | return retval; | |
113 | } | |
114 | name = cp+1; | |
115 | } else | |
116 | parent_ino = cwd; | |
117 | ||
118 | try_again: | |
119 | retval = ext2fs_symlink(current_fs, parent_ino, 0, name, target); | |
120 | if (retval == EXT2_ET_DIR_NO_SPACE) { | |
121 | retval = ext2fs_expand_dir(current_fs, parent_ino); | |
122 | if (retval) { | |
123 | com_err("do_symlink_internal", retval, "while expanding directory"); | |
124 | return retval; | |
125 | } | |
126 | goto try_again; | |
127 | } | |
128 | if (retval) { | |
129 | com_err("ext2fs_symlink", retval, 0); | |
130 | return retval; | |
131 | } | |
132 | ||
0d4deba2 RY |
133 | } |
134 | ||
135 | /* Make a directory in the fs */ | |
136 | errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st) | |
137 | { | |
138 | } | |
139 | ||
ce600dc4 RY |
140 | static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes) |
141 | { | |
142 | ext2_file_t e2_file; | |
143 | errcode_t retval; | |
144 | int got; | |
145 | unsigned int written; | |
146 | char *buf; | |
147 | char *ptr; | |
148 | char *zero_buf; | |
149 | int cmp; | |
150 | ||
151 | retval = ext2fs_file_open(current_fs, newfile, | |
152 | EXT2_FILE_WRITE, &e2_file); | |
153 | if (retval) | |
154 | return retval; | |
155 | ||
156 | retval = ext2fs_get_mem(bufsize, &buf); | |
157 | if (retval) { | |
158 | com_err("copy_file", retval, "can't allocate buffer\n"); | |
159 | return retval; | |
160 | } | |
161 | ||
162 | /* This is used for checking whether the whole block is zero */ | |
163 | retval = ext2fs_get_memzero(bufsize, &zero_buf); | |
164 | if (retval) { | |
165 | com_err("copy_file", retval, "can't allocate buffer\n"); | |
166 | ext2fs_free_mem(&buf); | |
167 | return retval; | |
168 | } | |
169 | ||
170 | while (1) { | |
171 | got = read(fd, buf, bufsize); | |
172 | if (got == 0) | |
173 | break; | |
174 | if (got < 0) { | |
175 | retval = errno; | |
176 | goto fail; | |
177 | } | |
178 | ptr = buf; | |
179 | ||
180 | /* Sparse copy */ | |
181 | if (make_holes) { | |
182 | /* Check whether all is zero */ | |
183 | cmp = memcmp(ptr, zero_buf, got); | |
184 | if (cmp == 0) { | |
185 | /* The whole block is zero, make a hole */ | |
186 | retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL); | |
187 | if (retval) | |
188 | goto fail; | |
189 | got = 0; | |
190 | } | |
191 | } | |
192 | ||
193 | /* Normal copy */ | |
194 | while (got > 0) { | |
195 | retval = ext2fs_file_write(e2_file, ptr, | |
196 | got, &written); | |
197 | if (retval) | |
198 | goto fail; | |
199 | ||
200 | got -= written; | |
201 | ptr += written; | |
202 | } | |
203 | } | |
204 | ext2fs_free_mem(&buf); | |
205 | ext2fs_free_mem(&zero_buf); | |
206 | retval = ext2fs_file_close(e2_file); | |
207 | return retval; | |
208 | ||
209 | fail: | |
210 | ext2fs_free_mem(&buf); | |
211 | ext2fs_free_mem(&zero_buf); | |
212 | (void) ext2fs_file_close(e2_file); | |
213 | return retval; | |
214 | } | |
215 | ||
0d4deba2 RY |
216 | /* Copy the native file to the fs */ |
217 | errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest) | |
218 | { | |
ce600dc4 RY |
219 | int fd; |
220 | struct stat statbuf; | |
221 | ext2_ino_t newfile; | |
222 | errcode_t retval; | |
223 | struct ext2_inode inode; | |
224 | int bufsize = IO_BUFSIZE; | |
225 | int make_holes = 0; | |
226 | ||
227 | fd = open(src, O_RDONLY); | |
228 | if (fd < 0) { | |
229 | com_err(src, errno, 0); | |
230 | return errno; | |
231 | } | |
232 | if (fstat(fd, &statbuf) < 0) { | |
233 | com_err(src, errno, 0); | |
234 | close(fd); | |
235 | return errno; | |
236 | } | |
237 | ||
238 | retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile); | |
239 | if (retval == 0) { | |
240 | com_err(__func__, 0, "The file '%s' already exists\n", dest); | |
241 | close(fd); | |
242 | return errno; | |
243 | } | |
244 | ||
245 | retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); | |
246 | if (retval) { | |
247 | com_err(__func__, retval, 0); | |
248 | close(fd); | |
249 | return errno; | |
250 | } | |
251 | #ifdef DEBUGFS | |
252 | printf("Allocated inode: %u\n", newfile); | |
253 | #endif | |
254 | retval = ext2fs_link(current_fs, cwd, dest, newfile, | |
255 | EXT2_FT_REG_FILE); | |
256 | if (retval == EXT2_ET_DIR_NO_SPACE) { | |
257 | retval = ext2fs_expand_dir(current_fs, cwd); | |
258 | if (retval) { | |
259 | com_err(__func__, retval, "while expanding directory"); | |
260 | close(fd); | |
261 | return errno; | |
262 | } | |
263 | retval = ext2fs_link(current_fs, cwd, dest, newfile, | |
264 | EXT2_FT_REG_FILE); | |
265 | } | |
266 | if (retval) { | |
267 | com_err(dest, retval, 0); | |
268 | close(fd); | |
269 | return errno; | |
270 | } | |
271 | if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile)) | |
272 | com_err(__func__, 0, "Warning: inode already set"); | |
273 | ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0); | |
274 | memset(&inode, 0, sizeof(inode)); | |
275 | inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG; | |
276 | inode.i_atime = inode.i_ctime = inode.i_mtime = | |
277 | current_fs->now ? current_fs->now : time(0); | |
278 | inode.i_links_count = 1; | |
279 | inode.i_size = statbuf.st_size; | |
280 | if (current_fs->super->s_feature_incompat & | |
281 | EXT3_FEATURE_INCOMPAT_EXTENTS) { | |
282 | int i; | |
283 | struct ext3_extent_header *eh; | |
284 | ||
285 | eh = (struct ext3_extent_header *) &inode.i_block[0]; | |
286 | eh->eh_depth = 0; | |
287 | eh->eh_entries = 0; | |
288 | eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC); | |
289 | i = (sizeof(inode.i_block) - sizeof(*eh)) / | |
290 | sizeof(struct ext3_extent); | |
291 | eh->eh_max = ext2fs_cpu_to_le16(i); | |
292 | inode.i_flags |= EXT4_EXTENTS_FL; | |
293 | } | |
294 | ||
295 | if ((retval = ext2fs_write_new_inode(current_fs, newfile, &inode))) { | |
296 | com_err(__func__, retval, "while creating inode %u", newfile); | |
297 | close(fd); | |
298 | return errno; | |
299 | } | |
300 | if (LINUX_S_ISREG(inode.i_mode)) { | |
301 | if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) { | |
302 | make_holes = 1; | |
303 | /* | |
304 | * Use I/O blocksize as buffer size when | |
305 | * copying sparse files. | |
306 | */ | |
307 | bufsize = statbuf.st_blksize; | |
308 | } | |
309 | retval = copy_file(fd, newfile, bufsize, make_holes); | |
310 | if (retval) | |
311 | com_err("copy_file", retval, 0); | |
312 | } | |
313 | close(fd); | |
314 | ||
315 | return 0; | |
0d4deba2 RY |
316 | } |
317 | ||
318 | /* Copy files from source_dir to fs */ | |
319 | errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir) | |
320 | { | |
052064b9 RY |
321 | const char *name; |
322 | DIR *dh; | |
323 | struct dirent *dent; | |
324 | struct stat st; | |
325 | char ln_target[PATH_MAX]; | |
326 | ext2_ino_t ino; | |
327 | errcode_t retval; | |
328 | int read_cnt; | |
329 | ||
330 | root = EXT2_ROOT_INO; | |
331 | ||
332 | if (chdir(source_dir) < 0) { | |
333 | com_err(__func__, errno, | |
334 | _("while changing working directory to \"%s\""), source_dir); | |
335 | return errno; | |
336 | } | |
337 | ||
338 | if (!(dh = opendir("."))) { | |
339 | com_err(__func__, errno, | |
340 | _("while openning directory \"%s\""), source_dir); | |
341 | return errno; | |
342 | } | |
343 | ||
344 | while((dent = readdir(dh))) { | |
345 | if((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, ".."))) | |
346 | continue; | |
347 | lstat(dent->d_name, &st); | |
348 | name = dent->d_name; | |
349 | ||
350 | switch(st.st_mode & S_IFMT) { | |
351 | case S_IFCHR: | |
352 | case S_IFBLK: | |
353 | case S_IFIFO: | |
354 | if ((retval = do_mknod_internal(parent_ino, name, &st))) { | |
355 | com_err(__func__, retval, | |
356 | _("while creating special file \"%s\""), name); | |
357 | return retval; | |
358 | } | |
359 | break; | |
360 | case S_IFSOCK: | |
361 | /* FIXME: there is no make socket function atm. */ | |
362 | com_err(__func__, 0, | |
363 | _("ignoring socket file \"%s\""), name); | |
364 | continue; | |
365 | case S_IFLNK: | |
366 | if((read_cnt = readlink(name, ln_target, sizeof(ln_target))) == -1) { | |
367 | com_err(__func__, errno, | |
368 | _("while trying to readlink \"%s\""), name); | |
369 | return errno; | |
370 | } | |
371 | ln_target[read_cnt] = '\0'; | |
372 | if ((retval = do_symlink_internal(parent_ino, name, ln_target))) { | |
373 | com_err(__func__, retval, | |
374 | _("while writing symlink\"%s\""), name); | |
375 | return retval; | |
376 | } | |
377 | break; | |
378 | case S_IFREG: | |
379 | if ((retval = do_write_internal(parent_ino, name, name))) { | |
380 | com_err(__func__, retval, | |
381 | _("while writing file \"%s\""), name); | |
382 | return retval; | |
383 | } | |
384 | break; | |
385 | case S_IFDIR: | |
386 | if ((retval = do_mkdir_internal(parent_ino, name, &st))) { | |
387 | com_err(__func__, retval, | |
388 | _("while making dir \"%s\""), name); | |
389 | return retval; | |
390 | } | |
391 | if ((retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino))) { | |
392 | com_err(name, retval, 0); | |
393 | return retval; | |
394 | } | |
395 | /* Populate the dir recursively*/ | |
396 | retval = populate_fs(ino, name); | |
397 | if (retval) { | |
398 | com_err(__func__, retval, _("while adding dir \"%s\""), name); | |
399 | return retval; | |
400 | } | |
401 | chdir(".."); | |
402 | break; | |
403 | default: | |
404 | com_err(__func__, 0, | |
405 | _("ignoring entry \"%s\""), name); | |
406 | } | |
407 | } | |
408 | closedir(dh); | |
409 | return retval; | |
0d4deba2 | 410 | } |