authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-01-30 16:06:18-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-01-30 16:06:18-05:00
log59c050e7ff83aeecd507cef24d7eb914ec59581d
tree9e5f2f3cc7b8378b08c119b8f9f5615054aef711
parentad8381e0d2936ffecaa0b54e51e26d6bdb98682e
signature Commit is signed but in an unrecognized format.

collapse os_file_mtime into os_file_open_r and check for directory

This is a manual merge of kristate's pull request #1754, due to conflicts + a couple fixups. closes #1754

3 files changed, 35 insertions(+), 46 deletions(-)

src/cache_hash.cpp+3-13
...@@ -222,14 +222,9 @@ static Error populate_file_hash(CacheHash *ch, CacheHashFile *chf, Buf *contents...@@ -222,14 +222,9 @@ static Error populate_file_hash(CacheHash *ch, CacheHashFile *chf, Buf *contents
222 assert(chf->path != nullptr);222 assert(chf->path != nullptr);
223223
224 OsFile this_file;224 OsFile this_file;
225 if ((err = os_file_open_r(chf->path, &this_file)))225 if ((err = os_file_open_r(chf->path, &this_file, &chf->mtime)))
226 return err;226 return err;
227227
228 if ((err = os_file_mtime(this_file, &chf->mtime))) {
229 os_file_close(this_file);
230 return err;
231 }
232
233 if ((err = hash_file(chf->bin_digest, this_file, contents))) {228 if ((err = hash_file(chf->bin_digest, this_file, contents))) {
234 os_file_close(this_file);229 os_file_close(this_file);
235 return err;230 return err;
...@@ -351,17 +346,12 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {...@@ -351,17 +346,12 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
351346
352 // if the mtime matches we can trust the digest347 // if the mtime matches we can trust the digest
353 OsFile this_file;348 OsFile this_file;
354 if ((err = os_file_open_r(chf->path, &this_file))) {349 OsTimeStamp actual_mtime;
350 if ((err = os_file_open_r(chf->path, &this_file, &actual_mtime))) {
355 fprintf(stderr, "Unable to open %s\n: %s", buf_ptr(chf->path), err_str(err));351 fprintf(stderr, "Unable to open %s\n: %s", buf_ptr(chf->path), err_str(err));
356 os_file_close(ch->manifest_file);352 os_file_close(ch->manifest_file);
357 return ErrorCacheUnavailable;353 return ErrorCacheUnavailable;
358 }354 }
359 OsTimeStamp actual_mtime;
360 if ((err = os_file_mtime(this_file, &actual_mtime))) {
361 os_file_close(this_file);
362 os_file_close(ch->manifest_file);
363 return err;
364 }
365 if (chf->mtime.sec == actual_mtime.sec && chf->mtime.nsec == actual_mtime.nsec) {355 if (chf->mtime.sec == actual_mtime.sec && chf->mtime.nsec == actual_mtime.nsec) {
366 os_file_close(this_file);356 os_file_close(this_file);
367 } else {357 } else {
src/os.cpp+31-31
...@@ -1808,7 +1808,7 @@ Error os_self_exe_shared_libs(ZigList<Buf *> &paths) {...@@ -1808,7 +1808,7 @@ Error os_self_exe_shared_libs(ZigList<Buf *> &paths) {
1808#endif1808#endif
1809}1809}
18101810
1811Error os_file_open_r(Buf *full_path, OsFile *out_file) {1811Error os_file_open_r(Buf *full_path, OsFile *out_file, OsTimeStamp *mtime) {
1812#if defined(ZIG_OS_WINDOWS)1812#if defined(ZIG_OS_WINDOWS)
1813 // TODO use CreateFileW1813 // TODO use CreateFileW
1814 HANDLE result = CreateFileA(buf_ptr(full_path), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);1814 HANDLE result = CreateFileA(buf_ptr(full_path), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
...@@ -1834,8 +1834,18 @@ Error os_file_open_r(Buf *full_path, OsFile *out_file) {...@@ -1834,8 +1834,18 @@ Error os_file_open_r(Buf *full_path, OsFile *out_file) {
1834 return ErrorUnexpected;1834 return ErrorUnexpected;
1835 }1835 }
1836 }1836 }
1837
1838 *out_file = result;1837 *out_file = result;
1838
1839 if (mtime != nullptr) {
1840 FILETIME last_write_time;
1841 if (!GetFileTime(file, nullptr, nullptr, &last_write_time)) {
1842 CloseHandle(result);
1843 return ErrorUnexpected;
1844 }
1845 mtime->sec = (((ULONGLONG) last_write_time.dwHighDateTime) << 32) + last_write_time.dwLowDateTime;
1846 mtime->nsec = 0;
1847 }
1848
1839 return ErrorNone;1849 return ErrorNone;
1840#else1850#else
1841 for (;;) {1851 for (;;) {
...@@ -1858,7 +1868,26 @@ Error os_file_open_r(Buf *full_path, OsFile *out_file) {...@@ -1858,7 +1868,26 @@ Error os_file_open_r(Buf *full_path, OsFile *out_file) {
1858 return ErrorFileSystem;1868 return ErrorFileSystem;
1859 }1869 }
1860 }1870 }
1871 struct stat statbuf;
1872 if (fstat(fd, &statbuf) == -1) {
1873 close(fd);
1874 return ErrorFileSystem;
1875 }
1876 if (S_ISDIR(statbuf.st_mode)) {
1877 close(fd);
1878 return ErrorIsDir;
1879 }
1861 *out_file = fd;1880 *out_file = fd;
1881
1882 if (mtime != nullptr) {
1883#if defined(ZIG_OS_DARWIN)
1884 mtime->sec = statbuf.st_mtimespec.tv_sec;
1885 mtime->nsec = statbuf.st_mtimespec.tv_nsec;
1886#else
1887 mtime->sec = statbuf.st_mtim.tv_sec;
1888 mtime->nsec = statbuf.st_mtim.tv_nsec;
1889#endif
1890 }
1862 return ErrorNone;1891 return ErrorNone;
1863 }1892 }
1864#endif1893#endif
...@@ -1948,35 +1977,6 @@ Error os_file_open_lock_rw(Buf *full_path, OsFile *out_file) {...@@ -1948,35 +1977,6 @@ Error os_file_open_lock_rw(Buf *full_path, OsFile *out_file) {
1948#endif1977#endif
1949}1978}
19501979
1951Error os_file_mtime(OsFile file, OsTimeStamp *mtime) {
1952#if defined(ZIG_OS_WINDOWS)
1953 FILETIME last_write_time;
1954 if (!GetFileTime(file, nullptr, nullptr, &last_write_time))
1955 return ErrorUnexpected;
1956 mtime->sec = (((ULONGLONG) last_write_time.dwHighDateTime) << 32) + last_write_time.dwLowDateTime;
1957 mtime->nsec = 0;
1958 return ErrorNone;
1959#elif defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD)
1960 struct stat statbuf;
1961 if (fstat(file, &statbuf) == -1)
1962 return ErrorFileSystem;
1963
1964 mtime->sec = statbuf.st_mtim.tv_sec;
1965 mtime->nsec = statbuf.st_mtim.tv_nsec;
1966 return ErrorNone;
1967#elif defined(ZIG_OS_DARWIN)
1968 struct stat statbuf;
1969 if (fstat(file, &statbuf) == -1)
1970 return ErrorFileSystem;
1971
1972 mtime->sec = statbuf.st_mtimespec.tv_sec;
1973 mtime->nsec = statbuf.st_mtimespec.tv_nsec;
1974 return ErrorNone;
1975#else
1976#error unimplemented
1977#endif
1978}
1979
1980Error os_file_read(OsFile file, void *ptr, size_t *len) {1980Error os_file_read(OsFile file, void *ptr, size_t *len) {
1981#if defined(ZIG_OS_WINDOWS)1981#if defined(ZIG_OS_WINDOWS)
1982 DWORD amt_read;1982 DWORD amt_read;
src/os.hpp+1-2
...@@ -101,9 +101,8 @@ bool os_path_is_absolute(Buf *path);...@@ -101,9 +101,8 @@ bool os_path_is_absolute(Buf *path);
101Error ATTRIBUTE_MUST_USE os_make_path(Buf *path);101Error ATTRIBUTE_MUST_USE os_make_path(Buf *path);
102Error ATTRIBUTE_MUST_USE os_make_dir(Buf *path);102Error ATTRIBUTE_MUST_USE os_make_dir(Buf *path);
103103
104Error ATTRIBUTE_MUST_USE os_file_open_r(Buf *full_path, OsFile *out_file);104Error ATTRIBUTE_MUST_USE os_file_open_r(Buf *full_path, OsFile *out_file, OsTimeStamp *mtime);
105Error ATTRIBUTE_MUST_USE os_file_open_lock_rw(Buf *full_path, OsFile *out_file);105Error ATTRIBUTE_MUST_USE os_file_open_lock_rw(Buf *full_path, OsFile *out_file);
106Error ATTRIBUTE_MUST_USE os_file_mtime(OsFile file, OsTimeStamp *mtime);
107Error ATTRIBUTE_MUST_USE os_file_read(OsFile file, void *ptr, size_t *len);106Error ATTRIBUTE_MUST_USE os_file_read(OsFile file, void *ptr, size_t *len);
108Error ATTRIBUTE_MUST_USE os_file_read_all(OsFile file, Buf *contents);107Error ATTRIBUTE_MUST_USE os_file_read_all(OsFile file, Buf *contents);
109Error ATTRIBUTE_MUST_USE os_file_overwrite(OsFile file, Buf *contents);108Error ATTRIBUTE_MUST_USE os_file_overwrite(OsFile file, Buf *contents);