@@ -50,7 +50,6 @@ type Cache struct {
5050// to share a cache directory (for example, if the directory were stored
5151// in a network file system). File locking is notoriously unreliable in
5252// network file systems and may not suffice to protect the cache.
53- //
5453func Open (dir string ) (* Cache , error ) {
5554 info , err := os .Stat (dir )
5655 if err != nil {
@@ -144,55 +143,52 @@ func (c *Cache) get(id ActionID) (Entry, error) {
144143 missing := func () (Entry , error ) {
145144 return Entry {}, errMissing
146145 }
147- failed := func (err error ) (Entry , error ) {
148- return Entry {}, err
149- }
150- fileName := c .fileName (id , "a" )
151- f , err := os .Open (fileName )
146+ f , err := os .Open (c .fileName (id , "a" ))
152147 if err != nil {
153148 if os .IsNotExist (err ) {
154149 return missing ()
155150 }
156- return failed ( err )
151+ return Entry {}, err
157152 }
158153 defer f .Close ()
159154 entry := make ([]byte , entrySize + 1 ) // +1 to detect whether f is too long
160- if n , readErr := io .ReadFull (f , entry ); n != entrySize || readErr != io .ErrUnexpectedEOF {
161- return failed ( fmt .Errorf ("read %d/%d bytes from %s with error %w" , n , entrySize , fileName , readErr ) )
155+ if n , err := io .ReadFull (f , entry ); n != entrySize || err != io .ErrUnexpectedEOF {
156+ return Entry {}, fmt .Errorf ("read %d/%d bytes from %s with error %w" , n , entrySize , c . fileName ( id , "a" ), err )
162157 }
163158 if entry [0 ] != 'v' || entry [1 ] != '1' || entry [2 ] != ' ' || entry [3 + hexSize ] != ' ' || entry [3 + hexSize + 1 + hexSize ] != ' ' || entry [3 + hexSize + 1 + hexSize + 1 + 20 ] != ' ' || entry [entrySize - 1 ] != '\n' {
164- return failed ( fmt .Errorf ("bad data in %s" , fileName ))
159+ return Entry {}, fmt .Errorf ("bad data in %s" , c . fileName ( id , "a" ))
165160 }
166161 eid , entry := entry [3 :3 + hexSize ], entry [3 + hexSize :]
167162 eout , entry := entry [1 :1 + hexSize ], entry [1 + hexSize :]
168163 esize , entry := entry [1 :1 + 20 ], entry [1 + 20 :]
169164 etime , entry := entry [1 :1 + 20 ], entry [1 + 20 :]
170165 var buf [HashSize ]byte
171- if _ , err = hex .Decode (buf [:], eid ); err != nil || buf != id {
172- return failed ( fmt .Errorf ("failed to hex decode eid data in %s: %w" , fileName , err ) )
166+ if _ , err : = hex .Decode (buf [:], eid ); err != nil || buf != id {
167+ return Entry {}, fmt .Errorf ("failed to hex decode eid data in %s: %w" , c . fileName ( id , "a" ), err )
173168 }
174- if _ , err = hex .Decode (buf [:], eout ); err != nil {
175- return failed ( fmt .Errorf ("failed to hex decode eout data in %s: %w" , fileName , err ) )
169+ if _ , err : = hex .Decode (buf [:], eout ); err != nil {
170+ return Entry {}, fmt .Errorf ("failed to hex decode eout data in %s: %w" , c . fileName ( id , "a" ), err )
176171 }
177172 i := 0
178173 for i < len (esize ) && esize [i ] == ' ' {
179174 i ++
180175 }
181176 size , err := strconv .ParseInt (string (esize [i :]), 10 , 64 )
182177 if err != nil || size < 0 {
183- return failed ( fmt .Errorf ("failed to parse esize int from %s with error %w" , fileName , err ) )
178+ return Entry {}, fmt .Errorf ("failed to parse esize int from %s with error %w" , c . fileName ( id , "a" ), err )
184179 }
185180 i = 0
186181 for i < len (etime ) && etime [i ] == ' ' {
187182 i ++
188183 }
189184 tm , err := strconv .ParseInt (string (etime [i :]), 10 , 64 )
190185 if err != nil || tm < 0 {
191- return failed ( fmt .Errorf ("failed to parse etime int from %s with error %w" , fileName , err ) )
186+ return Entry {}, fmt .Errorf ("failed to parse etime int from %s with error %w" , c . fileName ( id , "a" ), err )
192187 }
193188
194- if err = c .used (fileName ); err != nil {
195- return failed (fmt .Errorf ("failed to mark %s as used: %w" , fileName , err ))
189+ err = c .used (c .fileName (id , "a" ))
190+ if err != nil {
191+ return Entry {}, fmt .Errorf ("failed to mark %s as used: %w" , c .fileName (id , "a" ), err )
196192 }
197193
198194 return Entry {buf , size , time .Unix (0 , tm )}, nil
@@ -245,7 +241,8 @@ func (c *Cache) GetBytes(id ActionID) ([]byte, Entry, error) {
245241// OutputFile returns the name of the cache file storing output with the given OutputID.
246242func (c * Cache ) OutputFile (out OutputID ) (string , error ) {
247243 file := c .fileName (out , "d" )
248- if err := c .used (file ); err != nil {
244+ err := c .used (file )
245+ if err != nil {
249246 return "" , err
250247 }
251248 return file , nil
@@ -280,18 +277,19 @@ const (
280277// while still keeping the mtimes useful for cache trimming.
281278func (c * Cache ) used (file string ) error {
282279 info , err := os .Stat (file )
280+ if err == nil && c .now ().Sub (info .ModTime ()) < mtimeInterval {
281+ return nil
282+ }
283+
283284 if err != nil {
284285 if os .IsNotExist (err ) {
285286 return errMissing
286287 }
287288 return fmt .Errorf ("failed to stat file %s: %w" , file , err )
288289 }
289290
290- if c .now ().Sub (info .ModTime ()) < mtimeInterval {
291- return nil
292- }
293-
294- if err := os .Chtimes (file , c .now (), c .now ()); err != nil {
291+ err = os .Chtimes (file , c .now (), c .now ())
292+ if err != nil {
295293 return fmt .Errorf ("failed to change time of file %s: %w" , file , err )
296294 }
297295
@@ -403,7 +401,8 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify
403401 os .Remove (file )
404402 return err
405403 }
406- if err = os .Chtimes (file , c .now (), c .now ()); err != nil { // mainly for tests
404+ err = os .Chtimes (file , c .now (), c .now ()) // mainly for tests
405+ if err != nil {
407406 return fmt .Errorf ("failed to change time of file %s: %w" , file , err )
408407 }
409408
@@ -459,9 +458,10 @@ func (c *Cache) copyFile(file io.ReadSeeker, out OutputID, size int64) error {
459458 info , err := os .Stat (name )
460459 if err == nil && info .Size () == size {
461460 // Check hash.
462- if f , openErr := os .Open (name ); openErr == nil {
461+ if f , err := os .Open (name ); err == nil {
463462 h := sha256 .New ()
464- if _ , copyErr := io .Copy (h , f ); copyErr != nil {
463+ _ , copyErr := io .Copy (h , f )
464+ if copyErr != nil {
465465 return fmt .Errorf ("failed to copy to sha256: %w" , copyErr )
466466 }
467467
@@ -497,47 +497,49 @@ func (c *Cache) copyFile(file io.ReadSeeker, out OutputID, size int64) error {
497497 // before returning, to avoid leaving bad bytes in the file.
498498
499499 // Copy file to f, but also into h to double-check hash.
500- if _ , err = file .Seek (0 , 0 ); err != nil {
501- _ = f .Truncate (0 )
500+ if _ , err : = file .Seek (0 , 0 ); err != nil {
501+ f .Truncate (0 )
502502 return err
503503 }
504504 h := sha256 .New ()
505505 w := io .MultiWriter (f , h )
506- if _ , err = io .CopyN (w , file , size - 1 ); err != nil {
507- _ = f .Truncate (0 )
506+ if _ , err : = io .CopyN (w , file , size - 1 ); err != nil {
507+ f .Truncate (0 )
508508 return err
509509 }
510510 // Check last byte before writing it; writing it will make the size match
511511 // what other processes expect to find and might cause them to start
512512 // using the file.
513513 buf := make ([]byte , 1 )
514- if _ , err = file .Read (buf ); err != nil {
515- _ = f .Truncate (0 )
514+ if _ , err : = file .Read (buf ); err != nil {
515+ f .Truncate (0 )
516516 return err
517517 }
518- if n , wErr := h .Write (buf ); n != len (buf ) {
518+ n , wErr := h .Write (buf )
519+ if n != len (buf ) {
519520 return fmt .Errorf ("wrote to hash %d/%d bytes with error %w" , n , len (buf ), wErr )
520521 }
521522
522523 sum := h .Sum (nil )
523524 if ! bytes .Equal (sum , out [:]) {
524- _ = f .Truncate (0 )
525+ f .Truncate (0 )
525526 return fmt .Errorf ("file content changed underfoot" )
526527 }
527528
528529 // Commit cache file entry.
529- if _ , err = f .Write (buf ); err != nil {
530- _ = f .Truncate (0 )
530+ if _ , err : = f .Write (buf ); err != nil {
531+ f .Truncate (0 )
531532 return err
532533 }
533- if err = f .Close (); err != nil {
534+ if err : = f .Close (); err != nil {
534535 // Data might not have been written,
535536 // but file may look like it is the right size.
536537 // To be extra careful, remove cached file.
537538 os .Remove (name )
538539 return err
539540 }
540- if err = os .Chtimes (name , c .now (), c .now ()); err != nil { // mainly for tests
541+ err = os .Chtimes (name , c .now (), c .now ()) // mainly for tests
542+ if err != nil {
541543 return fmt .Errorf ("failed to change time of file %s: %w" , name , err )
542544 }
543545
0 commit comments