|
19 | 19 | #include <string.h>
|
20 | 20 | #include <sys/stat.h>
|
21 | 21 |
|
| 22 | +#ifdef WIN32 |
| 23 | +#include <windows.h> |
| 24 | +#endif |
| 25 | + |
22 | 26 | #include "util.h"
|
23 | 27 |
|
24 | 28 | namespace {
|
@@ -62,17 +66,34 @@ bool DiskInterface::MakeDirs(const std::string& path) {
|
62 | 66 | // RealDiskInterface -----------------------------------------------------------
|
63 | 67 |
|
64 | 68 | int RealDiskInterface::Stat(const std::string& path) {
|
| 69 | +#ifdef WIN32 |
| 70 | + WIN32_FILE_ATTRIBUTE_DATA attrs; |
| 71 | + if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &attrs)) { |
| 72 | + if (GetLastError() == ERROR_FILE_NOT_FOUND) |
| 73 | + return 0; |
| 74 | + Error("GetFileAttributesEx(%s): %s", path.c_str(), |
| 75 | + GetLastErrorString().c_str()); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + const FILETIME& filetime = attrs.ftLastWriteTime; |
| 79 | + // FILETIME is in 100-nanosecond increments since the Windows epoch. |
| 80 | + // We don't much care about epoch correctness but we do want the |
| 81 | + // resulting value to fit in an integer. |
| 82 | + uint64_t mtime = ((uint64_t)filetime.dwHighDateTime << 32) | |
| 83 | + ((uint64_t)filetime.dwLowDateTime); |
| 84 | + mtime /= 1000000000LL / 100; // 100ns -> s. |
| 85 | + mtime -= 12622770400LL; // 1600 epoch -> 2000 epoch (subtract 400 years). |
| 86 | + return mtime; |
| 87 | +#else |
65 | 88 | struct stat st;
|
66 | 89 | if (stat(path.c_str(), &st) < 0) {
|
67 |
| - if (errno == ENOENT) { |
| 90 | + if (errno == ENOENT) |
68 | 91 | return 0;
|
69 |
| - } else { |
70 |
| - Error("stat(%s): %s", path.c_str(), strerror(errno)); |
71 |
| - return -1; |
72 |
| - } |
| 92 | + Error("stat(%s): %s", path.c_str(), strerror(errno)); |
| 93 | + return -1; |
73 | 94 | }
|
74 |
| - |
75 | 95 | return st.st_mtime;
|
| 96 | +#endif |
76 | 97 | }
|
77 | 98 |
|
78 | 99 | bool RealDiskInterface::MakeDir(const std::string& path) {
|
|
0 commit comments