1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Streams for Tar files
4 // Author: Mike Wetherell
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/tarstrm.h"
22 #include "wx/hashmap.h"
28 #include "wx/buffer.h"
29 #include "wx/datetime.h"
30 #include "wx/ptr_scpd.h"
31 #include "wx/filename.h"
40 /////////////////////////////////////////////////////////////////////////////
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
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
= " ";
88 IMPLEMENT_DYNAMIC_CLASS(wxTarEntry
, wxArchiveEntry
)
89 IMPLEMENT_DYNAMIC_CLASS(wxTarClassFactory
, wxArchiveClassFactory
)
92 /////////////////////////////////////////////////////////////////////////////
95 static wxTarClassFactory g_wxTarClassFactory
;
97 wxTarClassFactory::wxTarClassFactory()
99 if (this == &g_wxTarClassFactory
)
103 const wxChar
* const *
104 wxTarClassFactory::GetProtocols(wxStreamProtocolType type
) const
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
};
112 case wxSTREAM_PROTOCOL
: return protocols
;
113 case wxSTREAM_MIMETYPE
: return mimetypes
;
114 case wxSTREAM_FILEEXTENSION
: return fileexts
;
115 default: return empty
;
120 /////////////////////////////////////////////////////////////////////////////
123 typedef wxFileOffset wxTarNumber
;
125 struct wxTarField
{ const wxChar
*name
; int pos
; };
127 class wxTarHeaderBlock
131 { memset(data
, 0, sizeof(data
)); }
132 wxTarHeaderBlock(const wxTarHeaderBlock
& hb
)
133 { memcpy(data
, hb
.data
, sizeof(data
)); }
135 bool Read(wxInputStream
& in
);
136 bool Write(wxOutputStream
& out
);
137 inline bool WriteField(wxOutputStream
& out
, int id
);
139 bool IsAllZeros() const;
140 wxUint32
Sum(bool SignedSum
= false);
141 wxUint32
SumField(int id
);
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
; }
148 bool SetOctal(int id
, wxTarNumber n
);
149 wxTarNumber
GetOctal(int id
);
150 bool SetPath(const wxString
& name
, wxMBConv
& conv
);
153 char data
[TAR_BLOCKSIZE
+ TAR_NUMFIELDS
];
154 static const wxTarField fields
[];
158 wxDEFINE_SCOPED_PTR_TYPE(wxTarHeaderBlock
);
160 // A table giving the field names and offsets in a tar header block
161 const wxTarField
wxTarHeaderBlock::fields
[] =
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
}
183 void wxTarHeaderBlock::check()
185 wxCOMPILE_TIME_ASSERT(
186 WXSIZEOF(fields
) == TAR_NUMFIELDS
+ 1,
187 Wrong_number_of_elements_in_fields_table
191 bool wxTarHeaderBlock::IsAllZeros() const
193 const char *p
= data
;
194 for (size_t i
= 0; i
< sizeof(data
); i
++)
200 wxUint32
wxTarHeaderBlock::Sum(bool SignedSum
/*=false*/)
202 // the chksum field itself should be blanks during the calculation
203 memset(Get(TAR_CHKSUM
), ' ', Len(TAR_CHKSUM
));
204 const char *p
= data
;
208 for (size_t i
= 0; i
< sizeof(data
); i
++)
209 n
+= (signed char)p
[i
];
211 for (size_t i
= 0; i
< sizeof(data
); i
++)
212 n
+= (unsigned char)p
[i
];
217 wxUint32
wxTarHeaderBlock::SumField(int id
)
219 unsigned char *p
= (unsigned char*)Get(id
);
220 unsigned char *q
= p
+ Len(id
);
229 bool wxTarHeaderBlock::Read(wxInputStream
& in
)
233 for (int id
= 0; id
< TAR_NUMFIELDS
&& ok
; id
++)
234 ok
= in
.Read(Get(id
), Len(id
)).LastRead() == Len(id
);
239 bool wxTarHeaderBlock::Write(wxOutputStream
& out
)
243 for (int id
= 0; id
< TAR_NUMFIELDS
&& ok
; id
++)
244 ok
= WriteField(out
, id
);
249 bool wxTarHeaderBlock::WriteField(wxOutputStream
& out
, int id
)
251 return out
.Write(Get(id
), Len(id
)).LastWrite() == Len(id
);
254 wxTarNumber
wxTarHeaderBlock::GetOctal(int id
)
257 const char *p
= Get(id
);
260 while (*p
>= '0' && *p
< '8')
261 n
= (n
<< 3) | (*p
++ - '0');
265 bool wxTarHeaderBlock::SetOctal(int id
, wxTarNumber n
)
267 // set an octal field, return true if the number fits
268 char *field
= Get(id
);
269 char *p
= field
+ Len(id
);
272 *--p
= '0' + (n
& 7);
278 bool wxTarHeaderBlock::SetPath(const wxString
& name
, wxMBConv
& conv
)
280 bool badconv
= false;
283 wxCharBuffer nameBuf
= name
.mb_str(conv
);
285 // if the conversion fails make an approximation
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
];
295 const char *mbName
= nameBuf
;
297 const char *mbName
= name
.c_str();
302 bool notGoingToFit
= false;
303 size_t len
= strlen(mbName
);
304 size_t maxname
= Len(TAR_NAME
);
305 size_t maxprefix
= Len(TAR_PREFIX
);
310 fits
= i
< maxprefix
&& len
- i
<= maxname
;
313 const char *p
= strchr(mbName
+ i
, '/');
315 nexti
= p
- mbName
+ 1;
316 if (!p
|| nexti
- 1 > maxprefix
)
317 notGoingToFit
= true;
320 if (fits
|| notGoingToFit
) {
321 strncpy(Get(TAR_NAME
), mbName
+ i
, maxname
);
323 strncpy(Get(TAR_PREFIX
), mbName
, i
- 1);
330 return fits
&& !badconv
;
334 /////////////////////////////////////////////////////////////////////////////
337 static wxFileOffset
RoundUpSize(wxFileOffset size
, int factor
= 1)
339 wxFileOffset chunk
= TAR_BLOCKSIZE
* factor
;
340 return ((size
+ chunk
- 1) / chunk
) * chunk
;
343 static wxString
GroupName()
347 if ((gr
= getgrgid(getgid())) != NULL
)
348 return wxString(gr
->gr_name
, wxConvLibc
);
353 static inline int UserId()
362 static inline int GroupId()
371 // ignore the size field for entry types 3, 4, 5 and 6
373 static inline wxFileOffset
GetDataSize(const wxTarEntry
& entry
)
375 switch (entry
.GetTypeFlag()) {
382 return entry
.GetSize();
387 /////////////////////////////////////////////////////////////////////////////
389 // Holds all the meta-data for a file in the tar
391 wxTarEntry::wxTarEntry(const wxString
& name
/*=wxEmptyString*/,
392 const wxDateTime
& dt
/*=wxDateTime::Now()*/,
393 wxFileOffset size
/*=0*/)
397 m_GroupId(GroupId()),
399 m_Offset(wxInvalidOffset
),
401 m_TypeFlag(wxTAR_REGTYPE
),
402 m_UserName(wxGetUserId()),
403 m_GroupName(GroupName()),
411 wxTarEntry::~wxTarEntry()
415 wxTarEntry::wxTarEntry(const wxTarEntry
& e
)
418 m_IsModeSet(e
.m_IsModeSet
),
419 m_UserId(e
.m_UserId
),
420 m_GroupId(e
.m_GroupId
),
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
)
435 wxTarEntry
& wxTarEntry::operator=(const wxTarEntry
& e
)
440 m_IsModeSet
= e
.m_IsModeSet
;
441 m_UserId
= e
.m_UserId
;
442 m_GroupId
= e
.m_GroupId
;
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
;
458 wxString
wxTarEntry::GetName(wxPathFormat format
/*=wxPATH_NATIVE*/) const
460 bool isDir
= IsDir() && !m_Name
.empty();
462 // optimisations for common (and easy) cases
463 switch (wxFileName::GetFormat(format
)) {
466 wxString
name(isDir
? m_Name
+ _T("\\") : m_Name
);
467 for (size_t i
= 0; i
< name
.length(); i
++)
468 if (name
[i
] == _T('/'))
474 return isDir
? m_Name
+ _T("/") : m_Name
;
483 fn
.AssignDir(m_Name
, wxPATH_UNIX
);
485 fn
.Assign(m_Name
, wxPATH_UNIX
);
487 return fn
.GetFullPath(format
);
490 void wxTarEntry::SetName(const wxString
& name
, wxPathFormat format
)
493 m_Name
= GetInternalName(name
, format
, &isDir
);
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.
502 wxString
wxTarEntry::GetInternalName(const wxString
& name
,
503 wxPathFormat format
/*=wxPATH_NATIVE*/,
504 bool *pIsDir
/*=NULL*/)
508 if (wxFileName::GetFormat(format
) != wxPATH_UNIX
)
509 internal
= wxFileName(name
, format
).GetFullPath(wxPATH_UNIX
);
513 bool isDir
= !internal
.empty() && internal
.Last() == '/';
517 internal
.erase(internal
.length() - 1);
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
;
529 bool wxTarEntry::IsDir() const
531 return m_TypeFlag
- wxTAR_DIRTYPE
== 0;
534 void wxTarEntry::SetIsDir(bool isDir
)
537 m_TypeFlag
= wxTAR_DIRTYPE
;
538 else if (m_TypeFlag
- wxTAR_DIRTYPE
== 0)
539 m_TypeFlag
= wxTAR_REGTYPE
;
542 void wxTarEntry::SetIsReadOnly(bool isReadOnly
)
550 int wxTarEntry::GetMode() const
552 if (m_IsModeSet
|| !IsDir())
555 return m_Mode
| 0111;
559 void wxTarEntry::SetMode(int mode
)
561 m_Mode
= mode
& 07777;
566 /////////////////////////////////////////////////////////////////////////////
569 wxDECLARE_SCOPED_PTR(wxTarEntry
, wxTarEntryPtr_
)
570 wxDEFINE_SCOPED_PTR (wxTarEntry
, wxTarEntryPtr_
)
572 wxTarInputStream::wxTarInputStream(wxInputStream
& stream
,
573 wxMBConv
& conv
/*=wxConvLocal*/)
574 : wxArchiveInputStream(stream
, conv
)
579 wxTarInputStream::wxTarInputStream(wxInputStream
*stream
,
580 wxMBConv
& conv
/*=wxConvLocal*/)
581 : wxArchiveInputStream(stream
, conv
)
586 void wxTarInputStream::Init()
588 m_pos
= wxInvalidOffset
;
590 m_size
= wxInvalidOffset
;
591 m_sumType
= SUM_UNKNOWN
;
592 m_tarType
= TYPE_USTAR
;
593 m_hdr
= new wxTarHeaderBlock
;
595 m_GlobalHeaderRecs
= NULL
;
596 m_lasterror
= m_parent_i_stream
->GetLastError();
599 wxTarInputStream::~wxTarInputStream()
603 delete m_GlobalHeaderRecs
;
606 wxTarEntry
*wxTarInputStream::GetNextEntry()
608 m_lasterror
= ReadHeaders();
613 wxTarEntryPtr_
entry(new wxTarEntry
);
615 entry
->SetMode(GetHeaderNumber(TAR_MODE
));
616 entry
->SetUserId(GetHeaderNumber(TAR_UID
));
617 entry
->SetGroupId(GetHeaderNumber(TAR_UID
));
618 entry
->SetSize(GetHeaderNumber(TAR_SIZE
));
620 entry
->SetOffset(m_offset
);
622 entry
->SetDateTime(GetHeaderDate(_T("mtime")));
623 entry
->SetAccessTime(GetHeaderDate(_T("atime")));
624 entry
->SetCreateTime(GetHeaderDate(_T("ctime")));
626 entry
->SetTypeFlag(*m_hdr
->Get(TAR_TYPEFLAG
));
627 bool isDir
= entry
->IsDir();
629 entry
->SetLinkName(GetHeaderString(TAR_LINKNAME
));
631 if (m_tarType
!= TYPE_OLDTAR
) {
632 entry
->SetUserName(GetHeaderString(TAR_UNAME
));
633 entry
->SetGroupName(GetHeaderString(TAR_GNAME
));
635 entry
->SetDevMajor(GetHeaderNumber(TAR_DEVMAJOR
));
636 entry
->SetDevMinor(GetHeaderNumber(TAR_DEVMINOR
));
639 entry
->SetName(GetHeaderPath(), wxPATH_UNIX
);
644 m_HeaderRecs
->clear();
646 m_size
= GetDataSize(*entry
);
649 return entry
.release();
652 bool wxTarInputStream::OpenEntry(wxTarEntry
& entry
)
654 wxFileOffset offset
= entry
.GetOffset();
656 if (GetLastError() != wxSTREAM_READ_ERROR
657 && m_parent_i_stream
->IsSeekable()
658 && m_parent_i_stream
->SeekI(offset
) == offset
)
661 m_size
= GetDataSize(entry
);
663 m_lasterror
= wxSTREAM_NO_ERROR
;
666 m_lasterror
= wxSTREAM_READ_ERROR
;
671 bool wxTarInputStream::OpenEntry(wxArchiveEntry
& entry
)
673 wxTarEntry
*tarEntry
= wxStaticCast(&entry
, wxTarEntry
);
674 return tarEntry
? OpenEntry(*tarEntry
) : false;
677 bool wxTarInputStream::CloseEntry()
679 if (m_lasterror
== wxSTREAM_READ_ERROR
)
684 wxFileOffset size
= RoundUpSize(m_size
);
685 wxFileOffset remainder
= size
- m_pos
;
687 if (remainder
&& m_parent_i_stream
->IsSeekable()) {
689 if (m_parent_i_stream
->SeekI(remainder
, wxFromCurrent
)
695 const int BUFSIZE
= 8192;
696 wxCharBuffer
buf(BUFSIZE
);
698 while (remainder
> 0 && m_parent_i_stream
->IsOk())
699 remainder
-= m_parent_i_stream
->Read(
700 buf
.data(), wxMin(BUFSIZE
, remainder
)).LastRead();
703 m_pos
= wxInvalidOffset
;
705 m_lasterror
= m_parent_i_stream
->GetLastError();
710 wxStreamError
wxTarInputStream::ReadHeaders()
713 return wxSTREAM_READ_ERROR
;
718 m_hdr
->Read(*m_parent_i_stream
);
719 if (m_parent_i_stream
->Eof())
720 wxLogError(_("incomplete header block in tar"));
721 if (!*m_parent_i_stream
)
722 return wxSTREAM_READ_ERROR
;
723 m_offset
+= TAR_BLOCKSIZE
;
725 // an all-zero header marks the end of the tar
726 if (m_hdr
->IsAllZeros())
729 // the checksum is supposed to be the unsigned sum of the header bytes,
730 // but there have been versions of tar that used the signed sum, so
731 // accept that too, but only if used throughout.
732 wxUint32 chksum
= m_hdr
->GetOctal(TAR_CHKSUM
);
735 if (m_sumType
!= SUM_SIGNED
) {
736 ok
= chksum
== m_hdr
->Sum();
737 if (m_sumType
== SUM_UNKNOWN
)
738 m_sumType
= ok
? SUM_UNSIGNED
: SUM_SIGNED
;
740 if (m_sumType
== SUM_SIGNED
)
741 ok
= chksum
== m_hdr
->Sum(true);
743 wxLogError(_("checksum failure reading tar header block"));
744 return wxSTREAM_READ_ERROR
;
747 if (strcmp(m_hdr
->Get(TAR_MAGIC
), USTAR_MAGIC
) == 0)
748 m_tarType
= TYPE_USTAR
;
749 else if (strcmp(m_hdr
->Get(TAR_MAGIC
), GNU_MAGIC
) == 0 &&
750 strcmp(m_hdr
->Get(TAR_VERSION
), GNU_VERION
) == 0)
751 m_tarType
= TYPE_GNUTAR
;
753 m_tarType
= TYPE_OLDTAR
;
755 if (m_tarType
!= TYPE_USTAR
)
758 switch (*m_hdr
->Get(TAR_TYPEFLAG
)) {
759 case 'g': ReadExtendedHeader(m_GlobalHeaderRecs
); break;
760 case 'x': ReadExtendedHeader(m_HeaderRecs
); break;
761 default: done
= true;
765 return wxSTREAM_NO_ERROR
;
768 wxString
wxTarInputStream::GetExtendedHeader(const wxString
& key
) const
770 wxTarHeaderRecords::iterator it
;
772 // look at normal extended header records first
774 it
= m_HeaderRecs
->find(key
);
775 if (it
!= m_HeaderRecs
->end())
776 return wxString(it
->second
.wc_str(wxConvUTF8
), GetConv());
779 // if not found, look at the global header records
780 if (m_GlobalHeaderRecs
) {
781 it
= m_GlobalHeaderRecs
->find(key
);
782 if (it
!= m_GlobalHeaderRecs
->end())
783 return wxString(it
->second
.wc_str(wxConvUTF8
), GetConv());
786 return wxEmptyString
;
789 wxString
wxTarInputStream::GetHeaderPath() const
793 if ((path
= GetExtendedHeader(_T("path"))) != wxEmptyString
)
796 path
= wxString(m_hdr
->Get(TAR_NAME
), GetConv());
797 if (m_tarType
!= TYPE_USTAR
)
800 const char *prefix
= m_hdr
->Get(TAR_PREFIX
);
801 return *prefix
? wxString(prefix
, GetConv()) + _T("/") + path
: path
;
804 wxDateTime
wxTarInputStream::GetHeaderDate(const wxString
& key
) const
808 // try extended header, stored as decimal seconds since the epoch
809 if ((value
= GetExtendedHeader(key
)) != wxEmptyString
) {
811 ll
.Assign(wxAtof(value
) * 1000.0);
815 if (key
== _T("mtime"))
816 return wxLongLong(m_hdr
->GetOctal(TAR_MTIME
)) * 1000L;
821 wxTarNumber
wxTarInputStream::GetHeaderNumber(int id
) const
825 if ((value
= GetExtendedHeader(m_hdr
->Name(id
))) != wxEmptyString
) {
827 const wxChar
*p
= value
;
831 n
= n
* 10 + (*p
++ - '0');
834 return m_hdr
->GetOctal(id
);
838 wxString
wxTarInputStream::GetHeaderString(int id
) const
842 if ((value
= GetExtendedHeader(m_hdr
->Name(id
))) != wxEmptyString
)
845 return wxString(m_hdr
->Get(id
), GetConv());
848 // An extended header consists of one or more records, each constructed:
849 // "%d %s=%s\n", <length>, <keyword>, <value>
850 // <length> is the byte length, <keyword> and <value> are UTF-8
852 bool wxTarInputStream::ReadExtendedHeader(wxTarHeaderRecords
*& recs
)
855 recs
= new wxTarHeaderRecords
;
857 // round length up to a whole number of blocks
858 size_t len
= m_hdr
->GetOctal(TAR_SIZE
);
859 size_t size
= RoundUpSize(len
);
861 // read in the whole header since it should be small
862 wxCharBuffer
buf(size
);
863 size_t lastread
= m_parent_i_stream
->Read(buf
.data(), size
).LastRead();
867 m_offset
+= lastread
;
869 size_t recPos
, recSize
;
872 for (recPos
= 0; recPos
< len
; recPos
+= recSize
) {
873 char *pRec
= buf
.data() + recPos
;
876 // read the record size (byte count in ascii decimal)
879 recSize
= recSize
* 10 + *p
++ - '0';
882 if (recPos
+ recSize
> len
)
884 if (recSize
< p
- pRec
+ (size_t)3 || *p
!= ' '
885 || pRec
[recSize
- 1] != '\012') {
890 // replace the final '\n' with a nul, to terminate value
891 pRec
[recSize
- 1] = 0;
892 // the key is here, following the space
895 // look forward for the '=', the value follows
896 while (*p
&& *p
!= '=')
902 // replace the '=' with a nul, to terminate the key
905 wxString
key(wxConvUTF8
.cMB2WC(pKey
), GetConv());
906 wxString
value(wxConvUTF8
.cMB2WC(p
), GetConv());
908 // an empty value unsets a previously given value
912 (*recs
)[key
] = value
;
915 if (!ok
|| recPos
< len
|| size
!= lastread
) {
916 wxLogWarning(_("invalid data in extended tar header"));
923 wxFileOffset
wxTarInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
926 wxLogError(_("tar entry not open"));
927 m_lasterror
= wxSTREAM_READ_ERROR
;
930 return wxInvalidOffset
;
933 case wxFromStart
: break;
934 case wxFromCurrent
: pos
+= m_pos
; break;
935 case wxFromEnd
: pos
+= m_size
; break;
938 if (pos
< 0 || m_parent_i_stream
->SeekI(m_offset
+ pos
) == wxInvalidOffset
)
939 return wxInvalidOffset
;
945 size_t wxTarInputStream::OnSysRead(void *buffer
, size_t size
)
948 wxLogError(_("tar entry not open"));
949 m_lasterror
= wxSTREAM_READ_ERROR
;
951 if (!IsOk() || !size
)
956 else if (m_pos
+ size
> m_size
+ (size_t)0)
957 size
= m_size
- m_pos
;
959 size_t lastread
= m_parent_i_stream
->Read(buffer
, size
).LastRead();
962 if (m_pos
>= m_size
) {
963 m_lasterror
= wxSTREAM_EOF
;
964 } else if (!m_parent_i_stream
->IsOk()) {
965 // any other error will have been reported by the underlying stream
966 if (m_parent_i_stream
->Eof())
967 wxLogError(_("unexpected end of file"));
968 m_lasterror
= wxSTREAM_READ_ERROR
;
975 /////////////////////////////////////////////////////////////////////////////
978 wxTarOutputStream::wxTarOutputStream(wxOutputStream
& stream
,
979 wxTarFormat format
/*=wxTAR_PAX*/,
980 wxMBConv
& conv
/*=wxConvLocal*/)
981 : wxArchiveOutputStream(stream
, conv
)
986 wxTarOutputStream::wxTarOutputStream(wxOutputStream
*stream
,
987 wxTarFormat format
/*=wxTAR_PAX*/,
988 wxMBConv
& conv
/*=wxConvLocal*/)
989 : wxArchiveOutputStream(stream
, conv
)
994 void wxTarOutputStream::Init(wxTarFormat format
)
996 m_pos
= wxInvalidOffset
;
997 m_maxpos
= wxInvalidOffset
;
998 m_size
= wxInvalidOffset
;
999 m_headpos
= wxInvalidOffset
;
1000 m_datapos
= wxInvalidOffset
;
1001 m_tarstart
= wxInvalidOffset
;
1003 m_pax
= format
== wxTAR_PAX
;
1004 m_BlockingFactor
= m_pax
? 10 : 20;
1007 m_hdr
= new wxTarHeaderBlock
;
1009 m_extendedHdr
= NULL
;
1011 m_lasterror
= m_parent_o_stream
->GetLastError();
1014 wxTarOutputStream::~wxTarOutputStream()
1020 delete [] m_extendedHdr
;
1023 bool wxTarOutputStream::PutNextEntry(wxTarEntry
*entry
)
1025 wxTarEntryPtr_
e(entry
);
1032 m_tarstart
= m_parent_o_stream
->TellO();
1035 if (m_tarstart
!= wxInvalidOffset
)
1036 m_headpos
= m_tarstart
+ m_tarsize
;
1038 if (WriteHeaders(*e
)) {
1041 m_size
= GetDataSize(*e
);
1042 if (m_tarstart
!= wxInvalidOffset
)
1043 m_datapos
= m_tarstart
+ m_tarsize
;
1045 // types that are not allowd any data
1046 const char nodata
[] = {
1047 wxTAR_LNKTYPE
, wxTAR_SYMTYPE
, wxTAR_CHRTYPE
, wxTAR_BLKTYPE
,
1048 wxTAR_DIRTYPE
, wxTAR_FIFOTYPE
, 0
1050 char typeflag
= e
->GetTypeFlag();
1052 // pax does now allow data for wxTAR_LNKTYPE
1053 if (!m_pax
|| typeflag
- wxTAR_LNKTYPE
!= 0)
1054 if (strchr(nodata
, typeflag
) != NULL
)
1061 bool wxTarOutputStream::PutNextEntry(const wxString
& name
,
1062 const wxDateTime
& dt
,
1065 return PutNextEntry(new wxTarEntry(name
, dt
, size
));
1068 bool wxTarOutputStream::PutNextDirEntry(const wxString
& name
,
1069 const wxDateTime
& dt
)
1071 wxTarEntry
*entry
= new wxTarEntry(name
, dt
);
1073 return PutNextEntry(entry
);
1076 bool wxTarOutputStream::PutNextEntry(wxArchiveEntry
*entry
)
1078 wxTarEntry
*tarEntry
= wxStaticCast(entry
, wxTarEntry
);
1081 return PutNextEntry(tarEntry
);
1084 bool wxTarOutputStream::CopyEntry(wxTarEntry
*entry
,
1085 wxTarInputStream
& inputStream
)
1087 if (PutNextEntry(entry
))
1089 return IsOk() && inputStream
.Eof();
1092 bool wxTarOutputStream::CopyEntry(wxArchiveEntry
*entry
,
1093 wxArchiveInputStream
& inputStream
)
1095 if (PutNextEntry(entry
))
1097 return IsOk() && inputStream
.Eof();
1100 bool wxTarOutputStream::CloseEntry()
1105 if (m_pos
< m_maxpos
) {
1106 wxASSERT(m_parent_o_stream
->IsSeekable());
1107 m_parent_o_stream
->SeekO(m_datapos
+ m_maxpos
);
1108 m_lasterror
= m_parent_o_stream
->GetLastError();
1113 wxFileOffset size
= RoundUpSize(m_pos
);
1115 memset(m_hdr
, 0, size
- m_pos
);
1116 m_parent_o_stream
->Write(m_hdr
, size
- m_pos
);
1117 m_lasterror
= m_parent_o_stream
->GetLastError();
1122 if (IsOk() && m_pos
!= m_size
)
1125 m_pos
= wxInvalidOffset
;
1126 m_maxpos
= wxInvalidOffset
;
1127 m_size
= wxInvalidOffset
;
1128 m_headpos
= wxInvalidOffset
;
1129 m_datapos
= wxInvalidOffset
;
1134 bool wxTarOutputStream::Close()
1139 memset(m_hdr
, 0, sizeof(*m_hdr
));
1140 int count
= (RoundUpSize(m_tarsize
+ 2 * TAR_BLOCKSIZE
, m_BlockingFactor
)
1141 - m_tarsize
) / TAR_BLOCKSIZE
;
1143 m_parent_o_stream
->Write(m_hdr
, TAR_BLOCKSIZE
);
1146 m_tarstart
= wxInvalidOffset
;
1147 m_lasterror
= m_parent_o_stream
->GetLastError();
1151 bool wxTarOutputStream::WriteHeaders(wxTarEntry
& entry
)
1153 memset(m_hdr
, 0, sizeof(*m_hdr
));
1155 SetHeaderPath(entry
.GetName(wxPATH_UNIX
));
1157 SetHeaderNumber(TAR_MODE
, entry
.GetMode());
1158 SetHeaderNumber(TAR_UID
, entry
.GetUserId());
1159 SetHeaderNumber(TAR_GID
, entry
.GetGroupId());
1161 if (entry
.GetSize() == wxInvalidOffset
)
1163 m_large
= SetHeaderNumber(TAR_SIZE
, entry
.GetSize());
1165 SetHeaderDate(_T("mtime"), entry
.GetDateTime());
1166 if (entry
.GetAccessTime().IsValid())
1167 SetHeaderDate(_T("atime"), entry
.GetAccessTime());
1168 if (entry
.GetCreateTime().IsValid())
1169 SetHeaderDate(_T("ctime"), entry
.GetCreateTime());
1171 *m_hdr
->Get(TAR_TYPEFLAG
) = entry
.GetTypeFlag();
1173 strcpy(m_hdr
->Get(TAR_MAGIC
), USTAR_MAGIC
);
1174 strcpy(m_hdr
->Get(TAR_VERSION
), USTAR_VERSION
);
1176 SetHeaderString(TAR_LINKNAME
, entry
.GetLinkName());
1177 SetHeaderString(TAR_UNAME
, entry
.GetUserName());
1178 SetHeaderString(TAR_GNAME
, entry
.GetGroupName());
1180 if (~entry
.GetDevMajor())
1181 SetHeaderNumber(TAR_DEVMAJOR
, entry
.GetDevMajor());
1182 if (~entry
.GetDevMinor())
1183 SetHeaderNumber(TAR_DEVMINOR
, entry
.GetDevMinor());
1185 m_chksum
= m_hdr
->Sum();
1186 m_hdr
->SetOctal(TAR_CHKSUM
, m_chksum
);
1188 m_chksum
-= m_hdr
->SumField(TAR_SIZE
);
1190 // The main header is now fully prepared so we know what extended headers
1191 // (if any) will be needed. Output any extended headers before writing
1193 if (m_extendedHdr
&& *m_extendedHdr
) {
1195 // the extended headers are written to the tar as a file entry,
1196 // so prepare a regular header block for the pseudo-file.
1198 m_hdr2
= new wxTarHeaderBlock
;
1199 memset(m_hdr2
, 0, sizeof(*m_hdr2
));
1201 // an old tar that doesn't understand extended headers will
1202 // extract it as a file, so give these fields reasonable values
1203 // so that the user will have access to read and remove it.
1204 m_hdr2
->SetPath(PaxHeaderPath(_T("%d/PaxHeaders.%p/%f"),
1205 entry
.GetName(wxPATH_UNIX
)), GetConv());
1206 m_hdr2
->SetOctal(TAR_MODE
, 0600);
1207 strcpy(m_hdr2
->Get(TAR_UID
), m_hdr
->Get(TAR_UID
));
1208 strcpy(m_hdr2
->Get(TAR_GID
), m_hdr
->Get(TAR_GID
));
1209 size_t length
= strlen(m_extendedHdr
);
1210 m_hdr2
->SetOctal(TAR_SIZE
, length
);
1211 strcpy(m_hdr2
->Get(TAR_MTIME
), m_hdr
->Get(TAR_MTIME
));
1212 *m_hdr2
->Get(TAR_TYPEFLAG
) = 'x';
1213 strcpy(m_hdr2
->Get(TAR_MAGIC
), USTAR_MAGIC
);
1214 strcpy(m_hdr2
->Get(TAR_VERSION
), USTAR_VERSION
);
1215 strcpy(m_hdr2
->Get(TAR_UNAME
), m_hdr
->Get(TAR_UNAME
));
1216 strcpy(m_hdr2
->Get(TAR_GNAME
), m_hdr
->Get(TAR_GNAME
));
1218 m_hdr2
->SetOctal(TAR_CHKSUM
, m_hdr2
->Sum());
1220 m_hdr2
->Write(*m_parent_o_stream
);
1221 m_tarsize
+= TAR_BLOCKSIZE
;
1223 size_t rounded
= RoundUpSize(length
);
1224 memset(m_extendedHdr
+ length
, 0, rounded
- length
);
1225 m_parent_o_stream
->Write(m_extendedHdr
, rounded
);
1226 m_tarsize
+= rounded
;
1231 // if don't have extended headers just report error
1232 if (!m_badfit
.empty()) {
1234 wxLogWarning(_("%s did not fit the tar header for entry '%s'"),
1235 m_badfit
.c_str(), entry
.GetName().c_str());
1239 m_hdr
->Write(*m_parent_o_stream
);
1240 m_tarsize
+= TAR_BLOCKSIZE
;
1241 m_lasterror
= m_parent_o_stream
->GetLastError();
1246 wxString
wxTarOutputStream::PaxHeaderPath(const wxString
& format
,
1247 const wxString
& path
)
1249 wxString d
= path
.BeforeLast(_T('/'));
1250 wxString f
= path
.AfterLast(_T('/'));
1256 ret
.reserve(format
.length() + path
.length() + 16);
1262 end
= format
.find('%', begin
);
1263 if (end
== wxString::npos
|| end
+ 1 >= format
.length())
1265 ret
<< format
.substr(begin
, end
- begin
);
1266 switch (format
[end
+ 1]) {
1267 case 'd': ret
<< d
; break;
1268 case 'f': ret
<< f
; break;
1269 case 'p': ret
<< wxGetProcessId(); break;
1270 case '%': ret
<< _T("%"); break;
1275 ret
<< format
.substr(begin
);
1280 bool wxTarOutputStream::ModifyHeader()
1282 wxFileOffset originalPos
= wxInvalidOffset
;
1283 wxFileOffset sizePos
= wxInvalidOffset
;
1285 if (!m_large
&& m_headpos
!= wxInvalidOffset
1286 && m_parent_o_stream
->IsSeekable())
1289 originalPos
= m_parent_o_stream
->TellO();
1290 if (originalPos
!= wxInvalidOffset
)
1292 m_parent_o_stream
->SeekO(m_headpos
+ m_hdr
->Offset(TAR_SIZE
));
1295 if (sizePos
== wxInvalidOffset
|| !m_hdr
->SetOctal(TAR_SIZE
, m_pos
)) {
1296 wxLogError(_("incorrect size given for tar entry"));
1297 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1301 m_chksum
+= m_hdr
->SumField(TAR_SIZE
);
1302 m_hdr
->SetOctal(TAR_CHKSUM
, m_chksum
);
1303 wxFileOffset sumPos
= m_headpos
+ m_hdr
->Offset(TAR_CHKSUM
);
1306 m_hdr
->WriteField(*m_parent_o_stream
, TAR_SIZE
) &&
1307 m_parent_o_stream
->SeekO(sumPos
) == sumPos
&&
1308 m_hdr
->WriteField(*m_parent_o_stream
, TAR_CHKSUM
) &&
1309 m_parent_o_stream
->SeekO(originalPos
) == originalPos
;
1312 void wxTarOutputStream::SetHeaderPath(const wxString
& name
)
1314 if (!m_hdr
->SetPath(name
, GetConv()) || (m_pax
&& !name
.IsAscii()))
1315 SetExtendedHeader(_T("path"), name
);
1318 bool wxTarOutputStream::SetHeaderNumber(int id
, wxTarNumber n
)
1320 if (m_hdr
->SetOctal(id
, n
)) {
1323 SetExtendedHeader(m_hdr
->Name(id
), wxLongLong(n
).ToString());
1328 void wxTarOutputStream::SetHeaderString(int id
, const wxString
& str
)
1330 strncpy(m_hdr
->Get(id
), str
.mb_str(GetConv()), m_hdr
->Len(id
));
1331 if (str
.length() > m_hdr
->Len(id
))
1332 SetExtendedHeader(m_hdr
->Name(id
), str
);
1335 void wxTarOutputStream::SetHeaderDate(const wxString
& key
,
1336 const wxDateTime
& datetime
)
1338 wxLongLong ll
= datetime
.IsValid() ? datetime
.GetValue() : wxLongLong(0);
1339 wxLongLong secs
= ll
/ 1000L;
1341 if (key
!= _T("mtime") || !m_hdr
->SetOctal(TAR_MTIME
, secs
.GetValue())
1342 || secs
<= 0 || secs
>= 0x7fffffff)
1345 if (ll
>= LONG_MIN
&& ll
<= LONG_MAX
) {
1346 str
.Printf(_T("%g"), ll
.ToLong() / 1000.0);
1348 str
= ll
.ToString();
1349 str
.insert(str
.end() - 3, '.');
1351 SetExtendedHeader(key
, str
);
1355 void wxTarOutputStream::SetExtendedHeader(const wxString
& key
,
1356 const wxString
& value
)
1359 const wxWX2WCbuf wide_key
= key
.wc_str(GetConv());
1360 const wxCharBuffer utf_key
= wxConvUTF8
.cWC2MB(wide_key
);
1362 const wxWX2WCbuf wide_value
= value
.wc_str(GetConv());
1363 const wxCharBuffer utf_value
= wxConvUTF8
.cWC2MB(wide_value
);
1365 // a small buffer to format the length field in
1367 // length of "99<space><key>=<value>\n"
1368 unsigned long length
= strlen(utf_value
) + strlen(utf_key
) + 5;
1369 sprintf(buf
, "%lu", length
);
1370 // the length includes itself
1371 size_t lenlen
= strlen(buf
);
1373 length
+= lenlen
- 2;
1374 sprintf(buf
, "%lu", length
);
1375 if (strlen(buf
) > lenlen
)
1376 sprintf(buf
, "%lu", ++length
);
1379 // reallocate m_extendedHdr if it's not big enough
1380 if (m_extendedSize
< length
) {
1381 size_t rounded
= RoundUpSize(length
);
1382 m_extendedSize
<<= 1;
1383 if (rounded
> m_extendedSize
)
1384 m_extendedSize
= rounded
;
1385 char *oldHdr
= m_extendedHdr
;
1386 m_extendedHdr
= new char[m_extendedSize
];
1388 strcpy(m_extendedHdr
, oldHdr
);
1395 // append the new record
1396 char *append
= strchr(m_extendedHdr
, 0);
1397 sprintf(append
, "%s %s=%s\012", buf
,
1398 (const char*)utf_key
, (const char*)utf_value
);
1401 // if not pax then make a list of fields to report as errors
1402 if (!m_badfit
.empty())
1403 m_badfit
+= _T(", ");
1408 void wxTarOutputStream::Sync()
1410 m_parent_o_stream
->Sync();
1413 wxFileOffset
wxTarOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
1416 wxLogError(_("tar entry not open"));
1417 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1419 if (!IsOk() || m_datapos
== wxInvalidOffset
)
1420 return wxInvalidOffset
;
1423 case wxFromStart
: break;
1424 case wxFromCurrent
: pos
+= m_pos
; break;
1425 case wxFromEnd
: pos
+= m_maxpos
; break;
1428 if (pos
< 0 || m_parent_o_stream
->SeekO(m_datapos
+ pos
) == wxInvalidOffset
)
1429 return wxInvalidOffset
;
1435 size_t wxTarOutputStream::OnSysWrite(const void *buffer
, size_t size
)
1438 wxLogError(_("tar entry not open"));
1439 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1441 if (!IsOk() || !size
)
1444 size_t lastwrite
= m_parent_o_stream
->Write(buffer
, size
).LastWrite();
1446 if (m_pos
> m_maxpos
)
1449 if (lastwrite
!= size
)
1450 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1455 #endif // wxUSE_TARSTREAM