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)
| 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. |
| 38 | func (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) { |
nothing calls this directly
no test coverage detected