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"
45 #define FACTOR (0x19db1ded53ea710LL)
46 #define NSPERSEC 10000000LL
47 #define SYMLINK_COOKIE "!<symlink>"
51 //static char file_name[_MAX_PATH+512];
52 //static char have_longname = 0;
53 //static int file_length;
59 #define vp if (_tar_verbose) fprintf
60 #define vp2 if (_tar_verbose>1) fprintf
62 archive_tar::archive_tar (io_stream
* original
)
68 vp2 (_tar_vfile
, "tar: open `%p'\n", original
);
72 state
.lasterr
= EBADF
;
75 state
.parent
= original
;
77 if (sizeof (state
.tar_header
) != 512)
79 /* drastic, but important */
80 fprintf (stderr
, "compilation error: tar header struct not 512"
81 " bytes (it's %d)\n", sizeof (state
.tar_header
));
87 archive_tar::read (void *buffer
, size_t len
)
93 archive_tar::write (const void *buffer
, size_t len
)
99 archive_tar::peek (void *buffer
, size_t len
)
105 archive_tar::error ()
107 return state
.lasterr
;
113 return state
.file_offset
;
117 archive_tar::seek (long where
, io_stream_seek_t whence
)
119 /* seeking in the parent archive doesn't make sense. although we could
121 Also, seeking might make sense for rewing..??
127 archive_tar::skip_file ()
129 while (state
.file_length
> state
.file_offset
)
131 int len
= state
.parent
->read (buf
, 512);
132 state
.file_offset
+= 512;
136 state
.file_length
= 0;
137 state
.file_offset
= 0;
138 state
.header_read
= 0;
143 archive_tar::next_file_name ()
147 if (state
.header_read
)
148 if (strlen (state
.filename
))
149 return state
.filename
;
154 int r
= state
.parent
->read (&state
.tar_header
, 512);
156 /* See if we're at end of file */
160 /* See if the header is all zeros (i.e. last block) */
162 for (r
= 512 / sizeof (int); r
; r
--)
163 n
|= ((int *) &state
.tar_header
)[r
- 1];
167 if (!state
.have_longname
&& state
.tar_header
.typeflag
!= 'L')
169 memcpy (state
.filename
, state
.tar_header
.name
, 100);
170 state
.filename
[100] = 0;
173 sscanf (state
.tar_header
.size
, "%o", &state
.file_length
);
174 state
.file_offset
= 0;
176 // vp2 (_tar_vfile, "%c %9d %s\n", state.tar_header.typeflag,
177 // state.file_length, state.filename);
179 switch (state
.tar_header
.typeflag
)
181 case 'L': /* GNU tar long name extension */
182 /* we read the 'file' into the long filename, then call back into here
183 * to find out if the actual file is a real file, or a special file..
185 if (state
.file_length
> _MAX_PATH
)
188 fprintf (stderr
, "error: long file name exceeds %d characters\n",
191 state
.parent
->read (&state
.tar_header
, 512);
192 sscanf (state
.tar_header
.size
, "%o", &state
.file_length
);
193 state
.file_offset
= 0;
195 return next_file_name ();
198 /* FIXME: this should be a single read() call */
199 while (state
.file_length
> state
.file_offset
)
202 state
.file_length
- state
.file_offset
>
203 512 ? 512 : state
.file_length
- state
.file_offset
;
204 if (state
.parent
->read (buf
, 512) < 512)
206 memcpy (c
, buf
, need
);
208 state
.file_offset
+= need
;
211 state
.have_longname
= 1;
212 return next_file_name ();
215 case '4': /* block */
217 fprintf (stderr
, "warning: not extracting special file %s\n",
220 return next_file_name ();
222 case '0': /* regular file */
223 case 0: /* regular file also */
224 case '2': /* symbolic link */
225 case '5': /* directory */
226 case '7': /* contiguous file */
227 state
.header_read
= 1;
228 return state
.filename
;
230 case '1': /* hard link, we just copy */
231 state
.header_read
= 1;
232 return state
.filename
;
235 fprintf (stderr
, "error: unknown (or unsupported) file type `%c'\n",
236 state
.tar_header
.typeflag
);
239 return next_file_name ();
244 archive_tar::~archive_tar ()
253 fix_time_stamp (char *path
)
260 sscanf (tar_header
.mtime
, "%o", &mtime
);
261 ftimev
= mtime
* NSPERSEC
+ FACTOR
;
262 ftime
.dwHighDateTime
= ftimev
>> 32;
263 ftime
.dwLowDateTime
= ftimev
;
264 h
= CreateFileA (path
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
266 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_BACKUP_SEMANTICS
, 0);
269 SetFileTime (h
, 0, 0, &ftime
);
275 archive_tar::next_file_type ()
277 switch (state
.tar_header
.typeflag
)
283 return ARCHIVE_FILE_REGULAR
;
285 return ARCHIVE_FILE_HARDLINK
;
287 return ARCHIVE_FILE_DIRECTORY
;
289 return ARCHIVE_FILE_SYMLINK
;
291 return ARCHIVE_FILE_INVALID
;
296 archive_tar::linktarget ()
298 /* TODO: consider .. path traversal issues */
299 if (next_file_type () == ARCHIVE_FILE_SYMLINK
||
300 next_file_type () == ARCHIVE_FILE_HARDLINK
)
301 return state
.tar_header
.linkname
;
306 archive_tar::extract_file ()
308 if (archive_children
)
310 archive_tar_file
*rv
= new archive_tar_file (state
);
315 archive_tar::get_mtime ()
318 return state
.parent
->get_mtime ();