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