]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/create_inode.c
create_inode: minor cleanups
[thirdparty/e2fsprogs.git] / misc / create_inode.c
CommitLineData
8f8d8a57
DW
1#include <time.h>
2#include <unistd.h>
3
0d4deba2
RY
4#include "create_inode.h"
5
052064b9
RY
6#if __STDC_VERSION__ < 199901L
7# if __GNUC__ >= 2
8# define __func__ __FUNCTION__
9# else
10# define __func__ "<unknown>"
11# endif
12#endif
13
ce600dc4
RY
14/* 64KiB is the minimium blksize to best minimize system call overhead. */
15#ifndef IO_BUFSIZE
16#define IO_BUFSIZE 64*1024
17#endif
18
19/* Block size for `st_blocks' */
20#ifndef S_BLKSIZE
21#define S_BLKSIZE 512
22#endif
23
f84894bc
RY
24/* For saving the hard links */
25int hdlink_cnt = HDLINK_CNT;
26
27/* Link an inode number to a directory */
28static errcode_t add_link(ext2_ino_t parent_ino, ext2_ino_t ino, const char *name)
29{
30 struct ext2_inode inode;
31 errcode_t retval;
32
33 retval = ext2fs_read_inode(current_fs, ino, &inode);
34 if (retval) {
35 com_err(__func__, retval, "while reading inode %u", ino);
36 return retval;
37 }
38
39 retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
40 if (retval == EXT2_ET_DIR_NO_SPACE) {
41 retval = ext2fs_expand_dir(current_fs, parent_ino);
42 if (retval) {
43 com_err(__func__, retval, "while expanding directory");
44 return retval;
45 }
46 retval = ext2fs_link(current_fs, parent_ino, name, ino, inode.i_flags);
47 }
48 if (retval) {
49 com_err(__func__, retval, "while linking %s", name);
50 return retval;
51 }
52
53 inode.i_links_count++;
54
55 retval = ext2fs_write_inode(current_fs, ino, &inode);
56 if (retval)
57 com_err(__func__, retval, "while writing inode %u", ino);
58
59 return retval;
60}
61
b6960654
RY
62/* Fill the uid, gid, mode and time for the inode */
63static void fill_inode(struct ext2_inode *inode, struct stat *st)
64{
65 if (st != NULL) {
66 inode->i_uid = st->st_uid;
67 inode->i_gid = st->st_gid;
68 inode->i_mode |= st->st_mode;
69 inode->i_atime = st->st_atime;
70 inode->i_mtime = st->st_mtime;
71 inode->i_ctime = st->st_ctime;
72 }
73}
74
75/* Set the uid, gid, mode and time for the inode */
76errcode_t set_inode_extra(ext2_ino_t cwd, ext2_ino_t ino, struct stat *st)
77{
78 errcode_t retval;
79 struct ext2_inode inode;
80
81 retval = ext2fs_read_inode(current_fs, ino, &inode);
82 if (retval) {
83 com_err(__func__, retval, "while reading inode %u", ino);
84 return retval;
85 }
86
87 fill_inode(&inode, st);
88
89 retval = ext2fs_write_inode(current_fs, ino, &inode);
90 if (retval) {
91 com_err(__func__, retval, "while writing inode %u", ino);
92 return retval;
93 }
94}
95
0d4deba2
RY
96/* Make a special file which is block, character and fifo */
97errcode_t do_mknod_internal(ext2_ino_t cwd, const char *name, struct stat *st)
98{
b2346118
RY
99 ext2_ino_t ino;
100 errcode_t retval;
101 struct ext2_inode inode;
102 unsigned long major, minor, mode;
103 int filetype;
104
105 switch(st->st_mode & S_IFMT) {
106 case S_IFCHR:
107 mode = LINUX_S_IFCHR;
108 filetype = EXT2_FT_CHRDEV;
109 break;
110 case S_IFBLK:
111 mode = LINUX_S_IFBLK;
112 filetype = EXT2_FT_BLKDEV;
113 break;
114 case S_IFIFO:
115 mode = LINUX_S_IFIFO;
116 filetype = EXT2_FT_FIFO;
117 break;
118 }
119
120 if (!(current_fs->flags & EXT2_FLAG_RW)) {
121 com_err(__func__, 0, "Filesystem opened read/only");
122 return -1;
123 }
124 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &ino);
125 if (retval) {
126 com_err(__func__, retval, 0);
127 return retval;
128 }
129
130#ifdef DEBUGFS
131 printf("Allocated inode: %u\n", ino);
132#endif
133 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
134 if (retval == EXT2_ET_DIR_NO_SPACE) {
135 retval = ext2fs_expand_dir(current_fs, cwd);
136 if (retval) {
137 com_err(__func__, retval, "while expanding directory");
138 return retval;
139 }
140 retval = ext2fs_link(current_fs, cwd, name, ino, filetype);
141 }
142 if (retval) {
143 com_err(name, retval, 0);
144 return -1;
145 }
146 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino))
147 com_err(__func__, 0, "Warning: inode already set");
148 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
149 memset(&inode, 0, sizeof(inode));
150 inode.i_mode = mode;
151 inode.i_atime = inode.i_ctime = inode.i_mtime =
152 current_fs->now ? current_fs->now : time(0);
153
154 major = major(st->st_rdev);
155 minor = minor(st->st_rdev);
156
157 if ((major < 256) && (minor < 256)) {
158 inode.i_block[0] = major * 256 + minor;
159 inode.i_block[1] = 0;
160 } else {
161 inode.i_block[0] = 0;
162 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
163 }
164 inode.i_links_count = 1;
165
166 retval = ext2fs_write_new_inode(current_fs, ino, &inode);
167 if (retval)
168 com_err(__func__, retval, "while creating inode %u", ino);
169
170 return retval;
0d4deba2
RY
171}
172
173/* Make a symlink name -> target */
174errcode_t do_symlink_internal(ext2_ino_t cwd, const char *name, char *target)
175{
b25ddc5e
RY
176 char *cp;
177 ext2_ino_t parent_ino;
178 errcode_t retval;
179 struct ext2_inode inode;
180 struct stat st;
181
182 cp = strrchr(name, '/');
183 if (cp) {
184 *cp = 0;
8f8d8a57
DW
185 retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino);
186 if (retval) {
b25ddc5e
RY
187 com_err(name, retval, 0);
188 return retval;
189 }
190 name = cp+1;
191 } else
192 parent_ino = cwd;
193
194try_again:
195 retval = ext2fs_symlink(current_fs, parent_ino, 0, name, target);
196 if (retval == EXT2_ET_DIR_NO_SPACE) {
197 retval = ext2fs_expand_dir(current_fs, parent_ino);
198 if (retval) {
199 com_err("do_symlink_internal", retval, "while expanding directory");
200 return retval;
201 }
202 goto try_again;
203 }
204 if (retval) {
205 com_err("ext2fs_symlink", retval, 0);
206 return retval;
207 }
208
0d4deba2
RY
209}
210
211/* Make a directory in the fs */
212errcode_t do_mkdir_internal(ext2_ino_t cwd, const char *name, struct stat *st)
213{
3068f03f
RY
214 char *cp;
215 ext2_ino_t parent_ino, ino;
216 errcode_t retval;
217 struct ext2_inode inode;
218
219
220 cp = strrchr(name, '/');
221 if (cp) {
222 *cp = 0;
8f8d8a57
DW
223 retval = ext2fs_namei(current_fs, root, cwd, name, &parent_ino);
224 if (retval) {
3068f03f
RY
225 com_err(name, retval, 0);
226 return retval;
227 }
228 name = cp+1;
229 } else
230 parent_ino = cwd;
231
232try_again:
233 retval = ext2fs_mkdir(current_fs, parent_ino, 0, name);
234 if (retval == EXT2_ET_DIR_NO_SPACE) {
235 retval = ext2fs_expand_dir(current_fs, parent_ino);
236 if (retval) {
237 com_err(__func__, retval, "while expanding directory");
238 return retval;
239 }
240 goto try_again;
241 }
242 if (retval) {
243 com_err("ext2fs_mkdir", retval, 0);
244 return retval;
245 }
0d4deba2
RY
246}
247
ce600dc4
RY
248static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
249{
250 ext2_file_t e2_file;
251 errcode_t retval;
252 int got;
253 unsigned int written;
254 char *buf;
255 char *ptr;
256 char *zero_buf;
257 int cmp;
258
259 retval = ext2fs_file_open(current_fs, newfile,
260 EXT2_FILE_WRITE, &e2_file);
261 if (retval)
262 return retval;
263
264 retval = ext2fs_get_mem(bufsize, &buf);
265 if (retval) {
266 com_err("copy_file", retval, "can't allocate buffer\n");
267 return retval;
268 }
269
270 /* This is used for checking whether the whole block is zero */
271 retval = ext2fs_get_memzero(bufsize, &zero_buf);
272 if (retval) {
273 com_err("copy_file", retval, "can't allocate buffer\n");
274 ext2fs_free_mem(&buf);
275 return retval;
276 }
277
278 while (1) {
279 got = read(fd, buf, bufsize);
280 if (got == 0)
281 break;
282 if (got < 0) {
283 retval = errno;
284 goto fail;
285 }
286 ptr = buf;
287
288 /* Sparse copy */
289 if (make_holes) {
290 /* Check whether all is zero */
291 cmp = memcmp(ptr, zero_buf, got);
292 if (cmp == 0) {
293 /* The whole block is zero, make a hole */
294 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
295 if (retval)
296 goto fail;
297 got = 0;
298 }
299 }
300
301 /* Normal copy */
302 while (got > 0) {
303 retval = ext2fs_file_write(e2_file, ptr,
304 got, &written);
305 if (retval)
306 goto fail;
307
308 got -= written;
309 ptr += written;
310 }
311 }
312 ext2fs_free_mem(&buf);
313 ext2fs_free_mem(&zero_buf);
314 retval = ext2fs_file_close(e2_file);
315 return retval;
316
317fail:
318 ext2fs_free_mem(&buf);
319 ext2fs_free_mem(&zero_buf);
320 (void) ext2fs_file_close(e2_file);
321 return retval;
322}
323
f84894bc
RY
324int is_hardlink(ext2_ino_t ino)
325{
326 int i;
327
328 for(i = 0; i < hdlinks.count; i++) {
329 if(hdlinks.hdl[i].src_ino == ino)
330 return i;
331 }
332 return -1;
333}
334
0d4deba2
RY
335/* Copy the native file to the fs */
336errcode_t do_write_internal(ext2_ino_t cwd, const char *src, const char *dest)
337{
ce600dc4
RY
338 int fd;
339 struct stat statbuf;
340 ext2_ino_t newfile;
341 errcode_t retval;
342 struct ext2_inode inode;
343 int bufsize = IO_BUFSIZE;
344 int make_holes = 0;
345
346 fd = open(src, O_RDONLY);
347 if (fd < 0) {
348 com_err(src, errno, 0);
349 return errno;
350 }
351 if (fstat(fd, &statbuf) < 0) {
352 com_err(src, errno, 0);
353 close(fd);
354 return errno;
355 }
356
357 retval = ext2fs_namei(current_fs, root, cwd, dest, &newfile);
358 if (retval == 0) {
ce600dc4 359 close(fd);
117b54c3 360 return EXT2_ET_FILE_EXISTS;
ce600dc4
RY
361 }
362
363 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
364 if (retval) {
365 com_err(__func__, retval, 0);
366 close(fd);
117b54c3 367 return retval;
ce600dc4
RY
368 }
369#ifdef DEBUGFS
370 printf("Allocated inode: %u\n", newfile);
371#endif
372 retval = ext2fs_link(current_fs, cwd, dest, newfile,
373 EXT2_FT_REG_FILE);
374 if (retval == EXT2_ET_DIR_NO_SPACE) {
375 retval = ext2fs_expand_dir(current_fs, cwd);
376 if (retval) {
377 com_err(__func__, retval, "while expanding directory");
378 close(fd);
117b54c3 379 return retval;
ce600dc4
RY
380 }
381 retval = ext2fs_link(current_fs, cwd, dest, newfile,
382 EXT2_FT_REG_FILE);
383 }
384 if (retval) {
385 com_err(dest, retval, 0);
386 close(fd);
387 return errno;
388 }
389 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, newfile))
390 com_err(__func__, 0, "Warning: inode already set");
391 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
392 memset(&inode, 0, sizeof(inode));
393 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
394 inode.i_atime = inode.i_ctime = inode.i_mtime =
395 current_fs->now ? current_fs->now : time(0);
396 inode.i_links_count = 1;
397 inode.i_size = statbuf.st_size;
d23b1965
TT
398 if (EXT2_HAS_INCOMPAT_FEATURE(current_fs->super,
399 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
400 inode.i_flags |= EXT4_INLINE_DATA_FL;
401 } else if (current_fs->super->s_feature_incompat &
402 EXT3_FEATURE_INCOMPAT_EXTENTS) {
ce600dc4
RY
403 int i;
404 struct ext3_extent_header *eh;
405
406 eh = (struct ext3_extent_header *) &inode.i_block[0];
407 eh->eh_depth = 0;
408 eh->eh_entries = 0;
409 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
410 i = (sizeof(inode.i_block) - sizeof(*eh)) /
411 sizeof(struct ext3_extent);
412 eh->eh_max = ext2fs_cpu_to_le16(i);
413 inode.i_flags |= EXT4_EXTENTS_FL;
414 }
415
8f8d8a57
DW
416 retval = ext2fs_write_new_inode(current_fs, newfile, &inode);
417 if (retval) {
ce600dc4
RY
418 com_err(__func__, retval, "while creating inode %u", newfile);
419 close(fd);
117b54c3 420 return retval;
ce600dc4 421 }
d23b1965
TT
422 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
423 retval = ext2fs_inline_data_init(current_fs, newfile);
117b54c3
DW
424 if (retval) {
425 com_err("copy_file", retval, 0);
426 close(fd);
427 return retval;
428 }
d23b1965 429 }
ce600dc4
RY
430 if (LINUX_S_ISREG(inode.i_mode)) {
431 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
432 make_holes = 1;
433 /*
434 * Use I/O blocksize as buffer size when
435 * copying sparse files.
436 */
437 bufsize = statbuf.st_blksize;
438 }
439 retval = copy_file(fd, newfile, bufsize, make_holes);
440 if (retval)
441 com_err("copy_file", retval, 0);
442 }
443 close(fd);
444
117b54c3 445 return retval;
0d4deba2
RY
446}
447
448/* Copy files from source_dir to fs */
449errcode_t populate_fs(ext2_ino_t parent_ino, const char *source_dir)
450{
052064b9
RY
451 const char *name;
452 DIR *dh;
453 struct dirent *dent;
454 struct stat st;
455 char ln_target[PATH_MAX];
f84894bc 456 unsigned int save_inode;
052064b9
RY
457 ext2_ino_t ino;
458 errcode_t retval;
459 int read_cnt;
f84894bc 460 int hdlink;
052064b9
RY
461
462 root = EXT2_ROOT_INO;
463
464 if (chdir(source_dir) < 0) {
465 com_err(__func__, errno,
466 _("while changing working directory to \"%s\""), source_dir);
467 return errno;
468 }
469
470 if (!(dh = opendir("."))) {
471 com_err(__func__, errno,
8f8d8a57 472 _("while opening directory \"%s\""), source_dir);
052064b9
RY
473 return errno;
474 }
475
8f8d8a57
DW
476 while ((dent = readdir(dh))) {
477 if ((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, "..")))
052064b9
RY
478 continue;
479 lstat(dent->d_name, &st);
480 name = dent->d_name;
481
f84894bc
RY
482 /* Check for hardlinks */
483 save_inode = 0;
484 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) && st.st_nlink > 1) {
485 hdlink = is_hardlink(st.st_ino);
486 if (hdlink >= 0) {
487 retval = add_link(parent_ino,
488 hdlinks.hdl[hdlink].dst_ino, name);
489 if (retval) {
490 com_err(__func__, retval, "while linking %s", name);
491 return retval;
492 }
493 continue;
494 } else
495 save_inode = 1;
496 }
497
052064b9
RY
498 switch(st.st_mode & S_IFMT) {
499 case S_IFCHR:
500 case S_IFBLK:
501 case S_IFIFO:
8f8d8a57
DW
502 retval = do_mknod_internal(parent_ino, name, &st);
503 if (retval) {
052064b9
RY
504 com_err(__func__, retval,
505 _("while creating special file \"%s\""), name);
506 return retval;
507 }
508 break;
509 case S_IFSOCK:
510 /* FIXME: there is no make socket function atm. */
511 com_err(__func__, 0,
512 _("ignoring socket file \"%s\""), name);
513 continue;
514 case S_IFLNK:
8f8d8a57
DW
515 read_cnt = readlink(name, ln_target, sizeof(ln_target));
516 if (read_cnt == -1) {
052064b9
RY
517 com_err(__func__, errno,
518 _("while trying to readlink \"%s\""), name);
519 return errno;
520 }
521 ln_target[read_cnt] = '\0';
8f8d8a57
DW
522 retval = do_symlink_internal(parent_ino, name, ln_target);
523 if (retval) {
052064b9
RY
524 com_err(__func__, retval,
525 _("while writing symlink\"%s\""), name);
526 return retval;
527 }
528 break;
529 case S_IFREG:
8f8d8a57
DW
530 retval = do_write_internal(parent_ino, name, name);
531 if (retval) {
052064b9
RY
532 com_err(__func__, retval,
533 _("while writing file \"%s\""), name);
534 return retval;
535 }
536 break;
537 case S_IFDIR:
8f8d8a57
DW
538 retval = do_mkdir_internal(parent_ino, name, &st);
539 if (retval) {
052064b9
RY
540 com_err(__func__, retval,
541 _("while making dir \"%s\""), name);
542 return retval;
543 }
8f8d8a57
DW
544 retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino);
545 if (retval) {
052064b9
RY
546 com_err(name, retval, 0);
547 return retval;
548 }
549 /* Populate the dir recursively*/
550 retval = populate_fs(ino, name);
551 if (retval) {
552 com_err(__func__, retval, _("while adding dir \"%s\""), name);
553 return retval;
554 }
555 chdir("..");
556 break;
557 default:
558 com_err(__func__, 0,
559 _("ignoring entry \"%s\""), name);
560 }
b6960654 561
8f8d8a57
DW
562 retval = ext2fs_namei(current_fs, root, parent_ino, name, &ino);
563 if (retval) {
b6960654
RY
564 com_err(name, retval, 0);
565 return retval;
566 }
567
8f8d8a57
DW
568 retval = set_inode_extra(parent_ino, ino, &st);
569 if (retval) {
b6960654
RY
570 com_err(__func__, retval,
571 _("while setting inode for \"%s\""), name);
572 return retval;
573 }
f84894bc
RY
574
575 /* Save the hardlink ino */
576 if (save_inode) {
577 /*
578 * Check whether need more memory, and we don't need
579 * free() since the lifespan will be over after the fs
580 * populated.
581 */
582 if (hdlinks.count == hdlink_cnt) {
583 if ((hdlinks.hdl = realloc (hdlinks.hdl,
584 (hdlink_cnt + HDLINK_CNT) *
585 sizeof (struct hdlink_s))) == NULL) {
586 com_err(name, errno, "Not enough memory");
587 return errno;
588 }
589 hdlink_cnt += HDLINK_CNT;
590 }
591 hdlinks.hdl[hdlinks.count].src_ino = st.st_ino;
592 hdlinks.hdl[hdlinks.count].dst_ino = ino;
593 hdlinks.count++;
594 }
052064b9
RY
595 }
596 closedir(dh);
597 return retval;
0d4deba2 598}