MCPcopy Index your code
hub / github.com/celer-pkg/celer / Store

Method Store

pkgcache/cache_artifact.go:99–180  ·  view source on GitHub ↗

Store compresses the package dir and store in cache, the meta is expected to be a string and would be used to calculate the hash key for cache.

(packageDir, meta string)

Source from the content-addressed store, hash-verified

97// Store compresses the package dir and store in cache,
98// the meta is expected to be a string and would be used to calculate the hash key for cache.
99func (a ArtifactConfig) Store(packageDir, meta string) error {
100 // skip storing cache when offline.
101 if a.ctx.Offline() {
102 return nil
103 }
104
105 if !fileio.PathExists(packageDir) {
106 return fmt.Errorf("package dir does not exist: %s", packageDir)
107 }
108
109 // Validate packageDir format and extract metadata.
110 // Path format: packages/platform/project/buildType/nameVersion
111 parts := strings.Split(filepath.ToSlash(packageDir), "/")
112 if len(parts) < 5 {
113 return fmt.Errorf("invalid package dir: %s", packageDir)
114 }
115
116 // Extract from path components.
117 nameVersion := parts[len(parts)-1]
118 buildType := parts[len(parts)-2]
119 projectName := parts[len(parts)-3]
120 platformName := parts[len(parts)-4]
121
122 // Validate nameVersion format (should be name@version)
123 versionParts := strings.Split(nameVersion, "@")
124 if len(versionParts) != 2 {
125 return fmt.Errorf("invalid package dir: %s", packageDir)
126 }
127
128 var (
129 libName = versionParts[0]
130 libVersion = versionParts[1]
131 )
132
133 // Extract tar.gz to a tmp dir.
134 archiveName := fmt.Sprintf("%s@%s.tar.gz", libName, libVersion)
135 if err := dirs.CleanTmpFilesDir(); err != nil {
136 return fmt.Errorf("failed to clean tmp files dir -> %w", err)
137 }
138 tempArchive, err := os.CreateTemp(dirs.TmpFilesDir, archiveName+".*")
139 if err != nil {
140 return err
141 }
142 tempArchivePath := tempArchive.Name()
143 tempArchive.Close()
144 defer os.Remove(tempArchivePath)
145
146 if err := fileio.Targz(tempArchivePath, packageDir, false); err != nil {
147 return err
148 }
149
150 artifactCacheDir := a.ctx.PkgCacheConfig().GetDir(context.PkgCacheDirArtifacts)
151 destDir := filepath.Join(artifactCacheDir, platformName, projectName, buildType, nameVersion)
152 metaDir := filepath.Join(destDir, "metas")
153
154 // Calculate checksum of metadata (this would be the cache key).
155 data := sha256.Sum256([]byte(meta))
156 hash := fmt.Sprintf("%x", data)

Callers

nothing calls this directly

Calls 12

PathExistsFunction · 0.92
CleanTmpFilesDirFunction · 0.92
TargzFunction · 0.92
SprintfMethod · 0.80
MkdirAllMethod · 0.80
CopyFileMethod · 0.80
WriteFileMethod · 0.80
OfflineMethod · 0.65
NameMethod · 0.65
GetDirMethod · 0.65
PkgCacheConfigMethod · 0.65
RemoveMethod · 0.45

Tested by

no test coverage detected