]>
Commit | Line | Data |
---|---|---|
dd1462c3 MW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: tarstrm.cpp | |
3 | // Purpose: Streams for Tar files | |
4 | // Author: Mike Wetherell | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 2004 Mike Wetherell | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | #if wxUSE_TARSTREAM | |
18 | ||
19 | #include "wx/tarstrm.h" | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/hashmap.h" | |
23 | #include "wx/intl.h" | |
24 | #include "wx/log.h" | |
25 | #include "wx/utils.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/buffer.h" | |
29 | #include "wx/datetime.h" | |
30 | #include "wx/ptr_scpd.h" | |
31 | #include "wx/filename.h" | |
32 | ||
33 | #include <ctype.h> | |
34 | ||
35 | #ifdef __UNIX__ | |
36 | #include <grp.h> | |
37 | #endif | |
38 | ||
39 | ||
40 | ///////////////////////////////////////////////////////////////////////////// | |
41 | // constants | |
42 | ||
43 | enum { | |
44 | TAR_NAME, | |
45 | TAR_MODE, | |
46 | TAR_UID, | |
47 | TAR_GID, | |
48 | TAR_SIZE, | |
49 | TAR_MTIME, | |
50 | TAR_CHKSUM, | |
51 | TAR_TYPEFLAG, | |
52 | TAR_LINKNAME, | |
53 | TAR_MAGIC, | |
54 | TAR_VERSION, | |
55 | TAR_UNAME, | |
56 | TAR_GNAME, | |
57 | TAR_DEVMAJOR, | |
58 | TAR_DEVMINOR, | |
59 | TAR_PREFIX, | |
60 | TAR_UNUSED, | |
61 | TAR_NUMFIELDS | |
62 | }; | |
63 | ||
64 | enum { | |
65 | TAR_BLOCKSIZE = 512, | |
66 | }; | |
67 | ||
68 | // checksum type | |
69 | enum { | |
70 | SUM_UNKNOWN, | |
71 | SUM_UNSIGNED, | |
72 | SUM_SIGNED | |
73 | }; | |
74 | ||
75 | // type of input tar | |
76 | enum { | |
77 | TYPE_OLDTAR, // fields after TAR_LINKNAME are invalid | |
78 | TYPE_GNUTAR, // all fields except TAR_PREFIX are valid | |
79 | TYPE_USTAR // all fields are valid | |
80 | }; | |
81 | ||
82 | // signatures | |
83 | static const char *USTAR_MAGIC = "ustar"; | |
84 | static const char *USTAR_VERSION = "00"; | |
85 | static const char *GNU_MAGIC = "ustar "; | |
86 | static const char *GNU_VERION = " "; | |
87 | ||
88 | IMPLEMENT_DYNAMIC_CLASS(wxTarEntry, wxArchiveEntry) | |
89 | IMPLEMENT_DYNAMIC_CLASS(wxTarClassFactory, wxArchiveClassFactory) | |
90 | ||
91 | ||
92 | ///////////////////////////////////////////////////////////////////////////// | |
93 | // Class factory | |
94 | ||
95 | static wxTarClassFactory g_wxTarClassFactory; | |
96 | ||
97 | wxTarClassFactory::wxTarClassFactory() | |
98 | { | |
99 | if (this == &g_wxTarClassFactory) | |
100 | PushFront(); | |
101 | } | |
102 | ||
103 | const wxChar * const * | |
104 | wxTarClassFactory::GetProtocols(wxStreamProtocolType type) const | |
105 | { | |
106 | static const wxChar *protocols[] = { _T("tar"), NULL }; | |
107 | static const wxChar *mimetypes[] = { _T("application/x-tar"), NULL }; | |
108 | static const wxChar *fileexts[] = { _T(".tar"), NULL }; | |
109 | static const wxChar *empty[] = { NULL }; | |
110 | ||
111 | switch (type) { | |
112 | case wxSTREAM_PROTOCOL: return protocols; | |
113 | case wxSTREAM_MIMETYPE: return mimetypes; | |
114 | case wxSTREAM_FILEEXTENSION: return fileexts; | |
115 | default: return empty; | |
116 | } | |
117 | } | |
118 | ||
119 | ||
120 | ///////////////////////////////////////////////////////////////////////////// | |
121 | // tar header block | |
122 | ||
123 | typedef wxFileOffset wxTarNumber; | |
124 | ||
125 | struct wxTarField { const wxChar *name; int pos; }; | |
126 | ||
127 | class wxTarHeaderBlock | |
128 | { | |
129 | public: | |
130 | wxTarHeaderBlock() | |
131 | { memset(data, 0, sizeof(data)); } | |
132 | wxTarHeaderBlock(const wxTarHeaderBlock& hb) | |
133 | { memcpy(data, hb.data, sizeof(data)); } | |
134 | ||
135 | bool Read(wxInputStream& in); | |
136 | bool Write(wxOutputStream& out); | |
137 | inline bool WriteField(wxOutputStream& out, int id); | |
138 | ||
139 | bool IsAllZeros() const; | |
140 | wxUint32 Sum(bool SignedSum = false); | |
141 | wxUint32 SumField(int id); | |
142 | ||
143 | char *Get(int id) { return data + fields[id].pos + id; } | |
144 | static size_t Len(int id) { return fields[id + 1].pos - fields[id].pos; } | |
145 | static const wxChar *Name(int id) { return fields[id].name; } | |
146 | static size_t Offset(int id) { return fields[id].pos; } | |
147 | ||
148 | bool SetOctal(int id, wxTarNumber n); | |
149 | wxTarNumber GetOctal(int id); | |
150 | bool SetPath(const wxString& name, wxMBConv& conv); | |
151 | ||
152 | private: | |
153 | char data[TAR_BLOCKSIZE + TAR_NUMFIELDS]; | |
154 | static const wxTarField fields[]; | |
155 | static void check(); | |
156 | }; | |
157 | ||
158 | wxDEFINE_SCOPED_PTR_TYPE(wxTarHeaderBlock); | |
159 | ||
160 | // A table giving the field names and offsets in a tar header block | |
161 | const wxTarField wxTarHeaderBlock::fields[] = | |
162 | { | |
163 | { _T("name"), 0 }, // 100 | |
164 | { _T("mode"), 100 }, // 8 | |
165 | { _T("uid"), 108 }, // 8 | |
166 | { _T("gid"), 116 }, // 8 | |
167 | { _T("size"), 124 }, // 12 | |
168 | { _T("mtime"), 136 }, // 12 | |
169 | { _T("chksum"), 148 }, // 8 | |
170 | { _T("typeflag"), 156 }, // 1 | |
171 | { _T("linkname"), 157 }, // 100 | |
172 | { _T("magic"), 257 }, // 6 | |
173 | { _T("version"), 263 }, // 2 | |
174 | { _T("uname"), 265 }, // 32 | |
175 | { _T("gname"), 297 }, // 32 | |
176 | { _T("devmajor"), 329 }, // 8 | |
177 | { _T("devminor"), 337 }, // 8 | |
178 | { _T("prefix"), 345 }, // 155 | |
179 | { _T("unused"), 500 }, // 12 | |
180 | { NULL, TAR_BLOCKSIZE } | |
181 | }; | |
182 | ||
183 | void wxTarHeaderBlock::check() | |
184 | { | |
185 | wxCOMPILE_TIME_ASSERT( | |
186 | WXSIZEOF(fields) == TAR_NUMFIELDS + 1, | |
187 | Wrong_number_of_elements_in_fields_table | |
188 | ); | |
189 | } | |
190 | ||
191 | bool wxTarHeaderBlock::IsAllZeros() const | |
192 | { | |
193 | const char *p = data; | |
194 | for (size_t i = 0; i < sizeof(data); i++) | |
195 | if (p[i]) | |
196 | return false; | |
197 | return true; | |
198 | } | |
199 | ||
200 | wxUint32 wxTarHeaderBlock::Sum(bool SignedSum /*=false*/) | |
201 | { | |
202 | // the chksum field itself should be blanks during the calculation | |
203 | memset(Get(TAR_CHKSUM), ' ', Len(TAR_CHKSUM)); | |
204 | const char *p = data; | |
205 | wxUint32 n = 0; | |
206 | ||
207 | if (SignedSum) | |
208 | for (size_t i = 0; i < sizeof(data); i++) | |
209 | n += (signed char)p[i]; | |
210 | else | |
211 | for (size_t i = 0; i < sizeof(data); i++) | |
212 | n += (unsigned char)p[i]; | |
213 | ||
214 | return n; | |
215 | } | |
216 | ||
217 | wxUint32 wxTarHeaderBlock::SumField(int id) | |
218 | { | |
219 | unsigned char *p = (unsigned char*)Get(id); | |
220 | unsigned char *q = p + Len(id); | |
221 | wxUint32 n = 0; | |
222 | ||
223 | while (p < q) | |
224 | n += *p++; | |
225 | ||
226 | return n; | |
227 | } | |
228 | ||
229 | bool wxTarHeaderBlock::Read(wxInputStream& in) | |
230 | { | |
231 | bool ok = true; | |
232 | ||
233 | for (int id = 0; id < TAR_NUMFIELDS && ok; id++) | |
234 | ok = in.Read(Get(id), Len(id)).LastRead() == Len(id); | |
235 | ||
236 | return ok; | |
237 | } | |
238 | ||
239 | bool wxTarHeaderBlock::Write(wxOutputStream& out) | |
240 | { | |
241 | bool ok = true; | |
242 | ||
243 | for (int id = 0; id < TAR_NUMFIELDS && ok; id++) | |
244 | ok = WriteField(out, id); | |
245 | ||
246 | return ok; | |
247 | } | |
248 | ||
249 | bool wxTarHeaderBlock::WriteField(wxOutputStream& out, int id) | |
250 | { | |
251 | return out.Write(Get(id), Len(id)).LastWrite() == Len(id); | |
252 | } | |
253 | ||
254 | wxTarNumber wxTarHeaderBlock::GetOctal(int id) | |
255 | { | |
256 | wxTarNumber n = 0; | |
257 | const char *p = Get(id); | |
258 | while (*p == ' ') | |
259 | p++; | |
260 | while (*p >= '0' && *p < '8') | |
261 | n = (n << 3) | (*p++ - '0'); | |
262 | return n; | |
263 | } | |
264 | ||
265 | bool wxTarHeaderBlock::SetOctal(int id, wxTarNumber n) | |
266 | { | |
267 | // set an octal field, return true if the number fits | |
268 | char *field = Get(id); | |
269 | char *p = field + Len(id); | |
270 | *--p = 0; | |
271 | while (p > field) { | |
272 | *--p = '0' + (n & 7); | |
273 | n >>= 3; | |
274 | } | |
275 | return n == 0; | |
276 | } | |
277 | ||
278 | bool wxTarHeaderBlock::SetPath(const wxString& name, wxMBConv& conv) | |
279 | { | |
280 | bool badconv = false; | |
281 | ||
282 | #if wxUSE_UNICODE | |
283 | wxCharBuffer nameBuf = name.mb_str(conv); | |
284 | ||
285 | // if the conversion fails make an approximation | |
286 | if (!nameBuf) { | |
287 | badconv = true; | |
288 | size_t len = name.length(); | |
289 | wxCharBuffer approx(len); | |
290 | for (size_t i = 0; i < len; i++) | |
291 | approx.data()[i] = name[i] & ~0x7F ? '_' : name[i]; | |
292 | nameBuf = approx; | |
293 | } | |
294 | ||
295 | const char *mbName = nameBuf; | |
296 | #else | |
297 | const char *mbName = name.c_str(); | |
298 | (void)conv; | |
299 | #endif | |
300 | ||
301 | bool fits; | |
302 | bool notGoingToFit = false; | |
303 | size_t len = strlen(mbName); | |
304 | size_t maxname = Len(TAR_NAME); | |
305 | size_t maxprefix = Len(TAR_PREFIX); | |
306 | size_t i = 0; | |
307 | size_t nexti = 0; | |
308 | ||
309 | for (;;) { | |
310 | fits = i < maxprefix && len - i <= maxname; | |
311 | ||
312 | if (!fits) { | |
313 | const char *p = strchr(mbName + i, '/'); | |
314 | if (p) | |
315 | nexti = p - mbName + 1; | |
316 | if (!p || nexti - 1 > maxprefix) | |
317 | notGoingToFit = true; | |
318 | } | |
319 | ||
320 | if (fits || notGoingToFit) { | |
321 | strncpy(Get(TAR_NAME), mbName + i, maxname); | |
322 | if (i > 0) | |
323 | strncpy(Get(TAR_PREFIX), mbName, i - 1); | |
324 | break; | |
325 | } | |
326 | ||
327 | i = nexti; | |
328 | } | |
329 | ||
330 | return fits && !badconv; | |
331 | } | |
332 | ||
333 | ||
334 | ///////////////////////////////////////////////////////////////////////////// | |
335 | // Some helpers | |
336 | ||
337 | static wxFileOffset RoundUpSize(wxFileOffset size, int factor = 1) | |
338 | { | |
339 | wxFileOffset chunk = TAR_BLOCKSIZE * factor; | |
340 | return ((size + chunk - 1) / chunk) * chunk; | |
341 | } | |
342 | ||
343 | static wxString GroupName() | |
344 | { | |
345 | #ifdef __UNIX__ | |
346 | group *gr; | |
347 | if ((gr = getgrgid(getgid())) != NULL) | |
348 | return wxString(gr->gr_name, wxConvLibc); | |
349 | #endif | |
350 | return _("unknown"); | |
351 | } | |
352 | ||
353 | static inline int UserId() | |
354 | { | |
355 | #ifdef __UNIX__ | |
356 | return getuid(); | |
357 | #else | |
358 | return 0; | |
359 | #endif | |
360 | } | |
361 | ||
362 | static inline int GroupId() | |
363 | { | |
364 | #ifdef __UNIX__ | |
365 | return getgid(); | |
366 | #else | |
367 | return 0; | |
368 | #endif | |
369 | } | |
370 | ||
371 | // ignore the size field for entry types 3, 4, 5 and 6 | |
372 | // | |
373 | static inline wxFileOffset GetDataSize(const wxTarEntry& entry) | |
374 | { | |
375 | switch (entry.GetTypeFlag()) { | |
376 | case wxTAR_CHRTYPE: | |
377 | case wxTAR_BLKTYPE: | |
378 | case wxTAR_DIRTYPE: | |
379 | case wxTAR_FIFOTYPE: | |
380 | return 0; | |
381 | default: | |
382 | return entry.GetSize(); | |
383 | }; | |
384 | } | |
385 | ||
386 | ||
387 | ///////////////////////////////////////////////////////////////////////////// | |
388 | // Tar Entry | |
389 | // Holds all the meta-data for a file in the tar | |
390 | ||
391 | wxTarEntry::wxTarEntry(const wxString& name /*=wxEmptyString*/, | |
392 | const wxDateTime& dt /*=wxDateTime::Now()*/, | |
393 | wxFileOffset size /*=0*/) | |
394 | : m_Mode(0644), | |
395 | m_IsModeSet(false), | |
396 | m_UserId(UserId()), | |
397 | m_GroupId(GroupId()), | |
398 | m_Size(size), | |
399 | m_Offset(wxInvalidOffset), | |
400 | m_ModifyTime(dt), | |
401 | m_TypeFlag(wxTAR_REGTYPE), | |
402 | m_UserName(wxGetUserId()), | |
403 | m_GroupName(GroupName()), | |
404 | m_DevMajor(~0), | |
405 | m_DevMinor(~0) | |
406 | { | |
407 | if (!name.empty()) | |
408 | SetName(name); | |
409 | } | |
410 | ||
411 | wxTarEntry::~wxTarEntry() | |
412 | { | |
413 | } | |
414 | ||
415 | wxTarEntry::wxTarEntry(const wxTarEntry& e) | |
416 | : m_Name(e.m_Name), | |
417 | m_Mode(e.m_Mode), | |
418 | m_IsModeSet(e.m_IsModeSet), | |
419 | m_UserId(e.m_UserId), | |
420 | m_GroupId(e.m_GroupId), | |
421 | m_Size(e.m_Size), | |
422 | m_Offset(e.m_Offset), | |
423 | m_ModifyTime(e.m_ModifyTime), | |
424 | m_AccessTime(e.m_AccessTime), | |
425 | m_CreateTime(e.m_CreateTime), | |
426 | m_TypeFlag(e.m_TypeFlag), | |
427 | m_LinkName(e.m_LinkName), | |
428 | m_UserName(e.m_UserName), | |
429 | m_GroupName(e.m_GroupName), | |
430 | m_DevMajor(e.m_DevMajor), | |
431 | m_DevMinor(e.m_DevMinor) | |
432 | { | |
433 | } | |
434 | ||
435 | wxTarEntry& wxTarEntry::operator=(const wxTarEntry& e) | |
436 | { | |
437 | if (&e != this) { | |
438 | m_Name = e.m_Name; | |
439 | m_Mode = e.m_Mode; | |
440 | m_IsModeSet = e.m_IsModeSet; | |
441 | m_UserId = e.m_UserId; | |
442 | m_GroupId = e.m_GroupId; | |
443 | m_Size = e.m_Size; | |
444 | m_Offset = e.m_Offset; | |
445 | m_ModifyTime = e.m_ModifyTime; | |
446 | m_AccessTime = e.m_AccessTime; | |
447 | m_CreateTime = e.m_CreateTime; | |
448 | m_TypeFlag = e.m_TypeFlag; | |
449 | m_LinkName = e.m_LinkName; | |
450 | m_UserName = e.m_UserName; | |
451 | m_GroupName = e.m_GroupName; | |
452 | m_DevMajor = e.m_DevMajor; | |
453 | m_DevMinor = e.m_DevMinor; | |
454 | } | |
455 | return *this; | |
456 | } | |
457 | ||
458 | wxString wxTarEntry::GetName(wxPathFormat format /*=wxPATH_NATIVE*/) const | |
459 | { | |
460 | bool isDir = IsDir() && !m_Name.empty(); | |
461 | ||
462 | // optimisations for common (and easy) cases | |
463 | switch (wxFileName::GetFormat(format)) { | |
464 | case wxPATH_DOS: | |
465 | { | |
466 | wxString name(isDir ? m_Name + _T("\\") : m_Name); | |
467 | for (size_t i = 0; i < name.length(); i++) | |
468 | if (name[i] == _T('/')) | |
469 | name[i] = _T('\\'); | |
470 | return name; | |
471 | } | |
472 | ||
473 | case wxPATH_UNIX: | |
474 | return isDir ? m_Name + _T("/") : m_Name; | |
475 | ||
476 | default: | |
477 | ; | |
478 | } | |
479 | ||
480 | wxFileName fn; | |
481 | ||
482 | if (isDir) | |
483 | fn.AssignDir(m_Name, wxPATH_UNIX); | |
484 | else | |
485 | fn.Assign(m_Name, wxPATH_UNIX); | |
486 | ||
487 | return fn.GetFullPath(format); | |
488 | } | |
489 | ||
490 | void wxTarEntry::SetName(const wxString& name, wxPathFormat format) | |
491 | { | |
492 | bool isDir; | |
493 | m_Name = GetInternalName(name, format, &isDir); | |
494 | SetIsDir(isDir); | |
495 | } | |
496 | ||
497 | // Static - Internally tars and zips use forward slashes for the path | |
498 | // separator, absolute paths aren't allowed, and directory names have a | |
499 | // trailing slash. This function converts a path into this internal format, | |
500 | // but without a trailing slash for a directory. | |
501 | // | |
502 | wxString wxTarEntry::GetInternalName(const wxString& name, | |
503 | wxPathFormat format /*=wxPATH_NATIVE*/, | |
504 | bool *pIsDir /*=NULL*/) | |
505 | { | |
506 | wxString internal; | |
507 | ||
508 | if (wxFileName::GetFormat(format) != wxPATH_UNIX) | |
509 | internal = wxFileName(name, format).GetFullPath(wxPATH_UNIX); | |
510 | else | |
511 | internal = name; | |
512 | ||
513 | bool isDir = !internal.empty() && internal.Last() == '/'; | |
514 | if (pIsDir) | |
515 | *pIsDir = isDir; | |
516 | if (isDir) | |
517 | internal.erase(internal.length() - 1); | |
518 | ||
519 | while (!internal.empty() && *internal.begin() == '/') | |
520 | internal.erase(0, 1); | |
521 | while (!internal.empty() && internal.compare(0, 2, _T("./")) == 0) | |
522 | internal.erase(0, 2); | |
523 | if (internal == _T(".") || internal == _T("..")) | |
524 | internal = wxEmptyString; | |
525 | ||
526 | return internal; | |
527 | } | |
528 | ||
529 | bool wxTarEntry::IsDir() const | |
530 | { | |
531 | return m_TypeFlag - wxTAR_DIRTYPE == 0; | |
532 | } | |
533 | ||
534 | void wxTarEntry::SetIsDir(bool isDir) | |
535 | { | |
536 | if (isDir) | |
537 | m_TypeFlag = wxTAR_DIRTYPE; | |
538 | else if (m_TypeFlag - wxTAR_DIRTYPE == 0) | |
539 | m_TypeFlag = wxTAR_REGTYPE; | |
540 | } | |
541 | ||
542 | void wxTarEntry::SetIsReadOnly(bool isReadOnly) | |
543 | { | |
544 | if (isReadOnly) | |
545 | m_Mode &= ~0222; | |
546 | else | |
547 | m_Mode |= 0200; | |
548 | } | |
549 | ||
550 | int wxTarEntry::GetMode() const | |
551 | { | |
552 | if (m_IsModeSet || !IsDir()) | |
553 | return m_Mode; | |
554 | else | |
555 | return m_Mode | 0111; | |
556 | ||
557 | } | |
558 | ||
559 | void wxTarEntry::SetMode(int mode) | |
560 | { | |
561 | m_Mode = mode & 07777; | |
562 | m_IsModeSet = true; | |
563 | } | |
564 | ||
565 | ||
566 | ///////////////////////////////////////////////////////////////////////////// | |
567 | // Input stream | |
568 | ||
569 | wxDECLARE_SCOPED_PTR(wxTarEntry, wxTarEntryPtr_) | |
570 | wxDEFINE_SCOPED_PTR (wxTarEntry, wxTarEntryPtr_) | |
571 | ||
572 | WX_DECLARE_STRING_HASH_MAP(wxString, wxTarHeaderRecords); | |
573 | ||
574 | wxTarInputStream::wxTarInputStream(wxInputStream& stream, | |
575 | wxMBConv& conv /*=wxConvLocal*/) | |
576 | : wxArchiveInputStream(stream, conv) | |
577 | { | |
578 | Init(); | |
579 | } | |
580 | ||
581 | wxTarInputStream::wxTarInputStream(wxInputStream *stream, | |
582 | wxMBConv& conv /*=wxConvLocal*/) | |
583 | : wxArchiveInputStream(stream, conv) | |
584 | { | |
585 | Init(); | |
586 | } | |
587 | ||
588 | void wxTarInputStream::Init() | |
589 | { | |
590 | m_pos = wxInvalidOffset; | |
591 | m_offset = 0; | |
592 | m_size = wxInvalidOffset; | |
593 | m_sumType = SUM_UNKNOWN; | |
594 | m_tarType = TYPE_USTAR; | |
595 | m_hdr = new wxTarHeaderBlock; | |
596 | m_HeaderRecs = NULL; | |
597 | m_GlobalHeaderRecs = NULL; | |
598 | m_lasterror = m_parent_i_stream->GetLastError(); | |
599 | } | |
600 | ||
601 | wxTarInputStream::~wxTarInputStream() | |
602 | { | |
603 | delete m_hdr; | |
604 | delete m_HeaderRecs; | |
605 | delete m_GlobalHeaderRecs; | |
606 | } | |
607 | ||
608 | wxTarEntry *wxTarInputStream::GetNextEntry() | |
609 | { | |
610 | m_lasterror = ReadHeaders(); | |
611 | ||
612 | if (!IsOk()) | |
613 | return NULL; | |
614 | ||
615 | wxTarEntryPtr_ entry(new wxTarEntry); | |
616 | ||
617 | entry->SetMode(GetHeaderNumber(TAR_MODE)); | |
618 | entry->SetUserId(GetHeaderNumber(TAR_UID)); | |
619 | entry->SetGroupId(GetHeaderNumber(TAR_UID)); | |
620 | entry->SetSize(GetHeaderNumber(TAR_SIZE)); | |
621 | ||
622 | entry->SetOffset(m_offset); | |
623 | ||
624 | entry->SetDateTime(GetHeaderDate(_T("mtime"))); | |
625 | entry->SetAccessTime(GetHeaderDate(_T("atime"))); | |
626 | entry->SetCreateTime(GetHeaderDate(_T("ctime"))); | |
627 | ||
628 | entry->SetTypeFlag(*m_hdr->Get(TAR_TYPEFLAG)); | |
629 | bool isDir = entry->IsDir(); | |
630 | ||
631 | entry->SetLinkName(GetHeaderString(TAR_LINKNAME)); | |
632 | ||
633 | if (m_tarType != TYPE_OLDTAR) { | |
634 | entry->SetUserName(GetHeaderString(TAR_UNAME)); | |
635 | entry->SetGroupName(GetHeaderString(TAR_GNAME)); | |
636 | ||
637 | entry->SetDevMajor(GetHeaderNumber(TAR_DEVMAJOR)); | |
638 | entry->SetDevMinor(GetHeaderNumber(TAR_DEVMINOR)); | |
639 | } | |
640 | ||
641 | entry->SetName(GetHeaderPath(), wxPATH_UNIX); | |
642 | if (isDir) | |
643 | entry->SetIsDir(); | |
644 | ||
645 | if (m_HeaderRecs) | |
646 | m_HeaderRecs->clear(); | |
647 | ||
648 | m_size = GetDataSize(*entry); | |
649 | m_pos = 0; | |
650 | ||
651 | return entry.release(); | |
652 | } | |
653 | ||
654 | bool wxTarInputStream::OpenEntry(wxTarEntry& entry) | |
655 | { | |
656 | wxFileOffset offset = entry.GetOffset(); | |
657 | ||
658 | if (GetLastError() != wxSTREAM_READ_ERROR | |
659 | && m_parent_i_stream->IsSeekable() | |
660 | && m_parent_i_stream->SeekI(offset) == offset) | |
661 | { | |
662 | m_offset = offset; | |
663 | m_size = GetDataSize(entry); | |
664 | m_pos = 0; | |
665 | m_lasterror = wxSTREAM_NO_ERROR; | |
666 | return true; | |
667 | } else { | |
668 | m_lasterror = wxSTREAM_READ_ERROR; | |
669 | return false; | |
670 | } | |
671 | } | |
672 | ||
673 | bool wxTarInputStream::OpenEntry(wxArchiveEntry& entry) | |
674 | { | |
675 | wxTarEntry *tarEntry = wxStaticCast(&entry, wxTarEntry); | |
676 | return tarEntry ? OpenEntry(*tarEntry) : false; | |
677 | } | |
678 | ||
679 | bool wxTarInputStream::CloseEntry() | |
680 | { | |
681 | if (m_lasterror == wxSTREAM_READ_ERROR) | |
682 | return false; | |
683 | if (!IsOpened()) | |
684 | return true; | |
685 | ||
686 | wxFileOffset size = RoundUpSize(m_size); | |
687 | wxFileOffset remainder = size - m_pos; | |
688 | ||
689 | if (remainder && m_parent_i_stream->IsSeekable()) { | |
690 | wxLogNull nolog; | |
691 | if (m_parent_i_stream->SeekI(remainder, wxFromCurrent) | |
692 | != wxInvalidOffset) | |
693 | remainder = 0; | |
694 | } | |
695 | ||
696 | if (remainder) { | |
697 | const int BUFSIZE = 8192; | |
698 | wxCharBuffer buf(BUFSIZE); | |
699 | ||
700 | while (remainder > 0 && m_parent_i_stream->IsOk()) | |
701 | remainder -= m_parent_i_stream->Read( | |
702 | buf.data(), wxMin(BUFSIZE, remainder)).LastRead(); | |
703 | } | |
704 | ||
705 | m_pos = wxInvalidOffset; | |
706 | m_offset += size; | |
707 | m_lasterror = m_parent_i_stream->GetLastError(); | |
708 | ||
709 | return IsOk(); | |
710 | } | |
711 | ||
712 | wxStreamError wxTarInputStream::ReadHeaders() | |
713 | { | |
714 | if (!CloseEntry()) | |
715 | return wxSTREAM_READ_ERROR; | |
716 | ||
717 | bool done = false; | |
718 | ||
719 | while (!done) { | |
720 | m_hdr->Read(*m_parent_i_stream); | |
721 | if (m_parent_i_stream->Eof()) | |
722 | wxLogError(_("incomplete header block in tar")); | |
723 | if (!*m_parent_i_stream) | |
724 | return wxSTREAM_READ_ERROR; | |
725 | m_offset += TAR_BLOCKSIZE; | |
726 | ||
727 | // an all-zero header marks the end of the tar | |
728 | if (m_hdr->IsAllZeros()) | |
729 | return wxSTREAM_EOF; | |
730 | ||
731 | // the checksum is supposed to be the unsigned sum of the header bytes, | |
732 | // but there have been versions of tar that used the signed sum, so | |
733 | // accept that too, but only if used throughout. | |
734 | wxUint32 chksum = m_hdr->GetOctal(TAR_CHKSUM); | |
735 | bool ok = false; | |
736 | ||
737 | if (m_sumType != SUM_SIGNED) { | |
738 | ok = chksum == m_hdr->Sum(); | |
739 | if (m_sumType == SUM_UNKNOWN) | |
740 | m_sumType = ok ? SUM_UNSIGNED : SUM_SIGNED; | |
741 | } | |
742 | if (m_sumType == SUM_SIGNED) | |
743 | ok = chksum == m_hdr->Sum(true); | |
744 | if (!ok) { | |
745 | wxLogError(_("checksum failure reading tar header block")); | |
746 | return wxSTREAM_READ_ERROR; | |
747 | } | |
748 | ||
749 | if (strcmp(m_hdr->Get(TAR_MAGIC), USTAR_MAGIC) == 0) | |
750 | m_tarType = TYPE_USTAR; | |
751 | else if (strcmp(m_hdr->Get(TAR_MAGIC), GNU_MAGIC) == 0 && | |
752 | strcmp(m_hdr->Get(TAR_VERSION), GNU_VERION) == 0) | |
753 | m_tarType = TYPE_GNUTAR; | |
754 | else | |
755 | m_tarType = TYPE_OLDTAR; | |
756 | ||
757 | if (m_tarType != TYPE_USTAR) | |
758 | break; | |
759 | ||
760 | switch (*m_hdr->Get(TAR_TYPEFLAG)) { | |
761 | case 'g': ReadExtendedHeader(m_GlobalHeaderRecs); break; | |
762 | case 'x': ReadExtendedHeader(m_HeaderRecs); break; | |
763 | default: done = true; | |
764 | } | |
765 | } | |
766 | ||
767 | return wxSTREAM_NO_ERROR; | |
768 | } | |
769 | ||
770 | wxString wxTarInputStream::GetExtendedHeader(const wxString& key) const | |
771 | { | |
772 | wxTarHeaderRecords::iterator it; | |
773 | ||
774 | // look at normal extended header records first | |
775 | if (m_HeaderRecs) { | |
776 | it = m_HeaderRecs->find(key); | |
777 | if (it != m_HeaderRecs->end()) | |
778 | return wxString(it->second.wc_str(wxConvUTF8), GetConv()); | |
779 | } | |
780 | ||
781 | // if not found, look at the global header records | |
782 | if (m_GlobalHeaderRecs) { | |
783 | it = m_GlobalHeaderRecs->find(key); | |
784 | if (it != m_GlobalHeaderRecs->end()) | |
785 | return wxString(it->second.wc_str(wxConvUTF8), GetConv()); | |
786 | } | |
787 | ||
788 | return wxEmptyString; | |
789 | } | |
790 | ||
791 | wxString wxTarInputStream::GetHeaderPath() const | |
792 | { | |
793 | wxString path; | |
794 | ||
795 | if ((path = GetExtendedHeader(_T("path"))) != wxEmptyString) | |
796 | return path; | |
797 | ||
798 | path = wxString(m_hdr->Get(TAR_NAME), GetConv()); | |
799 | if (m_tarType != TYPE_USTAR) | |
800 | return path; | |
801 | ||
802 | const char *prefix = m_hdr->Get(TAR_PREFIX); | |
803 | return *prefix ? wxString(prefix, GetConv()) + _T("/") + path : path; | |
804 | } | |
805 | ||
806 | wxDateTime wxTarInputStream::GetHeaderDate(const wxString& key) const | |
807 | { | |
808 | wxString value; | |
809 | ||
810 | // try extended header, stored as decimal seconds since the epoch | |
811 | if ((value = GetExtendedHeader(key)) != wxEmptyString) { | |
812 | wxLongLong ll; | |
813 | ll.Assign(wxAtof(value) * 1000.0); | |
814 | return ll; | |
815 | } | |
816 | ||
817 | if (key == _T("mtime")) | |
818 | return wxLongLong(m_hdr->GetOctal(TAR_MTIME)) * 1000L; | |
819 | ||
820 | return wxDateTime(); | |
821 | } | |
822 | ||
823 | wxTarNumber wxTarInputStream::GetHeaderNumber(int id) const | |
824 | { | |
825 | wxString value; | |
826 | ||
827 | if ((value = GetExtendedHeader(m_hdr->Name(id))) != wxEmptyString) { | |
828 | wxTarNumber n = 0; | |
829 | const wxChar *p = value; | |
830 | while (*p == ' ') | |
831 | p++; | |
832 | while (isdigit(*p)) | |
833 | n = n * 10 + (*p++ - '0'); | |
834 | return n; | |
835 | } else { | |
836 | return m_hdr->GetOctal(id); | |
837 | } | |
838 | } | |
839 | ||
840 | wxString wxTarInputStream::GetHeaderString(int id) const | |
841 | { | |
842 | wxString value; | |
843 | ||
844 | if ((value = GetExtendedHeader(m_hdr->Name(id))) != wxEmptyString) | |
845 | return value; | |
846 | ||
847 | return wxString(m_hdr->Get(id), GetConv()); | |
848 | } | |
849 | ||
850 | // An extended header consists of one or more records, each constructed: | |
851 | // "%d %s=%s\n", <length>, <keyword>, <value> | |
852 | // <length> is the byte length, <keyword> and <value> are UTF-8 | |
853 | ||
854 | bool wxTarInputStream::ReadExtendedHeader(wxTarHeaderRecords*& recs) | |
855 | { | |
856 | if (!recs) | |
857 | recs = new wxTarHeaderRecords; | |
858 | ||
859 | // round length up to a whole number of blocks | |
860 | size_t len = m_hdr->GetOctal(TAR_SIZE); | |
861 | size_t size = RoundUpSize(len); | |
862 | ||
863 | // read in the whole header since it should be small | |
864 | wxCharBuffer buf(size); | |
865 | size_t lastread = m_parent_i_stream->Read(buf.data(), size).LastRead(); | |
866 | if (lastread < len) | |
867 | len = lastread; | |
868 | buf.data()[len] = 0; | |
869 | m_offset += lastread; | |
870 | ||
871 | size_t recPos, recSize; | |
872 | bool ok = true; | |
873 | ||
874 | for (recPos = 0; recPos < len; recPos += recSize) { | |
875 | char *pRec = buf.data() + recPos; | |
876 | char *p = pRec; | |
877 | ||
878 | // read the record size (byte count in ascii decimal) | |
879 | recSize = 0; | |
880 | while (isdigit(*p)) | |
881 | recSize = recSize * 10 + *p++ - '0'; | |
882 | ||
883 | // validity checks | |
884 | if (recPos + recSize > len) | |
885 | break; | |
886 | if (recSize < p - pRec + (size_t)3 || *p != ' ' | |
887 | || pRec[recSize - 1] != '\012') { | |
888 | ok = false; | |
889 | continue; | |
890 | } | |
891 | ||
892 | // replace the final '\n' with a nul, to terminate value | |
893 | pRec[recSize - 1] = 0; | |
894 | // the key is here, following the space | |
895 | char *pKey = ++p; | |
896 | ||
897 | // look forward for the '=', the value follows | |
898 | while (*p && *p != '=') | |
899 | p++; | |
900 | if (!*p) { | |
901 | ok = false; | |
902 | continue; | |
903 | } | |
904 | // replace the '=' with a nul, to terminate the key | |
905 | *p++ = 0; | |
906 | ||
907 | wxString key(wxConvUTF8.cMB2WC(pKey), GetConv()); | |
908 | wxString value(wxConvUTF8.cMB2WC(p), GetConv()); | |
909 | ||
910 | // an empty value unsets a previously given value | |
911 | if (value.empty()) | |
912 | recs->erase(key); | |
913 | else | |
914 | (*recs)[key] = value; | |
915 | } | |
916 | ||
917 | if (!ok || recPos < len || size != lastread) { | |
918 | wxLogWarning(_("invalid data in extended tar header")); | |
919 | return false; | |
920 | } | |
921 | ||
922 | return true; | |
923 | } | |
924 | ||
925 | wxFileOffset wxTarInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) | |
926 | { | |
927 | if (!IsOpened()) { | |
928 | wxLogError(_("tar entry not open")); | |
929 | m_lasterror = wxSTREAM_READ_ERROR; | |
930 | } | |
931 | if (!IsOk()) | |
932 | return wxInvalidOffset; | |
933 | ||
934 | switch (mode) { | |
935 | case wxFromStart: break; | |
936 | case wxFromCurrent: pos += m_pos; break; | |
937 | case wxFromEnd: pos += m_size; break; | |
938 | } | |
939 | ||
940 | if (pos < 0 || m_parent_i_stream->SeekI(m_offset + pos) == wxInvalidOffset) | |
941 | return wxInvalidOffset; | |
942 | ||
943 | m_pos = pos; | |
944 | return m_pos; | |
945 | } | |
946 | ||
947 | size_t wxTarInputStream::OnSysRead(void *buffer, size_t size) | |
948 | { | |
949 | if (!IsOpened()) { | |
950 | wxLogError(_("tar entry not open")); | |
951 | m_lasterror = wxSTREAM_READ_ERROR; | |
952 | } | |
953 | if (!IsOk() || !size) | |
954 | return 0; | |
955 | ||
956 | if (m_pos >= m_size) | |
957 | size = 0; | |
958 | else if (m_pos + size > m_size + (size_t)0) | |
959 | size = m_size - m_pos; | |
960 | ||
961 | size_t lastread = m_parent_i_stream->Read(buffer, size).LastRead(); | |
962 | m_pos += lastread; | |
963 | ||
964 | if (m_pos >= m_size) { | |
965 | m_lasterror = wxSTREAM_EOF; | |
966 | } else if (!m_parent_i_stream->IsOk()) { | |
967 | // any other error will have been reported by the underlying stream | |
968 | if (m_parent_i_stream->Eof()) | |
969 | wxLogError(_("unexpected end of file")); | |
970 | m_lasterror = wxSTREAM_READ_ERROR; | |
971 | } | |
972 | ||
973 | return lastread; | |
974 | } | |
975 | ||
976 | ||
977 | ///////////////////////////////////////////////////////////////////////////// | |
978 | // Output stream | |
979 | ||
980 | wxTarOutputStream::wxTarOutputStream(wxOutputStream& stream, | |
981 | wxTarFormat format /*=wxTAR_PAX*/, | |
982 | wxMBConv& conv /*=wxConvLocal*/) | |
983 | : wxArchiveOutputStream(stream, conv) | |
984 | { | |
985 | Init(format); | |
986 | } | |
987 | ||
988 | wxTarOutputStream::wxTarOutputStream(wxOutputStream *stream, | |
989 | wxTarFormat format /*=wxTAR_PAX*/, | |
990 | wxMBConv& conv /*=wxConvLocal*/) | |
991 | : wxArchiveOutputStream(stream, conv) | |
992 | { | |
993 | Init(format); | |
994 | } | |
995 | ||
996 | void wxTarOutputStream::Init(wxTarFormat format) | |
997 | { | |
998 | m_pos = wxInvalidOffset; | |
999 | m_maxpos = wxInvalidOffset; | |
1000 | m_size = wxInvalidOffset; | |
1001 | m_headpos = wxInvalidOffset; | |
1002 | m_datapos = wxInvalidOffset; | |
1003 | m_tarstart = wxInvalidOffset; | |
1004 | m_tarsize = 0; | |
1005 | m_pax = format == wxTAR_PAX; | |
1006 | m_BlockingFactor = m_pax ? 10 : 20; | |
1007 | m_chksum = 0; | |
1008 | m_large = false; | |
1009 | m_hdr = new wxTarHeaderBlock; | |
1010 | m_hdr2 = NULL; | |
1011 | m_extendedHdr = NULL; | |
1012 | m_extendedSize = 0; | |
1013 | m_lasterror = m_parent_o_stream->GetLastError(); | |
1014 | } | |
1015 | ||
1016 | wxTarOutputStream::~wxTarOutputStream() | |
1017 | { | |
1018 | if (m_tarsize) | |
1019 | Close(); | |
1020 | delete m_hdr; | |
1021 | delete m_hdr2; | |
1022 | delete [] m_extendedHdr; | |
1023 | } | |
1024 | ||
1025 | bool wxTarOutputStream::PutNextEntry(wxTarEntry *entry) | |
1026 | { | |
1027 | wxTarEntryPtr_ e(entry); | |
1028 | ||
1029 | if (!CloseEntry()) | |
1030 | return false; | |
1031 | ||
1032 | if (!m_tarsize) { | |
1033 | wxLogNull nolog; | |
1034 | m_tarstart = m_parent_o_stream->TellO(); | |
1035 | } | |
1036 | ||
1037 | if (m_tarstart != wxInvalidOffset) | |
1038 | m_headpos = m_tarstart + m_tarsize; | |
1039 | ||
1040 | if (WriteHeaders(*e)) { | |
1041 | m_pos = 0; | |
1042 | m_maxpos = 0; | |
1043 | m_size = GetDataSize(*e); | |
1044 | if (m_tarstart != wxInvalidOffset) | |
1045 | m_datapos = m_tarstart + m_tarsize; | |
1046 | ||
1047 | // types that are not allowd any data | |
1048 | const char nodata[] = { | |
1049 | wxTAR_LNKTYPE, wxTAR_SYMTYPE, wxTAR_CHRTYPE, wxTAR_BLKTYPE, | |
1050 | wxTAR_DIRTYPE, wxTAR_FIFOTYPE, 0 | |
1051 | }; | |
1052 | char typeflag = e->GetTypeFlag(); | |
1053 | ||
1054 | // pax does now allow data for wxTAR_LNKTYPE | |
1055 | if (!m_pax || typeflag - wxTAR_LNKTYPE != 0) | |
1056 | if (strchr(nodata, typeflag) != NULL) | |
1057 | CloseEntry(); | |
1058 | } | |
1059 | ||
1060 | return IsOk(); | |
1061 | } | |
1062 | ||
1063 | bool wxTarOutputStream::PutNextEntry(const wxString& name, | |
1064 | const wxDateTime& dt, | |
1065 | wxFileOffset size) | |
1066 | { | |
1067 | return PutNextEntry(new wxTarEntry(name, dt, size)); | |
1068 | } | |
1069 | ||
1070 | bool wxTarOutputStream::PutNextDirEntry(const wxString& name, | |
1071 | const wxDateTime& dt) | |
1072 | { | |
1073 | wxTarEntry *entry = new wxTarEntry(name, dt); | |
1074 | entry->SetIsDir(); | |
1075 | return PutNextEntry(entry); | |
1076 | } | |
1077 | ||
1078 | bool wxTarOutputStream::PutNextEntry(wxArchiveEntry *entry) | |
1079 | { | |
1080 | wxTarEntry *tarEntry = wxStaticCast(entry, wxTarEntry); | |
1081 | if (!tarEntry) | |
1082 | delete entry; | |
1083 | return PutNextEntry(tarEntry); | |
1084 | } | |
1085 | ||
1086 | bool wxTarOutputStream::CopyEntry(wxTarEntry *entry, | |
1087 | wxTarInputStream& inputStream) | |
1088 | { | |
1089 | if (PutNextEntry(entry)) | |
1090 | Write(inputStream); | |
1091 | return IsOk() && inputStream.Eof(); | |
1092 | } | |
1093 | ||
1094 | bool wxTarOutputStream::CopyEntry(wxArchiveEntry *entry, | |
1095 | wxArchiveInputStream& inputStream) | |
1096 | { | |
1097 | if (PutNextEntry(entry)) | |
1098 | Write(inputStream); | |
1099 | return IsOk() && inputStream.Eof(); | |
1100 | } | |
1101 | ||
1102 | bool wxTarOutputStream::CloseEntry() | |
1103 | { | |
1104 | if (!IsOpened()) | |
1105 | return true; | |
1106 | ||
1107 | if (m_pos < m_maxpos) { | |
1108 | wxASSERT(m_parent_o_stream->IsSeekable()); | |
1109 | m_parent_o_stream->SeekO(m_datapos + m_maxpos); | |
1110 | m_lasterror = m_parent_o_stream->GetLastError(); | |
1111 | m_pos = m_maxpos; | |
1112 | } | |
1113 | ||
1114 | if (IsOk()) { | |
1115 | wxFileOffset size = RoundUpSize(m_pos); | |
1116 | if (size > m_pos) { | |
1117 | memset(m_hdr, 0, size - m_pos); | |
1118 | m_parent_o_stream->Write(m_hdr, size - m_pos); | |
1119 | m_lasterror = m_parent_o_stream->GetLastError(); | |
1120 | } | |
1121 | m_tarsize += size; | |
1122 | } | |
1123 | ||
1124 | if (IsOk() && m_pos != m_size) | |
1125 | ModifyHeader(); | |
1126 | ||
1127 | m_pos = wxInvalidOffset; | |
1128 | m_maxpos = wxInvalidOffset; | |
1129 | m_size = wxInvalidOffset; | |
1130 | m_headpos = wxInvalidOffset; | |
1131 | m_datapos = wxInvalidOffset; | |
1132 | ||
1133 | return IsOk(); | |
1134 | } | |
1135 | ||
1136 | bool wxTarOutputStream::Close() | |
1137 | { | |
1138 | if (!CloseEntry()) | |
1139 | return false; | |
1140 | ||
1141 | memset(m_hdr, 0, sizeof(*m_hdr)); | |
1142 | int count = (RoundUpSize(m_tarsize + 2 * TAR_BLOCKSIZE, m_BlockingFactor) | |
1143 | - m_tarsize) / TAR_BLOCKSIZE; | |
1144 | while (count--) | |
1145 | m_parent_o_stream->Write(m_hdr, TAR_BLOCKSIZE); | |
1146 | ||
1147 | m_tarsize = 0; | |
1148 | m_tarstart = wxInvalidOffset; | |
1149 | m_lasterror = m_parent_o_stream->GetLastError(); | |
1150 | return IsOk(); | |
1151 | } | |
1152 | ||
1153 | bool wxTarOutputStream::WriteHeaders(wxTarEntry& entry) | |
1154 | { | |
1155 | memset(m_hdr, 0, sizeof(*m_hdr)); | |
1156 | ||
1157 | SetHeaderPath(entry.GetName(wxPATH_UNIX)); | |
1158 | ||
1159 | SetHeaderNumber(TAR_MODE, entry.GetMode()); | |
1160 | SetHeaderNumber(TAR_UID, entry.GetUserId()); | |
1161 | SetHeaderNumber(TAR_GID, entry.GetGroupId()); | |
1162 | ||
1163 | if (entry.GetSize() == wxInvalidOffset) | |
1164 | entry.SetSize(0); | |
1165 | m_large = SetHeaderNumber(TAR_SIZE, entry.GetSize()); | |
1166 | ||
1167 | SetHeaderDate(_T("mtime"), entry.GetDateTime()); | |
1168 | if (entry.GetAccessTime().IsValid()) | |
1169 | SetHeaderDate(_T("atime"), entry.GetAccessTime()); | |
1170 | if (entry.GetCreateTime().IsValid()) | |
1171 | SetHeaderDate(_T("ctime"), entry.GetCreateTime()); | |
1172 | ||
1173 | *m_hdr->Get(TAR_TYPEFLAG) = entry.GetTypeFlag(); | |
1174 | ||
1175 | strcpy(m_hdr->Get(TAR_MAGIC), USTAR_MAGIC); | |
1176 | strcpy(m_hdr->Get(TAR_VERSION), USTAR_VERSION); | |
1177 | ||
1178 | SetHeaderString(TAR_LINKNAME, entry.GetLinkName()); | |
1179 | SetHeaderString(TAR_UNAME, entry.GetUserName()); | |
1180 | SetHeaderString(TAR_GNAME, entry.GetGroupName()); | |
1181 | ||
1182 | if (~entry.GetDevMajor()) | |
1183 | SetHeaderNumber(TAR_DEVMAJOR, entry.GetDevMajor()); | |
1184 | if (~entry.GetDevMinor()) | |
1185 | SetHeaderNumber(TAR_DEVMINOR, entry.GetDevMinor()); | |
1186 | ||
1187 | m_chksum = m_hdr->Sum(); | |
1188 | m_hdr->SetOctal(TAR_CHKSUM, m_chksum); | |
1189 | if (!m_large) | |
1190 | m_chksum -= m_hdr->SumField(TAR_SIZE); | |
1191 | ||
1192 | // The main header is now fully prepared so we know what extended headers | |
1193 | // (if any) will be needed. Output any extended headers before writing | |
1194 | // the main header. | |
1195 | if (m_extendedHdr && *m_extendedHdr) { | |
1196 | wxASSERT(m_pax); | |
1197 | // the extended headers are written to the tar as a file entry, | |
1198 | // so prepare a regular header block for the pseudo-file. | |
1199 | if (!m_hdr2) | |
1200 | m_hdr2 = new wxTarHeaderBlock; | |
1201 | memset(m_hdr2, 0, sizeof(*m_hdr2)); | |
1202 | ||
1203 | // an old tar that doesn't understand extended headers will | |
1204 | // extract it as a file, so give these fields reasonable values | |
1205 | // so that the user will have access to read and remove it. | |
1206 | m_hdr2->SetPath(PaxHeaderPath(_T("%d/PaxHeaders.%p/%f"), | |
1207 | entry.GetName(wxPATH_UNIX)), GetConv()); | |
1208 | m_hdr2->SetOctal(TAR_MODE, 0600); | |
1209 | strcpy(m_hdr2->Get(TAR_UID), m_hdr->Get(TAR_UID)); | |
1210 | strcpy(m_hdr2->Get(TAR_GID), m_hdr->Get(TAR_GID)); | |
1211 | size_t length = strlen(m_extendedHdr); | |
1212 | m_hdr2->SetOctal(TAR_SIZE, length); | |
1213 | strcpy(m_hdr2->Get(TAR_MTIME), m_hdr->Get(TAR_MTIME)); | |
1214 | *m_hdr2->Get(TAR_TYPEFLAG) = 'x'; | |
1215 | strcpy(m_hdr2->Get(TAR_MAGIC), USTAR_MAGIC); | |
1216 | strcpy(m_hdr2->Get(TAR_VERSION), USTAR_VERSION); | |
1217 | strcpy(m_hdr2->Get(TAR_UNAME), m_hdr->Get(TAR_UNAME)); | |
1218 | strcpy(m_hdr2->Get(TAR_GNAME), m_hdr->Get(TAR_GNAME)); | |
1219 | ||
1220 | m_hdr2->SetOctal(TAR_CHKSUM, m_hdr2->Sum()); | |
1221 | ||
1222 | m_hdr2->Write(*m_parent_o_stream); | |
1223 | m_tarsize += TAR_BLOCKSIZE; | |
1224 | ||
1225 | size_t rounded = RoundUpSize(length); | |
1226 | memset(m_extendedHdr + length, 0, rounded - length); | |
1227 | m_parent_o_stream->Write(m_extendedHdr, rounded); | |
1228 | m_tarsize += rounded; | |
1229 | ||
1230 | *m_extendedHdr = 0; | |
1231 | } | |
1232 | ||
1233 | // if don't have extended headers just report error | |
1234 | if (!m_badfit.empty()) { | |
1235 | wxASSERT(!m_pax); | |
1236 | wxLogWarning(_("%s did not fit the tar header for entry '%s'"), | |
1237 | m_badfit.c_str(), entry.GetName().c_str()); | |
1238 | m_badfit.clear(); | |
1239 | } | |
1240 | ||
1241 | m_hdr->Write(*m_parent_o_stream); | |
1242 | m_tarsize += TAR_BLOCKSIZE; | |
1243 | m_lasterror = m_parent_o_stream->GetLastError(); | |
1244 | ||
1245 | return IsOk(); | |
1246 | } | |
1247 | ||
1248 | wxString wxTarOutputStream::PaxHeaderPath(const wxString& format, | |
1249 | const wxString& path) | |
1250 | { | |
1251 | wxString d = path.BeforeLast(_T('/')); | |
1252 | wxString f = path.AfterLast(_T('/')); | |
1253 | wxString ret; | |
1254 | ||
1255 | if (d.empty()) | |
1256 | d = _T("."); | |
1257 | ||
1258 | ret.reserve(format.length() + path.length() + 16); | |
1259 | ||
1260 | size_t begin = 0; | |
1261 | size_t end; | |
1262 | ||
1263 | for (;;) { | |
1264 | end = format.find('%', begin); | |
1265 | if (end == wxString::npos || end + 1 >= format.length()) | |
1266 | break; | |
1267 | ret << format.substr(begin, end - begin); | |
1268 | switch (format[end + 1]) { | |
1269 | case 'd': ret << d; break; | |
1270 | case 'f': ret << f; break; | |
1271 | case 'p': ret << wxGetProcessId(); break; | |
1272 | case '%': ret << _T("%"); break; | |
1273 | } | |
1274 | begin = end + 2; | |
1275 | } | |
1276 | ||
1277 | ret << format.substr(begin); | |
1278 | ||
1279 | return ret; | |
1280 | } | |
1281 | ||
1282 | bool wxTarOutputStream::ModifyHeader() | |
1283 | { | |
1284 | wxFileOffset originalPos = wxInvalidOffset; | |
1285 | wxFileOffset sizePos = wxInvalidOffset; | |
1286 | ||
1287 | if (!m_large && m_headpos != wxInvalidOffset | |
1288 | && m_parent_o_stream->IsSeekable()) | |
1289 | { | |
1290 | wxLogNull nolog; | |
1291 | originalPos = m_parent_o_stream->TellO(); | |
1292 | if (originalPos != wxInvalidOffset) | |
1293 | sizePos = | |
1294 | m_parent_o_stream->SeekO(m_headpos + m_hdr->Offset(TAR_SIZE)); | |
1295 | } | |
1296 | ||
1297 | if (sizePos == wxInvalidOffset || !m_hdr->SetOctal(TAR_SIZE, m_pos)) { | |
1298 | wxLogError(_("incorrect size given for tar entry")); | |
1299 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
1300 | return false; | |
1301 | } | |
1302 | ||
1303 | m_chksum += m_hdr->SumField(TAR_SIZE); | |
1304 | m_hdr->SetOctal(TAR_CHKSUM, m_chksum); | |
1305 | wxFileOffset sumPos = m_headpos + m_hdr->Offset(TAR_CHKSUM); | |
1306 | ||
1307 | return | |
1308 | m_hdr->WriteField(*m_parent_o_stream, TAR_SIZE) && | |
1309 | m_parent_o_stream->SeekO(sumPos) == sumPos && | |
1310 | m_hdr->WriteField(*m_parent_o_stream, TAR_CHKSUM) && | |
1311 | m_parent_o_stream->SeekO(originalPos) == originalPos; | |
1312 | } | |
1313 | ||
1314 | void wxTarOutputStream::SetHeaderPath(const wxString& name) | |
1315 | { | |
1316 | if (!m_hdr->SetPath(name, GetConv()) || (m_pax && !name.IsAscii())) | |
1317 | SetExtendedHeader(_T("path"), name); | |
1318 | } | |
1319 | ||
1320 | bool wxTarOutputStream::SetHeaderNumber(int id, wxTarNumber n) | |
1321 | { | |
1322 | if (m_hdr->SetOctal(id, n)) { | |
1323 | return true; | |
1324 | } else { | |
1325 | SetExtendedHeader(m_hdr->Name(id), wxLongLong(n).ToString()); | |
1326 | return false; | |
1327 | } | |
1328 | } | |
1329 | ||
1330 | void wxTarOutputStream::SetHeaderString(int id, const wxString& str) | |
1331 | { | |
1332 | strncpy(m_hdr->Get(id), str.mb_str(GetConv()), m_hdr->Len(id)); | |
1333 | if (str.length() > m_hdr->Len(id)) | |
1334 | SetExtendedHeader(m_hdr->Name(id), str); | |
1335 | } | |
1336 | ||
1337 | void wxTarOutputStream::SetHeaderDate(const wxString& key, | |
1338 | const wxDateTime& datetime) | |
1339 | { | |
1340 | wxLongLong ll = datetime.IsValid() ? datetime.GetValue() : wxLongLong(0); | |
1341 | wxLongLong secs = ll / 1000L; | |
1342 | ||
1343 | if (key != _T("mtime") || !m_hdr->SetOctal(TAR_MTIME, secs.GetValue()) | |
1344 | || secs <= 0 || secs >= 0x7fffffff) | |
1345 | { | |
1346 | wxString str; | |
1347 | if (ll >= LONG_MIN && ll <= LONG_MAX) { | |
1348 | str.Printf(_T("%g"), ll.ToLong() / 1000.0); | |
1349 | } else { | |
1350 | str = ll.ToString(); | |
1351 | str.insert(str.end() - 3, '.'); | |
1352 | } | |
1353 | SetExtendedHeader(key, str); | |
1354 | } | |
1355 | } | |
1356 | ||
1357 | void wxTarOutputStream::SetExtendedHeader(const wxString& key, | |
1358 | const wxString& value) | |
1359 | { | |
1360 | if (m_pax) { | |
1361 | const wxWX2WCbuf wide_key = key.wc_str(GetConv()); | |
1362 | const wxCharBuffer utf_key = wxConvUTF8.cWC2MB(wide_key); | |
1363 | ||
1364 | const wxWX2WCbuf wide_value = value.wc_str(GetConv()); | |
1365 | const wxCharBuffer utf_value = wxConvUTF8.cWC2MB(wide_value); | |
1366 | ||
1367 | // a small buffer to format the length field in | |
1368 | char buf[32]; | |
1369 | // length of "99<space><key>=<value>\n" | |
1370 | unsigned long length = strlen(utf_value) + strlen(utf_key) + 5; | |
1371 | sprintf(buf, "%lu", length); | |
1372 | // the length includes itself | |
1373 | size_t lenlen = strlen(buf); | |
1374 | if (lenlen != 2) { | |
1375 | length += lenlen - 2; | |
1376 | sprintf(buf, "%lu", length); | |
1377 | if (strlen(buf) > lenlen) | |
1378 | sprintf(buf, "%lu", ++length); | |
1379 | } | |
1380 | ||
1381 | // reallocate m_extendedHdr if it's not big enough | |
1382 | if (m_extendedSize < length) { | |
1383 | size_t rounded = RoundUpSize(length); | |
1384 | m_extendedSize <<= 1; | |
1385 | if (rounded > m_extendedSize) | |
1386 | m_extendedSize = rounded; | |
1387 | char *oldHdr = m_extendedHdr; | |
1388 | m_extendedHdr = new char[m_extendedSize]; | |
1389 | if (oldHdr) { | |
1390 | strcpy(m_extendedHdr, oldHdr); | |
1391 | delete oldHdr; | |
1392 | } else { | |
1393 | *m_extendedHdr = 0; | |
1394 | } | |
1395 | } | |
1396 | ||
1397 | // append the new record | |
1398 | char *append = strchr(m_extendedHdr, 0); | |
1399 | sprintf(append, "%s %s=%s\012", buf, | |
1400 | (const char*)utf_key, (const char*)utf_value); | |
1401 | } | |
1402 | else { | |
1403 | // if not pax then make a list of fields to report as errors | |
1404 | if (!m_badfit.empty()) | |
1405 | m_badfit += _T(", "); | |
1406 | m_badfit += key; | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | void wxTarOutputStream::Sync() | |
1411 | { | |
1412 | m_parent_o_stream->Sync(); | |
1413 | } | |
1414 | ||
1415 | wxFileOffset wxTarOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) | |
1416 | { | |
1417 | if (!IsOpened()) { | |
1418 | wxLogError(_("tar entry not open")); | |
1419 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
1420 | } | |
1421 | if (!IsOk() || m_datapos == wxInvalidOffset) | |
1422 | return wxInvalidOffset; | |
1423 | ||
1424 | switch (mode) { | |
1425 | case wxFromStart: break; | |
1426 | case wxFromCurrent: pos += m_pos; break; | |
1427 | case wxFromEnd: pos += m_maxpos; break; | |
1428 | } | |
1429 | ||
1430 | if (pos < 0 || m_parent_o_stream->SeekO(m_datapos + pos) == wxInvalidOffset) | |
1431 | return wxInvalidOffset; | |
1432 | ||
1433 | m_pos = pos; | |
1434 | return m_pos; | |
1435 | } | |
1436 | ||
1437 | size_t wxTarOutputStream::OnSysWrite(const void *buffer, size_t size) | |
1438 | { | |
1439 | if (!IsOpened()) { | |
1440 | wxLogError(_("tar entry not open")); | |
1441 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
1442 | } | |
1443 | if (!IsOk() || !size) | |
1444 | return 0; | |
1445 | ||
1446 | size_t lastwrite = m_parent_o_stream->Write(buffer, size).LastWrite(); | |
1447 | m_pos += lastwrite; | |
1448 | if (m_pos > m_maxpos) | |
1449 | m_maxpos = m_pos; | |
1450 | ||
1451 | if (lastwrite != size) | |
1452 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
1453 | ||
1454 | return lastwrite; | |
1455 | } | |
1456 | ||
1457 | #endif // wxUSE_TARSTREAM |