authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-04-14 21:51:20-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 13:48:43-04:00
loge7657f2938e9061a546c5ad726a9575ad6a8eba3
treeebb4559150bb5bf7b169ddb9a7639a93fa4bee3d
parent967b9825a7b57586cd34f51d9b915505ffbd455d

Make `CacheHash.release` return an error

If a user doesn't care that the manifest failed to be written, they can simply ignore it. The program will still work; that particular cache item will simply not be cached.

1 files changed, 14 insertions(+), 10 deletions(-)

lib/std/cache_hash.zig+14-10
...@@ -311,13 +311,17 @@ pub const CacheHash = struct {...@@ -311,13 +311,17 @@ pub const CacheHash = struct {
311 try self.manifest_file.?.writeAll(contents.items);311 try self.manifest_file.?.writeAll(contents.items);
312 }312 }
313313
314 pub fn release(self: *@This()) void {314 /// Releases the manifest file and frees any memory the CacheHash was using.
315 /// `CacheHash.hit` must be called first.
316 ///
317 /// Will also attempt to write to the manifest file if the manifest is dirty.
318 /// Writing to the manifest file is the only way that this file can return an
319 /// error.
320 pub fn release(self: *@This()) !void {
315 debug.assert(self.manifest_file != null);321 debug.assert(self.manifest_file != null);
316322
317 if (self.manifest_dirty) {323 if (self.manifest_dirty) {
318 self.write_manifest() catch |err| {324 try self.write_manifest();
319 debug.warn("Unable to write cache file '{}': {}\n", .{ self.b64_digest, err });
320 };
321 }325 }
322326
323 self.manifest_file.?.close();327 self.manifest_file.?.close();
...@@ -366,7 +370,7 @@ test "cache file and then recall it" {...@@ -366,7 +370,7 @@ test "cache file and then recall it" {
366370
367 {371 {
368 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);372 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
369 defer ch.release();373 defer ch.release() catch unreachable;
370374
371 ch.add(true);375 ch.add(true);
372 ch.add(@as(u16, 1234));376 ch.add(@as(u16, 1234));
...@@ -380,7 +384,7 @@ test "cache file and then recall it" {...@@ -380,7 +384,7 @@ test "cache file and then recall it" {
380 }384 }
381 {385 {
382 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);386 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
383 defer ch.release();387 defer ch.release() catch unreachable;
384388
385 ch.add(true);389 ch.add(true);
386 ch.add(@as(u16, 1234));390 ch.add(@as(u16, 1234));
...@@ -420,7 +424,7 @@ test "check that changing a file makes cache fail" {...@@ -420,7 +424,7 @@ test "check that changing a file makes cache fail" {
420424
421 {425 {
422 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);426 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
423 defer ch.release();427 defer ch.release() catch unreachable;
424428
425 ch.add("1234");429 ch.add("1234");
426 try ch.addFile(temp_file);430 try ch.addFile(temp_file);
...@@ -435,7 +439,7 @@ test "check that changing a file makes cache fail" {...@@ -435,7 +439,7 @@ test "check that changing a file makes cache fail" {
435439
436 {440 {
437 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);441 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
438 defer ch.release();442 defer ch.release() catch unreachable;
439443
440 ch.add("1234");444 ch.add("1234");
441 try ch.addFile(temp_file);445 try ch.addFile(temp_file);
...@@ -462,7 +466,7 @@ test "no file inputs" {...@@ -462,7 +466,7 @@ test "no file inputs" {
462466
463 {467 {
464 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);468 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
465 defer ch.release();469 defer ch.release() catch unreachable;
466470
467 ch.add("1234");471 ch.add("1234");
468472
...@@ -473,7 +477,7 @@ test "no file inputs" {...@@ -473,7 +477,7 @@ test "no file inputs" {
473 }477 }
474 {478 {
475 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);479 var ch = try CacheHash.init(testing.allocator, temp_manifest_dir);
476 defer ch.release();480 defer ch.release() catch unreachable;
477481
478 ch.add("1234");482 ch.add("1234");
479483