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