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