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");
|
|
142 }
|
|
143
|
|
144 strncpy (name, block, 100);
|
|
145 name[100] = '\0';
|
|
146 sprintf (fullname, "%s/%s", basedir, name);
|
|
147 printf ("%s\n", fullname);
|
|
148 type = block[156];
|
|
149
|
|
150 switch (type)
|
|
151 {
|
|
152 case '0':
|
|
153 case '\0':
|
|
154 directory = 0;
|
|
155 break;
|
|
156 case '5':
|
|
157 directory = 1;
|
|
158 break;
|
|
159 default:
|
|
160 fprintf (stderr, "Error: unknown type flag %c. Exiting.\n", type);
|
|
161 break;
|
|
162 }
|
|
163
|
|
164 if (directory)
|
|
165 {
|
|
166 in_block = 0;
|
|
167
|
|
168 /* makepath will ignore the final path component, so make sure
|
|
169 dirnames have a trailing slash */
|
327
|
170
|
495
|
171 if (fullname[strlen (fullname)-1] != '/')
|
|
172 strcat (fullname, "/");
|
|
173 if (makepath (fullname))
|
|
174 {
|
|
175 fprintf (stderr, "Error: cannot create directory %s. Exiting.\n",
|
|
176 fullname);
|
|
177 exit (-2);
|
|
178 }
|
|
179 continue;
|
|
180 }
|
|
181 else
|
|
182 { /*file */
|
|
183 in_block = 1;
|
|
184 if (outfile)
|
|
185 {
|
|
186 if (fclose (outfile))
|
|
187 {
|
|
188 fprintf (stderr, "Error: cannot close file %s. Exiting.\n",
|
|
189 fullname);
|
|
190 exit (-2);
|
|
191 }
|
|
192 outfile = (FILE*)0;
|
|
193 }
|
|
194
|
|
195 if (!(outfile = fopen (fullname, "wb")))
|
|
196 {
|
|
197 /*try creating the directory, maybe it's not there */
|
|
198 if (makepath (fullname))
|
|
199 {
|
|
200 fprintf (stderr, "Error: cannot create file %s. Exiting.\n",
|
|
201 fullname);
|
|
202 exit (-2);
|
|
203 }
|
|
204 /* now try again to open the file */
|
|
205 if (!(outfile = fopen (fullname, "wb")))
|
|
206 {
|
|
207 fprintf (stderr, "Error: cannot create file %s. Exiting.\n",
|
|
208 fullname);
|
|
209 exit (-2);
|
|
210 }
|
|
211 }
|
|
212
|
|
213 strncpy (osize, block+124, 12);
|
|
214 osize[12] = '\0';
|
|
215 size = octal (osize);
|
|
216 if (size<0)
|
|
217 {
|
|
218 fprintf (stderr, "Error: invalid size in tar header. Exiting.\n");
|
|
219 exit (-2);
|
|
220 }
|
|
221 }
|
|
222 } else { /* write or continue writing file contents */
|
|
223 nbytes = size>512? 512:size;
|
327
|
224
|
495
|
225 nwritten = fwrite (block, 1, nbytes, outfile);
|
|
226 if (nwritten != nbytes)
|
|
227 {
|
|
228 fprintf (stderr, "Error: only wrote %d bytes to file %s. Exiting.\n",
|
|
229 nwritten, fullname);
|
|
230 }
|
|
231 size -= nbytes;
|
|
232 if (size==0)
|
|
233 in_block = 0;
|
|
234 }
|
327
|
235 }
|
495
|
236 return 0;
|
|
237 }
|
327
|
238
|
|
239
|
|
240
|