2 * Copyright (c) 2000, 2001, 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. */
19 static const char *cvsid
=
26 #include <sys/types.h>
27 #include <sys/fcntl.h>
30 //#include "zlib/zlib.h"
31 #include "io_stream.h"
32 //#include "compress.h"
34 #include "archive_tar.h"
37 #include "filemanip.h"
44 #define FACTOR (0x19db1ded53ea710LL)
45 #define NSPERSEC 10000000LL
46 #define SYMLINK_COOKIE "!<symlink>"
50 //static char file_name[_MAX_PATH+512];
51 //static char have_longname = 0;
52 //static int file_length;
58 #define vp if (_tar_verbose) fprintf
59 #define vp2 if (_tar_verbose>1) fprintf
61 archive_tar::archive_tar (io_stream
* original
)
67 vp2 (_tar_vfile
, "tar: open `%p'\n", original
);
71 state
.lasterr
= EBADF
;
74 state
.parent
= original
;
76 if (sizeof (state
.tar_header
) != 512)
78 /* drastic, but important */
79 fprintf (stderr
, "compilation error: tar header struct not 512"
80 " bytes (it's %d)\n", sizeof (state
.tar_header
));
86 archive_tar::read (void *buffer
, size_t len
)
92 archive_tar::write (const void *buffer
, size_t len
)
98 archive_tar::peek (void *buffer
, size_t len
)
104 archive_tar::error ()
106 return state
.lasterr
;
112 return state
.file_offset
;
116 archive_tar::seek (long where
, io_stream_seek_t whence
)
118 /* seeking in the parent archive doesn't make sense. although we could
120 Also, seeking might make sense for rewing..??
126 archive_tar::skip_file ()
128 while (state
.file_length
> state
.file_offset
)
130 int len
= state
.parent
->read (buf
, 512);
131 state
.file_offset
+= 512;
135 state
.file_length
= 0;
136 state
.file_offset
= 0;
137 state
.header_read
= 0;
142 archive_tar::next_file_name ()
146 if (state
.header_read
)
147 if (strlen (state
.filename
))
148 return state
.filename
;
153 int r
= state
.parent
->read (&state
.tar_header
, 512);
155 /* See if we're at end of file */
159 /* See if the header is all zeros (i.e. last block) */
161 for (r
= 512 / sizeof (int); r
; r
--)
162 n
|= ((int *) &state
.tar_header
)[r
- 1];
166 if (!state
.have_longname
&& state
.tar_header
.typeflag
!= 'L')
168 memcpy (state
.filename
, state
.tar_header
.name
, 100);
169 state
.filename
[100] = 0;
172 sscanf (state
.tar_header
.size
, "%o", &state
.file_length
);
173 state
.file_offset
= 0;
175 // vp2 (_tar_vfile, "%c %9d %s\n", state.tar_header.typeflag,
176 // state.file_length, state.filename);
178 switch (state
.tar_header
.typeflag
)
180 case 'L': /* GNU tar long name extension */
181 /* we read the 'file' into the long filename, then call back into here
182 * to find out if the actual file is a real file, or a special file..
184 if (state
.file_length
> _MAX_PATH
)
187 fprintf (stderr
, "error: long file name exceeds %d characters\n",
190 state
.parent
->read (&state
.tar_header
, 512);
191 sscanf (state
.tar_header
.size
, "%o", &state
.file_length
);
192 state
.file_offset
= 0;
194 return next_file_name ();
197 /* FIXME: this should be a single read() call */
198 while (state
.file_length
> state
.file_offset
)
201 state
.file_length
- state
.file_offset
>
202 512 ? 512 : state
.file_length
- state
.file_offset
;
203 if (state
.parent
->read (buf
, 512) < 512)
205 memcpy (c
, buf
, need
);
207 state
.file_offset
+= need
;
210 state
.have_longname
= 1;
211 return next_file_name ();
214 case '4': /* block */
216 fprintf (stderr
, "warning: not extracting special file %s\n",
219 return next_file_name ();
221 case '0': /* regular file */
222 case 0: /* regular file also */
223 case '2': /* symbolic link */
224 case '5': /* directory */
225 case '7': /* contiguous file */
226 state
.header_read
= 1;
227 return state
.filename
;
229 case '1': /* hard link, we just copy */
230 state
.header_read
= 1;
231 return state
.filename
;
234 fprintf (stderr
, "error: unknown (or unsupported) file type `%c'\n",
235 state
.tar_header
.typeflag
);
238 return next_file_name ();
243 archive_tar::~archive_tar ()
252 fix_time_stamp (char *path
)
259 sscanf (tar_header
.mtime
, "%o", &mtime
);
260 ftimev
= mtime
* NSPERSEC
+ FACTOR
;
261 ftime
.dwHighDateTime
= ftimev
>> 32;
262 ftime
.dwLowDateTime
= ftimev
;
263 h
= CreateFileA (path
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
265 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, 0);
268 SetFileTime (h
, 0, 0, &ftime
);
274 archive_tar::next_file_type ()
276 switch (state
.tar_header
.typeflag
)
282 return ARCHIVE_FILE_REGULAR
;
284 return ARCHIVE_FILE_HARDLINK
;
286 return ARCHIVE_FILE_DIRECTORY
;
288 return ARCHIVE_FILE_SYMLINK
;
290 return ARCHIVE_FILE_INVALID
;
295 archive_tar::linktarget ()
297 /* TODO: consider .. path traversal issues */
298 if (next_file_type () == ARCHIVE_FILE_SYMLINK
||
299 next_file_type () == ARCHIVE_FILE_HARDLINK
)
300 return state
.tar_header
.linkname
;
305 archive_tar::extract_file ()
307 if (archive_children
)
309 archive_tar_file
*rv
= new archive_tar_file (state
);
314 archive_tar::get_mtime ()
317 return state
.parent
->get_mtime ();