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