327
|
1
|
|
2 /* Minitar: extract .tar.gz files on Win32 platforms.
|
|
3 Uses zlib for decompression.
|
|
4
|
|
5 This is very simple-minded, it ignores checksums, and any type of file
|
|
6 that is not a plain file or a directory. Nonetheless it is useful.
|
|
7
|
|
8 Author: Charles G. Waldman (cgw@pgt.com), Aug 4 1998
|
|
9
|
|
10 This file is placed in the public domain; you can
|
|
11 do whatever you like with it. There is NO WARRANTY.
|
|
12 If it breaks, you get to keep both pieces */
|
|
13
|
|
14
|
|
15 #include <stdio.h>
|
438
|
16 #include <errno.h>
|
464
|
17 #include <string.h>
|
|
18 #include <io.h>
|
327
|
19
|
|
20 #include <zlib.h>
|
|
21
|
464
|
22 static int
|
495
|
23 Usage (char *name)
|
327
|
24 {
|
495
|
25 fprintf (stderr, "Usage: %s file.tar.gz [base-dir]\n", name);
|
|
26 fprintf (stderr, "\tExtracts the contents compressed tar file to base-dir\n");
|
|
27 exit (-1);
|
|
28 return 0;
|
327
|
29 }
|
|
30
|
|
31
|
|
32 #define BLOCKSIZE 512
|
|
33 #define MAXNAMELEN 1024
|
|
34
|
464
|
35 static int
|
495
|
36 octal (char *str)
|
327
|
37 {
|
|
38 int ret = -1;
|
495
|
39 sscanf (str, "%o", &ret);
|
327
|
40 return ret;
|
|
41 }
|
|
42
|
|
43 /* this is like mkdir -p, except if there is no trailing slash,
|
|
44 the final component is assumed to be a file, rather than a
|
|
45 path component, so it is not created as a directory */
|
|
46
|
464
|
47 static int
|
495
|
48 makepath (char *path)
|
327
|
49 {
|
|
50 char tmp[MAXNAMELEN];
|
|
51 char *cp;
|
|
52
|
495
|
53 for (cp=path; cp; cp = (char*)strchr (cp+1, '/'))
|
|
54 {
|
|
55 if (!*cp)
|
|
56 break;
|
|
57 if (*cp != '/')
|
|
58 continue;
|
|
59 strncpy (tmp, path, cp-path);
|
|
60 tmp[cp-path] = '\0';
|
|
61 if (strlen (tmp) == 0)
|
|
62 continue;
|
464
|
63 #ifdef WIN32_NATIVE
|
495
|
64 if (mkdir (tmp))
|
464
|
65 #else
|
495
|
66 if (mkdir (tmp, 0777))
|
464
|
67 #endif
|
495
|
68 {
|
|
69 if (errno == EEXIST)
|
|
70 continue;
|
|
71 else
|
|
72 return -1;
|
|
73 }
|
327
|
74 }
|
|
75 return 0;
|
|
76 }
|
|
77
|
495
|
78
|
327
|
79
|
464
|
80 int
|
495
|
81 main (int argc, char **argv)
|
327
|
82 {
|
|
83 char fullname[MAXNAMELEN];
|
|
84 char *basedir = ".";
|
|
85 char *tarfile;
|
|
86 int size;
|
|
87 char osize[13];
|
|
88 char name[101];
|
|
89 char magic[7];
|
|
90 char type;
|
|
91
|
|
92 gzFile *infile = (gzFile*)0;
|
|
93 FILE *outfile = (FILE*)0;
|
|
94
|
|
95 char block[BLOCKSIZE];
|
|
96 int nbytes, nread, nwritten;
|
|
97
|
|
98 int in_block = 0;
|
|
99 int directory = 0;
|
|
100
|
|
101 if (argc < 2 || argc > 3)
|
495
|
102 Usage (argv[0]);
|
327
|
103
|
|
104 tarfile = argv[1];
|
|
105 if (argc==3)
|
|
106 basedir = argv[2];
|
|
107
|
495
|
108 if (! (infile = gzopen (tarfile, "rb")))
|
|
109 {
|
|
110 fprintf (stderr, "Cannot open %s\n", tarfile);
|
|
111 exit (-2);
|
|
112 }
|
327
|
113
|
495
|
114 while (1)
|
|
115 {
|
|
116 nread = gzread (infile, block, 512);
|
327
|
117
|
495
|
118 if (!in_block && nread == 0)
|
327
|
119 break;
|
|
120
|
495
|
121 if (nread != BLOCKSIZE)
|
|
122 {
|
|
123 fprintf (stderr, "Error: incomplete block read. Exiting.\n");
|
|
124 exit (-2);
|
327
|
125 }
|
|
126
|
495
|
127 if (!in_block)
|
|
128 {
|
|
129 if (block[0]=='\0') /* We're done */
|
|
130 break;
|
|
131
|
|
132 strncpy (magic, block+257, 6);
|
|
133 magic[6] = '\0';
|
|
134 if (strcmp (magic, "ustar "))
|
|
135 {
|
|
136 fprintf (stderr,
|
|
137 "Error: incorrect magic number in tar header. Exiting\n");
|
|
138 }
|
|
139
|
|
140 strncpy (name, block, 100);
|
|
141 name[100] = '\0';
|
|
142 sprintf (fullname, "%s/%s", basedir, name);
|
|
143 printf ("%s\n", fullname);
|
|
144 type = block[156];
|
|
145
|
|
146 switch (type)
|
|
147 {
|
|
148 case '0':
|
|
149 case '\0':
|
|
150 directory = 0;
|
|
151 break;
|
|
152 case '5':
|
|
153 directory = 1;
|
|
154 break;
|
|
155 default:
|
|
156 fprintf (stderr, "Error: unknown type flag %c. Exiting.\n", type);
|
|
157 break;
|
|
158 }
|
|
159
|
|
160 if (directory)
|
|
161 {
|
|
162 in_block = 0;
|
|
163
|
|
164 /* makepath will ignore the final path component, so make sure
|
|
165 dirnames have a trailing slash */
|
327
|
166
|
495
|
167 if (fullname[strlen (fullname)-1] != '/')
|
|
168 strcat (fullname, "/");
|
|
169 if (makepath (fullname))
|
|
170 {
|
|
171 fprintf (stderr, "Error: cannot create directory %s. Exiting.\n",
|
|
172 fullname);
|
|
173 exit (-2);
|
|
174 }
|
|
175 continue;
|
|
176 }
|
|
177 else
|
|
178 { /*file */
|
|
179 in_block = 1;
|
|
180 if (outfile)
|
|
181 {
|
|
182 if (fclose (outfile))
|
|
183 {
|
|
184 fprintf (stderr, "Error: cannot close file %s. Exiting.\n",
|
|
185 fullname);
|
|
186 exit (-2);
|
|
187 }
|
|
188 outfile = (FILE*)0;
|
|
189 }
|
|
190
|
|
191 if (!(outfile = fopen (fullname, "wb")))
|
|
192 {
|
|
193 /*try creating the directory, maybe it's not there */
|
|
194 if (makepath (fullname))
|
|
195 {
|
|
196 fprintf (stderr, "Error: cannot create file %s. Exiting.\n",
|
|
197 fullname);
|
|
198 exit (-2);
|
|
199 }
|
|
200 /* now try again to open the file */
|
|
201 if (!(outfile = fopen (fullname, "wb")))
|
|
202 {
|
|
203 fprintf (stderr, "Error: cannot create file %s. Exiting.\n",
|
|
204 fullname);
|
|
205 exit (-2);
|
|
206 }
|
|
207 }
|
|
208
|
|
209 strncpy (osize, block+124, 12);
|
|
210 osize[12] = '\0';
|
|
211 size = octal (osize);
|
|
212 if (size<0)
|
|
213 {
|
|
214 fprintf (stderr, "Error: invalid size in tar header. Exiting.\n");
|
|
215 exit (-2);
|
|
216 }
|
|
217 }
|
|
218 } else { /* write or continue writing file contents */
|
|
219 nbytes = size>512? 512:size;
|
327
|
220
|
495
|
221 nwritten = fwrite (block, 1, nbytes, outfile);
|
|
222 if (nwritten != nbytes)
|
|
223 {
|
|
224 fprintf (stderr, "Error: only wrote %d bytes to file %s. Exiting.\n",
|
|
225 nwritten, fullname);
|
|
226 }
|
|
227 size -= nbytes;
|
|
228 if (size==0)
|
|
229 in_block = 0;
|
|
230 }
|
327
|
231 }
|
495
|
232 return 0;
|
|
233 }
|
327
|
234
|
|
235
|
|
236
|