NCBI C++ ToolKit
archive_.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: archive_.cpp 89844 2020-04-27 21:27:40Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Vladimir Ivanov, Anton Lavrentiev
27  *
28  * File Description:
29  * Compression archive API - Common base classes and interfaces.
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
35 #include <util/error_codes.hpp>
36 
37 
38 #if !defined(NCBI_OS_MSWIN) && !defined(NCBI_OS_UNIX)
39 # error "Class CArchive can be defined on MS-Windows and UNIX platforms only!"
40 #endif
41 
42 #if defined(NCBI_OS_UNIX)
43 # include <unistd.h>
44 # include <sys/types.h>
45 # ifdef NCBI_OS_DARWIN
46 // macOS supplies these as inline functions rather than macros.
47 # define major major
48 # define minor minor
49 # define makedev makedev
50 # endif
51 # if defined(HAVE_SYS_SYSMACROS_H)
52 # include <sys/sysmacros.h>
53 # endif
54 # if defined (NCBI_OS_IRIX)
55 # include <sys/mkdev.h>
56 # if !defined(major) || !defined(minor) || !defined(makedev)
57 # error "Device macros undefined in this UNIX build!"
58 # endif
59 # endif
60 #endif //NCBI_OS
61 
62 
63 #define NCBI_USE_ERRCODE_X Util_Compress
64 
65 
67 
68 
69 
70 /////////////////////////////////////////////////////////////////////////
71 //
72 // CArchiveEntryInfo
73 //
74 
76 {
77  // Raw mode gets returned here (as kept in the info)
78  return (m_Stat.st_mode & 07777);
79 }
80 
81 
83  CDirEntry::TMode* grp_mode,
84  CDirEntry::TMode* oth_mode,
85  CDirEntry::TSpecialModeBits* special_bits) const
86 {
87  CFile::ModeFromModeT(GetMode(), usr_mode, grp_mode, oth_mode, special_bits);
88 }
89 
90 
91 unsigned int CArchiveEntryInfo::GetMajor(void) const
92 {
93 #ifdef major
96  return major(m_Stat.st_rdev);
97  }
98 #else
99  if (sizeof(int) >= 4 && sizeof(m_Stat.st_rdev) >= 4) {
100  return (*((unsigned int*) &m_Stat.st_rdev) >> 16) & 0xFFFF;
101  }
102 #endif //major
103  return (unsigned int)(-1);
104 }
105 
106 
107 unsigned int CArchiveEntryInfo::GetMinor(void) const
108 {
109 #ifdef minor
112  return minor(m_Stat.st_rdev);
113  }
114 #else
115  if (sizeof(int) >= 4 && sizeof(m_Stat.st_rdev) >= 4) {
116  return *((unsigned int*) &m_Stat.st_rdev) & 0xFFFF;
117  }
118 #endif //minor
119  return (unsigned int)(-1);
120 }
121 
122 
124 {
125  return (m_Index == info.m_Index &&
126  m_Type == info.m_Type &&
127  m_Name == info.m_Name &&
128  m_LinkName == info.m_LinkName &&
129  m_UserName == info.m_UserName &&
130  m_GroupName == info.m_GroupName &&
131  memcmp(&m_Stat,&info.m_Stat, sizeof(m_Stat)) == 0);
132 }
133 
134 
136 {
137  switch (type) {
138  case CDirEntry::eFile:
139  return '-';
140  case CDirEntry::eDir:
141  return 'd';
142  case CDirEntry::eLink:
143  return 'l';
144  case CDirEntry::ePipe:
145  return 'p';
147  return 'c';
149  return 'b';
150  default:
151  break;
152  }
153  return '?';
154 }
155 
156 
158 {
159  string user(info.GetUserName());
160  if (user.empty()) {
161  NStr::UIntToString(user, info.GetUserId());
162  }
163  string group(info.GetGroupName());
164  if (group.empty()) {
165  NStr::UIntToString(group, info.GetGroupId());
166  }
167  return user + '/' + group;
168 }
169 
170 
171 static string s_MajorMinor(unsigned int n)
172 {
173  return n != (unsigned int)(-1) ? NStr::UIntToString(n) : "?";
174 }
175 
176 
178 {
179  if (info.GetType() == CDirEntry::eCharSpecial ||
180  info.GetType() == CDirEntry::eBlockSpecial) {
181  unsigned int major = info.GetMajor();
182  unsigned int minor = info.GetMinor();
183  return s_MajorMinor(major) + ',' + s_MajorMinor(minor);
184  } else if (info.GetType() == CDirEntry::eDir ||
185  info.GetType() == CDirEntry::eLink) {
186  return string("-");
187  }
188  return NStr::UInt8ToString(info.GetSize());
189 }
190 
191 
192 ostream& operator << (ostream& os, const CArchiveEntryInfo& info)
193 {
194  // Entry mode
195  CDirEntry::TMode usr, grp, oth;
197  CDirEntry::ModeFromModeT(info.GetMode(), &usr, &grp, &oth, &special);
198 
199  // Entry modification time
200  string mtime;
201  if (info.GetModificationTime()) {
202  CTime t(info.GetModificationTime());
203  mtime = t.ToLocalTime().AsString("Y-M-D h:m:s");
204  }
205 
206  os << s_TypeAsChar(info.GetType())
207  << CDirEntry::ModeToString(usr, grp, oth, special, CDirEntry::eModeFormat_List) << ' '
208  << setw(17) << s_UserGroupAsString(info) << ' '
209  << setw(10) << s_SizeOrMajorMinor(info) << ' '
210  << setw(19) << mtime << " "
211  << info.GetName();
212 
213  if (info.GetType() == CDirEntry::eLink) {
214  os << " -> " << info.GetLinkName();
215  }
216  return os;
217 }
218 
219 
static string s_UserGroupAsString(const CArchiveEntryInfo &info)
Definition: archive_.cpp:157
static string s_SizeOrMajorMinor(const CArchiveEntryInfo &info)
Definition: archive_.cpp:177
static string s_MajorMinor(unsigned int n)
Definition: archive_.cpp:171
static char s_TypeAsChar(CArchiveEntryInfo::EType type)
Definition: archive_.cpp:135
Archive API.
CArchiveEntryInfo class.
Definition: archive_.hpp:112
CTime –.
Definition: ncbitime.hpp:296
string
Definition: cgiapp.hpp:690
string m_GroupName
Group name.
Definition: archive_.hpp:166
bool operator==(const CArchiveEntryInfo &info) const
Definition: archive_.cpp:123
size_t m_Index
Entry index in the archive.
Definition: archive_.hpp:160
mode_t GetMode(void) const
Definition: archive_.cpp:75
CDirEntry::EType m_Type
Type.
Definition: archive_.hpp:162
string m_LinkName
Link name if type is eLink.
Definition: archive_.hpp:164
TNcbiSys_stat m_Stat
Direntry-compatible info (as applicable)
Definition: archive_.hpp:161
string m_Name
Entry name.
Definition: archive_.hpp:163
ostream & operator<<(ostream &os, const CArchiveEntryInfo &info)
Nice TOC (table of contents) printout.
Definition: archive_.cpp:192
unsigned int GetMinor(void) const
Definition: archive_.cpp:107
string m_UserName
User name.
Definition: archive_.hpp:165
unsigned int GetMajor(void) const
Definition: archive_.cpp:91
EType
Directory entry type.
Definition: ncbifile.hpp:783
unsigned int TSpecialModeBits
Bitwise OR of ESpecialModeBits.
Definition: ncbifile.hpp:1180
unsigned int TMode
Bitwise OR of "EMode".
Definition: ncbifile.hpp:1173
static string ModeToString(TMode user_mode, TMode group_mode, TMode other_mode, TSpecialModeBits special, EModeStringFormat format=eModeFormat_Default)
Convert permission modes to string representation using one of predefined formats.
Definition: ncbifile.cpp:1355
static void ModeFromModeT(mode_t mode, TMode *user_mode, TMode *group_mode=0, TMode *other_mode=0, TSpecialModeBits *special=0)
Convert mode_t to permission mode(s).
Definition: ncbifile.cpp:1246
unsigned int mode_t
Definition: ncbifile.hpp:84
@ eDir
Directory.
Definition: ncbifile.hpp:785
@ eFile
Regular file.
Definition: ncbifile.hpp:784
@ eBlockSpecial
Block special (UNIX only)
Definition: ncbifile.hpp:791
@ eLink
Symbolic link (UNIX only)
Definition: ncbifile.hpp:787
@ eCharSpecial
Character special.
Definition: ncbifile.hpp:792
@ ePipe
Pipe.
Definition: ncbifile.hpp:786
@ eModeFormat_List
Shell list 'ls -l' like format ("rwxrwxrwx")
Definition: ncbifile.hpp:1356
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string UIntToString(unsigned int value, TNumToStringFlags flags=0, int base=10)
Convert UInt to string.
Definition: ncbistr.hpp:5103
static string UInt8ToString(Uint8 value, TNumToStringFlags flags=0, int base=10)
Convert UInt8 to string.
Definition: ncbistr.hpp:5162
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
Definition of all error codes used in util (xutil.lib).
yy_size_t n
static MDB_envinfo info
Definition: mdb_load.c:37
EIPRangeType t
Definition: ncbi_localip.c:101
Definition: type.c:6
Modified on Fri Sep 20 14:57:35 2024 by modify_doxy.py rev. 669887