| 1336 | } |
| 1337 | |
| 1338 | bool SystemTools::SameFile(std::string const& file1, std::string const& file2) |
| 1339 | { |
| 1340 | #ifdef _WIN32 |
| 1341 | HANDLE hFile1, hFile2; |
| 1342 | |
| 1343 | hFile1 = |
| 1344 | CreateFileW(Encoding::ToWide(file1).c_str(), GENERIC_READ, FILE_SHARE_READ, |
| 1345 | nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 1346 | hFile2 = |
| 1347 | CreateFileW(Encoding::ToWide(file2).c_str(), GENERIC_READ, FILE_SHARE_READ, |
| 1348 | nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); |
| 1349 | if (hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE) { |
| 1350 | if (hFile1 != INVALID_HANDLE_VALUE) { |
| 1351 | CloseHandle(hFile1); |
| 1352 | } |
| 1353 | if (hFile2 != INVALID_HANDLE_VALUE) { |
| 1354 | CloseHandle(hFile2); |
| 1355 | } |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
| 1359 | BY_HANDLE_FILE_INFORMATION fiBuf1; |
| 1360 | BY_HANDLE_FILE_INFORMATION fiBuf2; |
| 1361 | GetFileInformationByHandle(hFile1, &fiBuf1); |
| 1362 | GetFileInformationByHandle(hFile2, &fiBuf2); |
| 1363 | CloseHandle(hFile1); |
| 1364 | CloseHandle(hFile2); |
| 1365 | return (fiBuf1.dwVolumeSerialNumber == fiBuf2.dwVolumeSerialNumber && |
| 1366 | fiBuf1.nFileIndexHigh == fiBuf2.nFileIndexHigh && |
| 1367 | fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow); |
| 1368 | #else |
| 1369 | struct stat fileStat1, fileStat2; |
| 1370 | if (stat(file1.c_str(), &fileStat1) == 0 && |
| 1371 | stat(file2.c_str(), &fileStat2) == 0) { |
| 1372 | // see if the files are the same file |
| 1373 | // check the device inode and size |
| 1374 | if (memcmp(&fileStat2.st_dev, &fileStat1.st_dev, |
| 1375 | sizeof(fileStat1.st_dev)) == 0 && |
| 1376 | memcmp(&fileStat2.st_ino, &fileStat1.st_ino, |
| 1377 | sizeof(fileStat1.st_ino)) == 0 && |
| 1378 | fileStat2.st_size == fileStat1.st_size) { |
| 1379 | return true; |
| 1380 | } |
| 1381 | } |
| 1382 | return false; |
| 1383 | #endif |
| 1384 | } |
| 1385 | |
| 1386 | bool SystemTools::PathExists(std::string const& path) |
| 1387 | { |