MCPcopy Create free account
hub / github.com/celer-pkg/celer / Restore

Method Restore

pkgcache/cache_artifact.go:38–95  ·  view source on GitHub ↗

Restore restores the cached package to package directory if cache hit, and return the archive path. If cache miss, just return empty string without error.

(nameVersion, buildHash, packageDir string)

Source from the content-addressed store, hash-verified

36// Restore restores the cached package to package directory if cache hit, and return the archive path.
37// If cache miss, just return empty string without error.
38func (a ArtifactConfig) Restore(nameVersion, buildHash, packageDir string) (string, error) {
39 // skip restore cache when offline.
40 if a.ctx.Offline() {
41 return "", nil
42 }
43
44 platformName := a.ctx.Platform().GetName()
45 projectName := a.ctx.Project().GetName()
46 buildType := a.ctx.BuildType()
47
48 artifactCacheDir := a.ctx.PkgCacheConfig().GetDir(context.PkgCacheDirArtifacts)
49 archiveDir := filepath.Join(artifactCacheDir, platformName, projectName, buildType, nameVersion)
50 archivePath := filepath.Join(archiveDir, buildHash+".tar.gz")
51 if !fileio.PathExists(archivePath) {
52 color.PrintWarning("======== no artifact found for %s and it'll build from source ========", nameVersion)
53 return "", nil // not an error even not exist.
54 }
55
56 // The meta file hash should be the same as hash that calcuated dynamically.
57 metaPath := filepath.Join(archiveDir, "metas", buildHash+".meta")
58 metaBytes, err := os.ReadFile(metaPath)
59 if err != nil {
60 if errors.Is(err, os.ErrNotExist) {
61 return "", fmt.Errorf("cache archive exists but metadata is missing: %s", metaPath)
62 }
63 return "", err
64 }
65 metaHash := sha256.Sum256(metaBytes)
66 if fmt.Sprintf("%x", metaHash) != buildHash {
67 return "", fmt.Errorf("cache metadata checksum mismatch for %s", nameVersion)
68 }
69
70 // Create tmp dir for extracting inside.
71 if err := dirs.CleanTmpFilesDir(); err != nil {
72 return "", fmt.Errorf("failed to clean tmp files dir -> %w", err)
73 }
74 tempDir, err := os.MkdirTemp(dirs.TmpFilesDir, "pkgcache-extract-*")
75 if err != nil {
76 return "", err
77 }
78 defer os.RemoveAll(tempDir)
79
80 // Extract to a tmp dir and move back to dest dir.
81 if err := fileio.Extract(archivePath, tempDir); err != nil {
82 return "", err
83 }
84 if err := os.RemoveAll(packageDir); err != nil {
85 return "", err
86 }
87 if err := os.MkdirAll(filepath.Dir(packageDir), os.ModePerm); err != nil {
88 return "", err
89 }
90 if err := os.Rename(tempDir, packageDir); err != nil {
91 return "", err
92 }
93
94 return archivePath, nil
95}

Callers

nothing calls this directly

Calls 14

PathExistsFunction · 0.92
PrintWarningFunction · 0.92
CleanTmpFilesDirFunction · 0.92
ExtractFunction · 0.92
IsMethod · 0.80
SprintfMethod · 0.80
MkdirAllMethod · 0.80
OfflineMethod · 0.65
GetNameMethod · 0.65
PlatformMethod · 0.65
ProjectMethod · 0.65
BuildTypeMethod · 0.65

Tested by

no test coverage detected