@@ -122,41 +122,38 @@ func (ff *hardlinkingFileFetcher) GetFile(ctx context.Context, blobDigest digest
122122 return err
123123 }
124124
125- downloadErr := ff .base .GetFile (ctx , blobDigest , directory , name , isExecutable )
126- if downloadErr == nil {
127- // The file was downloaded successfully. Place it into the
128- // cache, so that successive calls may use it.
129- ff .filesLock .Lock ()
130- if _ , ok := ff .filesSize [key ]; ! ok {
131- ff .evictionLock .Lock ()
132-
133- // Remove old files from the cache if necessary.
134- sizeBytes := blobDigest .GetSizeBytes ()
135- if err := ff .makeSpace (sizeBytes ); err != nil {
136- downloadErr = err
137- } else {
138- // Hardlink the file into the cache.
139- if err := directory .Link (name , ff .cacheDirectory , path .MustNewComponent (key )); err != nil && ! os .IsExist (err ) {
140- downloadErr = util .StatusWrapfWithCode (err , codes .Internal , "Failed to add cached file %#v" , key )
141- } else {
142- ff .evictionSet .Insert (key )
143- ff .filesSize [key ] = sizeBytes
144- ff .filesTotalSize += sizeBytes
145- }
146- }
147- ff .evictionLock .Unlock ()
148- } else {
149- // The file was already part of our bookkeeping,
150- // but was missing on disk. Repair this by adding
151- // a link to the newly downloaded file.
152- if err := directory .Link (name , ff .cacheDirectory , path .MustNewComponent (key )); err != nil && ! os .IsExist (err ) {
153- downloadErr = util .StatusWrapfWithCode (err , codes .Internal , "Failed to repair cached file %#v" , key )
154- }
155- }
156- ff .filesLock .Unlock ()
125+ // Download the file at the intended location.
126+ if err := ff .base .GetFile (ctx , blobDigest , directory , name , isExecutable ); err != nil {
127+ return err
157128 }
158129
159- return downloadErr
130+ ff .filesLock .Lock ()
131+ defer ff .filesLock .Unlock ()
132+ if _ , ok := ff .filesSize [key ]; ! ok {
133+ ff .evictionLock .Lock ()
134+ defer ff .evictionLock .Unlock ()
135+
136+ // Remove old files from the cache if necessary.
137+ sizeBytes := blobDigest .GetSizeBytes ()
138+ if err := ff .makeSpace (sizeBytes ); err != nil {
139+ return err
140+ }
141+
142+ // Hardlink the file into the cache.
143+ if err := directory .Link (name , ff .cacheDirectory , path .MustNewComponent (key )); err != nil && ! os .IsExist (err ) {
144+ return util .StatusWrapfWithCode (err , codes .Internal , "Failed to add cached file %#v" , key )
145+ }
146+ ff .evictionSet .Insert (key )
147+ ff .filesSize [key ] = sizeBytes
148+ ff .filesTotalSize += sizeBytes
149+ } else {
150+ // Even though the file is part of our bookkeeping, we
151+ // observed it didn't exist. Repair this inconsistency.
152+ if err := directory .Link (name , ff .cacheDirectory , path .MustNewComponent (key )); err != nil && ! os .IsExist (err ) {
153+ return util .StatusWrapfWithCode (err , codes .Internal , "Failed to repair cached file %#v" , key )
154+ }
155+ }
156+ return nil
160157}
161158
162159// tryLinkFromCache attempts to create a hardlink from the cache to a
0 commit comments