1 /*
2 * create_inode.c --- create an inode
3 *
4 * Copyright (C) 2014 Robert Yang <liezhi.yang@windriver.com>
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12 #define _FILE_OFFSET_BITS 64
13 #define _LARGEFILE64_SOURCE 1
14 #define _GNU_SOURCE 1
15
16 #include "config.h"
17 #include <time.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <limits.h> /* for PATH_MAX */
22 #include <dirent.h> /* for scandir() and alphasort() */
23 #if defined HAVE_SYS_XATTR_H
24 #include <sys/xattr.h>
25 #elif defined HAVE_ATTR_XATTR_H
26 #include <attr/xattr.h>
27 #endif
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
30 #endif
31 #ifdef HAVE_SYS_SYSMACROS_H
32 #include <sys/sysmacros.h>
33 #endif
34
35 #include <ext2fs/ext2fs.h>
36 #include <ext2fs/ext2_types.h>
37 #include <ext2fs/fiemap.h>
38
39 #include "create_inode.h"
40 #include "support/nls-enable.h"
41
42 /* 64KiB is the minimum blksize to best minimize system call overhead. */
43 #define COPY_FILE_BUFLEN 65536
44
ext2_file_type(unsigned int mode)45 static int ext2_file_type(unsigned int mode)
46 {
47 if (LINUX_S_ISREG(mode))
48 return EXT2_FT_REG_FILE;
49
50 if (LINUX_S_ISDIR(mode))
51 return EXT2_FT_DIR;
52
53 if (LINUX_S_ISCHR(mode))
54 return EXT2_FT_CHRDEV;
55
56 if (LINUX_S_ISBLK(mode))
57 return EXT2_FT_BLKDEV;
58
59 if (LINUX_S_ISLNK(mode))
60 return EXT2_FT_SYMLINK;
61
62 if (LINUX_S_ISFIFO(mode))
63 return EXT2_FT_FIFO;
64
65 if (LINUX_S_ISSOCK(mode))
66 return EXT2_FT_SOCK;
67
68 return 0;
69 }
70
71 /* Link an inode number to a directory */
add_link(ext2_filsys fs,ext2_ino_t parent_ino,ext2_ino_t ino,const char * name)72 static errcode_t add_link(ext2_filsys fs, ext2_ino_t parent_ino,
73 ext2_ino_t ino, const char *name)
74 {
75 struct ext2_inode inode;
76 errcode_t retval;
77
78 retval = ext2fs_read_inode(fs, ino, &inode);
79 if (retval) {
80 com_err(__func__, retval, _("while reading inode %u"), ino);
81 return retval;
82 }
83
84 retval = ext2fs_link(fs, parent_ino, name, ino,
85 ext2_file_type(inode.i_mode));
86 if (retval == EXT2_ET_DIR_NO_SPACE) {
87 retval = ext2fs_expand_dir(fs, parent_ino);
88 if (retval) {
89 com_err(__func__, retval,
90 _("while expanding directory"));
91 return retval;
92 }
93 retval = ext2fs_link(fs, parent_ino, name, ino,
94 ext2_file_type(inode.i_mode));
95 }
96 if (retval) {
97 com_err(__func__, retval, _("while linking \"%s\""), name);
98 return retval;
99 }
100
101 inode.i_links_count++;
102
103 retval = ext2fs_write_inode(fs, ino, &inode);
104 if (retval)
105 com_err(__func__, retval, _("while writing inode %u"), ino);
106
107 return retval;
108 }
109
110 /* Set the uid, gid, mode and time for the inode */
set_inode_extra(ext2_filsys fs,ext2_ino_t ino,struct stat * st)111 static errcode_t set_inode_extra(ext2_filsys fs, ext2_ino_t ino,
112 struct stat *st)
113 {
114 errcode_t retval;
115 struct ext2_inode inode;
116
117 retval = ext2fs_read_inode(fs, ino, &inode);
118 if (retval) {
119 com_err(__func__, retval, _("while reading inode %u"), ino);
120 return retval;
121 }
122
123 inode.i_uid = st->st_uid;
124 inode.i_gid = st->st_gid;
125 inode.i_mode |= st->st_mode;
126 inode.i_atime = st->st_atime;
127 inode.i_mtime = st->st_mtime;
128 inode.i_ctime = st->st_ctime;
129
130 retval = ext2fs_write_inode(fs, ino, &inode);
131 if (retval)
132 com_err(__func__, retval, _("while writing inode %u"), ino);
133 return retval;
134 }
135
136 #ifdef HAVE_LLISTXATTR
set_inode_xattr(ext2_filsys fs,ext2_ino_t ino,const char * filename)137 static errcode_t set_inode_xattr(ext2_filsys fs, ext2_ino_t ino,
138 const char *filename)
139 {
140 errcode_t retval, close_retval;
141 struct ext2_xattr_handle *handle;
142 ssize_t size, value_size;
143 char *list = NULL;
144 int i;
145
146 if (no_copy_xattrs)
147 return 0;
148
149 size = llistxattr(filename, NULL, 0);
150 if (size == -1) {
151 retval = errno;
152 com_err(__func__, retval, _("while listing attributes of \"%s\""),
153 filename);
154 return retval;
155 } else if (size == 0) {
156 return 0;
157 }
158
159 retval = ext2fs_xattrs_open(fs, ino, &handle);
160 if (retval) {
161 if (retval == EXT2_ET_MISSING_EA_FEATURE)
162 return 0;
163 com_err(__func__, retval, _("while opening inode %u"), ino);
164 return retval;
165 }
166
167 retval = ext2fs_get_mem(size, &list);
168 if (retval) {
169 com_err(__func__, retval, _("while allocating memory"));
170 goto out;
171 }
172
173 size = llistxattr(filename, list, size);
174 if (size == -1) {
175 retval = errno;
176 com_err(__func__, retval, _("while listing attributes of \"%s\""),
177 filename);
178 goto out;
179 }
180
181 for (i = 0; i < size; i += strlen(&list[i]) + 1) {
182 const char *name = &list[i];
183 char *value;
184
185 value_size = lgetxattr(filename, name, NULL, 0);
186 if (value_size == -1) {
187 retval = errno;
188 com_err(__func__, retval,
189 _("while reading attribute \"%s\" of \"%s\""),
190 name, filename);
191 break;
192 }
193
194 retval = ext2fs_get_mem(value_size, &value);
195 if (retval) {
196 com_err(__func__, retval, _("while allocating memory"));
197 break;
198 }
199
200 value_size = lgetxattr(filename, name, value, value_size);
201 if (value_size == -1) {
202 ext2fs_free_mem(&value);
203 retval = errno;
204 com_err(__func__, retval,
205 _("while reading attribute \"%s\" of \"%s\""),
206 name, filename);
207 break;
208 }
209
210 retval = ext2fs_xattr_set(handle, name, value, value_size);
211 ext2fs_free_mem(&value);
212 if (retval) {
213 com_err(__func__, retval,
214 _("while writing attribute \"%s\" to inode %u"),
215 name, ino);
216 break;
217 }
218
219 }
220 out:
221 ext2fs_free_mem(&list);
222 close_retval = ext2fs_xattrs_close(&handle);
223 if (close_retval) {
224 com_err(__func__, retval, _("while closing inode %u"), ino);
225 retval = retval ? retval : close_retval;
226 }
227 return retval;
228 return 0;
229 }
230 #else /* HAVE_LLISTXATTR */
set_inode_xattr(ext2_filsys fs EXT2FS_ATTR ((unused)),ext2_ino_t ino EXT2FS_ATTR ((unused)),const char * filename EXT2FS_ATTR ((unused)))231 static errcode_t set_inode_xattr(ext2_filsys fs EXT2FS_ATTR((unused)),
232 ext2_ino_t ino EXT2FS_ATTR((unused)),
233 const char *filename EXT2FS_ATTR((unused)))
234 {
235 return 0;
236 }
237 #endif /* HAVE_LLISTXATTR */
238
239 #ifndef _WIN32
240 /* Make a special files (block and character devices), fifo's, and sockets */
do_mknod_internal(ext2_filsys fs,ext2_ino_t cwd,const char * name,unsigned int st_mode,unsigned int st_rdev)241 errcode_t do_mknod_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
242 unsigned int st_mode, unsigned int st_rdev)
243 {
244 ext2_ino_t ino;
245 errcode_t retval;
246 struct ext2_inode inode;
247 unsigned long devmajor, devminor, mode;
248 int filetype;
249
250 switch(st_mode & S_IFMT) {
251 case S_IFCHR:
252 mode = LINUX_S_IFCHR;
253 filetype = EXT2_FT_CHRDEV;
254 break;
255 case S_IFBLK:
256 mode = LINUX_S_IFBLK;
257 filetype = EXT2_FT_BLKDEV;
258 break;
259 case S_IFIFO:
260 mode = LINUX_S_IFIFO;
261 filetype = EXT2_FT_FIFO;
262 break;
263 #ifndef _WIN32
264 case S_IFSOCK:
265 mode = LINUX_S_IFSOCK;
266 filetype = EXT2_FT_SOCK;
267 break;
268 #endif
269 default:
270 return EXT2_ET_INVALID_ARGUMENT;
271 }
272
273 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &ino);
274 if (retval) {
275 com_err(__func__, retval, _("while allocating inode \"%s\""),
276 name);
277 return retval;
278 }
279
280 #ifdef DEBUGFS
281 printf("Allocated inode: %u\n", ino);
282 #endif
283 retval = ext2fs_link(fs, cwd, name, ino, filetype);
284 if (retval == EXT2_ET_DIR_NO_SPACE) {
285 retval = ext2fs_expand_dir(fs, cwd);
286 if (retval) {
287 com_err(__func__, retval,
288 _("while expanding directory"));
289 return retval;
290 }
291 retval = ext2fs_link(fs, cwd, name, ino, filetype);
292 }
293 if (retval) {
294 com_err(name, retval, _("while creating inode \"%s\""), name);
295 return retval;
296 }
297 if (ext2fs_test_inode_bitmap2(fs->inode_map, ino))
298 com_err(__func__, 0, "Warning: inode already set");
299 ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
300 memset(&inode, 0, sizeof(inode));
301 inode.i_mode = mode;
302 inode.i_atime = inode.i_ctime = inode.i_mtime =
303 fs->now ? fs->now : time(0);
304
305 if (filetype != S_IFIFO) {
306 devmajor = major(st_rdev);
307 devminor = minor(st_rdev);
308
309 if ((devmajor < 256) && (devminor < 256)) {
310 inode.i_block[0] = devmajor * 256 + devminor;
311 inode.i_block[1] = 0;
312 } else {
313 inode.i_block[0] = 0;
314 inode.i_block[1] = (devminor & 0xff) | (devmajor << 8) |
315 ((devminor & ~0xff) << 12);
316 }
317 }
318 inode.i_links_count = 1;
319
320 retval = ext2fs_write_new_inode(fs, ino, &inode);
321 if (retval)
322 com_err(__func__, retval, _("while writing inode %u"), ino);
323
324 return retval;
325 }
326 #endif
327
328 /* Make a symlink name -> target */
do_symlink_internal(ext2_filsys fs,ext2_ino_t cwd,const char * name,char * target,ext2_ino_t root)329 errcode_t do_symlink_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
330 char *target, ext2_ino_t root)
331 {
332 char *cp;
333 ext2_ino_t parent_ino;
334 errcode_t retval;
335
336 cp = strrchr(name, '/');
337 if (cp) {
338 *cp = 0;
339 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
340 if (retval) {
341 com_err(name, retval, 0);
342 return retval;
343 }
344 name = cp+1;
345 } else
346 parent_ino = cwd;
347
348 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
349 if (retval == EXT2_ET_DIR_NO_SPACE) {
350 retval = ext2fs_expand_dir(fs, parent_ino);
351 if (retval) {
352 com_err("do_symlink_internal", retval,
353 _("while expanding directory"));
354 return retval;
355 }
356 retval = ext2fs_symlink(fs, parent_ino, 0, name, target);
357 }
358 if (retval)
359 com_err("ext2fs_symlink", retval,
360 _("while creating symlink \"%s\""), name);
361 return retval;
362 }
363
364 /* Make a directory in the fs */
do_mkdir_internal(ext2_filsys fs,ext2_ino_t cwd,const char * name,ext2_ino_t root)365 errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name,
366 ext2_ino_t root)
367 {
368 char *cp;
369 ext2_ino_t parent_ino;
370 errcode_t retval;
371
372
373 cp = strrchr(name, '/');
374 if (cp) {
375 *cp = 0;
376 retval = ext2fs_namei(fs, root, cwd, name, &parent_ino);
377 if (retval) {
378 com_err(name, retval, _("while looking up \"%s\""),
379 name);
380 return retval;
381 }
382 name = cp+1;
383 } else
384 parent_ino = cwd;
385
386 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
387 if (retval == EXT2_ET_DIR_NO_SPACE) {
388 retval = ext2fs_expand_dir(fs, parent_ino);
389 if (retval) {
390 com_err(__func__, retval,
391 _("while expanding directory"));
392 return retval;
393 }
394 retval = ext2fs_mkdir(fs, parent_ino, 0, name);
395 }
396 if (retval)
397 com_err("ext2fs_mkdir", retval,
398 _("while creating directory \"%s\""), name);
399 return retval;
400 }
401
402 #if !defined HAVE_PREAD64 && !defined HAVE_PREAD
my_pread(int fd,void * buf,size_t count,off_t offset)403 static ssize_t my_pread(int fd, void *buf, size_t count, off_t offset)
404 {
405 if (lseek(fd, offset, SEEK_SET) < 0)
406 return 0;
407
408 return read(fd, buf, count);
409 }
410 #endif /* !defined HAVE_PREAD64 && !defined HAVE_PREAD */
411
copy_file_chunk(ext2_filsys fs,int fd,ext2_file_t e2_file,off_t start,off_t end,char * buf,char * zerobuf)412 static errcode_t copy_file_chunk(ext2_filsys fs, int fd, ext2_file_t e2_file,
413 off_t start, off_t end, char *buf,
414 char *zerobuf)
415 {
416 off_t off, bpos;
417 ssize_t got, blen;
418 unsigned int written;
419 char *ptr;
420 errcode_t err = 0;
421
422 for (off = start; off < end; off += COPY_FILE_BUFLEN) {
423 #ifdef HAVE_PREAD64
424 got = pread64(fd, buf, COPY_FILE_BUFLEN, off);
425 #elif HAVE_PREAD
426 got = pread(fd, buf, COPY_FILE_BUFLEN, off);
427 #else
428 got = my_pread(fd, buf, COPY_FILE_BUFLEN, off);
429 #endif
430 if (got < 0) {
431 err = errno;
432 goto fail;
433 }
434 for (bpos = 0, ptr = buf; bpos < got; bpos += fs->blocksize) {
435 blen = fs->blocksize;
436 if (blen > got - bpos)
437 blen = got - bpos;
438 if (memcmp(ptr, zerobuf, blen) == 0) {
439 ptr += blen;
440 continue;
441 }
442 err = ext2fs_file_llseek(e2_file, off + bpos,
443 EXT2_SEEK_SET, NULL);
444 if (err)
445 goto fail;
446 while (blen > 0) {
447 err = ext2fs_file_write(e2_file, ptr, blen,
448 &written);
449 if (err)
450 goto fail;
451 if (written == 0) {
452 err = EIO;
453 goto fail;
454 }
455 blen -= written;
456 ptr += written;
457 }
458 }
459 }
460 fail:
461 return err;
462 }
463
464 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
try_lseek_copy(ext2_filsys fs,int fd,struct stat * statbuf,ext2_file_t e2_file,char * buf,char * zerobuf)465 static errcode_t try_lseek_copy(ext2_filsys fs, int fd, struct stat *statbuf,
466 ext2_file_t e2_file, char *buf, char *zerobuf)
467 {
468 off_t data = 0, hole;
469 off_t data_blk, hole_blk;
470 errcode_t err = 0;
471
472 /* Try to use SEEK_DATA and SEEK_HOLE */
473 while (data < statbuf->st_size) {
474 data = lseek(fd, data, SEEK_DATA);
475 if (data < 0) {
476 if (errno == ENXIO)
477 break;
478 return EXT2_ET_UNIMPLEMENTED;
479 }
480 hole = lseek(fd, data, SEEK_HOLE);
481 if (hole < 0)
482 return EXT2_ET_UNIMPLEMENTED;
483
484 data_blk = data & ~(off_t)(fs->blocksize - 1);
485 hole_blk = ((hole + (off_t)(fs->blocksize - 1)) &
486 ~(off_t)(fs->blocksize - 1));
487 err = copy_file_chunk(fs, fd, e2_file, data_blk, hole_blk, buf,
488 zerobuf);
489 if (err)
490 return err;
491
492 data = hole;
493 }
494
495 return err;
496 }
497 #endif /* SEEK_DATA and SEEK_HOLE */
498
499 #if defined(FS_IOC_FIEMAP)
try_fiemap_copy(ext2_filsys fs,int fd,ext2_file_t e2_file,char * buf,char * zerobuf)500 static errcode_t try_fiemap_copy(ext2_filsys fs, int fd, ext2_file_t e2_file,
501 char *buf, char *zerobuf)
502 {
503 #define EXTENT_MAX_COUNT 512
504 struct fiemap *fiemap_buf;
505 struct fiemap_extent *ext_buf, *ext;
506 int ext_buf_size, fie_buf_size;
507 off_t pos = 0;
508 unsigned int i;
509 errcode_t err;
510
511 ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
512 fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
513
514 err = ext2fs_get_memzero(fie_buf_size, &fiemap_buf);
515 if (err)
516 return err;
517
518 ext_buf = fiemap_buf->fm_extents;
519 memset(fiemap_buf, 0, fie_buf_size);
520 fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
521 fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
522 fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
523
524 do {
525 fiemap_buf->fm_start = pos;
526 memset(ext_buf, 0, ext_buf_size);
527 err = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
528 if (err < 0 && (errno == EOPNOTSUPP || errno == ENOTTY)) {
529 err = EXT2_ET_UNIMPLEMENTED;
530 goto out;
531 } else if (err < 0) {
532 err = errno;
533 goto out;
534 } else if (fiemap_buf->fm_mapped_extents == 0)
535 goto out;
536 for (i = 0, ext = ext_buf; i < fiemap_buf->fm_mapped_extents;
537 i++, ext++) {
538 err = copy_file_chunk(fs, fd, e2_file, ext->fe_logical,
539 ext->fe_logical + ext->fe_length,
540 buf, zerobuf);
541 if (err)
542 goto out;
543 }
544
545 ext--;
546 /* Record file's logical offset this time */
547 pos = ext->fe_logical + ext->fe_length;
548 /*
549 * If fm_extents array has been filled and
550 * there are extents left, continue to cycle.
551 */
552 } while (fiemap_buf->fm_mapped_extents == EXTENT_MAX_COUNT &&
553 !(ext->fe_flags & FIEMAP_EXTENT_LAST));
554 out:
555 ext2fs_free_mem(&fiemap_buf);
556 return err;
557 }
558 #endif /* FS_IOC_FIEMAP */
559
copy_file(ext2_filsys fs,int fd,struct stat * statbuf,ext2_ino_t ino)560 static errcode_t copy_file(ext2_filsys fs, int fd, struct stat *statbuf,
561 ext2_ino_t ino)
562 {
563 ext2_file_t e2_file;
564 char *buf = NULL, *zerobuf = NULL;
565 errcode_t err, close_err;
566
567 err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &e2_file);
568 if (err)
569 return err;
570
571 err = ext2fs_get_mem(COPY_FILE_BUFLEN, &buf);
572 if (err)
573 goto out;
574
575 err = ext2fs_get_memzero(fs->blocksize, &zerobuf);
576 if (err)
577 goto out;
578
579 #if defined(SEEK_DATA) && defined(SEEK_HOLE)
580 err = try_lseek_copy(fs, fd, statbuf, e2_file, buf, zerobuf);
581 if (err != EXT2_ET_UNIMPLEMENTED)
582 goto out;
583 #endif
584
585 #if defined(FS_IOC_FIEMAP)
586 err = try_fiemap_copy(fs, fd, e2_file, buf, zerobuf);
587 if (err != EXT2_ET_UNIMPLEMENTED)
588 goto out;
589 #endif
590
591 err = copy_file_chunk(fs, fd, e2_file, 0, statbuf->st_size, buf,
592 zerobuf);
593 out:
594 ext2fs_free_mem(&zerobuf);
595 ext2fs_free_mem(&buf);
596 close_err = ext2fs_file_close(e2_file);
597 if (err == 0)
598 err = close_err;
599 return err;
600 }
601
is_hardlink(struct hdlinks_s * hdlinks,dev_t dev,ino_t ino)602 static int is_hardlink(struct hdlinks_s *hdlinks, dev_t dev, ino_t ino)
603 {
604 int i;
605
606 for (i = 0; i < hdlinks->count; i++) {
607 if (hdlinks->hdl[i].src_dev == dev &&
608 hdlinks->hdl[i].src_ino == ino)
609 return i;
610 }
611 return -1;
612 }
613
614 /* Copy the native file to the fs */
do_write_internal(ext2_filsys fs,ext2_ino_t cwd,const char * src,const char * dest,ext2_ino_t root)615 errcode_t do_write_internal(ext2_filsys fs, ext2_ino_t cwd, const char *src,
616 const char *dest, ext2_ino_t root)
617 {
618 int fd;
619 struct stat statbuf;
620 ext2_ino_t newfile;
621 errcode_t retval;
622 struct ext2_inode inode;
623
624 fd = ext2fs_open_file(src, O_RDONLY, 0);
625 if (fd < 0) {
626 retval = errno;
627 com_err(__func__, retval, _("while opening \"%s\" to copy"),
628 src);
629 return retval;
630 }
631 if (fstat(fd, &statbuf) < 0) {
632 retval = errno;
633 goto out;
634 }
635
636 retval = ext2fs_namei(fs, root, cwd, dest, &newfile);
637 if (retval == 0) {
638 retval = EXT2_ET_FILE_EXISTS;
639 goto out;
640 }
641
642 retval = ext2fs_new_inode(fs, cwd, 010755, 0, &newfile);
643 if (retval)
644 goto out;
645 #ifdef DEBUGFS
646 printf("Allocated inode: %u\n", newfile);
647 #endif
648 retval = ext2fs_link(fs, cwd, dest, newfile,
649 EXT2_FT_REG_FILE);
650 if (retval == EXT2_ET_DIR_NO_SPACE) {
651 retval = ext2fs_expand_dir(fs, cwd);
652 if (retval)
653 goto out;
654 retval = ext2fs_link(fs, cwd, dest, newfile,
655 EXT2_FT_REG_FILE);
656 }
657 if (retval)
658 goto out;
659 if (ext2fs_test_inode_bitmap2(fs->inode_map, newfile))
660 com_err(__func__, 0, "Warning: inode already set");
661 ext2fs_inode_alloc_stats2(fs, newfile, +1, 0);
662 memset(&inode, 0, sizeof(inode));
663 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
664 inode.i_atime = inode.i_ctime = inode.i_mtime =
665 fs->now ? fs->now : time(0);
666 inode.i_links_count = 1;
667 retval = ext2fs_inode_size_set(fs, &inode, statbuf.st_size);
668 if (retval)
669 goto out;
670 if (ext2fs_has_feature_inline_data(fs->super)) {
671 inode.i_flags |= EXT4_INLINE_DATA_FL;
672 } else if (ext2fs_has_feature_extents(fs->super)) {
673 ext2_extent_handle_t handle;
674
675 inode.i_flags &= ~EXT4_EXTENTS_FL;
676 retval = ext2fs_extent_open2(fs, newfile, &inode, &handle);
677 if (retval)
678 goto out;
679 ext2fs_extent_free(handle);
680 }
681
682 retval = ext2fs_write_new_inode(fs, newfile, &inode);
683 if (retval)
684 goto out;
685 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
686 retval = ext2fs_inline_data_init(fs, newfile);
687 if (retval)
688 goto out;
689 }
690 if (LINUX_S_ISREG(inode.i_mode)) {
691 retval = copy_file(fs, fd, &statbuf, newfile);
692 if (retval)
693 goto out;
694 }
695 out:
696 close(fd);
697 return retval;
698 }
699
700 struct file_info {
701 char *path;
702 size_t path_len;
703 size_t path_max_len;
704 };
705
path_append(struct file_info * target,const char * file)706 static errcode_t path_append(struct file_info *target, const char *file)
707 {
708 if (strlen(file) + target->path_len + 1 > target->path_max_len) {
709 void *p;
710 target->path_max_len *= 2;
711 p = realloc(target->path, target->path_max_len);
712 if (p == NULL)
713 return EXT2_ET_NO_MEMORY;
714 target->path = p;
715 }
716 target->path_len += sprintf(target->path + target->path_len, "/%s",
717 file);
718 return 0;
719 }
720
721 #ifdef _WIN32
scandir(const char * dir_name,struct dirent *** name_list,int (* filter)(const struct dirent *),int (* compar)(const struct dirent **,const struct dirent **))722 static int scandir(const char *dir_name, struct dirent ***name_list,
723 int (*filter)(const struct dirent*),
724 int (*compar)(const struct dirent**, const struct dirent**)) {
725 DIR *dir;
726 struct dirent *dent;
727 struct dirent **temp_list = NULL;
728 size_t temp_list_size = 0; // unit: num of dirent
729 size_t num_dent = 0;
730
731 dir = opendir(dir_name);
732 if (dir == NULL) {
733 return -1;
734 }
735
736 while ((dent = readdir(dir))) {
737 if (filter != NULL && !(*filter)(dent))
738 continue;
739
740 // re-allocate the list
741 if (num_dent == temp_list_size) {
742 size_t new_list_size = temp_list_size + 32;
743 struct dirent **new_list = (struct dirent**)realloc(
744 temp_list, new_list_size * sizeof(struct dirent*));
745 if (new_list == NULL) {
746 goto out;
747 }
748 temp_list_size = new_list_size;
749 temp_list = new_list;
750 }
751 // add the copy of dirent to the list
752 temp_list[num_dent] = (struct dirent*)malloc((dent->d_reclen + 3) & ~3);
753 memcpy(temp_list[num_dent], dent, dent->d_reclen);
754 num_dent++;
755 }
756
757 if (compar != NULL) {
758 qsort(temp_list, num_dent, sizeof(struct dirent*),
759 (int (*)(const void*, const void*))compar);
760 }
761
762 // release the temp list
763 *name_list = temp_list;
764 temp_list = NULL;
765
766 out:
767 if (temp_list != NULL) {
768 while (num_dent > 0) {
769 free(temp_list[--num_dent]);
770 }
771 free(temp_list);
772 num_dent = -1;
773 }
774 closedir(dir);
775 return num_dent;
776 }
777
alphasort(const struct dirent ** a,const struct dirent ** b)778 static int alphasort(const struct dirent **a, const struct dirent **b) {
779 return strcoll((*a)->d_name, (*b)->d_name);
780 }
781 #endif
782
783 /* Copy files from source_dir to fs in alphabetical order */
__populate_fs(ext2_filsys fs,ext2_ino_t parent_ino,const char * source_dir,ext2_ino_t root,struct hdlinks_s * hdlinks,struct file_info * target,struct fs_ops_callbacks * fs_callbacks)784 static errcode_t __populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
785 const char *source_dir, ext2_ino_t root,
786 struct hdlinks_s *hdlinks,
787 struct file_info *target,
788 struct fs_ops_callbacks *fs_callbacks)
789 {
790 const char *name;
791 struct dirent **dent;
792 struct stat st;
793 char *ln_target = NULL;
794 unsigned int save_inode;
795 ext2_ino_t ino;
796 errcode_t retval = 0;
797 int read_cnt;
798 int hdlink;
799 size_t cur_dir_path_len;
800 int i, num_dents;
801
802 if (chdir(source_dir) < 0) {
803 retval = errno;
804 com_err(__func__, retval,
805 _("while changing working directory to \"%s\""),
806 source_dir);
807 return retval;
808 }
809
810 num_dents = scandir(".", &dent, NULL, alphasort);
811
812 if (num_dents < 0) {
813 retval = errno;
814 com_err(__func__, retval,
815 _("while scanning directory \"%s\""), source_dir);
816 return retval;
817 }
818
819 for (i = 0; i < num_dents; free(dent[i]), i++) {
820 name = dent[i]->d_name;
821 if ((!strcmp(name, ".")) || (!strcmp(name, "..")))
822 continue;
823 if (lstat(name, &st)) {
824 retval = errno;
825 com_err(__func__, retval, _("while lstat \"%s\""),
826 name);
827 goto out;
828 }
829
830 /* Check for hardlinks */
831 save_inode = 0;
832 if (!S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode) &&
833 st.st_nlink > 1) {
834 hdlink = is_hardlink(hdlinks, st.st_dev, st.st_ino);
835 if (hdlink >= 0) {
836 retval = add_link(fs, parent_ino,
837 hdlinks->hdl[hdlink].dst_ino,
838 name);
839 if (retval) {
840 com_err(__func__, retval,
841 "while linking %s", name);
842 goto out;
843 }
844 continue;
845 } else
846 save_inode = 1;
847 }
848
849 cur_dir_path_len = target->path_len;
850 retval = path_append(target, name);
851 if (retval) {
852 com_err(__func__, retval,
853 "while appending %s", name);
854 goto out;
855 }
856
857 if (fs_callbacks && fs_callbacks->create_new_inode) {
858 retval = fs_callbacks->create_new_inode(fs,
859 target->path, name, parent_ino, root,
860 st.st_mode & S_IFMT);
861 if (retval)
862 goto out;
863 }
864
865 switch(st.st_mode & S_IFMT) {
866 case S_IFCHR:
867 case S_IFBLK:
868 case S_IFIFO:
869 #ifndef _WIN32
870 case S_IFSOCK:
871 retval = do_mknod_internal(fs, parent_ino, name,
872 st.st_mode, st.st_rdev);
873 if (retval) {
874 com_err(__func__, retval,
875 _("while creating special file "
876 "\"%s\""), name);
877 goto out;
878 }
879 break;
880 case S_IFLNK:
881 ln_target = malloc(st.st_size + 1);
882 if (ln_target == NULL) {
883 com_err(__func__, retval,
884 _("malloc failed"));
885 goto out;
886 }
887 read_cnt = readlink(name, ln_target,
888 st.st_size + 1);
889 if (read_cnt == -1) {
890 retval = errno;
891 com_err(__func__, retval,
892 _("while trying to read link \"%s\""),
893 name);
894 free(ln_target);
895 goto out;
896 }
897 if (read_cnt > st.st_size) {
898 com_err(__func__, retval,
899 _("symlink increased in size "
900 "between lstat() and readlink()"));
901 free(ln_target);
902 goto out;
903 }
904 ln_target[read_cnt] = '\0';
905 retval = do_symlink_internal(fs, parent_ino, name,
906 ln_target, root);
907 free(ln_target);
908 if (retval) {
909 com_err(__func__, retval,
910 _("while writing symlink\"%s\""),
911 name);
912 goto out;
913 }
914 break;
915 #endif
916 case S_IFREG:
917 retval = do_write_internal(fs, parent_ino, name, name,
918 root);
919 if (retval) {
920 com_err(__func__, retval,
921 _("while writing file \"%s\""), name);
922 goto out;
923 }
924 break;
925 case S_IFDIR:
926 /* Don't choke on /lost+found */
927 if (parent_ino == EXT2_ROOT_INO &&
928 strcmp(name, "lost+found") == 0)
929 goto find_lnf;
930 retval = do_mkdir_internal(fs, parent_ino, name,
931 root);
932 if (retval) {
933 com_err(__func__, retval,
934 _("while making dir \"%s\""), name);
935 goto out;
936 }
937 find_lnf:
938 retval = ext2fs_namei(fs, root, parent_ino,
939 name, &ino);
940 if (retval) {
941 com_err(name, retval, 0);
942 goto out;
943 }
944 /* Populate the dir recursively*/
945 retval = __populate_fs(fs, ino, name, root, hdlinks,
946 target, fs_callbacks);
947 if (retval)
948 goto out;
949 if (chdir("..")) {
950 retval = errno;
951 com_err(__func__, retval,
952 _("while changing directory"));
953 goto out;
954 }
955 break;
956 default:
957 com_err(__func__, 0,
958 _("ignoring entry \"%s\""), name);
959 }
960
961 retval = ext2fs_namei(fs, root, parent_ino, name, &ino);
962 if (retval) {
963 com_err(name, retval, _("while looking up \"%s\""),
964 name);
965 goto out;
966 }
967
968 retval = set_inode_extra(fs, ino, &st);
969 if (retval) {
970 com_err(__func__, retval,
971 _("while setting inode for \"%s\""), name);
972 goto out;
973 }
974
975 retval = set_inode_xattr(fs, ino, name);
976 if (retval) {
977 com_err(__func__, retval,
978 _("while setting xattrs for \"%s\""), name);
979 goto out;
980 }
981
982 if (fs_callbacks && fs_callbacks->end_create_new_inode) {
983 retval = fs_callbacks->end_create_new_inode(fs,
984 target->path, name, parent_ino, root,
985 st.st_mode & S_IFMT);
986 if (retval)
987 goto out;
988 }
989
990 /* Save the hardlink ino */
991 if (save_inode) {
992 /*
993 * Check whether need more memory, and we don't need
994 * free() since the lifespan will be over after the fs
995 * populated.
996 */
997 if (hdlinks->count == hdlinks->size) {
998 void *p = realloc(hdlinks->hdl,
999 (hdlinks->size + HDLINK_CNT) *
1000 sizeof(struct hdlink_s));
1001 if (p == NULL) {
1002 retval = EXT2_ET_NO_MEMORY;
1003 com_err(name, retval,
1004 _("while saving inode data"));
1005 goto out;
1006 }
1007 hdlinks->hdl = p;
1008 hdlinks->size += HDLINK_CNT;
1009 }
1010 hdlinks->hdl[hdlinks->count].src_dev = st.st_dev;
1011 hdlinks->hdl[hdlinks->count].src_ino = st.st_ino;
1012 hdlinks->hdl[hdlinks->count].dst_ino = ino;
1013 hdlinks->count++;
1014 }
1015 target->path_len = cur_dir_path_len;
1016 target->path[target->path_len] = 0;
1017 }
1018
1019 out:
1020 for (; i < num_dents; free(dent[i]), i++);
1021 free(dent);
1022 return retval;
1023 }
1024
populate_fs2(ext2_filsys fs,ext2_ino_t parent_ino,const char * source_dir,ext2_ino_t root,struct fs_ops_callbacks * fs_callbacks)1025 errcode_t populate_fs2(ext2_filsys fs, ext2_ino_t parent_ino,
1026 const char *source_dir, ext2_ino_t root,
1027 struct fs_ops_callbacks *fs_callbacks)
1028 {
1029 struct file_info file_info;
1030 struct hdlinks_s hdlinks;
1031 errcode_t retval;
1032
1033 if (!(fs->flags & EXT2_FLAG_RW)) {
1034 com_err(__func__, 0, "Filesystem opened readonly");
1035 return EROFS;
1036 }
1037
1038 hdlinks.count = 0;
1039 hdlinks.size = HDLINK_CNT;
1040 hdlinks.hdl = realloc(NULL, hdlinks.size * sizeof(struct hdlink_s));
1041 if (hdlinks.hdl == NULL) {
1042 retval = errno;
1043 com_err(__func__, retval, _("while allocating memory"));
1044 return retval;
1045 }
1046
1047 file_info.path_len = 0;
1048 file_info.path_max_len = 255;
1049 file_info.path = calloc(file_info.path_max_len, 1);
1050
1051 retval = __populate_fs(fs, parent_ino, source_dir, root, &hdlinks,
1052 &file_info, fs_callbacks);
1053
1054 free(file_info.path);
1055 free(hdlinks.hdl);
1056 return retval;
1057 }
1058
populate_fs(ext2_filsys fs,ext2_ino_t parent_ino,const char * source_dir,ext2_ino_t root)1059 errcode_t populate_fs(ext2_filsys fs, ext2_ino_t parent_ino,
1060 const char *source_dir, ext2_ino_t root)
1061 {
1062 return populate_fs2(fs, parent_ino, source_dir, root, NULL);
1063 }
1064