Fix: Issue with copying

This commit is contained in:
kayos@tcp.direct 2024-06-19 09:45:23 -07:00
parent 0170b7cd90
commit b618f7348c
No known key found for this signature in database
GPG key ID: 4B841471B4BEE979
3 changed files with 70 additions and 5 deletions

View file

@ -4,7 +4,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"path/filepath"
"regexp" "regexp"
"strings"
"github.com/klauspost/cpuid/v2" "github.com/klauspost/cpuid/v2"
@ -300,12 +302,32 @@ func CopyToSystem(isRoot bool, conffile, sysfile string) {
// ExecAndLogSudo will write to the logger, so just print here // ExecAndLogSudo will write to the logger, so just print here
fmt.Printf("Copying: %s to %s\n", conffile, sysfile) fmt.Printf("Copying: %s to %s\n", conffile, sysfile)
if isRoot {
logger.Printf("Copying %s to %s\n", conffile, sysfile)
fmt.Printf("Copying %s to %s\n", conffile, sysfile)
fDat, err := os.ReadFile(conffile)
common.ErrorCheck(err, fmt.Sprintf("Failed to read %s", conffile))
err = os.WriteFile(sysfile, fDat, 0644)
common.ErrorCheck(err, fmt.Sprintf("Failed to write %s", sysfile))
return
}
if !filepath.IsAbs(conffile) {
conffile, _ = filepath.Abs(conffile)
}
conffile = strings.ReplaceAll(conffile, " ", "\\ ")
cmd := fmt.Sprintf("cp -v %s %s", conffile, sysfile)
err := command.ExecAndLogSudo(isRoot, false, cmd)
errMsg := ""
if err != nil {
errMsg = err.Error()
}
// [command.ExecAndLogSudo] will log the command's output // [command.ExecAndLogSudo] will log the command's output
common.ErrorCheck(command.ExecAndLogSudo(isRoot, false, common.ErrorCheck(err, fmt.Sprintf("Failed to copy %s to %s:\n%s", conffile, sysfile, errMsg))
fmt.Sprintf("cp -v \"%s\" %s", conffile, sysfile),
), // if error, log and exit
fmt.Sprintf("Failed to copy %s to %s", conffile, sysfile),
)
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
// note that if we failed the error check, the following will not appear in the log! // note that if we failed the error check, the following will not appear in the log!

View file

@ -0,0 +1,33 @@
package configs
import (
"os"
"path/filepath"
"testing"
)
func TestCopyToSystem(t *testing.T) {
if err := os.Mkdir("testdir", 0755); err != nil {
t.Fatal(err)
}
tFilePath := filepath.Join("testdir", "testfile")
if err := os.WriteFile(tFilePath, []byte("test"), 0644); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.RemoveAll("testdir"); err != nil {
t.Fatal(err)
}
})
isRoot := os.Getuid() == 0
switch isRoot {
case true:
t.Run("TestCopyToSystem_AsRoot", func(t *testing.T) {
CopyToSystem(true, tFilePath, "/etc/testfile")
})
default:
t.Run("TestCopyToSystem_AsUser", func(t *testing.T) {
CopyToSystem(false, tFilePath, "/etc/testfile")
})
}
}

View file

@ -133,8 +133,14 @@ func ExecAndLogSudo(isRoot, noisy bool, cmd string) error {
fmt.Printf("Executing (elevated): %s\nSee debug.log for detailed output\n", cmd) fmt.Printf("Executing (elevated): %s\nSee debug.log for detailed output\n", cmd)
} }
wd, err := os.Getwd()
if err != nil {
return err
}
cs := strings.Fields(cmd) cs := strings.Fields(cmd)
r := exec.Command(cs[0], cs[1:]...) r := exec.Command(cs[0], cs[1:]...)
r.Dir = wd
cmdCombinedOut, err := r.CombinedOutput() cmdCombinedOut, err := r.CombinedOutput()
outStr := string(cmdCombinedOut) outStr := string(cmdCombinedOut)
@ -148,5 +154,9 @@ func ExecAndLogSudo(isRoot, noisy bool, cmd string) error {
fmt.Printf("%s\n", outStr) fmt.Printf("%s\n", outStr)
} }
if err != nil {
err = fmt.Errorf("failed to execute %s: %w\n%s", cmd, err, outStr)
}
return err return err
} }