Skip to content

Commit 457b45b

Browse files
committed
Refactor cache path and temporary file handling
- Move common path and file creation logic to utils.go - Add `BuildCachePath` function for consistent cache path generation - Add `WipFile` function for temporary file creation with directory handling - Update cache and hardlink managers to use the new utility functions Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 39666b4 commit 457b45b

File tree

3 files changed

+58
-32
lines changed

3 files changed

+58
-32
lines changed

cache/cache.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,7 @@ func (dc *directoryCache) Get(key string, opts ...Option) (Reader, error) {
228228
opt = o(opt)
229229
}
230230

231-
// 1. Try to get from hardlink first
232-
if dc.hlManager != nil {
233-
if linkPath, exists := dc.hlManager.GetLink(key); exists {
234-
if r, err := os.Open(linkPath); err == nil {
235-
return &reader{
236-
ReaderAt: r,
237-
closeFunc: r.Close,
238-
}, nil
239-
}
240-
// If failed to open, continue with normal flow
241-
}
242-
}
243-
244-
// 2. Try to get from memory cache
231+
// Try to get from memory cache
245232
if !dc.direct && !opt.direct {
246233
if b, done, ok := dc.cache.Get(key); ok {
247234
return &reader{
@@ -265,13 +252,21 @@ func (dc *directoryCache) Get(key string, opts ...Option) (Reader, error) {
265252
}
266253
}
267254

268-
// 3. Read from disk
269-
file, err := os.Open(dc.cachePath(key))
255+
// Try to get from hardlink first
256+
filepath := BuildCachePath(dc.directory, key)
257+
if dc.hlManager != nil {
258+
if linkPath, exists := dc.hlManager.GetLink(key); exists {
259+
filepath = linkPath
260+
}
261+
}
262+
263+
// Read from disk
264+
file, err := os.Open(filepath)
270265
if err != nil {
271266
return nil, fmt.Errorf("failed to open blob file for %q: %w", key, err)
272267
}
273268

274-
// 4. If in direct mode, don't cache file descriptor
269+
// If in direct mode, don't cache file descriptor
275270
if dc.direct || opt.direct {
276271
return &reader{
277272
ReaderAt: file,
@@ -284,7 +279,7 @@ func (dc *directoryCache) Get(key string, opts ...Option) (Reader, error) {
284279
}, nil
285280
}
286281

287-
// 5. Cache file descriptor
282+
// Cache file descriptor
288283
return &reader{
289284
ReaderAt: file,
290285
closeFunc: func() error {
@@ -323,7 +318,7 @@ func (dc *directoryCache) Add(key string, opts ...Option) (Writer, error) {
323318
}
324319

325320
// Create temporary file
326-
w, err := dc.wipFile(key)
321+
w, err := WipFile(dc.wipDirectory, key)
327322
if err != nil {
328323
return nil, err
329324
}
@@ -337,7 +332,7 @@ func (dc *directoryCache) Add(key string, opts ...Option) (Writer, error) {
337332
}
338333

339334
// Commit file
340-
targetPath := dc.cachePath(key)
335+
targetPath := BuildCachePath(dc.directory, key)
341336
if err := os.MkdirAll(filepath.Dir(targetPath), 0700); err != nil {
342337
return fmt.Errorf("failed to create cache directory: %w", err)
343338
}
@@ -393,14 +388,6 @@ func (dc *directoryCache) isClosed() bool {
393388
return closed
394389
}
395390

396-
func (dc *directoryCache) cachePath(key string) string {
397-
return filepath.Join(dc.directory, key[:2], key)
398-
}
399-
400-
func (dc *directoryCache) wipFile(key string) (*os.File, error) {
401-
return os.CreateTemp(dc.wipDirectory, key+"-*")
402-
}
403-
404391
func NewMemoryCache() BlobCache {
405392
return &MemoryCache{
406393
Membuf: map[string]*bytes.Buffer{},
@@ -491,7 +478,7 @@ func (dc *directoryCache) CreateHardlink(key string) error {
491478
return nil
492479
}
493480
log.L.Debugf("Creating hardlink for key %q", key)
494-
return dc.hlManager.CreateLink(key, dc.cachePath(key))
481+
return dc.hlManager.CreateLink(key, BuildCachePath(dc.directory, key))
495482
}
496483

497484
func (dc *directoryCache) HasHardlink(key string) bool {

cache/hardlink.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,13 @@ func (hm *HardlinkManager) CreateLink(key string, sourcePath string) error {
130130
}
131131

132132
// Create hardlink in hardlinks directory
133-
linkPath := filepath.Join(hm.hlDir, key[:2], key)
133+
linkPath := BuildCachePath(hm.hlDir, key)
134134
if err := os.MkdirAll(filepath.Dir(linkPath), 0700); err != nil {
135135
return fmt.Errorf("failed to create link dir: %w", err)
136136
}
137137

138138
// Create temporary link path
139-
tmpLinkPath := linkPath + ".tmp"
140-
139+
tmpLinkPath := BuildCachePath(hm.hlDir, key+".wip")
141140
// Remove temporary link if it exists
142141
os.Remove(tmpLinkPath)
143142

cache/utils.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cache
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
)
24+
25+
// BuildCachePath returns the path for a cache entry with the given key
26+
//
27+
//revive:disable:exported
28+
func BuildCachePath(directory string, key string) string {
29+
return filepath.Join(directory, key[:2], key)
30+
}
31+
32+
//revive:enable:exported
33+
34+
// WipFile creates a temporary file in the given directory with the given key pattern
35+
func WipFile(wipDirectory string, key string) (*os.File, error) {
36+
if err := os.MkdirAll(wipDirectory, 0700); err != nil {
37+
return nil, fmt.Errorf("failed to create wip directory: %w", err)
38+
}
39+
return os.CreateTemp(wipDirectory, key+"-*")
40+
}

0 commit comments

Comments
 (0)