Skip to content

Commit 92702af

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 a15051c commit 92702af

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,15 +252,23 @@ func (dc *directoryCache) Get(key string, opts ...Option) (Reader, error) {
265252
}
266253
}
267254

268-
// 3. Open the cache file and read the target region
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+
// Open the cache file and read the target region
269264
// TODO: If the target cache is write-in-progress, should we wait for the completion
270265
// or simply report the cache miss?
271-
file, err := os.Open(dc.cachePath(key))
266+
file, err := os.Open(filepath)
272267
if err != nil {
273268
return nil, fmt.Errorf("failed to open blob file for %q: %w", key, err)
274269
}
275270

276-
// 4. If in direct mode, don't cache file descriptor
271+
// If in direct mode, don't cache file descriptor
277272
// This option is useful for preventing memory cache from being polluted by data
278273
// that won't be accessed immediately.
279274
if dc.direct || opt.direct {
@@ -288,7 +283,7 @@ func (dc *directoryCache) Get(key string, opts ...Option) (Reader, error) {
288283
}, nil
289284
}
290285

291-
// 5. Cache file descriptor
286+
// Cache file descriptor
292287
// TODO: should we cache the entire file data on memory?
293288
// but making I/O (possibly huge) on every fetching
294289
// might be costly.
@@ -330,7 +325,7 @@ func (dc *directoryCache) Add(key string, opts ...Option) (Writer, error) {
330325
}
331326

332327
// Create temporary file
333-
w, err := dc.wipFile(key)
328+
w, err := WipFile(dc.wipDirectory, key)
334329
if err != nil {
335330
return nil, err
336331
}
@@ -344,7 +339,7 @@ func (dc *directoryCache) Add(key string, opts ...Option) (Writer, error) {
344339
}
345340

346341
// Commit file
347-
targetPath := dc.cachePath(key)
342+
targetPath := BuildCachePath(dc.directory, key)
348343
if err := os.MkdirAll(filepath.Dir(targetPath), 0700); err != nil {
349344
return fmt.Errorf("failed to create cache directory: %w", err)
350345
}
@@ -400,14 +395,6 @@ func (dc *directoryCache) isClosed() bool {
400395
return closed
401396
}
402397

403-
func (dc *directoryCache) cachePath(key string) string {
404-
return filepath.Join(dc.directory, key[:2], key)
405-
}
406-
407-
func (dc *directoryCache) wipFile(key string) (*os.File, error) {
408-
return os.CreateTemp(dc.wipDirectory, key+"-*")
409-
}
410-
411398
func NewMemoryCache() BlobCache {
412399
return &MemoryCache{
413400
Membuf: map[string]*bytes.Buffer{},
@@ -498,7 +485,7 @@ func (dc *directoryCache) CreateHardlink(key string) error {
498485
return nil
499486
}
500487
log.L.Debugf("Creating hardlink for key %q", key)
501-
return dc.hlManager.CreateLink(key, dc.cachePath(key))
488+
return dc.hlManager.CreateLink(key, BuildCachePath(dc.directory, key))
502489
}
503490

504491
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)