w1146869587
2021-11-05 cbf39419a8299e7d119618e5e8e1b1eb35f72f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <efsw/FileInfo.hpp>
#include <efsw/FileSystem.hpp>
#include <efsw/String.hpp>
 
#ifndef _DARWIN_FEATURE_64_BIT_INODE
#define _DARWIN_FEATURE_64_BIT_INODE
#endif
 
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
 
#include <sys/stat.h>
 
#include <limits.h>
#include <stdlib.h>
 
#ifdef EFSW_COMPILER_MSVC
    #ifndef S_ISDIR
    #define S_ISDIR(f) ((f)&_S_IFDIR)
    #endif
 
    #ifndef S_ISREG
    #define S_ISREG(f) ((f)&_S_IFREG)
    #endif
 
    #ifndef S_ISRDBL
    #define S_ISRDBL(f) ((f)&_S_IREAD)
    #endif
#else
    #include <unistd.h>
 
    #ifndef S_ISRDBL
    #define S_ISRDBL(f) ((f)&S_IRUSR)
    #endif
#endif
 
namespace efsw {
 
bool FileInfo::exists( const std::string& filePath )
{
    FileInfo fi( filePath );
    return fi.exists();
}
 
bool FileInfo::isLink( const std::string& filePath )
{
    FileInfo fi( filePath, true );
    return fi.isLink();
}
 
bool FileInfo::inodeSupported()
{
    #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32
    return true;
    #else
    return false;
    #endif
}
 
FileInfo::FileInfo() :
    ModificationTime(0),
    OwnerId(0),
    GroupId(0),
    Permissions(0),
    Inode(0)
{}
 
FileInfo::FileInfo( const std::string& filepath ) :
    Filepath( filepath ),
    ModificationTime(0),
    OwnerId(0),
    GroupId(0),
    Permissions(0),
    Inode(0)
{
    getInfo();
}
 
FileInfo::FileInfo( const std::string& filepath, bool linkInfo ) :
    Filepath( filepath ),
    ModificationTime(0),
    OwnerId(0),
    GroupId(0),
    Permissions(0),
    Inode(0)
{
    if ( linkInfo )
    {
        getRealInfo();
    }
    else
    {
        getInfo();
    }
}
 
void FileInfo::getInfo()
{
    #if EFSW_PLATFORM == EFSW_PLATFORM_WIN32
    if ( Filepath.size() == 3 && Filepath[1] == ':' && Filepath[2] == FileSystem::getOSSlash() )
    {
        Filepath += FileSystem::getOSSlash();
    }
    #endif
 
    /// Why i'm doing this? stat in mingw32 doesn't work for directories if the dir path ends with a path slash
    bool slashAtEnd = FileSystem::slashAtEnd( Filepath );
 
    if ( slashAtEnd )
    {
        FileSystem::dirRemoveSlashAtEnd( Filepath );
    }
 
    #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32
    struct stat st;
    int res = stat( Filepath.c_str(), &st );
    #else
    struct _stat st;
    int res = _wstat( String::fromUtf8( Filepath ).toWideString().c_str(), &st );
    #endif
 
    if ( 0 == res )
    {
        ModificationTime    = st.st_mtime;
        Size                = st.st_size;
        OwnerId                = st.st_uid;
        GroupId                = st.st_gid;
        Permissions            = st.st_mode;
        Inode                = st.st_ino;
    }
 
    if ( slashAtEnd )
    {
        FileSystem::dirAddSlashAtEnd( Filepath );
    }
}
 
void FileInfo::getRealInfo()
{
    bool slashAtEnd = FileSystem::slashAtEnd( Filepath );
 
    if ( slashAtEnd )
    {
        FileSystem::dirRemoveSlashAtEnd( Filepath );
    }
 
    #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32
    struct stat st;
    int res = lstat( Filepath.c_str(), &st );
    #else
    struct _stat st;
    int res = _wstat( String::fromUtf8( Filepath ).toWideString().c_str(), &st );
    #endif
 
    if ( 0 == res )
    {
        ModificationTime    = st.st_mtime;
        Size                = st.st_size;
        OwnerId                = st.st_uid;
        GroupId                = st.st_gid;
        Permissions            = st.st_mode;
        Inode                = st.st_ino;
    }
 
    if ( slashAtEnd )
    {
        FileSystem::dirAddSlashAtEnd( Filepath );
    }
}
 
bool FileInfo::operator==( const FileInfo& Other ) const
{
    return (    ModificationTime    == Other.ModificationTime &&
                Size                == Other.Size &&
                OwnerId                == Other.OwnerId &&
                GroupId                == Other.GroupId &&
                Permissions            == Other.Permissions &&
                Inode                == Other.Inode
    );
}
 
bool FileInfo::isDirectory() const
{
    return 0 != S_ISDIR(Permissions);
}
 
bool FileInfo::isRegularFile() const
{
    return 0 != S_ISREG(Permissions);
}
 
bool FileInfo::isReadable() const
{
#if EFSW_PLATFORM != EFSW_PLATFORM_WIN32
    static bool isRoot = getuid() == 0;
    return isRoot || 0 != S_ISRDBL(Permissions);
#else
    return 0 != S_ISRDBL(Permissions);
#endif
}
 
bool FileInfo::isLink() const
{
#if EFSW_PLATFORM != EFSW_PLATFORM_WIN32
    return S_ISLNK(Permissions);
#else
    return false;
#endif
}
 
std::string FileInfo::linksTo()
{
#if EFSW_PLATFORM != EFSW_PLATFORM_WIN32
    if ( isLink() )
    {
        char * ch = realpath( Filepath.c_str(), NULL);
 
        if ( NULL != ch )
        {
            std::string tstr( ch );
 
            free( ch );
 
            return tstr;
        }
    }
#endif
    return std::string("");
}
 
bool FileInfo::exists()
{
    bool slashAtEnd = FileSystem::slashAtEnd( Filepath );
 
    if ( slashAtEnd )
    {
        FileSystem::dirRemoveSlashAtEnd(Filepath);
    }
 
#if EFSW_PLATFORM != EFSW_PLATFORM_WIN32
    struct stat st;
    int res = stat( Filepath.c_str(), &st );
#else
    struct _stat st;
    int res = _wstat( String::fromUtf8( Filepath ).toWideString().c_str(), &st );
#endif
 
    if (slashAtEnd)
    {
        FileSystem::dirAddSlashAtEnd(Filepath);
    }
 
    return 0 == res;
}
 
FileInfo& FileInfo::operator=( const FileInfo& Other )
{
    this->Filepath            = Other.Filepath;
    this->Size                = Other.Size;
    this->ModificationTime    = Other.ModificationTime;
    this->GroupId            = Other.GroupId;
    this->OwnerId            = Other.OwnerId;
    this->Permissions        = Other.Permissions;
    this->Inode                = Other.Inode;
    return *this;
}
 
bool FileInfo::sameInode( const FileInfo& Other ) const
{
    return inodeSupported() && Inode == Other.Inode;
}
 
bool FileInfo::operator!=( const FileInfo& Other ) const
{
    return !(*this == Other);
}