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. */
20 #include <sys/types.h>
21 #include <sys/fcntl.h>
24 //#include "zlib/zlib.h"
25 #include "io_stream.h"
26 //#include "compress.h"
29 #include "archive_tar.h"
32 #include "filemanip.h"
38 #define SYMLINK_COOKIE "!<symlink>"
46 archive_tar::archive_tar (io_stream
* original
)
51 LogBabblePrintf("tar: open `%p'\n", original
);
55 state
.lasterr
= EBADF
;
58 state
.parent
= original
;
60 if (sizeof (state
.tar_header
) != 512)
62 /* drastic, but important */
63 Log (LOG_TIMESTAMP
) << "compilation error: tar header struct not 512"
64 << " bytes (it's " << sizeof (state
.tar_header
)
71 archive_tar::read (void *buffer
, size_t len
)
77 archive_tar::write (const void *buffer
, size_t len
)
83 archive_tar::peek (void *buffer
, size_t len
)
97 return state
.file_offset
;
101 archive_tar::seek (long where
, io_stream_seek_t whence
)
103 /* Because the parent stream is compressed, we can only easily support
104 seek()-ing to rewind to the start */
105 if ((whence
== IO_SEEK_SET
) && (where
== 0))
107 state
.header_read
= 0;
108 return state
.parent
->seek(where
, whence
);
115 archive_tar::skip_file ()
117 while (state
.file_length
> state
.file_offset
)
119 int len
= state
.parent
->read (buf
, 512);
120 state
.file_offset
+= 512;
124 state
.file_length
= 0;
125 state
.file_offset
= 0;
126 state
.header_read
= 0;
131 archive_tar::next_file_name ()
135 if (state
.header_read
)
137 if (strlen (state
.filename
))
138 return state
.filename
;
141 return std::string();
144 int r
= state
.parent
->read (&state
.tar_header
, 512);
146 /* See if we're at end of file */
148 return std::string();
150 /* See if the header is all zeros (i.e. last block) */
152 for (r
= 512 / sizeof (int); r
; r
--)
153 n
|= ((int *) &state
.tar_header
)[r
- 1];
155 return std::string();
157 if (!state
.have_longname
&& state
.tar_header
.typeflag
!= 'L')
159 memcpy (state
.filename
, state
.tar_header
.name
, 100);
160 state
.filename
[100] = 0;
162 else if (state
.have_longname
)
163 state
.have_longname
= 0;
165 sscanf (state
.tar_header
.size
, "%zo", &state
.file_length
);
166 state
.file_offset
= 0;
169 LogBabblePrintf ("%c %9d %s\n", state
.tar_header
.typeflag
,
170 state
.file_length
, state
.filename
);
172 switch (state
.tar_header
.typeflag
)
174 case 'L': /* GNU tar long name extension */
175 /* we read the 'file' into the long filename, then call back into here
176 * to find out if the actual file is a real file, or a special file..
178 if (state
.file_length
> CYG_PATH_MAX
)
181 LogPlainPrintf( "error: long file name exceeds %d characters\n",
184 state
.parent
->read (&state
.tar_header
, 512);
185 sscanf (state
.tar_header
.size
, "%zo", &state
.file_length
);
186 state
.file_offset
= 0;
188 return next_file_name ();
191 /* FIXME: this should be a single read() call */
192 while (state
.file_length
> state
.file_offset
)
195 state
.file_length
- state
.file_offset
>
196 512 ? 512 : state
.file_length
- state
.file_offset
;
197 if (state
.parent
->read (buf
, 512) < 512)
198 // FIXME: What's up with the "0"? It's probably a mistake, and
199 // should be "". It used to be written as 0, and was subject to a
200 // bizarre implicit conversion by the unwise String(int)
203 memcpy (c
, buf
, need
);
205 state
.file_offset
+= need
;
208 state
.have_longname
= 1;
209 return next_file_name ();
212 case '4': /* block */
214 LogPlainPrintf ("warning: not extracting special file %s\n",
217 return next_file_name ();
219 case '0': /* regular file */
220 case 0: /* regular file also */
221 case '2': /* symbolic link */
222 case '5': /* directory */
223 case '7': /* contiguous file */
224 state
.header_read
= 1;
225 return state
.filename
;
227 case '1': /* hard link, we just copy */
228 state
.header_read
= 1;
229 return state
.filename
;
232 LogPlainPrintf ("error: unknown (or unsupported) file type `%c'\n",
233 state
.tar_header
.typeflag
);
236 case 'g': /* POSIX.1-2001 global extended header */
237 case 'x': /* POSIX.1-2001 extended header */
239 return next_file_name ();
241 return std::string();
244 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
;
301 return std::string();
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 ();
322 archive_tar::get_mode ()
325 return state
.parent
->get_mode ();