diff options
| author | Li Jin <dragon-fly@qq.com> | 2022-11-15 17:23:46 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2022-11-15 17:52:09 +0800 |
| commit | 94f8330613877b3582d32bd11abd83a97b4399ad (patch) | |
| tree | 5359de314be1ebde17f8d1e48632a97d18f9e50f /src/3rdParty/efsw/FileInfo.cpp | |
| parent | 60f8f00a022ac08701792b2897b72d8c99b50f52 (diff) | |
| download | yuescript-94f8330613877b3582d32bd11abd83a97b4399ad.tar.gz yuescript-94f8330613877b3582d32bd11abd83a97b4399ad.tar.bz2 yuescript-94f8330613877b3582d32bd11abd83a97b4399ad.zip | |
adding -w option to Yuescript tool.
Diffstat (limited to 'src/3rdParty/efsw/FileInfo.cpp')
| -rwxr-xr-x | src/3rdParty/efsw/FileInfo.cpp | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/src/3rdParty/efsw/FileInfo.cpp b/src/3rdParty/efsw/FileInfo.cpp new file mode 100755 index 0000000..707f617 --- /dev/null +++ b/src/3rdParty/efsw/FileInfo.cpp | |||
| @@ -0,0 +1,240 @@ | |||
| 1 | #include <efsw/FileInfo.hpp> | ||
| 2 | #include <efsw/FileSystem.hpp> | ||
| 3 | #include <efsw/String.hpp> | ||
| 4 | |||
| 5 | #ifndef _DARWIN_FEATURE_64_BIT_INODE | ||
| 6 | #define _DARWIN_FEATURE_64_BIT_INODE | ||
| 7 | #endif | ||
| 8 | |||
| 9 | #ifndef _FILE_OFFSET_BITS | ||
| 10 | #define _FILE_OFFSET_BITS 64 | ||
| 11 | #endif | ||
| 12 | |||
| 13 | #include <sys/stat.h> | ||
| 14 | |||
| 15 | #include <limits.h> | ||
| 16 | #include <stdlib.h> | ||
| 17 | |||
| 18 | #ifdef EFSW_COMPILER_MSVC | ||
| 19 | #ifndef S_ISDIR | ||
| 20 | #define S_ISDIR( f ) ( (f)&_S_IFDIR ) | ||
| 21 | #endif | ||
| 22 | |||
| 23 | #ifndef S_ISREG | ||
| 24 | #define S_ISREG( f ) ( (f)&_S_IFREG ) | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #ifndef S_ISRDBL | ||
| 28 | #define S_ISRDBL( f ) ( (f)&_S_IREAD ) | ||
| 29 | #endif | ||
| 30 | #else | ||
| 31 | #include <unistd.h> | ||
| 32 | |||
| 33 | #ifndef S_ISRDBL | ||
| 34 | #define S_ISRDBL( f ) ( (f)&S_IRUSR ) | ||
| 35 | #endif | ||
| 36 | #endif | ||
| 37 | |||
| 38 | namespace efsw { | ||
| 39 | |||
| 40 | bool FileInfo::exists( const std::string& filePath ) { | ||
| 41 | FileInfo fi( filePath ); | ||
| 42 | return fi.exists(); | ||
| 43 | } | ||
| 44 | |||
| 45 | bool FileInfo::isLink( const std::string& filePath ) { | ||
| 46 | FileInfo fi( filePath, true ); | ||
| 47 | return fi.isLink(); | ||
| 48 | } | ||
| 49 | |||
| 50 | bool FileInfo::inodeSupported() { | ||
| 51 | #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32 | ||
| 52 | return true; | ||
| 53 | #else | ||
| 54 | return false; | ||
| 55 | #endif | ||
| 56 | } | ||
| 57 | |||
| 58 | FileInfo::FileInfo() : | ||
| 59 | ModificationTime( 0 ), OwnerId( 0 ), GroupId( 0 ), Permissions( 0 ), Inode( 0 ) {} | ||
| 60 | |||
| 61 | FileInfo::FileInfo( const std::string& filepath ) : | ||
| 62 | Filepath( filepath ), | ||
| 63 | ModificationTime( 0 ), | ||
| 64 | OwnerId( 0 ), | ||
| 65 | GroupId( 0 ), | ||
| 66 | Permissions( 0 ), | ||
| 67 | Inode( 0 ) { | ||
| 68 | getInfo(); | ||
| 69 | } | ||
| 70 | |||
| 71 | FileInfo::FileInfo( const std::string& filepath, bool linkInfo ) : | ||
| 72 | Filepath( filepath ), | ||
| 73 | ModificationTime( 0 ), | ||
| 74 | OwnerId( 0 ), | ||
| 75 | GroupId( 0 ), | ||
| 76 | Permissions( 0 ), | ||
| 77 | Inode( 0 ) { | ||
| 78 | if ( linkInfo ) { | ||
| 79 | getRealInfo(); | ||
| 80 | } else { | ||
| 81 | getInfo(); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | void FileInfo::getInfo() { | ||
| 86 | #if EFSW_PLATFORM == EFSW_PLATFORM_WIN32 | ||
| 87 | if ( Filepath.size() == 3 && Filepath[1] == ':' && Filepath[2] == FileSystem::getOSSlash() ) { | ||
| 88 | Filepath += FileSystem::getOSSlash(); | ||
| 89 | } | ||
| 90 | #endif | ||
| 91 | |||
| 92 | /// Why i'm doing this? stat in mingw32 doesn't work for directories if the dir path ends with a | ||
| 93 | /// path slash | ||
| 94 | bool slashAtEnd = FileSystem::slashAtEnd( Filepath ); | ||
| 95 | |||
| 96 | if ( slashAtEnd ) { | ||
| 97 | FileSystem::dirRemoveSlashAtEnd( Filepath ); | ||
| 98 | } | ||
| 99 | |||
| 100 | #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32 | ||
| 101 | struct stat st; | ||
| 102 | int res = stat( Filepath.c_str(), &st ); | ||
| 103 | #else | ||
| 104 | struct _stat st; | ||
| 105 | int res = _wstat( String::fromUtf8( Filepath ).toWideString().c_str(), &st ); | ||
| 106 | #endif | ||
| 107 | |||
| 108 | if ( 0 == res ) { | ||
| 109 | ModificationTime = st.st_mtime; | ||
| 110 | Size = st.st_size; | ||
| 111 | OwnerId = st.st_uid; | ||
| 112 | GroupId = st.st_gid; | ||
| 113 | Permissions = st.st_mode; | ||
| 114 | Inode = st.st_ino; | ||
| 115 | } | ||
| 116 | |||
| 117 | if ( slashAtEnd ) { | ||
| 118 | FileSystem::dirAddSlashAtEnd( Filepath ); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | void FileInfo::getRealInfo() { | ||
| 123 | bool slashAtEnd = FileSystem::slashAtEnd( Filepath ); | ||
| 124 | |||
| 125 | if ( slashAtEnd ) { | ||
| 126 | FileSystem::dirRemoveSlashAtEnd( Filepath ); | ||
| 127 | } | ||
| 128 | |||
| 129 | #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32 | ||
| 130 | struct stat st; | ||
| 131 | int res = lstat( Filepath.c_str(), &st ); | ||
| 132 | #else | ||
| 133 | struct _stat st; | ||
| 134 | int res = _wstat( String::fromUtf8( Filepath ).toWideString().c_str(), &st ); | ||
| 135 | #endif | ||
| 136 | |||
| 137 | if ( 0 == res ) { | ||
| 138 | ModificationTime = st.st_mtime; | ||
| 139 | Size = st.st_size; | ||
| 140 | OwnerId = st.st_uid; | ||
| 141 | GroupId = st.st_gid; | ||
| 142 | Permissions = st.st_mode; | ||
| 143 | Inode = st.st_ino; | ||
| 144 | } | ||
| 145 | |||
| 146 | if ( slashAtEnd ) { | ||
| 147 | FileSystem::dirAddSlashAtEnd( Filepath ); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | bool FileInfo::operator==( const FileInfo& Other ) const { | ||
| 152 | return ( ModificationTime == Other.ModificationTime && Size == Other.Size && | ||
| 153 | OwnerId == Other.OwnerId && GroupId == Other.GroupId && | ||
| 154 | Permissions == Other.Permissions && Inode == Other.Inode ); | ||
| 155 | } | ||
| 156 | |||
| 157 | bool FileInfo::isDirectory() const { | ||
| 158 | return 0 != S_ISDIR( Permissions ); | ||
| 159 | } | ||
| 160 | |||
| 161 | bool FileInfo::isRegularFile() const { | ||
| 162 | return 0 != S_ISREG( Permissions ); | ||
| 163 | } | ||
| 164 | |||
| 165 | bool FileInfo::isReadable() const { | ||
| 166 | #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32 | ||
| 167 | static bool isRoot = getuid() == 0; | ||
| 168 | return isRoot || 0 != S_ISRDBL( Permissions ); | ||
| 169 | #else | ||
| 170 | return 0 != S_ISRDBL( Permissions ); | ||
| 171 | #endif | ||
| 172 | } | ||
| 173 | |||
| 174 | bool FileInfo::isLink() const { | ||
| 175 | #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32 | ||
| 176 | return S_ISLNK( Permissions ); | ||
| 177 | #else | ||
| 178 | return false; | ||
| 179 | #endif | ||
| 180 | } | ||
| 181 | |||
| 182 | std::string FileInfo::linksTo() { | ||
| 183 | #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32 | ||
| 184 | if ( isLink() ) { | ||
| 185 | char* ch = realpath( Filepath.c_str(), NULL ); | ||
| 186 | |||
| 187 | if ( NULL != ch ) { | ||
| 188 | std::string tstr( ch ); | ||
| 189 | |||
| 190 | free( ch ); | ||
| 191 | |||
| 192 | return tstr; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | #endif | ||
| 196 | return std::string( "" ); | ||
| 197 | } | ||
| 198 | |||
| 199 | bool FileInfo::exists() { | ||
| 200 | bool slashAtEnd = FileSystem::slashAtEnd( Filepath ); | ||
| 201 | |||
| 202 | if ( slashAtEnd ) { | ||
| 203 | FileSystem::dirRemoveSlashAtEnd( Filepath ); | ||
| 204 | } | ||
| 205 | |||
| 206 | #if EFSW_PLATFORM != EFSW_PLATFORM_WIN32 | ||
| 207 | struct stat st; | ||
| 208 | int res = stat( Filepath.c_str(), &st ); | ||
| 209 | #else | ||
| 210 | struct _stat st; | ||
| 211 | int res = _wstat( String::fromUtf8( Filepath ).toWideString().c_str(), &st ); | ||
| 212 | #endif | ||
| 213 | |||
| 214 | if ( slashAtEnd ) { | ||
| 215 | FileSystem::dirAddSlashAtEnd( Filepath ); | ||
| 216 | } | ||
| 217 | |||
| 218 | return 0 == res; | ||
| 219 | } | ||
| 220 | |||
| 221 | FileInfo& FileInfo::operator=( const FileInfo& Other ) { | ||
| 222 | this->Filepath = Other.Filepath; | ||
| 223 | this->Size = Other.Size; | ||
| 224 | this->ModificationTime = Other.ModificationTime; | ||
| 225 | this->GroupId = Other.GroupId; | ||
| 226 | this->OwnerId = Other.OwnerId; | ||
| 227 | this->Permissions = Other.Permissions; | ||
| 228 | this->Inode = Other.Inode; | ||
| 229 | return *this; | ||
| 230 | } | ||
| 231 | |||
| 232 | bool FileInfo::sameInode( const FileInfo& Other ) const { | ||
| 233 | return inodeSupported() && Inode == Other.Inode; | ||
| 234 | } | ||
| 235 | |||
| 236 | bool FileInfo::operator!=( const FileInfo& Other ) const { | ||
| 237 | return !( *this == Other ); | ||
| 238 | } | ||
| 239 | |||
| 240 | } // namespace efsw | ||
