diff options
author | Slack Coder <slackcoder@server.ky> | 2022-04-26 14:48:33 -0500 |
---|---|---|
committer | Slack Coder <slackcoder@server.ky> | 2022-04-26 14:48:33 -0500 |
commit | 0dfe636b3c0ece1fd52d88fbc75fcb9f45be7793 (patch) | |
tree | a729774ab2a89335af32d60b1d95a68c88dc56b3 | |
parent | 5d4066dd1e90fa704b67384327af72b3ec121102 (diff) | |
download | pkgtools-go-0dfe636b3c0ece1fd52d88fbc75fcb9f45be7793.tar.xz |
installpkg: only untar what is needed
Avoid unarchiving the package's '/install' directory. Required files from
the directory are installed already into the system's package database.
-rw-r--r-- | installpkg.go | 55 |
1 files changed, 13 insertions, 42 deletions
diff --git a/installpkg.go b/installpkg.go index 7e10e61..82532a9 100644 --- a/installpkg.go +++ b/installpkg.go @@ -6,9 +6,7 @@ import ( "io" "os" "os/exec" - "path" "path/filepath" - "strings" "time" "github.com/juju/fslock" @@ -101,12 +99,10 @@ func (s *InstallPkgFlags) SetEnvValues() { } } -func runInstallScript(flags *InstallPkgFlags, installPath string) error { - installPath, err := filepath.Abs(installPath) - if err != nil { - return err - } - +func runInstallScript( + flags *InstallPkgFlags, + pkgBase string, +) error { l := fslock.New(FileLockPath(flags.LockDir, filepath.Base(PackageInstallScript))) if err := l.Lock(); err != nil { return err @@ -123,7 +119,7 @@ func runInstallScript(flags *InstallPkgFlags, installPath string) error { } defer os.Chdir(cwd) - installScript := path.Join(installPath, filepath.Base(PackageInstallScript)) + installScript := filepath.Join(flags.Root, ADMDir, "scripts", pkgBase) if _, err := os.Stat(installScript); !os.IsNotExist(err) { cmd := exec.Command("/bin/bash", installScript) cmd.Stdin = os.Stdout @@ -294,13 +290,13 @@ func writeToDatabase(target string, pkg string) error { return nil } -// ExtractSlackwarePkg unarchives the slackware package into the given +// extractSlackwarePkg unarchives the slackware package into the given // directory. The package metadata, under the archives '/install' path, is put // into a temporary directory and returned. -func ExtractSlackwarePkg(flags *InstallPkgFlags, fp string) (string, error) { +func extractSlackwarePkg(flags *InstallPkgFlags, fp string) error { slackPkg, err := OpenSlackwarePkg(fp) if err != nil { - return "", errors.Wrap(err, "extracting package") + return errors.Wrap(err, "extracting package") } defer slackPkg.Close() @@ -310,31 +306,12 @@ func ExtractSlackwarePkg(flags *InstallPkgFlags, fp string) (string, error) { Chown: flags.chown, }) - tmpDir := os.TempDir() - if _, err := os.Stat(tmpDir); os.IsNotExist(err) { - tmpDir = "/" - } else if err != nil { - return "", errors.Wrap(err, "extracting package") - } - tempInstallDir, err := os.MkdirTemp(tmpDir, "installpkg-*") - if err != nil { - return "", errors.Wrap(err, "extracting package") - } - toTempInstallDir := NewTarExtractor(&TarCfg{ - Root: tempInstallDir, - Chmod: flags.chmod, - Chown: flags.chown, - }) - err = FilterTar( slackPkg, TarFilterFunc(func(h *tar.Header, r io.Reader) error { if ok, err := IsParentDir(PackageInstallPath, h.Name); ok { - h.Name = strings.TrimPrefix(h.Name, PackageInstallPath+"/") - if h.Name == "" { - h.Name = "." - } - return toTempInstallDir.FilterTar(h, r) + // important install files are already written + // to the targets package database. } else if err != nil { return errors.Wrap(err, "installing package") } @@ -342,7 +319,7 @@ func ExtractSlackwarePkg(flags *InstallPkgFlags, fp string) (string, error) { return toRoot.FilterTar(h, r) }), ) - return tempInstallDir, errors.Wrap(err, "installing package") + return errors.Wrap(err, "installing package") } func InstallPkg(flags *InstallPkgFlags, pkgs ...string) error { @@ -360,7 +337,7 @@ func InstallPkg(flags *InstallPkgFlags, pkgs ...string) error { return errors.Wrap(err, "writing package to database") } - installPath, err := ExtractSlackwarePkg(flags, pkg) + err := extractSlackwarePkg(flags, pkg) if err != nil { return errors.Wrap(err, "writing package to database") } @@ -373,16 +350,10 @@ func InstallPkg(flags *InstallPkgFlags, pkgs ...string) error { _ = runLDConfig(flags.LockDir) - // TODO: command should still run after failed 'install script' execution - err = runInstallScript(flags, installPath) + err = runInstallScript(flags, PackageBase(pkg)) if err != nil { fmt.Fprintln(os.Stderr, errors.Wrap(err, "running install script")) } - - err = os.RemoveAll(installPath) - if err != nil { - return err - } } return nil |