aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/cacheitem/cacheitem.go
diff options
context:
space:
mode:
author简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
committer简律纯 <hsiangnianian@outlook.com>2023-04-28 01:36:44 +0800
commitdd84b9d64fb98746a230cd24233ff50a562c39c9 (patch)
treeb583261ef00b3afe72ec4d6dacb31e57779a6faf /cli/internal/cacheitem/cacheitem.go
parent0b46fcd72ac34382387b2bcf9095233efbcc52f4 (diff)
downloadHydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.tar.gz
HydroRoll-dd84b9d64fb98746a230cd24233ff50a562c39c9.zip
Diffstat (limited to 'cli/internal/cacheitem/cacheitem.go')
-rw-r--r--cli/internal/cacheitem/cacheitem.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/cli/internal/cacheitem/cacheitem.go b/cli/internal/cacheitem/cacheitem.go
new file mode 100644
index 0000000..2fb2c3b
--- /dev/null
+++ b/cli/internal/cacheitem/cacheitem.go
@@ -0,0 +1,76 @@
+// Package cacheitem is an abstraction over the creation and restoration of a cache
+package cacheitem
+
+import (
+ "archive/tar"
+ "bufio"
+ "crypto/sha512"
+ "errors"
+ "io"
+ "os"
+
+ "github.com/vercel/turbo/cli/internal/turbopath"
+)
+
+var (
+ errMissingSymlinkTarget = errors.New("symlink restoration is delayed")
+ errCycleDetected = errors.New("links in the cache are cyclic")
+ errTraversal = errors.New("tar attempts to write outside of directory")
+ errNameMalformed = errors.New("file name is malformed")
+ errNameWindowsUnsafe = errors.New("file name is not Windows-safe")
+ errUnsupportedFileType = errors.New("attempted to restore unsupported file type")
+)
+
+// CacheItem is a `tar` utility with a little bit extra.
+type CacheItem struct {
+ // Path is the location on disk for the CacheItem.
+ Path turbopath.AbsoluteSystemPath
+ // Anchor is the position on disk at which the CacheItem will be restored.
+ Anchor turbopath.AbsoluteSystemPath
+
+ // For creation.
+ tw *tar.Writer
+ zw io.WriteCloser
+ fileBuffer *bufio.Writer
+ handle *os.File
+ compressed bool
+}
+
+// Close any open pipes
+func (ci *CacheItem) Close() error {
+ if ci.tw != nil {
+ if err := ci.tw.Close(); err != nil {
+ return err
+ }
+ }
+
+ if ci.zw != nil {
+ if err := ci.zw.Close(); err != nil {
+ return err
+ }
+ }
+
+ if ci.fileBuffer != nil {
+ if err := ci.fileBuffer.Flush(); err != nil {
+ return err
+ }
+ }
+
+ if ci.handle != nil {
+ if err := ci.handle.Close(); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+// GetSha returns the SHA-512 hash for the CacheItem.
+func (ci *CacheItem) GetSha() ([]byte, error) {
+ sha := sha512.New()
+ if _, err := io.Copy(sha, ci.handle); err != nil {
+ return nil, err
+ }
+
+ return sha.Sum(nil), nil
+}