Skip to content

Commit

Permalink
Fix cache dir writing permissions
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin Zhao <[email protected]>
  • Loading branch information
GZGavinZhao committed Nov 11, 2023
1 parent a9bf4a4 commit f0c16eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
34 changes: 20 additions & 14 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,34 @@ func (p *Package) CreateDirs(o *Overlay) error {
p.GetSourceDir(o),
}

// Add cache directories.
if p.Type == PackageTypeYpkg {
for _, cache := range Caches {
dirs = append(dirs, filepath.Join(o.MountPoint, cache.CacheDir[1:]))
}
}

for _, p := range dirs {
if err := os.MkdirAll(p, 0o0755); err != nil {
return fmt.Errorf("Failed to create required directory %s. Reason: %w\n", p, err)
return fmt.Errorf("Failed to create required directory %s. Reason: %w", p, err)
}
}

// Ensure we have root owned cache directories.
//
// Currently we are only using build caches for YPKG builds to reduce
// maintenance burden.
// Create cache directories
if p.Type == PackageTypeYpkg {
for _, cache := range Caches {
if err := os.MkdirAll(filepath.Join(CacheDirectory, cache.Name), 0o0755); err != nil {
return fmt.Errorf("Failed to create cache directory %s for %s, reason: %w\n", cache.CacheDir, cache.Name, err)
inRootCacheDir := filepath.Join(o.MountPoint, cache.CacheDir[1:])
hostCacheDir := filepath.Join(CacheDirectory, cache.Name)

// Cache directories in build root.
if err := os.MkdirAll(inRootCacheDir, 0o0755); err != nil {
return fmt.Errorf("Failed to create cache directory %s in build root, reason: %w", inRootCacheDir, err)
}

// Cache directory in host.
// Ensure we have root owned cache directories.
if err := os.MkdirAll(hostCacheDir, 0o0755); err != nil {
return fmt.Errorf("Failed to create cache directory %s for %s, reason: %w", cache.CacheDir, cache.Name, err)
}

// Ensure the build user can write to the cache directories.
if err := os.Chown(hostCacheDir, BuildUserID, BuildUserGID); err != nil {
return fmt.Errorf("Failed to chown cache directory %s in build root, reason: %w", inRootCacheDir, err)
}

}
}

Expand Down
12 changes: 11 additions & 1 deletion builder/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ var (
CacheDir: path.Join(BuildUserHome, ".cache", "sccache"),
}

Caches = []Cache{Ccache, Sccache}
Bazel = Cache{
Name: "bazel",
CacheDir: path.Join(BuildUserHome, ".cache", "bazel"),
}

GoBuild = Cache{
Name: "go-build",
CacheDir: path.Join(BuildUserHome, ".cache", "go-build"),
}

Caches = []Cache{Bazel, Ccache, GoBuild, Sccache}
)

type Cache struct {
Expand Down

0 comments on commit f0c16eb

Please sign in to comment.