annotate netinstall/tar.cc @ 448:3078fd1074e8 r21-2-39

Import from CVS: tag r21-2-39
author cvs
date Mon, 13 Aug 2007 11:38:25 +0200
parents
children 3d3049ae1304
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
448
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
1 /*
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
2 * Copyright (c) 2000, Red Hat, Inc.
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
3 *
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
7 * (at your option) any later version.
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
8 *
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
9 * A copy of the GNU General Public License can be found at
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
10 * http://www.gnu.org/
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
11 *
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
12 * Written by DJ Delorie <dj@cygnus.com>
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
13 *
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
14 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
15
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
16 /* Built-in tar functionality. See tar.h for usage. */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
17
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
18 #include <stdio.h>
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
19 #include <stdlib.h>
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
20 #include <sys/types.h>
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
21 #include <sys/stat.h>
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
22
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
23 #include "win32.h"
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
24 #include <zlib.h>
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
25 #include "tar.h"
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
26 #include "mkdir.h"
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
27 #include "log.h"
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
28
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
29 #include "port.h"
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
30
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
31 #if defined(CYGWIN) || defined(MINGW)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
32 #define FACTOR (0x19db1ded53ea710LL)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
33 #define NSPERSEC 10000000LL
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
34 #else
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
35 __int64 FACTOR=0x19db1ded53ea710L;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
36 __int64 NSPERSEC=10000000L;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
37 #endif
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
38 #define SYMLINK_COOKIE "!<symlink>"
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
39
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
40 typedef struct {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
41 char name[100]; /* 0 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
42 char mode[8]; /* 100 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
43 char uid[8]; /* 108 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
44 char gid[8]; /* 116 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
45 char size[12]; /* 124 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
46 char mtime[12]; /* 136 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
47 char chksum[8]; /* 148 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
48 char typeflag; /* 156 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
49 char linkname[100]; /* 157 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
50 char magic[6]; /* 257 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
51 char version[2]; /* 263 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
52 char uname[32]; /* 265 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
53 char gname[32]; /* 297 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
54 char devmajor[8]; /* 329 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
55 char devminor[8]; /* 337 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
56 char prefix[155]; /* 345 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
57 char junk[12]; /* 500 */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
58 } tar_header_type;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
59
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
60 typedef struct tar_map_result_type_s {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
61 struct tar_map_result_type_s *next;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
62 char *stored_name;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
63 char *mapped_name;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
64 } tar_map_result_type;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
65
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
66 static tar_map_result_type *tar_map_result = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
67
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
68 static int err;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
69
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
70 static char file_name[_MAX_PATH+512];
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
71 static char have_longname = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
72 static int file_length;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
73
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
74 static tar_header_type tar_header;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
75 static char buf[512];
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
76
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
77 static int _tar_file_size = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
78 int _tar_verbose = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
79 FILE * _tar_vfile = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
80 #define vp if (_tar_verbose) fprintf
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
81 #define vp2 if (_tar_verbose>1) fprintf
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
82
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
83 static gzFile g = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
84
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
85 static char *
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
86 xstrdup (char *c)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
87 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
88 char *r = (char *) malloc (strlen (c) + 1);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
89 if (!r)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
90 exit_setup (1);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
91 strcpy (r, c);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
92 return r;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
93 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
94
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
95 int
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
96 tar_open (char *pathname)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
97 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
98 struct stat s;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
99 if (_tar_vfile == 0)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
100 _tar_vfile = stderr;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
101
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
102 vp2 (_tar_vfile, "tar: open `%s'\n", pathname);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
103 if (stat (pathname, &s) < 0)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
104 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
105 _tar_file_size = s.st_size;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
106
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
107 g = gzopen (pathname, "rb");
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
108 if (sizeof (tar_header) != 512)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
109 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
110 /* drastic, but important */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
111 fprintf (stderr, "compilation error: tar header struct not 512"
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
112 " bytes (it's %d)\n", sizeof (tar_header));
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
113 exit_setup (1);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
114 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
115 err = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
116 return g ? 0 : 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
117 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
118
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
119 int
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
120 tar_ftell ()
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
121 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
122 return gztell (g);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
123 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
124
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
125 static void
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
126 skip_file ()
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
127 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
128 while (file_length > 0)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
129 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
130 gzread (g, buf, 512);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
131 file_length -= 512;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
132 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
133 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
134
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
135 char *
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
136 tar_next_file ()
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
137 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
138 int r, n;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
139 char *c;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
140 r = gzread (g, &tar_header, 512);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
141
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
142 /* See if we're at end of file */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
143 if (r != 512)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
144 return 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
145
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
146 /* See if the header is all zeros (i.e. last block) */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
147 n = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
148 for (r = 512/sizeof (int); r; r--)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
149 n |= ((int *)&tar_header)[r-1];
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
150 if (n == 0)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
151 return 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
152
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
153 if (!have_longname && tar_header.typeflag != 'L')
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
154 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
155 memcpy (file_name, tar_header.name, 100);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
156 file_name[100] = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
157 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
158
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
159 sscanf (tar_header.size, "%o", &file_length);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
160
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
161 vp2 (_tar_vfile, "%c %9d %s\n", tar_header.typeflag, file_length, file_name);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
162
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
163 switch (tar_header.typeflag)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
164 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
165 case 'L': /* GNU tar long name extension */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
166 if (file_length > _MAX_PATH)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
167 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
168 skip_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
169 fprintf (stderr, "error: long file name exceeds %d characters\n",
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
170 _MAX_PATH);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
171 err ++;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
172 gzread (g, &tar_header, 512);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
173 sscanf (tar_header.size, "%o", &file_length);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
174 skip_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
175 return tar_next_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
176 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
177 c = file_name;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
178 while (file_length > 0)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
179 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
180 int need = file_length > 512 ? 512 : file_length;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
181 if (gzread (g, buf, 512) < 512)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
182 return 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
183 memcpy (c, buf, need);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
184 c += need;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
185 file_length -= need;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
186 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
187 *c = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
188 have_longname = 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
189 return tar_next_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
190
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
191 case '3': /* char */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
192 case '4': /* block */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
193 case '6': /* fifo */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
194 fprintf (stderr, "warning: not extracting special file %s\n",
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
195 file_name);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
196 err ++;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
197 return tar_next_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
198
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
199 case '0': /* regular file */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
200 case 0: /* regular file also */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
201 case '2': /* symbolic link */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
202 case '5': /* directory */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
203 case '7': /* contiguous file */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
204 return file_name;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
205
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
206 case '1': /* hard link, we just copy */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
207 return file_name;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
208
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
209 default:
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
210 fprintf (stderr, "error: unknown (or unsupported) file type `%c'\n",
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
211 tar_header.typeflag);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
212 err ++;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
213 skip_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
214 return tar_next_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
215 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
216 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
217
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
218 static void
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
219 fix_time_stamp (char *path)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
220 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
221 int mtime;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
222 #if defined(CYGWIN) || defined(MINGW)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
223 long long ftimev;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
224 #else
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
225 __int64 ftimev;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
226 #endif
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
227 FILETIME ftime;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
228 HANDLE h;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
229
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
230 sscanf (tar_header.mtime, "%o", &mtime);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
231 ftimev = mtime * NSPERSEC + FACTOR;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
232 ftime.dwHighDateTime = ftimev >> 32;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
233 ftime.dwLowDateTime = ftimev;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
234 h = CreateFileA (path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
235 0, OPEN_EXISTING,
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
236 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, 0);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
237 if (h)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
238 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
239 SetFileTime (h, 0, 0, &ftime);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
240 CloseHandle (h);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
241 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
242 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
243
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
244 static FILE *
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
245 common_fopen (char *path)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
246 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
247 FILE *out;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
248 out = fopen (path, "wb");
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
249 if (!out)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
250 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
251 /* maybe we need to create a directory */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
252 if (mkdir_p (0, path))
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
253 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
254 skip_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
255 return 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
256 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
257 out = fopen (path, "wb");
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
258 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
259 if (!out)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
260 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
261 fprintf (stderr, "unable to write to file %s\n", path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
262 perror ("The error was");
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
263 skip_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
264 return 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
265 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
266 return out;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
267 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
268
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
269 static void
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
270 prepare_for_file (char *path)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
271 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
272 DWORD w;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
273 mkdir_p (0, path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
274
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
275 w = GetFileAttributes (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
276 if (w != 0xffffffff && w & FILE_ATTRIBUTE_DIRECTORY)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
277 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
278 char *tmp = (char *) malloc (strlen (path) + 10);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
279 int i = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
280 do {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
281 i++;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
282 sprintf (tmp, "%s.old-%d", path, i);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
283 } while (GetFileAttributes (tmp) != 0xffffffff);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
284 fprintf (stderr, "warning: moving directory \"%s\" out of the way.\n", path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
285 MoveFile (path, tmp);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
286 free (tmp);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
287 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
288
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
289 DeleteFileA (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
290 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
291
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
292 int
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
293 tar_read_file (char *path)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
294 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
295 FILE *out, *copy;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
296 HANDLE h;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
297 DWORD w;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
298 int got;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
299 tar_map_result_type *tmr;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
300
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
301 switch (tar_header.typeflag)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
302 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
303 case '0': /* regular files */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
304 case 0:
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
305 case '7':
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
306 vp (_tar_vfile, "F %s\n", path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
307 prepare_for_file (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
308 out = common_fopen (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
309 if (!out)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
310 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
311
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
312 while (file_length > 0)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
313 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
314 int put;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
315 int want = file_length > 512 ? 512 : file_length;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
316 got = gzread (g, buf, 512);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
317 if (got < 512)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
318 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
319 fprintf (stderr, "tar: unexpected end of file reading %s\n", path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
320 fclose (out);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
321 remove (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
322 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
323 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
324 put = fwrite (buf, 1, want, out);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
325 if (put < want)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
326 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
327 fprintf (stderr, "tar: out of disk space writing %s\n", path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
328 fclose (out);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
329 remove (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
330 skip_file ();
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
331 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
332 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
333 file_length -= want;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
334 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
335 fclose (out);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
336
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
337 fix_time_stamp (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
338
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
339 /* we need this to do hard links below */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
340 tmr = (tar_map_result_type *) malloc (sizeof (tar_map_result_type));
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
341 tmr->next = tar_map_result;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
342 tmr->stored_name = xstrdup (file_name);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
343 tmr->mapped_name = xstrdup (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
344 tar_map_result = tmr;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
345
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
346 return 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
347
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
348 case '1': /* hard links; we just copy */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
349 for (tmr = tar_map_result; tmr; tmr=tmr->next)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
350 if (strcmp (tmr->stored_name, tar_header.linkname) == 0)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
351 break;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
352 if (!tmr)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
353 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
354 fprintf (stderr, "tar: can't find %s to link %s to\n",
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
355 tar_header.linkname, path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
356 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
357 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
358 vp (_tar_vfile, "H %s <- %s\n", path, tmr->mapped_name);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
359 prepare_for_file (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
360 copy = fopen (tmr->mapped_name, "rb");
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
361 if (!copy)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
362 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
363 fprintf (stderr, "tar: unable to read %s\n", tmr->mapped_name);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
364 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
365 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
366 out = common_fopen (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
367 if (!out)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
368 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
369
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
370 while ((got = fread (buf, 1, 512, copy)) > 0)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
371 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
372 int put = fwrite (buf, 1, got, out);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
373 if (put < got)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
374 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
375 fprintf (stderr, "tar: out of disk space writing %s\n", path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
376 fclose (out);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
377 fclose (copy);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
378 remove (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
379 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
380 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
381 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
382 fclose (out);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
383 fclose (copy);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
384
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
385 fix_time_stamp (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
386 return 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
387
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
388 case '5': /* directories */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
389 vp (_tar_vfile, "D %s\n", path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
390 while (path[0] && path[strlen (path)-1] == '/')
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
391 path[strlen (path) - 1] = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
392 return mkdir_p (1, path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
393
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
394
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
395 case '2': /* symbolic links */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
396 vp (_tar_vfile, "L %s -> %s\n", path, tar_header.linkname);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
397 prepare_for_file (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
398 h = CreateFileA (path, GENERIC_WRITE, 0, 0, CREATE_NEW,
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
399 FILE_ATTRIBUTE_NORMAL, 0);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
400 if (h == INVALID_HANDLE_VALUE)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
401 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
402 fprintf (stderr, "error: unable to create symlink \"%s\" -> \"%s\"\n",
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
403 path, tar_header.linkname);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
404 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
405 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
406 strcpy (buf, SYMLINK_COOKIE);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
407 strcat (buf, tar_header.linkname);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
408 if (WriteFile (h, buf, strlen (buf) + 1, &w, NULL))
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
409 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
410 CloseHandle (h);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
411 SetFileAttributesA (path, FILE_ATTRIBUTE_SYSTEM);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
412 return 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
413 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
414 CloseHandle (h);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
415 fprintf (stderr, "error: unable to write symlink \"%s\"\n", path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
416 DeleteFileA (path);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
417 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
418 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
419
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
420 return 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
421 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
422
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
423 int
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
424 tar_close ()
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
425 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
426 #if 0
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
427 while (tar_map_result)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
428 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
429 tar_map_result_type *t = tar_map_result->next;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
430 free (tar_map_result->stored_name);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
431 free (tar_map_result->mapped_name);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
432 free (tar_map_result);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
433 tar_map_result = t;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
434 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
435 #endif
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
436 tar_map_result = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
437
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
438 if (gzclose (g))
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
439 err ++;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
440 return err; /* includes errors for skipped files, etc */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
441 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
442
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
443 typedef struct {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
444 char *from;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
445 int from_len;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
446 char *to;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
447 int to_len;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
448 } map_type;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
449
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
450 static map_type *map;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
451 static int nmaps;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
452
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
453 int
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
454 tar_auto (char *pathname, char **maplist)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
455 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
456 char *c;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
457 int errcount = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
458 int i, j;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
459 map_type mtemp;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
460 char newname[_MAX_PATH+512];
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
461 static char twiddles[] = "|\b/\b-\b\\\b";
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
462 int t = 0;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
463
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
464 for (nmaps=0; maplist[nmaps*2]; nmaps++) ;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
465 map = (map_type *) malloc ((nmaps+1) * sizeof (map_type));
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
466 for (nmaps=0; maplist[nmaps*2]; nmaps++)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
467 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
468 map[nmaps].from = maplist[nmaps*2];
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
469 map[nmaps].from_len = strlen (maplist[nmaps*2]);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
470 map[nmaps].to = maplist[nmaps*2+1];
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
471 map[nmaps].to_len = strlen (maplist[nmaps*2+1]);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
472 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
473 /* bubble sort - expect the maps to be short */
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
474 for (i=0; i<nmaps-1; i++)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
475 for (j=i+1; j<nmaps; j++)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
476 if (map[i].from_len < map[j].from_len)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
477 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
478 mtemp = map[i];
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
479 map[i] = map[j];
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
480 map[j] = mtemp;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
481 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
482
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
483 if ((tar_open (pathname)))
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
484 return 1;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
485 while (c = tar_next_file ())
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
486 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
487 int l = strlen (c);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
488 for (i=0; i<nmaps; i++)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
489 if (l >= map[i].from_len
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
490 && strncmp (c, map[i].from, map[i].from_len) == 0)
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
491 {
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
492 strcpy (newname, map[i].to);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
493 strcpy (newname+map[i].to_len, c + map[i].from_len);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
494 c = newname;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
495 break;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
496 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
497
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
498 t = (t+2) % 8;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
499 fwrite (twiddles+t, 1, 2, stderr);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
500
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
501 if (tar_read_file (c))
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
502 errcount ++;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
503 }
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
504 if (tar_close ())
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
505 errcount ++;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
506
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
507 fwrite (" \b", 1, 2, stderr);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
508
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
509 vp2 (_tar_vfile, "tar_auto returns %d\n", errcount);
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
510 return errcount;
3078fd1074e8 Import from CVS: tag r21-2-39
cvs
parents:
diff changeset
511 }