authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-13 16:17:12-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-04-13 16:17:12-04:00
log4a2ccd6fb811f044932cd660d1bee67c8664d483
treefce717d3b80052fbd64cf0bec9f93d622821e3c8
parent9d229791c68827f8890f0715dc05e3380693645d
parent93e89b3b7efeaa41f4f11fbb022b962d2244dab2
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2266 from bnoordhuis/fix-cache-lseek-ebadf

don't close cache manifest file prematurely

3 files changed, 19 insertions(+), 17 deletions(-)

src/cache_hash.cpp+13-13
......@@ -256,10 +256,10 @@ static Error populate_file_hash(CacheHash *ch, CacheHashFile *chf, Buf *contents
256256 }
257257
258258 if ((err = hash_file(chf->bin_digest, this_file, contents))) {
259 os_file_close(this_file);
259 os_file_close(&this_file);
260260 return err;
261261 }
262 os_file_close(this_file);
262 os_file_close(&this_file);
263263
264264 blake2b_update(&ch->blake, chf->bin_digest, 48);
265265
......@@ -300,7 +300,7 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
300300 Buf line_buf = BUF_INIT;
301301 buf_resize(&line_buf, 512);
302302 if ((err = os_file_read_all(ch->manifest_file, &line_buf))) {
303 os_file_close(ch->manifest_file);
303 os_file_close(&ch->manifest_file);
304304 return err;
305305 }
306306
......@@ -389,14 +389,14 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
389389 OsFileAttr actual_attr;
390390 if ((err = os_file_open_r(chf->path, &this_file, &actual_attr))) {
391391 fprintf(stderr, "Unable to open %s\n: %s", buf_ptr(chf->path), err_str(err));
392 os_file_close(ch->manifest_file);
392 os_file_close(&ch->manifest_file);
393393 return ErrorCacheUnavailable;
394394 }
395395 if (chf->attr.mtime.sec == actual_attr.mtime.sec &&
396396 chf->attr.mtime.nsec == actual_attr.mtime.nsec &&
397397 chf->attr.inode == actual_attr.inode)
398398 {
399 os_file_close(this_file);
399 os_file_close(&this_file);
400400 } else {
401401 // we have to recompute the digest.
402402 // later we'll rewrite the manifest with the new mtime/digest values
......@@ -411,11 +411,11 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
411411
412412 uint8_t actual_digest[48];
413413 if ((err = hash_file(actual_digest, this_file, nullptr))) {
414 os_file_close(this_file);
415 os_file_close(ch->manifest_file);
414 os_file_close(&this_file);
415 os_file_close(&ch->manifest_file);
416416 return err;
417417 }
418 os_file_close(this_file);
418 os_file_close(&this_file);
419419 if (memcmp(chf->bin_digest, actual_digest, 48) != 0) {
420420 memcpy(chf->bin_digest, actual_digest, 48);
421421 // keep going until we have the input file digests
......@@ -433,12 +433,12 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
433433 CacheHashFile *chf = &ch->files.at(file_i);
434434 if ((err = populate_file_hash(ch, chf, nullptr))) {
435435 fprintf(stderr, "Unable to hash %s: %s\n", buf_ptr(chf->path), err_str(err));
436 os_file_close(ch->manifest_file);
436 os_file_close(&ch->manifest_file);
437437 return ErrorCacheUnavailable;
438438 }
439439 }
440 if (return_code != ErrorNone) {
441 os_file_close(ch->manifest_file);
440 if (return_code != ErrorNone && return_code != ErrorInvalidFormat) {
441 os_file_close(&ch->manifest_file);
442442 }
443443 return return_code;
444444 }
......@@ -453,7 +453,7 @@ Error cache_add_file_fetch(CacheHash *ch, Buf *resolved_path, Buf *contents) {
453453 CacheHashFile *chf = ch->files.add_one();
454454 chf->path = resolved_path;
455455 if ((err = populate_file_hash(ch, chf, contents))) {
456 os_file_close(ch->manifest_file);
456 os_file_close(&ch->manifest_file);
457457 return err;
458458 }
459459
......@@ -586,6 +586,6 @@ void cache_release(CacheHash *ch) {
586586 }
587587 }
588588
589 os_file_close(ch->manifest_file);
589 os_file_close(&ch->manifest_file);
590590}
591591
src/os.cpp+5-3
......@@ -2081,11 +2081,13 @@ Error os_file_overwrite(OsFile file, Buf *contents) {
20812081#endif
20822082}
20832083
2084void os_file_close(OsFile file) {
2084void os_file_close(OsFile *file) {
20852085#if defined(ZIG_OS_WINDOWS)
2086 CloseHandle(file);
2086 CloseHandle(*file);
2087 *file = NULL;
20872088#else
2088 close(file);
2089 close(*file);
2090 *file = -1;
20892091#endif
20902092}
20912093
src/os.hpp+1-1
......@@ -121,7 +121,7 @@ Error ATTRIBUTE_MUST_USE os_file_open_lock_rw(Buf *full_path, OsFile *out_file);
121121Error ATTRIBUTE_MUST_USE os_file_read(OsFile file, void *ptr, size_t *len);
122122Error ATTRIBUTE_MUST_USE os_file_read_all(OsFile file, Buf *contents);
123123Error ATTRIBUTE_MUST_USE os_file_overwrite(OsFile file, Buf *contents);
124void os_file_close(OsFile file);
124void os_file_close(OsFile *file);
125125
126126Error ATTRIBUTE_MUST_USE os_write_file(Buf *full_path, Buf *contents);
127127Error ATTRIBUTE_MUST_USE os_copy_file(Buf *src_path, Buf *dest_path);