2 * Copyright (c) 2000, Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by DJ Delorie <dj@cygnus.com>
16 /* Built-in tar functionality. See tar.h for usage. */
20 #include <sys/types.h>
31 #if defined(CYGWIN) || defined(MINGW)
32 #define FACTOR (0x19db1ded53ea710LL)
33 #define NSPERSEC 10000000LL
35 __int64 FACTOR=0x19db1ded53ea710L;
36 __int64 NSPERSEC=10000000L;
38 #define SYMLINK_COOKIE "!<symlink>"
41 char name[100]; /* 0 */
42 char mode[8]; /* 100 */
43 char uid[8]; /* 108 */
44 char gid[8]; /* 116 */
45 char size[12]; /* 124 */
46 char mtime[12]; /* 136 */
47 char chksum[8]; /* 148 */
48 char typeflag; /* 156 */
49 char linkname[100]; /* 157 */
50 char magic[6]; /* 257 */
51 char version[2]; /* 263 */
52 char uname[32]; /* 265 */
53 char gname[32]; /* 297 */
54 char devmajor[8]; /* 329 */
55 char devminor[8]; /* 337 */
56 char prefix[155]; /* 345 */
57 char junk[12]; /* 500 */
60 typedef struct tar_map_result_type_s {
61 struct tar_map_result_type_s *next;
64 } tar_map_result_type;
66 static tar_map_result_type *tar_map_result = 0;
70 static char file_name[_MAX_PATH+512];
71 static char have_longname = 0;
72 static int file_length;
74 static tar_header_type tar_header;
77 static int _tar_file_size = 0;
79 FILE * _tar_vfile = 0;
80 #define vp if (_tar_verbose) fprintf
81 #define vp2 if (_tar_verbose>1) fprintf
88 char *r = (char *) malloc (strlen (c) + 1);
96 tar_open (char *pathname)
102 vp2 (_tar_vfile, "tar: open `%s'\n", pathname);
103 if (stat (pathname, &s) < 0)
105 _tar_file_size = s.st_size;
107 g = gzopen (pathname, "rb");
108 if (sizeof (tar_header) != 512)
110 /* drastic, but important */
111 fprintf (stderr, "compilation error: tar header struct not 512"
112 " bytes (it's %d)\n", sizeof (tar_header));
128 while (file_length > 0)
130 gzread (g, buf, 512);
140 r = gzread (g, &tar_header, 512);
142 /* See if we're at end of file */
146 /* See if the header is all zeros (i.e. last block) */
148 for (r = 512/sizeof (int); r; r--)
149 n |= ((int *)&tar_header)[r-1];
153 if (!have_longname && tar_header.typeflag != 'L')
155 memcpy (file_name, tar_header.name, 100);
159 sscanf (tar_header.size, "%o", &file_length);
161 vp2 (_tar_vfile, "%c %9d %s\n", tar_header.typeflag, file_length, file_name);
163 switch (tar_header.typeflag)
165 case 'L': /* GNU tar long name extension */
166 if (file_length > _MAX_PATH)
169 fprintf (stderr, "error: long file name exceeds %d characters\n",
172 gzread (g, &tar_header, 512);
173 sscanf (tar_header.size, "%o", &file_length);
175 return tar_next_file ();
178 while (file_length > 0)
180 int need = file_length > 512 ? 512 : file_length;
181 if (gzread (g, buf, 512) < 512)
183 memcpy (c, buf, need);
189 return tar_next_file ();
192 case '4': /* block */
194 fprintf (stderr, "warning: not extracting special file %s\n",
197 return tar_next_file ();
199 case '0': /* regular file */
200 case 0: /* regular file also */
201 case '2': /* symbolic link */
202 case '5': /* directory */
203 case '7': /* contiguous file */
206 case '1': /* hard link, we just copy */
210 fprintf (stderr, "error: unknown (or unsupported) file type `%c'\n",
211 tar_header.typeflag);
214 return tar_next_file ();
219 fix_time_stamp (char *path)
222 #if defined(CYGWIN) || defined(MINGW)
230 sscanf (tar_header.mtime, "%o", &mtime);
231 ftimev = mtime * NSPERSEC + FACTOR;
232 ftime.dwHighDateTime = ftimev >> 32;
233 ftime.dwLowDateTime = ftimev;
234 h = CreateFileA (path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
236 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, 0);
239 SetFileTime (h, 0, 0, &ftime);
245 common_fopen (char *path)
248 out = fopen (path, "wb");
251 /* maybe we need to create a directory */
252 if (mkdir_p (0, path))
257 out = fopen (path, "wb");
261 fprintf (stderr, "unable to write to file %s\n", path);
262 perror ("The error was");
270 prepare_for_file (char *path)
275 w = GetFileAttributes (path);
276 if (w != 0xffffffff && w & FILE_ATTRIBUTE_DIRECTORY)
278 char *tmp = (char *) malloc (strlen (path) + 10);
282 sprintf (tmp, "%s.old-%d", path, i);
283 } while (GetFileAttributes (tmp) != 0xffffffff);
284 fprintf (stderr, "warning: moving directory \"%s\" out of the way.\n", path);
285 MoveFile (path, tmp);
293 tar_read_file (char *path)
299 tar_map_result_type *tmr;
301 switch (tar_header.typeflag)
303 case '0': /* regular files */
306 vp (_tar_vfile, "F %s\n", path);
307 prepare_for_file (path);
308 out = common_fopen (path);
312 while (file_length > 0)
315 int want = file_length > 512 ? 512 : file_length;
316 got = gzread (g, buf, 512);
319 fprintf (stderr, "tar: unexpected end of file reading %s\n", path);
324 put = fwrite (buf, 1, want, out);
327 fprintf (stderr, "tar: out of disk space writing %s\n", path);
337 fix_time_stamp (path);
339 /* we need this to do hard links below */
340 tmr = (tar_map_result_type *) malloc (sizeof (tar_map_result_type));
341 tmr->next = tar_map_result;
342 tmr->stored_name = xstrdup (file_name);
343 tmr->mapped_name = xstrdup (path);
344 tar_map_result = tmr;
348 case '1': /* hard links; we just copy */
349 for (tmr = tar_map_result; tmr; tmr=tmr->next)
350 if (strcmp (tmr->stored_name, tar_header.linkname) == 0)
354 fprintf (stderr, "tar: can't find %s to link %s to\n",
355 tar_header.linkname, path);
358 vp (_tar_vfile, "H %s <- %s\n", path, tmr->mapped_name);
359 prepare_for_file (path);
360 copy = fopen (tmr->mapped_name, "rb");
363 fprintf (stderr, "tar: unable to read %s\n", tmr->mapped_name);
366 out = common_fopen (path);
370 while ((got = fread (buf, 1, 512, copy)) > 0)
372 int put = fwrite (buf, 1, got, out);
375 fprintf (stderr, "tar: out of disk space writing %s\n", path);
385 fix_time_stamp (path);
388 case '5': /* directories */
389 vp (_tar_vfile, "D %s\n", path);
390 while (path[0] && path[strlen (path)-1] == '/')
391 path[strlen (path) - 1] = 0;
392 return mkdir_p (1, path);
395 case '2': /* symbolic links */
396 vp (_tar_vfile, "L %s -> %s\n", path, tar_header.linkname);
397 prepare_for_file (path);
398 h = CreateFileA (path, GENERIC_WRITE, 0, 0, CREATE_NEW,
399 FILE_ATTRIBUTE_NORMAL, 0);
400 if (h == INVALID_HANDLE_VALUE)
402 fprintf (stderr, "error: unable to create symlink \"%s\" -> \"%s\"\n",
403 path, tar_header.linkname);
406 strcpy (buf, SYMLINK_COOKIE);
407 strcat (buf, tar_header.linkname);
408 if (WriteFile (h, buf, strlen (buf) + 1, &w, NULL))
411 SetFileAttributesA (path, FILE_ATTRIBUTE_SYSTEM);
415 fprintf (stderr, "error: unable to write symlink \"%s\"\n", path);
427 while (tar_map_result)
429 tar_map_result_type *t = tar_map_result->next;
430 free (tar_map_result->stored_name);
431 free (tar_map_result->mapped_name);
432 free (tar_map_result);
440 return err; /* includes errors for skipped files, etc */
450 static map_type *map;
454 tar_auto (char *pathname, char **maplist)
460 char newname[_MAX_PATH+512];
461 static char twiddles[] = "|\b/\b-\b\\\b";
464 for (nmaps=0; maplist[nmaps*2]; nmaps++) ;
465 map = (map_type *) malloc ((nmaps+1) * sizeof (map_type));
466 for (nmaps=0; maplist[nmaps*2]; nmaps++)
468 map[nmaps].from = maplist[nmaps*2];
469 map[nmaps].from_len = strlen (maplist[nmaps*2]);
470 map[nmaps].to = maplist[nmaps*2+1];
471 map[nmaps].to_len = strlen (maplist[nmaps*2+1]);
473 /* bubble sort - expect the maps to be short */
474 for (i=0; i<nmaps-1; i++)
475 for (j=i+1; j<nmaps; j++)
476 if (map[i].from_len < map[j].from_len)
483 if ((tar_open (pathname)))
485 while (c = tar_next_file ())
488 for (i=0; i<nmaps; i++)
489 if (l >= map[i].from_len
490 && strncmp (c, map[i].from, map[i].from_len) == 0)
492 strcpy (newname, map[i].to);
493 strcpy (newname+map[i].to_len, c + map[i].from_len);
499 fwrite (twiddles+t, 1, 2, stderr);
501 if (tar_read_file (c))
507 fwrite (" \b", 1, 2, stderr);
509 vp2 (_tar_vfile, "tar_auto returns %d\n", errcount);