11#include " ItemStat.hpp"
2- #include " system.hpp"
32
43#include < boost/filesystem/operations.hpp>
54
6- #include < sys/types.h>
75#include < sys/stat.h>
6+ #include < pwd.h>
7+ #include < grp.h>
88
99
1010namespace bfs = boost::filesystem;
@@ -65,6 +65,45 @@ ItemStat::ItemStat( const Item& item, const bool approximative )
6565 }
6666}
6767
68+ std::string ItemStat::getUserName () const
69+ {
70+ #ifdef __UNIX__
71+ passwd* user = getpwuid (userId);
72+ if (user == NULL )
73+ return std::string (" unknown" );
74+ return std::string (user->pw_name ? user->pw_name : " unknown" );
75+ #else
76+ return std::string (" not implemented" );
77+ #endif
78+ }
79+
80+ std::string ItemStat::getGroupName () const
81+ {
82+ #ifdef __UNIX__
83+ group* group = getgrgid (groupId);
84+ if (group == NULL )
85+ return std::string (" unknown" );
86+ return std::string (group->gr_name ? group->gr_name : " unknown" );
87+ #else
88+ return std::string (" not implemented" );
89+ #endif
90+ }
91+
92+ #ifdef __UNIX__
93+ void ItemStat::setPermissions ( const mode_t & protection )
94+ {
95+ ownerCanRead = protection & S_IRUSR;
96+ ownerCanWrite = protection & S_IWUSR;
97+ ownerCanExecute = protection & S_IXUSR;
98+ groupCanRead = protection & S_IRGRP;
99+ groupCanWrite = protection & S_IWGRP;
100+ groupCanExecute = protection & S_IXGRP;
101+ otherCanRead = protection & S_IROTH;
102+ otherCanWrite = protection & S_IWOTH;
103+ otherCanExecute = protection & S_IXOTH;
104+ }
105+ #endif
106+
68107void ItemStat::statLink ( const boost::filesystem::path& path )
69108{
70109 boost::system::error_code errorCode;
@@ -87,8 +126,11 @@ void ItemStat::statLink( const boost::filesystem::path& path )
87126 accessTime = statInfos.st_atime ;
88127 creationTime = statInfos.st_ctime ;
89128 size = statInfos.st_size ;
129+ minSize = size;
130+ maxSize = size;
90131 // size on hard-drive (takes hardlinks into account)
91132 sizeOnDisk = (statInfos.st_blocks / nbHardLinks) * 512 ;
133+ setPermissions (statInfos.st_mode );
92134#else
93135 fullNbHardLinks = nbHardLinks = 1 ;
94136 deviceId = 0 ;
@@ -112,9 +154,20 @@ void ItemStat::setDefaultValues(){
112154 creationTime = 0 ;
113155 sizeOnDisk = 0 ;
114156 size = 0 ;
157+ minSize = 0 ;
158+ maxSize = 0 ;
115159 realSize = 0 ;
116160 fullNbHardLinks = 0 ;
117161 modificationTime = -1 ;
162+ ownerCanRead = false ;
163+ ownerCanWrite = false ;
164+ ownerCanExecute = false ;
165+ groupCanRead = false ;
166+ groupCanWrite = false ;
167+ groupCanExecute = false ;
168+ otherCanRead = false ;
169+ otherCanWrite = false ;
170+ otherCanExecute = false ;
118171}
119172
120173void ItemStat::statFolder ( const boost::filesystem::path& path )
@@ -141,8 +194,11 @@ void ItemStat::statFolder( const boost::filesystem::path& path )
141194 accessTime = statInfos.st_atime ;
142195 creationTime = statInfos.st_ctime ;
143196 size = statInfos.st_size ;
197+ minSize = size;
198+ maxSize = size;
144199 // size on hard-drive (takes hardlinks into account)
145200 sizeOnDisk = statInfos.st_blocks * 512 ;
201+ setPermissions (statInfos.st_mode );
146202#else
147203 deviceId = 0 ;
148204 inodeId = 0 ;
@@ -164,6 +220,8 @@ void ItemStat::statFile( const boost::filesystem::path& path )
164220 boost::system::error_code errorCode;
165221 fullNbHardLinks = nbHardLinks = bfs::hard_link_count ( path, errorCode );
166222 size = bfs::file_size ( path, errorCode );
223+ minSize = size;
224+ maxSize = size;
167225 modificationTime = bfs::last_write_time ( path, errorCode );
168226 int stat_status = -1 ;
169227
@@ -183,8 +241,8 @@ void ItemStat::statFile( const boost::filesystem::path& path )
183241 accessTime = statInfos.st_atime ;
184242 creationTime = statInfos.st_ctime ;
185243 // size on hard-drive (takes hardlinks into account)
186- sizeOnDisk = (statInfos.st_blocks / nbHardLinks) * 512 ;
187-
244+ sizeOnDisk = (statInfos.st_blocks / nbHardLinks) * 512 ;
245+ setPermissions (statInfos. st_mode );
188246#else
189247 deviceId = 0 ;
190248 inodeId = 0 ;
@@ -219,6 +277,7 @@ void ItemStat::statSequence( const Item& item, const bool approximative )
219277 userId = statInfos.st_uid ;
220278 groupId = statInfos.st_gid ;
221279 accessTime = statInfos.st_atime ;
280+ setPermissions (statInfos.st_mode );
222281#else
223282 deviceId = 0 ;
224283 inodeId = 0 ;
@@ -230,6 +289,8 @@ void ItemStat::statSequence( const Item& item, const bool approximative )
230289 modificationTime = 0 ;
231290 fullNbHardLinks = 0 ;
232291 size = 0 ;
292+ minSize = 0 ;
293+ maxSize = 0 ;
233294 realSize = 0 ;
234295 sizeOnDisk = 0 ;
235296 creationTime = 0 ;
@@ -250,14 +311,44 @@ void ItemStat::statSequence( const Item& item, const bool approximative )
250311
251312 ItemStat fileStat (type, filepath);
252313
314+ // use the most restrictive permissions in the sequence
315+ #ifdef __UNIX__
316+ // user
317+ if ( ownerCanRead && ! fileStat.ownerCanRead )
318+ ownerCanRead = false ;
319+ if ( ownerCanWrite && ! fileStat.ownerCanWrite )
320+ ownerCanWrite = false ;
321+ if ( ownerCanExecute && ! fileStat.ownerCanExecute )
322+ ownerCanExecute = false ;
323+ // group
324+ if ( groupCanRead && ! fileStat.groupCanRead )
325+ groupCanRead = false ;
326+ if ( groupCanWrite && ! fileStat.groupCanWrite )
327+ groupCanWrite = false ;
328+ if ( groupCanExecute && ! fileStat.groupCanExecute )
329+ groupCanExecute = false ;
330+ // other
331+ if ( otherCanRead && ! fileStat.otherCanRead )
332+ otherCanRead = false ;
333+ if ( otherCanWrite && ! fileStat.otherCanWrite )
334+ otherCanWrite = false ;
335+ if ( otherCanExecute && ! fileStat.otherCanExecute )
336+ otherCanExecute = false ;
337+ #endif
338+
253339 // use the latest modification date in the sequence
254340 if ( fileStat.modificationTime > modificationTime )
255341 modificationTime = fileStat.modificationTime ;
256342 if ( creationTime == 0 || creationTime > fileStat.creationTime )
257343 creationTime = fileStat.creationTime ;
258344
345+ // compute sizes
259346 fullNbHardLinks += fileStat.fullNbHardLinks ;
260347 size += fileStat.size ;
348+ if ( minSize == 0 || minSize > fileStat.size )
349+ minSize = fileStat.size ;
350+ if ( maxSize == 0 || maxSize < fileStat.size )
351+ maxSize = fileStat.size ;
261352 realSize += fileStat.realSize ;
262353 sizeOnDisk += fileStat.sizeOnDisk ;
263354 }
0 commit comments