Skip to content

Commit

Permalink
Merge pull request #12 from rmohr/sandbox
Browse files Browse the repository at this point in the history
Prepare bazeldnf for creating sandboxes for bazel
  • Loading branch information
rmohr authored Aug 3, 2021
2 parents d6cbe7c + 1ca9b79 commit 75fd5e0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"root.go",
"rpm2tar.go",
"rpmtree.go",
"sandbox.go",
"tar2files.go",
"verify.go",
],
Expand Down
4 changes: 0 additions & 4 deletions cmd/ldd.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ func NewLddCmd() *cobra.Command {
}
for _, dep := range dependencies {
if strings.HasPrefix(dep, tmpRoot) {
if strings.HasPrefix(filepath.Base(dep), "ld-") {
// don't link against ld itself
continue
}
files = append(files, strings.TrimPrefix(dep, tmpRoot))
}
}
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var rootCmd = &cobra.Command{
}

func Execute() {
rootCmd.AddCommand(NewSandboxCmd())
rootCmd.AddCommand(NewFetchCmd())
rootCmd.AddCommand(NewInitCmd())
rootCmd.AddCommand(NewRpmTreeCmd())
Expand Down
41 changes: 41 additions & 0 deletions cmd/sandbox.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"os"
"path/filepath"

"github.com/rmohr/bazeldnf/pkg/rpm"
"github.com/spf13/cobra"
)

type sandboxOpts struct {
tar string
sandboxRoot string
name string
}

var sandboxopts = sandboxOpts{}

func NewSandboxCmd() *cobra.Command {

sandboxCmd := &cobra.Command{
Use: "sandbox",
Short: "Extract a set of RPMs to a specific location",
RunE: func(cmd *cobra.Command, objects []string) error {
rootDir := filepath.Join(sandboxopts.sandboxRoot, "sandbox", sandboxopts.name, "root")
if err := os.RemoveAll(rootDir); err != nil {
return fmt.Errorf("failed to do the initial cleanup on the sandbox: %v", err)
}
if err := os.MkdirAll(rootDir, os.ModePerm); err != nil {
return fmt.Errorf("failed to create the sandbox base directory: %v", err)
}
return rpm.Untar(rootDir, sandboxopts.tar)
},
}

sandboxCmd.Flags().StringVarP(&sandboxopts.tar, "input", "i", "", "Tar file with all dependencies")
sandboxCmd.Flags().StringVar(&sandboxopts.sandboxRoot, "sandbox", ".bazeldnf", "Root directory of the sandbox")
sandboxCmd.Flags().StringVarP(&sandboxopts.name, "name", "n", "default", "Sandbox name")
return sandboxCmd
}
8 changes: 7 additions & 1 deletion internal/bazeldnf.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("@bazel_skylib//lib:shell.bzl", "shell")

def _bazeldnf_impl(ctx):
transitive_dependencies = []
out_file = ctx.actions.declare_file(ctx.label.name + ".bash")
args = []
if ctx.attr.command:
Expand All @@ -11,6 +12,7 @@ def _bazeldnf_impl(ctx):
args += ["--rpmtree", ctx.attr.rpmtree]
if ctx.file.tar:
args += ["-i", ctx.file.tar.path]
transitive_dependencies += [ctx.attr.tar.files]
for lib in ctx.attr.libs:
args += [lib]

Expand All @@ -24,7 +26,10 @@ def _bazeldnf_impl(ctx):
substitutions = substitutions,
is_executable = True,
)
runfiles = ctx.runfiles(files = [ctx.executable._bazeldnf])
runfiles = ctx.runfiles(
files = [ctx.executable._bazeldnf],
transitive_files = depset([], transitive = transitive_dependencies),
)
return [DefaultInfo(
files = depset([out_file]),
runfiles = runfiles,
Expand All @@ -38,6 +43,7 @@ _bazeldnf = rule(
values = [
"",
"ldd",
"sandbox",
],
default = "",
),
Expand Down
25 changes: 21 additions & 4 deletions pkg/rpm/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func Untar(tmpRoot string, tarFile string) error {
}
defer reader.Close()
tarReader := tar.NewReader(reader)
hardLinks := map[string]string{}

for {
entry, err := tarReader.Next()
Expand Down Expand Up @@ -150,9 +151,12 @@ func Untar(tmpRoot string, tarFile string) error {
if err != nil {
return err
}
defer writer.Close()
if _, err := io.Copy(writer, tarReader); err != nil {
return err
}
writer.Close()
os.Chmod(target, os.FileMode(entry.Mode))
return nil
}()
if err != nil {
Expand All @@ -165,23 +169,36 @@ func Untar(tmpRoot string, tarFile string) error {
return err
}
linkname := entry.Linkname
var abs string
if strings.HasPrefix(linkname, "/") {
linkname = filepath.Join(tmpRoot, linkname)
abs = linkname
} else {
abs = filepath.Join(filepath.Dir(target), linkname)
linkname, err = filepath.Rel(filepath.Dir(target), linkname)
}
abs := filepath.Join(filepath.Dir(target), linkname)
if _, err := filepath.Rel(tmpRoot, abs); err != nil {
return err
}
if err = os.Symlink(linkname, target); err != nil {
return err
}
case tar.TypeLink:
dir := filepath.Dir(target)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
hardLinks[target] = entry.Linkname
default:
log.Debugf("Skipping %s with type %v", entry.Name, entry.Typeflag)
}
}

for target, source := range hardLinks {
source := filepath.Join(tmpRoot, source)
if err := os.Link(source, target); err != nil {
return fmt.Errorf("failed to create hard link from %s to %s", target, source)

}
}
return nil
}

Expand Down

0 comments on commit 75fd5e0

Please sign in to comment.