X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5f77e766a3d9d90d2295612532a28fce7dbd2542..6e1609b1e0784f299933742e02c58462df809acc:/src/common/tarstrm.cpp diff --git a/src/common/tarstrm.cpp b/src/common/tarstrm.cpp index ea47b5bc1f..38020b7d25 100644 --- a/src/common/tarstrm.cpp +++ b/src/common/tarstrm.cpp @@ -28,10 +28,12 @@ #include "wx/datetime.h" #include "wx/ptr_scpd.h" #include "wx/filename.h" +#include "wx/thread.h" #include #ifdef __UNIX__ +#include #include #endif @@ -247,7 +249,7 @@ bool wxTarHeaderBlock::Write(wxOutputStream& out) return ok; } -bool wxTarHeaderBlock::WriteField(wxOutputStream& out, int id) +inline bool wxTarHeaderBlock::WriteField(wxOutputStream& out, int id) { return out.Write(Get(id), Len(id)).LastWrite() == Len(id); } @@ -270,7 +272,7 @@ bool wxTarHeaderBlock::SetOctal(int id, wxTarNumber n) char *p = field + Len(id); *--p = 0; while (p > field) { - *--p = '0' + (n & 7); + *--p = char('0' + (n & 7)); n >>= 3; } return n == 0; @@ -289,7 +291,10 @@ bool wxTarHeaderBlock::SetPath(const wxString& name, wxMBConv& conv) size_t len = name.length(); wxCharBuffer approx(len); for (size_t i = 0; i < len; i++) - approx.data()[i] = name[i] & ~0x7F ? '_' : name[i]; + { + wxChar c = name[i]; + approx.data()[i] = c & ~0x7F ? '_' : c; + } nameBuf = approx; } @@ -341,32 +346,99 @@ static wxFileOffset RoundUpSize(wxFileOffset size, int factor = 1) return ((size + chunk - 1) / chunk) * chunk; } -static wxString GroupName() -{ #ifdef __UNIX__ - group *gr; - if ((gr = getgrgid(getgid())) != NULL) - return wxString(gr->gr_name, wxConvLibc); + +static wxString wxTarUserName(int uid) +{ + struct passwd *ppw; + +#ifdef HAVE_GETPWUID_R +#if defined HAVE_SYSCONF && defined _SC_GETPW_R_SIZE_MAX + long pwsize = sysconf(_SC_GETPW_R_SIZE_MAX); + size_t bufsize(wxMin(wxMax(1024l, pwsize), 32768l)); +#else + size_t bufsize = 1024; +#endif + wxCharBuffer buf(bufsize); + struct passwd pw; + + if (getpwuid_r(uid, &pw, buf.data(), bufsize, &ppw) == 0) + return wxString(pw.pw_name, wxConvLibc); +#else + if ((ppw = getpwuid(uid)) != NULL) + return wxString(ppw->pw_name, wxConvLibc); #endif return _("unknown"); } -static inline int UserId() +static wxString wxTarGroupName(int gid) { -#ifdef __UNIX__ - return getuid(); + struct group *pgr; +#ifdef HAVE_GETGRGID_R +#if defined HAVE_SYSCONF && defined _SC_GETGR_R_SIZE_MAX + long grsize = sysconf(_SC_GETGR_R_SIZE_MAX); + size_t bufsize(wxMin(wxMax(1024l, grsize), 32768l)); +#else + size_t bufsize = 1024; +#endif + wxCharBuffer buf(bufsize); + struct group gr; + + if (getgrgid_r(gid, &gr, buf.data(), bufsize, &pgr) == 0) + return wxString(gr.gr_name, wxConvLibc); #else - return 0; + if ((pgr = getgrgid(gid)) != NULL) + return wxString(pgr->gr_name, wxConvLibc); #endif + return _("unknown"); } -static inline int GroupId() +#endif // __UNIX__ + +// Cache the user and group names since getting them can be expensive, +// get both names and ids at the same time. +// +struct wxTarUser +{ + wxTarUser(); + ~wxTarUser() { delete [] uname; delete [] gname; } + + int uid; + int gid; + + wxChar *uname; + wxChar *gname; +}; + +wxTarUser::wxTarUser() { #ifdef __UNIX__ - return getgid(); + uid = getuid(); + gid = getgid(); + wxString usr = wxTarUserName(uid); + wxString grp = wxTarGroupName(gid); #else - return 0; + uid = 0; + gid = 0; + wxString usr = wxGetUserId(); + wxString grp = _("unknown"); #endif + + uname = new wxChar[usr.length() + 1]; + wxStrcpy(uname, usr.c_str()); + + gname = new wxChar[grp.length() + 1]; + wxStrcpy(gname, grp.c_str()); +} + +static const wxTarUser& wxGetTarUser() +{ +#if wxUSE_THREADS + static wxCriticalSection cs; + wxCriticalSectionLocker lock(cs); +#endif + static wxTarUser tu; + return tu; } // ignore the size field for entry types 3, 4, 5 and 6 @@ -381,7 +453,7 @@ static inline wxFileOffset GetDataSize(const wxTarEntry& entry) return 0; default: return entry.GetSize(); - }; + } } @@ -394,14 +466,14 @@ wxTarEntry::wxTarEntry(const wxString& name /*=wxEmptyString*/, wxFileOffset size /*=0*/) : m_Mode(0644), m_IsModeSet(false), - m_UserId(UserId()), - m_GroupId(GroupId()), + m_UserId(wxGetTarUser().uid), + m_GroupId(wxGetTarUser().gid), m_Size(size), m_Offset(wxInvalidOffset), m_ModifyTime(dt), m_TypeFlag(wxTAR_REGTYPE), - m_UserName(wxGetUserId()), - m_GroupName(GroupName()), + m_UserName(wxGetTarUser().uname), + m_GroupName(wxGetTarUser().gname), m_DevMajor(~0), m_DevMinor(~0) { @@ -414,7 +486,8 @@ wxTarEntry::~wxTarEntry() } wxTarEntry::wxTarEntry(const wxTarEntry& e) - : m_Name(e.m_Name), + : wxArchiveEntry(), + m_Name(e.m_Name), m_Mode(e.m_Mode), m_IsModeSet(e.m_IsModeSet), m_UserId(e.m_UserId), @@ -825,8 +898,8 @@ wxTarNumber wxTarInputStream::GetHeaderNumber(int id) const if ((value = GetExtendedHeader(m_hdr->Name(id))) != wxEmptyString) { wxTarNumber n = 0; - const wxChar *p = value; - while (*p == ' ') + wxString::const_iterator p = value.begin(); + while (*p == ' ' && p != value.end()) p++; while (isdigit(*p)) n = n * 10 + (*p++ - '0'); @@ -1048,10 +1121,10 @@ bool wxTarOutputStream::PutNextEntry(wxTarEntry *entry) wxTAR_LNKTYPE, wxTAR_SYMTYPE, wxTAR_CHRTYPE, wxTAR_BLKTYPE, wxTAR_DIRTYPE, wxTAR_FIFOTYPE, 0 }; - char typeflag = e->GetTypeFlag(); + int typeflag = e->GetTypeFlag(); // pax does now allow data for wxTAR_LNKTYPE - if (!m_pax || typeflag - wxTAR_LNKTYPE != 0) + if (!m_pax || typeflag != wxTAR_LNKTYPE) if (strchr(nodata, typeflag) != NULL) CloseEntry(); } @@ -1161,7 +1234,7 @@ bool wxTarOutputStream::WriteHeaders(wxTarEntry& entry) if (entry.GetSize() == wxInvalidOffset) entry.SetSize(0); - m_large = SetHeaderNumber(TAR_SIZE, entry.GetSize()); + m_large = !SetHeaderNumber(TAR_SIZE, entry.GetSize()); SetHeaderDate(_T("mtime"), entry.GetDateTime()); if (entry.GetAccessTime().IsValid()) @@ -1169,7 +1242,7 @@ bool wxTarOutputStream::WriteHeaders(wxTarEntry& entry) if (entry.GetCreateTime().IsValid()) SetHeaderDate(_T("ctime"), entry.GetCreateTime()); - *m_hdr->Get(TAR_TYPEFLAG) = entry.GetTypeFlag(); + *m_hdr->Get(TAR_TYPEFLAG) = char(entry.GetTypeFlag()); strcpy(m_hdr->Get(TAR_MAGIC), USTAR_MAGIC); strcpy(m_hdr->Get(TAR_VERSION), USTAR_VERSION); @@ -1264,7 +1337,7 @@ wxString wxTarOutputStream::PaxHeaderPath(const wxString& format, if (end == wxString::npos || end + 1 >= format.length()) break; ret << format.substr(begin, end - begin); - switch (format[end + 1]) { + switch ( format[end + 1].GetValue() ) { case 'd': ret << d; break; case 'f': ret << f; break; case 'p': ret << wxGetProcessId(); break; @@ -1339,7 +1412,8 @@ void wxTarOutputStream::SetHeaderDate(const wxString& key, wxLongLong ll = datetime.IsValid() ? datetime.GetValue() : wxLongLong(0); wxLongLong secs = ll / 1000L; - if (key != _T("mtime") || !m_hdr->SetOctal(TAR_MTIME, secs.GetValue()) + if (key != _T("mtime") + || !m_hdr->SetOctal(TAR_MTIME, wxTarNumber(secs.GetValue())) || secs <= 0 || secs >= 0x7fffffff) { wxString str; @@ -1357,11 +1431,16 @@ void wxTarOutputStream::SetExtendedHeader(const wxString& key, const wxString& value) { if (m_pax) { +#if wxUSE_UNICODE + const wxCharBuffer utf_key = key.utf8_str(); + const wxCharBuffer utf_value = value.utf8_str(); +#else const wxWX2WCbuf wide_key = key.wc_str(GetConv()); const wxCharBuffer utf_key = wxConvUTF8.cWC2MB(wide_key); const wxWX2WCbuf wide_value = value.wc_str(GetConv()); const wxCharBuffer utf_value = wxConvUTF8.cWC2MB(wide_value); +#endif // wxUSE_UNICODE/!wxUSE_UNICODE // a small buffer to format the length field in char buf[32];