Skip to content

Commit

Permalink
Fix files being created even when download failed
Browse files Browse the repository at this point in the history
This was causing failures during repeated fetches when the first fails
to download files.

`HttpClient` was creating a temporary file under a unique name as a
placeholder, then the download would fail and not write anything to that
temp file, and finally it would rename that file to the expected name.
The bad file existing was triggering a cache hit and causing failures
once the files could not be extracted from.

Preventing the rename is not thorough enough, because the temporary
files were still left in the cache.

Bug: 395979597
Test: cvd fetch --target_directory=/tmp/cvd/cache_test --default_build=11407015/cf_x86_64_phone-userdebug_coverage
Test: # expected failure becaue the file is not found
Test: ls /tmp/cvd/cache_test  # no host tools or images
Test: ls
/tmp/cvd/cache/<user_id>/11407015/cf_x86_64_phone-userdebug_coverage
Test: # directories for build+target exist, but no files leftover
  • Loading branch information
cjreynol committed Feb 13, 2025
1 parent 1f0d4fb commit 99ba695
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions base/cvd/cuttlefish/host/libs/web/http_client/http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,16 @@ class CurlClient : public HttpClient {
stream.write(data, size);
return !stream.fail();
};
auto http_response = CF_EXPECT(DownloadToCallback(callback, url, headers));
HttpResponse<void> http_response = CF_EXPECT(DownloadToCallback(callback, url, headers));

LOG(DEBUG) << "Downloaded '" << total_dl << "' total bytes from '" << url
<< "' to '" << path << "'.";

CF_EXPECT(RenameFile(temp_path, path));
if (http_response.HttpSuccess()) {
CF_EXPECT(RenameFile(temp_path, path));
} else {
CF_EXPECTF(RemoveFile(temp_path), "Unable to remove temporary file \"{}\"\nMay require manual removal", temp_path);
}
return HttpResponse<std::string>{path, http_response.http_code};
}

Expand Down

0 comments on commit 99ba695

Please sign in to comment.