-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Naoki MATSUMOTO <[email protected]>
- Loading branch information
Showing
4 changed files
with
136 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package oci | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
) | ||
|
||
var defaultCachePath = filepath.Join(os.TempDir(), "ctr-cli", "cache") | ||
|
||
type BlobCache struct { | ||
cachePath string | ||
} | ||
|
||
func NewBlobCache() (*BlobCache, error) { | ||
c := &BlobCache{ | ||
cachePath: defaultCachePath, | ||
} | ||
err := os.MkdirAll(c.cachePath, 0755) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create cache dir %s: %v", c.cachePath, err) | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (b *BlobCache) Store(d *v1.Hash, r io.Reader) error { | ||
name := filepath.Join(b.cachePath, d.String()) | ||
f, err := os.Create(name) | ||
if err != nil { | ||
return fmt.Errorf("failed to create blob %s: %v", name, err) | ||
} | ||
defer f.Close() | ||
|
||
_, err = io.Copy(f, r) | ||
if err != nil { | ||
return fmt.Errorf("failed to copy: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (b *BlobCache) StoreBytes(d *v1.Hash, bytes []byte) error { | ||
name := filepath.Join(b.cachePath, d.String()) | ||
f, err := os.Create(name) | ||
if err != nil { | ||
return fmt.Errorf("failed to create blob %s: %v", name, err) | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.Write(bytes) | ||
if err != nil { | ||
return fmt.Errorf("failed to write content: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (b *BlobCache) Get(d *v1.Hash) (io.ReadCloser, error) { | ||
name := filepath.Join(b.cachePath, d.String()) | ||
f, err := os.Open(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return f, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters