Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/archival/libunarchive/data_extract_all.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (hide 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)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 niro 532 /* 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 niro 816 void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
10 niro 532 {
11     file_header_t *file_header = archive_handle->file_header;
12     int dst_fd;
13     int res;
14    
15 niro 816 if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
16 niro 984 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 niro 532 }
23    
24 niro 984 if (archive_handle->ah_flags & ARCHIVE_UNLINK_OLD) {
25 niro 816 /* Remove the entry if it exists */
26 niro 984 if ((!S_ISDIR(file_header->mode))
27 niro 532 && (unlink(file_header->name) == -1)
28     && (errno != ENOENT)
29     ) {
30 niro 984 bb_perror_msg_and_die("can't remove old file %s",
31 niro 816 file_header->name);
32 niro 532 }
33     }
34 niro 816 else if (archive_handle->ah_flags & ARCHIVE_EXTRACT_NEWER) {
35 niro 532 /* Remove the existing entry if its older than the extracted entry */
36 niro 984 struct stat existing_sb;
37     if (lstat(file_header->name, &existing_sb) == -1) {
38 niro 532 if (errno != ENOENT) {
39 niro 984 bb_perror_msg_and_die("can't stat old file");
40 niro 532 }
41     }
42 niro 984 else if (existing_sb.st_mtime >= file_header->mtime) {
43 niro 816 if (!(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
44 niro 532 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 niro 984 bb_perror_msg_and_die("can't remove old file %s",
52 niro 532 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 niro 984 if (S_ISREG(file_header->mode)
59     && file_header->link_target
60     && file_header->size == 0
61 niro 532 ) {
62     /* hard link */
63 niro 816 res = link(file_header->link_target, file_header->name);
64     if ((res == -1) && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)) {
65 niro 984 bb_perror_msg("can't create %slink "
66 niro 816 "from %s to %s", "hard",
67     file_header->name,
68     file_header->link_target);
69 niro 532 }
70     } else {
71     /* Create the filesystem entry */
72     switch (file_header->mode & S_IFMT) {
73     case S_IFREG: {
74     /* Regular file */
75 niro 984 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 niro 532 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 niro 816 if ((res == -1)
89     && (errno != EISDIR) /* btw, Linux doesn't return this */
90     && (errno != EEXIST)
91     && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
92 niro 532 ) {
93 niro 984 bb_perror_msg("can't make dir %s", file_header->name);
94 niro 532 }
95     break;
96     case S_IFLNK:
97     /* Symlink */
98 niro 816 res = symlink(file_header->link_target, file_header->name);
99 niro 532 if ((res == -1)
100 niro 816 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
101 niro 532 ) {
102 niro 984 bb_perror_msg("can't create %slink "
103 niro 816 "from %s to %s", "sym",
104 niro 532 file_header->name,
105 niro 816 file_header->link_target);
106 niro 532 }
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 niro 816 && !(archive_handle->ah_flags & ARCHIVE_EXTRACT_QUIET)
115 niro 532 ) {
116 niro 984 bb_perror_msg("can't create node %s", file_header->name);
117 niro 532 }
118     break;
119     default:
120     bb_error_msg_and_die("unrecognized file type");
121     }
122     }
123    
124 niro 984 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_OWNER)) {
125 niro 816 #if ENABLE_FEATURE_TAR_UNAME_GNAME
126 niro 984 if (!(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)) {
127     uid_t uid = file_header->uid;
128     gid_t gid = file_header->gid;
129 niro 816
130 niro 984 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 niro 816 #endif
143 niro 984 chown(file_header->name, file_header->uid, file_header->gid);
144 niro 532 }
145 niro 984 if (!S_ISLNK(file_header->mode)) {
146 niro 816 /* uclibc has no lchmod, glibc is even stranger -
147     * it has lchmod which seems to do nothing!
148     * so we use chmod... */
149 niro 984 if (!(archive_handle->ah_flags & ARCHIVE_DONT_RESTORE_PERM)) {
150 niro 816 chmod(file_header->name, file_header->mode);
151     }
152     /* same for utime */
153 niro 984 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 niro 816 }
160 niro 532 }
161     }