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

Method Store

pkgcache/cache_repo.go:38–126  ·  view source on GitHub ↗

Store packs a source tree into repo cache. - for archive sources, repoDir is the source dir in buildtrees. - for archive source, the archiveFile is the path to the original archive file.

(nameVersion, repoUrl, repoDir, archiveFile string)

Source from the content-addressed store, hash-verified

36// - for archive sources, repoDir is the source dir in buildtrees.
37// - for archive source, the archiveFile is the path to the original archive file.
38func (r RepoConfig) Store(nameVersion, repoUrl, repoDir, archiveFile string) (string, error) {
39 // skip storing cache when offline.
40 if r.ctx.Offline() {
41 return "", nil
42 }
43
44 // skip when pkgcache is not writable.
45 if !r.writable {
46 return "", nil
47 }
48
49 // Only third-party libraries can be cached.
50 if !r.shouldCacheRepo(nameVersion) {
51 return "", nil
52 }
53
54 // Create folder to store repo archive.
55 cacheRepoDir := r.ctx.PkgCacheConfig().GetDir(context.PkgCacheDirRepos)
56 if err := r.chattrFS.MkdirAll(cacheRepoDir, fileio.CacheDirPerm); err != nil {
57 return "", err
58 }
59
60 if strings.HasSuffix(repoUrl, ".git") {
61 commit, err := git.GetCommitHash(repoDir)
62 if err != nil {
63 return "", fmt.Errorf("read current commit -> %w", err)
64 }
65
66 // Ignore when repo archive is stored before.
67 // Archive name will be like: x264@stable/472338e072b6a83fd47825cc91cef81dc848e564.tar.gz
68 archivePath := filepath.Join(cacheRepoDir, nameVersion, commit+".tar.gz")
69 if fileio.PathExists(archivePath) {
70 return "", nil
71 }
72
73 // Create repo name folder.
74 if err := r.chattrFS.MkdirAll(filepath.Dir(archivePath), fileio.CacheDirPerm); err != nil {
75 return "", err
76 }
77
78 // Compress to temp dir first (outside cache), then copy to final path.
79 // chattr +a allows creating new files but not renaming.
80 if err := dirs.CleanTmpFilesDir(); err != nil {
81 return "", fmt.Errorf("failed to clean tmp files dir -> %w", err)
82 }
83 tempArchivePath := filepath.Join(dirs.TmpFilesDir, fmt.Sprintf("%s-%d.tar.gz", nameVersion, time.Now().UnixMilli()))
84 if err := fileio.Targz(tempArchivePath, repoDir, false); err != nil {
85 return "", err
86 }
87 defer os.Remove(tempArchivePath)
88 if err := r.chattrFS.CopyFile(tempArchivePath, archivePath); err != nil {
89 return "", err
90 }
91
92 return archivePath, nil
93 } else {
94 // Skip when original archive is not available (e.g. file:/// URLs).
95 if !fileio.PathExists(archiveFile) {

Callers

nothing calls this directly

Calls 14

shouldCacheRepoMethod · 0.95
GetCommitHashFunction · 0.92
PathExistsFunction · 0.92
CleanTmpFilesDirFunction · 0.92
TargzFunction · 0.92
ComputeSHA256Function · 0.92
ExtFunction · 0.92
MkdirAllMethod · 0.80
SprintfMethod · 0.80
CopyFileMethod · 0.80
OfflineMethod · 0.65
GetDirMethod · 0.65

Tested by

no test coverage detected