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
222222 assert(chf->path != nullptr);
223223
224224 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)))
226226 return err;
227227
228 if ((err = os_file_mtime(this_file, &chf->mtime))) {
229 os_file_close(this_file);
230 return err;
231 }
232
233228 if ((err = hash_file(chf->bin_digest, this_file, contents))) {
234229 os_file_close(this_file);
235230 return err;
......@@ -351,17 +346,12 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
351346
352347 // if the mtime matches we can trust the digest
353348 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))) {
355351 fprintf(stderr, "Unable to open %s\n: %s", buf_ptr(chf->path), err_str(err));
356352 os_file_close(ch->manifest_file);
357353 return ErrorCacheUnavailable;
358354 }
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 }
365355 if (chf->mtime.sec == actual_mtime.sec && chf->mtime.nsec == actual_mtime.nsec) {
366356 os_file_close(this_file);
367357 } else {
src/os.cpp+31-31
......@@ -1808,7 +1808,7 @@ Error os_self_exe_shared_libs(ZigList<Buf *> &paths) {
18081808#endif
18091809}
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) {
18121812#if defined(ZIG_OS_WINDOWS)
18131813 // TODO use CreateFileW
18141814 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) {
18341834 return ErrorUnexpected;
18351835 }
18361836 }
1837
18381837 *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
18391849 return ErrorNone;
18401850#else
18411851 for (;;) {
......@@ -1858,7 +1868,26 @@ Error os_file_open_r(Buf *full_path, OsFile *out_file) {
18581868 return ErrorFileSystem;
18591869 }
18601870 }
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 }
18611880 *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 }
18621891 return ErrorNone;
18631892 }
18641893#endif
......@@ -1948,35 +1977,6 @@ Error os_file_open_lock_rw(Buf *full_path, OsFile *out_file) {
19481977#endif
19491978}
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
19801980Error os_file_read(OsFile file, void *ptr, size_t *len) {
19811981#if defined(ZIG_OS_WINDOWS)
19821982 DWORD amt_read;
src/os.hpp+1-2
......@@ -101,9 +101,8 @@ bool os_path_is_absolute(Buf *path);
101101Error ATTRIBUTE_MUST_USE os_make_path(Buf *path);
102102Error 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);
105105Error 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);
107106Error ATTRIBUTE_MUST_USE os_file_read(OsFile file, void *ptr, size_t *len);
108107Error ATTRIBUTE_MUST_USE os_file_read_all(OsFile file, Buf *contents);
109108Error ATTRIBUTE_MUST_USE os_file_overwrite(OsFile file, Buf *contents);