Contents of /trunk/mkinitrd-magellan/busybox/archival/libunarchive/data_extract_all.c
Parent Directory | Revision Log
Revision 984 -
(show annotations)
(download)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 4688 byte(s)
Sun May 30 11:32:42 2010 UTC (13 years, 11 months ago) by niro
File MIME type: text/plain
File size: 4688 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
4 | */ |
5 | |
6 | #include "libbb.h" |
7 | #include "unarchive.h" |
8 | |
9 | void FAST_FUNC data_extract_all(archive_handle_t *archive_handle) |
10 | { |
11 | file_header_t *file_header = archive_handle->file_header; |
12 | int dst_fd; |
13 | int res; |
14 | |
15 | if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) { |
16 | char *slash = strrchr(file_header->name, '/'); |
17 | if (slash) { |
18 | *slash = '\0'; |
19 | bb_make_directory(file_header->name, -1, FILEUTILS_RECUR); |
20 | *slash = '/'; |
21 | } |
22 | } |
23 | |
24 | if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) { |
25 | /* Remove the entry if it exists */ |
26 | if ((!S_ISDIR(file_header->mode)) |
27 | && (unlink(file_header->name) == -1) |
28 | && (errno != ENOENT) |
29 | ) { |
30 | bb_perror_msg_and_die("can't remove old file %s", |
31 | file_header->name); |
32 | } |
33 | } |
34 | else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) { |
35 | /* Remove the existing entry if its older than the extracted entry */ |
36 | struct stat existing_sb; |
37 | if (lstat(file_header->name, &existing_sb) == -1) { |
38 | if (errno != ENOENT) { |
39 | bb_perror_msg_and_die("can't stat old file"); |
40 | } |
41 | } |
42 | else if (existing_sb.st_mtime >= file_header->mtime) { |
43 | if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) { |
44 | bb_error_msg("%s not created: newer or " |
45 | "same age file exists", file_header->name); |
46 | } |
47 | data_skip(archive_handle); |
48 | return; |
49 | } |
50 | else if ((unlink(file_header->name) == -1) && (errno != EISDIR)) { |
51 | bb_perror_msg_and_die("can't remove old file %s", |
52 | file_header->name); |
53 | } |
54 | } |
55 | |
56 | /* Handle hard links separately |
57 | * We identified hard links as regular files of size 0 with a symlink */ |
58 | if (S_ISREG(file_header->mode) |
59 | && file_header->link_target |
60 | && file_header->size == 0 |
61 | ) { |
62 | /* hard link */ |
63 | res = link(file_header->link_target, file_header->name); |
64 | if ((res == -1) && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) { |
65 | bb_perror_msg("can't create %slink " |
66 | "from %s to %s", "hard", |
67 | file_header->name, |
68 | file_header->link_target); |
69 | } |
70 | } else { |
71 | /* Create the filesystem entry */ |
72 | switch (file_header->mode & S_IFMT) { |
73 | case S_IFREG: { |
74 | /* Regular file */ |
75 | int flags = O_WRONLY | O_CREAT | O_EXCL; |
76 | if (archive_handle->ah_flags & ARCHIVE_O_TRUNC) |
77 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
78 | dst_fd = xopen3(file_header->name, |
79 | flags, |
80 | file_header->mode |
81 | ); |
82 | bb_copyfd_exact_size(archive_handle->src_fd, dst_fd, file_header->size); |
83 | close(dst_fd); |
84 | break; |
85 | } |
86 | case S_IFDIR: |
87 | res = mkdir(file_header->name, file_header->mode); |
88 | if ((res == -1) |
89 | && (errno != EISDIR) /* btw, Linux doesn't return this */ |
90 | && (errno != EEXIST) |
91 | && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET) |
92 | ) { |
93 | bb_perror_msg("can't make dir %s", file_header->name); |
94 | } |
95 | break; |
96 | case S_IFLNK: |
97 | /* Symlink */ |
98 | res = symlink(file_header->link_target, file_header->name); |
99 | if ((res == -1) |
100 | && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET) |
101 | ) { |
102 | bb_perror_msg("can't create %slink " |
103 | "from %s to %s", "sym", |
104 | file_header->name, |
105 | file_header->link_target); |
106 | } |
107 | break; |
108 | case S_IFSOCK: |
109 | case S_IFBLK: |
110 | case S_IFCHR: |
111 | case S_IFIFO: |
112 | res = mknod(file_header->name, file_header->mode, file_header->device); |
113 | if ((res == -1) |
114 | && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET) |
115 | ) { |
116 | bb_perror_msg("can't create node %s", file_header->name); |
117 | } |
118 | break; |
119 | default: |
120 | bb_error_msg_and_die("unrecognized file type"); |
121 | } |
122 | } |
123 | |
124 | if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) { |
125 | #if ENABLE_FEATURE_TAR_UNAME_GNAME |
126 | if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) { |
127 | uid_t uid = file_header->uid; |
128 | gid_t gid = file_header->gid; |
129 | |
130 | if (file_header->tar__uname) { |
131 | //TODO: cache last name/id pair? |
132 | struct passwd *pwd = getpwnam(file_header->tar__uname); |
133 | if (pwd) uid = pwd->pw_uid; |
134 | } |
135 | if (file_header->tar__gname) { |
136 | struct group *grp = getgrnam(file_header->tar__gname); |
137 | if (grp) gid = grp->gr_gid; |
138 | } |
139 | /* GNU tar 1.15.1 uses chown, not lchown */ |
140 | chown(file_header->name, uid, gid); |
141 | } else |
142 | #endif |
143 | chown(file_header->name, file_header->uid, file_header->gid); |
144 | } |
145 | if (!S_ISLNK(file_header->mode)) { |
146 | /* uclibc has no lchmod, glibc is even stranger - |
147 | * it has lchmod which seems to do nothing! |
148 | * so we use chmod... */ |
149 | if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) { |
150 | chmod(file_header->name, file_header->mode); |
151 | } |
152 | /* same for utime */ |
153 | if (archive_handle->ah_flags & ARCHIVE_RESTORE_DATE) { |
154 | struct timeval t[2]; |
155 | |
156 | t[1].tv_sec = t[0].tv_sec = file_header->mtime; |
157 | t[1].tv_usec = t[0].tv_usec = 0; |
158 | utimes(file_header->name, t); |
159 | } |
160 | } |
161 | } |