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