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"
27 #include "wx/buffer.h"
28 #include "wx/datetime.h"
29 #include "wx/ptr_scpd.h"
30 #include "wx/filename.h"
39 /////////////////////////////////////////////////////////////////////////////
76 TYPE_OLDTAR
, // fields after TAR_LINKNAME are invalid
77 TYPE_GNUTAR
, // all fields except TAR_PREFIX are valid
78 TYPE_USTAR
// all fields are valid
82 static const char *USTAR_MAGIC
= "ustar";
83 static const char *USTAR_VERSION
= "00";
84 static const char *GNU_MAGIC
= "ustar ";
85 static const char *GNU_VERION
= " ";
87 IMPLEMENT_DYNAMIC_CLASS(wxTarEntry
, wxArchiveEntry
)
88 IMPLEMENT_DYNAMIC_CLASS(wxTarClassFactory
, wxArchiveClassFactory
)
91 /////////////////////////////////////////////////////////////////////////////
94 static wxTarClassFactory g_wxTarClassFactory
;
96 wxTarClassFactory::wxTarClassFactory()
98 if (this == &g_wxTarClassFactory
)
102 const wxChar
* const *
103 wxTarClassFactory::GetProtocols(wxStreamProtocolType type
) const
105 static const wxChar
*protocols
[] = { _T("tar"), NULL
};
106 static const wxChar
*mimetypes
[] = { _T("application/x-tar"), NULL
};
107 static const wxChar
*fileexts
[] = { _T(".tar"), NULL
};
108 static const wxChar
*empty
[] = { NULL
};
111 case wxSTREAM_PROTOCOL
: return protocols
;
112 case wxSTREAM_MIMETYPE
: return mimetypes
;
113 case wxSTREAM_FILEEXT
: return fileexts
;
114 default: return empty
;
119 /////////////////////////////////////////////////////////////////////////////
122 typedef wxFileOffset wxTarNumber
;
124 struct wxTarField
{ const wxChar
*name
; int pos
; };
126 class wxTarHeaderBlock
130 { memset(data
, 0, sizeof(data
)); }
131 wxTarHeaderBlock(const wxTarHeaderBlock
& hb
)
132 { memcpy(data
, hb
.data
, sizeof(data
)); }
134 bool Read(wxInputStream
& in
);
135 bool Write(wxOutputStream
& out
);
136 inline bool WriteField(wxOutputStream
& out
, int id
);
138 bool IsAllZeros() const;
139 wxUint32
Sum(bool SignedSum
= false);
140 wxUint32
SumField(int id
);
142 char *Get(int id
) { return data
+ fields
[id
].pos
+ id
; }
143 static size_t Len(int id
) { return fields
[id
+ 1].pos
- fields
[id
].pos
; }
144 static const wxChar
*Name(int id
) { return fields
[id
].name
; }
145 static size_t Offset(int id
) { return fields
[id
].pos
; }
147 bool SetOctal(int id
, wxTarNumber n
);
148 wxTarNumber
GetOctal(int id
);
149 bool SetPath(const wxString
& name
, wxMBConv
& conv
);
152 char data
[TAR_BLOCKSIZE
+ TAR_NUMFIELDS
];
153 static const wxTarField fields
[];
157 wxDEFINE_SCOPED_PTR_TYPE(wxTarHeaderBlock
)
159 // A table giving the field names and offsets in a tar header block
160 const wxTarField
wxTarHeaderBlock::fields
[] =
162 { _T("name"), 0 }, // 100
163 { _T("mode"), 100 }, // 8
164 { _T("uid"), 108 }, // 8
165 { _T("gid"), 116 }, // 8
166 { _T("size"), 124 }, // 12
167 { _T("mtime"), 136 }, // 12
168 { _T("chksum"), 148 }, // 8
169 { _T("typeflag"), 156 }, // 1
170 { _T("linkname"), 157 }, // 100
171 { _T("magic"), 257 }, // 6
172 { _T("version"), 263 }, // 2
173 { _T("uname"), 265 }, // 32
174 { _T("gname"), 297 }, // 32
175 { _T("devmajor"), 329 }, // 8
176 { _T("devminor"), 337 }, // 8
177 { _T("prefix"), 345 }, // 155
178 { _T("unused"), 500 }, // 12
179 { NULL
, TAR_BLOCKSIZE
}
182 void wxTarHeaderBlock::check()
185 wxCOMPILE_TIME_ASSERT(
186 WXSIZEOF(fields
) == TAR_NUMFIELDS
+ 1,
187 Wrong_number_of_elements_in_fields_table
192 bool wxTarHeaderBlock::IsAllZeros() const
194 const char *p
= data
;
195 for (size_t i
= 0; i
< sizeof(data
); i
++)
201 wxUint32
wxTarHeaderBlock::Sum(bool SignedSum
/*=false*/)
203 // the chksum field itself should be blanks during the calculation
204 memset(Get(TAR_CHKSUM
), ' ', Len(TAR_CHKSUM
));
205 const char *p
= data
;
209 for (size_t i
= 0; i
< sizeof(data
); i
++)
210 n
+= (signed char)p
[i
];
212 for (size_t i
= 0; i
< sizeof(data
); i
++)
213 n
+= (unsigned char)p
[i
];
218 wxUint32
wxTarHeaderBlock::SumField(int id
)
220 unsigned char *p
= (unsigned char*)Get(id
);
221 unsigned char *q
= p
+ Len(id
);
230 bool wxTarHeaderBlock::Read(wxInputStream
& in
)
234 for (int id
= 0; id
< TAR_NUMFIELDS
&& ok
; id
++)
235 ok
= in
.Read(Get(id
), Len(id
)).LastRead() == Len(id
);
240 bool wxTarHeaderBlock::Write(wxOutputStream
& out
)
244 for (int id
= 0; id
< TAR_NUMFIELDS
&& ok
; id
++)
245 ok
= WriteField(out
, id
);
250 inline bool wxTarHeaderBlock::WriteField(wxOutputStream
& out
, int id
)
252 return out
.Write(Get(id
), Len(id
)).LastWrite() == Len(id
);
255 wxTarNumber
wxTarHeaderBlock::GetOctal(int id
)
258 const char *p
= Get(id
);
261 while (*p
>= '0' && *p
< '8')
262 n
= (n
<< 3) | (*p
++ - '0');
266 bool wxTarHeaderBlock::SetOctal(int id
, wxTarNumber n
)
268 // set an octal field, return true if the number fits
269 char *field
= Get(id
);
270 char *p
= field
+ Len(id
);
273 *--p
= char('0' + (n
& 7));
279 bool wxTarHeaderBlock::SetPath(const wxString
& name
, wxMBConv
& conv
)
281 bool badconv
= false;
284 wxCharBuffer nameBuf
= name
.mb_str(conv
);
286 // if the conversion fails make an approximation
289 size_t len
= name
.length();
290 wxCharBuffer
approx(len
);
291 for (size_t i
= 0; i
< len
; i
++)
294 approx
.data()[i
] = c
& ~0x7F ? '_' : c
;
299 const char *mbName
= nameBuf
;
301 const char *mbName
= name
.c_str();
306 bool notGoingToFit
= false;
307 size_t len
= strlen(mbName
);
308 size_t maxname
= Len(TAR_NAME
);
309 size_t maxprefix
= Len(TAR_PREFIX
);
314 fits
= i
< maxprefix
&& len
- i
<= maxname
;
317 const char *p
= strchr(mbName
+ i
, '/');
319 nexti
= p
- mbName
+ 1;
320 if (!p
|| nexti
- 1 > maxprefix
)
321 notGoingToFit
= true;
324 if (fits
|| notGoingToFit
) {
325 strncpy(Get(TAR_NAME
), mbName
+ i
, maxname
);
327 strncpy(Get(TAR_PREFIX
), mbName
, i
- 1);
334 return fits
&& !badconv
;
338 /////////////////////////////////////////////////////////////////////////////
341 static wxFileOffset
RoundUpSize(wxFileOffset size
, int factor
= 1)
343 wxFileOffset chunk
= TAR_BLOCKSIZE
* factor
;
344 return ((size
+ chunk
- 1) / chunk
) * chunk
;
347 static wxString
GroupName()
351 if ((gr
= getgrgid(getgid())) != NULL
)
352 return wxString(gr
->gr_name
, wxConvLibc
);
357 static inline int UserId()
366 static inline int GroupId()
375 // ignore the size field for entry types 3, 4, 5 and 6
377 static inline wxFileOffset
GetDataSize(const wxTarEntry
& entry
)
379 switch (entry
.GetTypeFlag()) {
386 return entry
.GetSize();
391 /////////////////////////////////////////////////////////////////////////////
393 // Holds all the meta-data for a file in the tar
395 wxTarEntry::wxTarEntry(const wxString
& name
/*=wxEmptyString*/,
396 const wxDateTime
& dt
/*=wxDateTime::Now()*/,
397 wxFileOffset size
/*=0*/)
401 m_GroupId(GroupId()),
403 m_Offset(wxInvalidOffset
),
405 m_TypeFlag(wxTAR_REGTYPE
),
406 m_UserName(wxGetUserId()),
407 m_GroupName(GroupName()),
415 wxTarEntry::~wxTarEntry()
419 wxTarEntry::wxTarEntry(const wxTarEntry
& e
)
423 m_IsModeSet(e
.m_IsModeSet
),
424 m_UserId(e
.m_UserId
),
425 m_GroupId(e
.m_GroupId
),
427 m_Offset(e
.m_Offset
),
428 m_ModifyTime(e
.m_ModifyTime
),
429 m_AccessTime(e
.m_AccessTime
),
430 m_CreateTime(e
.m_CreateTime
),
431 m_TypeFlag(e
.m_TypeFlag
),
432 m_LinkName(e
.m_LinkName
),
433 m_UserName(e
.m_UserName
),
434 m_GroupName(e
.m_GroupName
),
435 m_DevMajor(e
.m_DevMajor
),
436 m_DevMinor(e
.m_DevMinor
)
440 wxTarEntry
& wxTarEntry::operator=(const wxTarEntry
& e
)
445 m_IsModeSet
= e
.m_IsModeSet
;
446 m_UserId
= e
.m_UserId
;
447 m_GroupId
= e
.m_GroupId
;
449 m_Offset
= e
.m_Offset
;
450 m_ModifyTime
= e
.m_ModifyTime
;
451 m_AccessTime
= e
.m_AccessTime
;
452 m_CreateTime
= e
.m_CreateTime
;
453 m_TypeFlag
= e
.m_TypeFlag
;
454 m_LinkName
= e
.m_LinkName
;
455 m_UserName
= e
.m_UserName
;
456 m_GroupName
= e
.m_GroupName
;
457 m_DevMajor
= e
.m_DevMajor
;
458 m_DevMinor
= e
.m_DevMinor
;
463 wxString
wxTarEntry::GetName(wxPathFormat format
/*=wxPATH_NATIVE*/) const
465 bool isDir
= IsDir() && !m_Name
.empty();
467 // optimisations for common (and easy) cases
468 switch (wxFileName::GetFormat(format
)) {
471 wxString
name(isDir
? m_Name
+ _T("\\") : m_Name
);
472 for (size_t i
= 0; i
< name
.length(); i
++)
473 if (name
[i
] == _T('/'))
479 return isDir
? m_Name
+ _T("/") : m_Name
;
488 fn
.AssignDir(m_Name
, wxPATH_UNIX
);
490 fn
.Assign(m_Name
, wxPATH_UNIX
);
492 return fn
.GetFullPath(format
);
495 void wxTarEntry::SetName(const wxString
& name
, wxPathFormat format
)
498 m_Name
= GetInternalName(name
, format
, &isDir
);
502 // Static - Internally tars and zips use forward slashes for the path
503 // separator, absolute paths aren't allowed, and directory names have a
504 // trailing slash. This function converts a path into this internal format,
505 // but without a trailing slash for a directory.
507 wxString
wxTarEntry::GetInternalName(const wxString
& name
,
508 wxPathFormat format
/*=wxPATH_NATIVE*/,
509 bool *pIsDir
/*=NULL*/)
513 if (wxFileName::GetFormat(format
) != wxPATH_UNIX
)
514 internal
= wxFileName(name
, format
).GetFullPath(wxPATH_UNIX
);
518 bool isDir
= !internal
.empty() && internal
.Last() == '/';
522 internal
.erase(internal
.length() - 1);
524 while (!internal
.empty() && *internal
.begin() == '/')
525 internal
.erase(0, 1);
526 while (!internal
.empty() && internal
.compare(0, 2, _T("./")) == 0)
527 internal
.erase(0, 2);
528 if (internal
== _T(".") || internal
== _T(".."))
529 internal
= wxEmptyString
;
534 bool wxTarEntry::IsDir() const
536 return m_TypeFlag
== wxTAR_DIRTYPE
;
539 void wxTarEntry::SetIsDir(bool isDir
)
542 m_TypeFlag
= wxTAR_DIRTYPE
;
543 else if (m_TypeFlag
== wxTAR_DIRTYPE
)
544 m_TypeFlag
= wxTAR_REGTYPE
;
547 void wxTarEntry::SetIsReadOnly(bool isReadOnly
)
555 int wxTarEntry::GetMode() const
557 if (m_IsModeSet
|| !IsDir())
560 return m_Mode
| 0111;
564 void wxTarEntry::SetMode(int mode
)
566 m_Mode
= mode
& 07777;
571 /////////////////////////////////////////////////////////////////////////////
574 wxDECLARE_SCOPED_PTR(wxTarEntry
, wxTarEntryPtr_
)
575 wxDEFINE_SCOPED_PTR (wxTarEntry
, wxTarEntryPtr_
)
577 wxTarInputStream::wxTarInputStream(wxInputStream
& stream
,
578 wxMBConv
& conv
/*=wxConvLocal*/)
579 : wxArchiveInputStream(stream
, conv
)
584 wxTarInputStream::wxTarInputStream(wxInputStream
*stream
,
585 wxMBConv
& conv
/*=wxConvLocal*/)
586 : wxArchiveInputStream(stream
, conv
)
591 void wxTarInputStream::Init()
593 m_pos
= wxInvalidOffset
;
595 m_size
= wxInvalidOffset
;
596 m_sumType
= SUM_UNKNOWN
;
597 m_tarType
= TYPE_USTAR
;
598 m_hdr
= new wxTarHeaderBlock
;
600 m_GlobalHeaderRecs
= NULL
;
601 m_lasterror
= m_parent_i_stream
->GetLastError();
604 wxTarInputStream::~wxTarInputStream()
608 delete m_GlobalHeaderRecs
;
611 wxTarEntry
*wxTarInputStream::GetNextEntry()
613 m_lasterror
= ReadHeaders();
618 wxTarEntryPtr_
entry(new wxTarEntry
);
620 entry
->SetMode(GetHeaderNumber(TAR_MODE
));
621 entry
->SetUserId(GetHeaderNumber(TAR_UID
));
622 entry
->SetGroupId(GetHeaderNumber(TAR_UID
));
623 entry
->SetSize(GetHeaderNumber(TAR_SIZE
));
625 entry
->SetOffset(m_offset
);
627 entry
->SetDateTime(GetHeaderDate(_T("mtime")));
628 entry
->SetAccessTime(GetHeaderDate(_T("atime")));
629 entry
->SetCreateTime(GetHeaderDate(_T("ctime")));
631 entry
->SetTypeFlag(*m_hdr
->Get(TAR_TYPEFLAG
));
632 bool isDir
= entry
->IsDir();
634 entry
->SetLinkName(GetHeaderString(TAR_LINKNAME
));
636 if (m_tarType
!= TYPE_OLDTAR
) {
637 entry
->SetUserName(GetHeaderString(TAR_UNAME
));
638 entry
->SetGroupName(GetHeaderString(TAR_GNAME
));
640 entry
->SetDevMajor(GetHeaderNumber(TAR_DEVMAJOR
));
641 entry
->SetDevMinor(GetHeaderNumber(TAR_DEVMINOR
));
644 entry
->SetName(GetHeaderPath(), wxPATH_UNIX
);
649 m_HeaderRecs
->clear();
651 m_size
= GetDataSize(*entry
);
654 return entry
.release();
657 bool wxTarInputStream::OpenEntry(wxTarEntry
& entry
)
659 wxFileOffset offset
= entry
.GetOffset();
661 if (GetLastError() != wxSTREAM_READ_ERROR
662 && m_parent_i_stream
->IsSeekable()
663 && m_parent_i_stream
->SeekI(offset
) == offset
)
666 m_size
= GetDataSize(entry
);
668 m_lasterror
= wxSTREAM_NO_ERROR
;
671 m_lasterror
= wxSTREAM_READ_ERROR
;
676 bool wxTarInputStream::OpenEntry(wxArchiveEntry
& entry
)
678 wxTarEntry
*tarEntry
= wxStaticCast(&entry
, wxTarEntry
);
679 return tarEntry
? OpenEntry(*tarEntry
) : false;
682 bool wxTarInputStream::CloseEntry()
684 if (m_lasterror
== wxSTREAM_READ_ERROR
)
689 wxFileOffset size
= RoundUpSize(m_size
);
690 wxFileOffset remainder
= size
- m_pos
;
692 if (remainder
&& m_parent_i_stream
->IsSeekable()) {
694 if (m_parent_i_stream
->SeekI(remainder
, wxFromCurrent
)
700 const int BUFSIZE
= 8192;
701 wxCharBuffer
buf(BUFSIZE
);
703 while (remainder
> 0 && m_parent_i_stream
->IsOk())
704 remainder
-= m_parent_i_stream
->Read(
705 buf
.data(), wxMin(BUFSIZE
, remainder
)).LastRead();
708 m_pos
= wxInvalidOffset
;
710 m_lasterror
= m_parent_i_stream
->GetLastError();
715 wxStreamError
wxTarInputStream::ReadHeaders()
718 return wxSTREAM_READ_ERROR
;
723 m_hdr
->Read(*m_parent_i_stream
);
724 if (m_parent_i_stream
->Eof())
725 wxLogError(_("incomplete header block in tar"));
726 if (!*m_parent_i_stream
)
727 return wxSTREAM_READ_ERROR
;
728 m_offset
+= TAR_BLOCKSIZE
;
730 // an all-zero header marks the end of the tar
731 if (m_hdr
->IsAllZeros())
734 // the checksum is supposed to be the unsigned sum of the header bytes,
735 // but there have been versions of tar that used the signed sum, so
736 // accept that too, but only if used throughout.
737 wxUint32 chksum
= m_hdr
->GetOctal(TAR_CHKSUM
);
740 if (m_sumType
!= SUM_SIGNED
) {
741 ok
= chksum
== m_hdr
->Sum();
742 if (m_sumType
== SUM_UNKNOWN
)
743 m_sumType
= ok
? SUM_UNSIGNED
: SUM_SIGNED
;
745 if (m_sumType
== SUM_SIGNED
)
746 ok
= chksum
== m_hdr
->Sum(true);
748 wxLogError(_("checksum failure reading tar header block"));
749 return wxSTREAM_READ_ERROR
;
752 if (strcmp(m_hdr
->Get(TAR_MAGIC
), USTAR_MAGIC
) == 0)
753 m_tarType
= TYPE_USTAR
;
754 else if (strcmp(m_hdr
->Get(TAR_MAGIC
), GNU_MAGIC
) == 0 &&
755 strcmp(m_hdr
->Get(TAR_VERSION
), GNU_VERION
) == 0)
756 m_tarType
= TYPE_GNUTAR
;
758 m_tarType
= TYPE_OLDTAR
;
760 if (m_tarType
!= TYPE_USTAR
)
763 switch (*m_hdr
->Get(TAR_TYPEFLAG
)) {
764 case 'g': ReadExtendedHeader(m_GlobalHeaderRecs
); break;
765 case 'x': ReadExtendedHeader(m_HeaderRecs
); break;
766 default: done
= true;
770 return wxSTREAM_NO_ERROR
;
773 wxString
wxTarInputStream::GetExtendedHeader(const wxString
& key
) const
775 wxTarHeaderRecords::iterator it
;
777 // look at normal extended header records first
779 it
= m_HeaderRecs
->find(key
);
780 if (it
!= m_HeaderRecs
->end())
781 return wxString(it
->second
.wc_str(wxConvUTF8
), GetConv());
784 // if not found, look at the global header records
785 if (m_GlobalHeaderRecs
) {
786 it
= m_GlobalHeaderRecs
->find(key
);
787 if (it
!= m_GlobalHeaderRecs
->end())
788 return wxString(it
->second
.wc_str(wxConvUTF8
), GetConv());
791 return wxEmptyString
;
794 wxString
wxTarInputStream::GetHeaderPath() const
798 if ((path
= GetExtendedHeader(_T("path"))) != wxEmptyString
)
801 path
= wxString(m_hdr
->Get(TAR_NAME
), GetConv());
802 if (m_tarType
!= TYPE_USTAR
)
805 const char *prefix
= m_hdr
->Get(TAR_PREFIX
);
806 return *prefix
? wxString(prefix
, GetConv()) + _T("/") + path
: path
;
809 wxDateTime
wxTarInputStream::GetHeaderDate(const wxString
& key
) const
813 // try extended header, stored as decimal seconds since the epoch
814 if ((value
= GetExtendedHeader(key
)) != wxEmptyString
) {
816 ll
.Assign(wxAtof(value
) * 1000.0);
820 if (key
== _T("mtime"))
821 return wxLongLong(m_hdr
->GetOctal(TAR_MTIME
)) * 1000L;
826 wxTarNumber
wxTarInputStream::GetHeaderNumber(int id
) const
830 if ((value
= GetExtendedHeader(m_hdr
->Name(id
))) != wxEmptyString
) {
832 const wxChar
*p
= value
;
836 n
= n
* 10 + (*p
++ - '0');
839 return m_hdr
->GetOctal(id
);
843 wxString
wxTarInputStream::GetHeaderString(int id
) const
847 if ((value
= GetExtendedHeader(m_hdr
->Name(id
))) != wxEmptyString
)
850 return wxString(m_hdr
->Get(id
), GetConv());
853 // An extended header consists of one or more records, each constructed:
854 // "%d %s=%s\n", <length>, <keyword>, <value>
855 // <length> is the byte length, <keyword> and <value> are UTF-8
857 bool wxTarInputStream::ReadExtendedHeader(wxTarHeaderRecords
*& recs
)
860 recs
= new wxTarHeaderRecords
;
862 // round length up to a whole number of blocks
863 size_t len
= m_hdr
->GetOctal(TAR_SIZE
);
864 size_t size
= RoundUpSize(len
);
866 // read in the whole header since it should be small
867 wxCharBuffer
buf(size
);
868 size_t lastread
= m_parent_i_stream
->Read(buf
.data(), size
).LastRead();
872 m_offset
+= lastread
;
874 size_t recPos
, recSize
;
877 for (recPos
= 0; recPos
< len
; recPos
+= recSize
) {
878 char *pRec
= buf
.data() + recPos
;
881 // read the record size (byte count in ascii decimal)
884 recSize
= recSize
* 10 + *p
++ - '0';
887 if (recPos
+ recSize
> len
)
889 if (recSize
< p
- pRec
+ (size_t)3 || *p
!= ' '
890 || pRec
[recSize
- 1] != '\012') {
895 // replace the final '\n' with a nul, to terminate value
896 pRec
[recSize
- 1] = 0;
897 // the key is here, following the space
900 // look forward for the '=', the value follows
901 while (*p
&& *p
!= '=')
907 // replace the '=' with a nul, to terminate the key
910 wxString
key(wxConvUTF8
.cMB2WC(pKey
), GetConv());
911 wxString
value(wxConvUTF8
.cMB2WC(p
), GetConv());
913 // an empty value unsets a previously given value
917 (*recs
)[key
] = value
;
920 if (!ok
|| recPos
< len
|| size
!= lastread
) {
921 wxLogWarning(_("invalid data in extended tar header"));
928 wxFileOffset
wxTarInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
931 wxLogError(_("tar entry not open"));
932 m_lasterror
= wxSTREAM_READ_ERROR
;
935 return wxInvalidOffset
;
938 case wxFromStart
: break;
939 case wxFromCurrent
: pos
+= m_pos
; break;
940 case wxFromEnd
: pos
+= m_size
; break;
943 if (pos
< 0 || m_parent_i_stream
->SeekI(m_offset
+ pos
) == wxInvalidOffset
)
944 return wxInvalidOffset
;
950 size_t wxTarInputStream::OnSysRead(void *buffer
, size_t size
)
953 wxLogError(_("tar entry not open"));
954 m_lasterror
= wxSTREAM_READ_ERROR
;
956 if (!IsOk() || !size
)
961 else if (m_pos
+ size
> m_size
+ (size_t)0)
962 size
= m_size
- m_pos
;
964 size_t lastread
= m_parent_i_stream
->Read(buffer
, size
).LastRead();
967 if (m_pos
>= m_size
) {
968 m_lasterror
= wxSTREAM_EOF
;
969 } else if (!m_parent_i_stream
->IsOk()) {
970 // any other error will have been reported by the underlying stream
971 if (m_parent_i_stream
->Eof())
972 wxLogError(_("unexpected end of file"));
973 m_lasterror
= wxSTREAM_READ_ERROR
;
980 /////////////////////////////////////////////////////////////////////////////
983 wxTarOutputStream::wxTarOutputStream(wxOutputStream
& stream
,
984 wxTarFormat format
/*=wxTAR_PAX*/,
985 wxMBConv
& conv
/*=wxConvLocal*/)
986 : wxArchiveOutputStream(stream
, conv
)
991 wxTarOutputStream::wxTarOutputStream(wxOutputStream
*stream
,
992 wxTarFormat format
/*=wxTAR_PAX*/,
993 wxMBConv
& conv
/*=wxConvLocal*/)
994 : wxArchiveOutputStream(stream
, conv
)
999 void wxTarOutputStream::Init(wxTarFormat format
)
1001 m_pos
= wxInvalidOffset
;
1002 m_maxpos
= wxInvalidOffset
;
1003 m_size
= wxInvalidOffset
;
1004 m_headpos
= wxInvalidOffset
;
1005 m_datapos
= wxInvalidOffset
;
1006 m_tarstart
= wxInvalidOffset
;
1008 m_pax
= format
== wxTAR_PAX
;
1009 m_BlockingFactor
= m_pax
? 10 : 20;
1012 m_hdr
= new wxTarHeaderBlock
;
1014 m_extendedHdr
= NULL
;
1016 m_lasterror
= m_parent_o_stream
->GetLastError();
1019 wxTarOutputStream::~wxTarOutputStream()
1025 delete [] m_extendedHdr
;
1028 bool wxTarOutputStream::PutNextEntry(wxTarEntry
*entry
)
1030 wxTarEntryPtr_
e(entry
);
1037 m_tarstart
= m_parent_o_stream
->TellO();
1040 if (m_tarstart
!= wxInvalidOffset
)
1041 m_headpos
= m_tarstart
+ m_tarsize
;
1043 if (WriteHeaders(*e
)) {
1046 m_size
= GetDataSize(*e
);
1047 if (m_tarstart
!= wxInvalidOffset
)
1048 m_datapos
= m_tarstart
+ m_tarsize
;
1050 // types that are not allowd any data
1051 const char nodata
[] = {
1052 wxTAR_LNKTYPE
, wxTAR_SYMTYPE
, wxTAR_CHRTYPE
, wxTAR_BLKTYPE
,
1053 wxTAR_DIRTYPE
, wxTAR_FIFOTYPE
, 0
1055 int typeflag
= e
->GetTypeFlag();
1057 // pax does now allow data for wxTAR_LNKTYPE
1058 if (!m_pax
|| typeflag
!= wxTAR_LNKTYPE
)
1059 if (strchr(nodata
, typeflag
) != NULL
)
1066 bool wxTarOutputStream::PutNextEntry(const wxString
& name
,
1067 const wxDateTime
& dt
,
1070 return PutNextEntry(new wxTarEntry(name
, dt
, size
));
1073 bool wxTarOutputStream::PutNextDirEntry(const wxString
& name
,
1074 const wxDateTime
& dt
)
1076 wxTarEntry
*entry
= new wxTarEntry(name
, dt
);
1078 return PutNextEntry(entry
);
1081 bool wxTarOutputStream::PutNextEntry(wxArchiveEntry
*entry
)
1083 wxTarEntry
*tarEntry
= wxStaticCast(entry
, wxTarEntry
);
1086 return PutNextEntry(tarEntry
);
1089 bool wxTarOutputStream::CopyEntry(wxTarEntry
*entry
,
1090 wxTarInputStream
& inputStream
)
1092 if (PutNextEntry(entry
))
1094 return IsOk() && inputStream
.Eof();
1097 bool wxTarOutputStream::CopyEntry(wxArchiveEntry
*entry
,
1098 wxArchiveInputStream
& inputStream
)
1100 if (PutNextEntry(entry
))
1102 return IsOk() && inputStream
.Eof();
1105 bool wxTarOutputStream::CloseEntry()
1110 if (m_pos
< m_maxpos
) {
1111 wxASSERT(m_parent_o_stream
->IsSeekable());
1112 m_parent_o_stream
->SeekO(m_datapos
+ m_maxpos
);
1113 m_lasterror
= m_parent_o_stream
->GetLastError();
1118 wxFileOffset size
= RoundUpSize(m_pos
);
1120 memset(m_hdr
, 0, size
- m_pos
);
1121 m_parent_o_stream
->Write(m_hdr
, size
- m_pos
);
1122 m_lasterror
= m_parent_o_stream
->GetLastError();
1127 if (IsOk() && m_pos
!= m_size
)
1130 m_pos
= wxInvalidOffset
;
1131 m_maxpos
= wxInvalidOffset
;
1132 m_size
= wxInvalidOffset
;
1133 m_headpos
= wxInvalidOffset
;
1134 m_datapos
= wxInvalidOffset
;
1139 bool wxTarOutputStream::Close()
1144 memset(m_hdr
, 0, sizeof(*m_hdr
));
1145 int count
= (RoundUpSize(m_tarsize
+ 2 * TAR_BLOCKSIZE
, m_BlockingFactor
)
1146 - m_tarsize
) / TAR_BLOCKSIZE
;
1148 m_parent_o_stream
->Write(m_hdr
, TAR_BLOCKSIZE
);
1151 m_tarstart
= wxInvalidOffset
;
1152 m_lasterror
= m_parent_o_stream
->GetLastError();
1156 bool wxTarOutputStream::WriteHeaders(wxTarEntry
& entry
)
1158 memset(m_hdr
, 0, sizeof(*m_hdr
));
1160 SetHeaderPath(entry
.GetName(wxPATH_UNIX
));
1162 SetHeaderNumber(TAR_MODE
, entry
.GetMode());
1163 SetHeaderNumber(TAR_UID
, entry
.GetUserId());
1164 SetHeaderNumber(TAR_GID
, entry
.GetGroupId());
1166 if (entry
.GetSize() == wxInvalidOffset
)
1168 m_large
= !SetHeaderNumber(TAR_SIZE
, entry
.GetSize());
1170 SetHeaderDate(_T("mtime"), entry
.GetDateTime());
1171 if (entry
.GetAccessTime().IsValid())
1172 SetHeaderDate(_T("atime"), entry
.GetAccessTime());
1173 if (entry
.GetCreateTime().IsValid())
1174 SetHeaderDate(_T("ctime"), entry
.GetCreateTime());
1176 *m_hdr
->Get(TAR_TYPEFLAG
) = char(entry
.GetTypeFlag());
1178 strcpy(m_hdr
->Get(TAR_MAGIC
), USTAR_MAGIC
);
1179 strcpy(m_hdr
->Get(TAR_VERSION
), USTAR_VERSION
);
1181 SetHeaderString(TAR_LINKNAME
, entry
.GetLinkName());
1182 SetHeaderString(TAR_UNAME
, entry
.GetUserName());
1183 SetHeaderString(TAR_GNAME
, entry
.GetGroupName());
1185 if (~entry
.GetDevMajor())
1186 SetHeaderNumber(TAR_DEVMAJOR
, entry
.GetDevMajor());
1187 if (~entry
.GetDevMinor())
1188 SetHeaderNumber(TAR_DEVMINOR
, entry
.GetDevMinor());
1190 m_chksum
= m_hdr
->Sum();
1191 m_hdr
->SetOctal(TAR_CHKSUM
, m_chksum
);
1193 m_chksum
-= m_hdr
->SumField(TAR_SIZE
);
1195 // The main header is now fully prepared so we know what extended headers
1196 // (if any) will be needed. Output any extended headers before writing
1198 if (m_extendedHdr
&& *m_extendedHdr
) {
1200 // the extended headers are written to the tar as a file entry,
1201 // so prepare a regular header block for the pseudo-file.
1203 m_hdr2
= new wxTarHeaderBlock
;
1204 memset(m_hdr2
, 0, sizeof(*m_hdr2
));
1206 // an old tar that doesn't understand extended headers will
1207 // extract it as a file, so give these fields reasonable values
1208 // so that the user will have access to read and remove it.
1209 m_hdr2
->SetPath(PaxHeaderPath(_T("%d/PaxHeaders.%p/%f"),
1210 entry
.GetName(wxPATH_UNIX
)), GetConv());
1211 m_hdr2
->SetOctal(TAR_MODE
, 0600);
1212 strcpy(m_hdr2
->Get(TAR_UID
), m_hdr
->Get(TAR_UID
));
1213 strcpy(m_hdr2
->Get(TAR_GID
), m_hdr
->Get(TAR_GID
));
1214 size_t length
= strlen(m_extendedHdr
);
1215 m_hdr2
->SetOctal(TAR_SIZE
, length
);
1216 strcpy(m_hdr2
->Get(TAR_MTIME
), m_hdr
->Get(TAR_MTIME
));
1217 *m_hdr2
->Get(TAR_TYPEFLAG
) = 'x';
1218 strcpy(m_hdr2
->Get(TAR_MAGIC
), USTAR_MAGIC
);
1219 strcpy(m_hdr2
->Get(TAR_VERSION
), USTAR_VERSION
);
1220 strcpy(m_hdr2
->Get(TAR_UNAME
), m_hdr
->Get(TAR_UNAME
));
1221 strcpy(m_hdr2
->Get(TAR_GNAME
), m_hdr
->Get(TAR_GNAME
));
1223 m_hdr2
->SetOctal(TAR_CHKSUM
, m_hdr2
->Sum());
1225 m_hdr2
->Write(*m_parent_o_stream
);
1226 m_tarsize
+= TAR_BLOCKSIZE
;
1228 size_t rounded
= RoundUpSize(length
);
1229 memset(m_extendedHdr
+ length
, 0, rounded
- length
);
1230 m_parent_o_stream
->Write(m_extendedHdr
, rounded
);
1231 m_tarsize
+= rounded
;
1236 // if don't have extended headers just report error
1237 if (!m_badfit
.empty()) {
1239 wxLogWarning(_("%s did not fit the tar header for entry '%s'"),
1240 m_badfit
.c_str(), entry
.GetName().c_str());
1244 m_hdr
->Write(*m_parent_o_stream
);
1245 m_tarsize
+= TAR_BLOCKSIZE
;
1246 m_lasterror
= m_parent_o_stream
->GetLastError();
1251 wxString
wxTarOutputStream::PaxHeaderPath(const wxString
& format
,
1252 const wxString
& path
)
1254 wxString d
= path
.BeforeLast(_T('/'));
1255 wxString f
= path
.AfterLast(_T('/'));
1261 ret
.reserve(format
.length() + path
.length() + 16);
1267 end
= format
.find('%', begin
);
1268 if (end
== wxString::npos
|| end
+ 1 >= format
.length())
1270 ret
<< format
.substr(begin
, end
- begin
);
1271 switch ( format
[end
+ 1].GetValue() ) {
1272 case 'd': ret
<< d
; break;
1273 case 'f': ret
<< f
; break;
1274 case 'p': ret
<< wxGetProcessId(); break;
1275 case '%': ret
<< _T("%"); break;
1280 ret
<< format
.substr(begin
);
1285 bool wxTarOutputStream::ModifyHeader()
1287 wxFileOffset originalPos
= wxInvalidOffset
;
1288 wxFileOffset sizePos
= wxInvalidOffset
;
1290 if (!m_large
&& m_headpos
!= wxInvalidOffset
1291 && m_parent_o_stream
->IsSeekable())
1294 originalPos
= m_parent_o_stream
->TellO();
1295 if (originalPos
!= wxInvalidOffset
)
1297 m_parent_o_stream
->SeekO(m_headpos
+ m_hdr
->Offset(TAR_SIZE
));
1300 if (sizePos
== wxInvalidOffset
|| !m_hdr
->SetOctal(TAR_SIZE
, m_pos
)) {
1301 wxLogError(_("incorrect size given for tar entry"));
1302 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1306 m_chksum
+= m_hdr
->SumField(TAR_SIZE
);
1307 m_hdr
->SetOctal(TAR_CHKSUM
, m_chksum
);
1308 wxFileOffset sumPos
= m_headpos
+ m_hdr
->Offset(TAR_CHKSUM
);
1311 m_hdr
->WriteField(*m_parent_o_stream
, TAR_SIZE
) &&
1312 m_parent_o_stream
->SeekO(sumPos
) == sumPos
&&
1313 m_hdr
->WriteField(*m_parent_o_stream
, TAR_CHKSUM
) &&
1314 m_parent_o_stream
->SeekO(originalPos
) == originalPos
;
1317 void wxTarOutputStream::SetHeaderPath(const wxString
& name
)
1319 if (!m_hdr
->SetPath(name
, GetConv()) || (m_pax
&& !name
.IsAscii()))
1320 SetExtendedHeader(_T("path"), name
);
1323 bool wxTarOutputStream::SetHeaderNumber(int id
, wxTarNumber n
)
1325 if (m_hdr
->SetOctal(id
, n
)) {
1328 SetExtendedHeader(m_hdr
->Name(id
), wxLongLong(n
).ToString());
1333 void wxTarOutputStream::SetHeaderString(int id
, const wxString
& str
)
1335 strncpy(m_hdr
->Get(id
), str
.mb_str(GetConv()), m_hdr
->Len(id
));
1336 if (str
.length() > m_hdr
->Len(id
))
1337 SetExtendedHeader(m_hdr
->Name(id
), str
);
1340 void wxTarOutputStream::SetHeaderDate(const wxString
& key
,
1341 const wxDateTime
& datetime
)
1343 wxLongLong ll
= datetime
.IsValid() ? datetime
.GetValue() : wxLongLong(0);
1344 wxLongLong secs
= ll
/ 1000L;
1346 if (key
!= _T("mtime")
1347 || !m_hdr
->SetOctal(TAR_MTIME
, wxTarNumber(secs
.GetValue()))
1348 || secs
<= 0 || secs
>= 0x7fffffff)
1351 if (ll
>= LONG_MIN
&& ll
<= LONG_MAX
) {
1352 str
.Printf(_T("%g"), ll
.ToLong() / 1000.0);
1354 str
= ll
.ToString();
1355 str
.insert(str
.end() - 3, '.');
1357 SetExtendedHeader(key
, str
);
1361 void wxTarOutputStream::SetExtendedHeader(const wxString
& key
,
1362 const wxString
& value
)
1365 const wxWX2WCbuf wide_key
= key
.wc_str(GetConv());
1366 const wxCharBuffer utf_key
= wxConvUTF8
.cWC2MB(wide_key
);
1368 const wxWX2WCbuf wide_value
= value
.wc_str(GetConv());
1369 const wxCharBuffer utf_value
= wxConvUTF8
.cWC2MB(wide_value
);
1371 // a small buffer to format the length field in
1373 // length of "99<space><key>=<value>\n"
1374 unsigned long length
= strlen(utf_value
) + strlen(utf_key
) + 5;
1375 sprintf(buf
, "%lu", length
);
1376 // the length includes itself
1377 size_t lenlen
= strlen(buf
);
1379 length
+= lenlen
- 2;
1380 sprintf(buf
, "%lu", length
);
1381 if (strlen(buf
) > lenlen
)
1382 sprintf(buf
, "%lu", ++length
);
1385 // reallocate m_extendedHdr if it's not big enough
1386 if (m_extendedSize
< length
) {
1387 size_t rounded
= RoundUpSize(length
);
1388 m_extendedSize
<<= 1;
1389 if (rounded
> m_extendedSize
)
1390 m_extendedSize
= rounded
;
1391 char *oldHdr
= m_extendedHdr
;
1392 m_extendedHdr
= new char[m_extendedSize
];
1394 strcpy(m_extendedHdr
, oldHdr
);
1401 // append the new record
1402 char *append
= strchr(m_extendedHdr
, 0);
1403 sprintf(append
, "%s %s=%s\012", buf
,
1404 (const char*)utf_key
, (const char*)utf_value
);
1407 // if not pax then make a list of fields to report as errors
1408 if (!m_badfit
.empty())
1409 m_badfit
+= _T(", ");
1414 void wxTarOutputStream::Sync()
1416 m_parent_o_stream
->Sync();
1419 wxFileOffset
wxTarOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
1422 wxLogError(_("tar entry not open"));
1423 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1425 if (!IsOk() || m_datapos
== wxInvalidOffset
)
1426 return wxInvalidOffset
;
1429 case wxFromStart
: break;
1430 case wxFromCurrent
: pos
+= m_pos
; break;
1431 case wxFromEnd
: pos
+= m_maxpos
; break;
1434 if (pos
< 0 || m_parent_o_stream
->SeekO(m_datapos
+ pos
) == wxInvalidOffset
)
1435 return wxInvalidOffset
;
1441 size_t wxTarOutputStream::OnSysWrite(const void *buffer
, size_t size
)
1444 wxLogError(_("tar entry not open"));
1445 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1447 if (!IsOk() || !size
)
1450 size_t lastwrite
= m_parent_o_stream
->Write(buffer
, size
).LastWrite();
1452 if (m_pos
> m_maxpos
)
1455 if (lastwrite
!= size
)
1456 m_lasterror
= wxSTREAM_WRITE_ERROR
;
1461 #endif // wxUSE_TARSTREAM