Skip to content

Commit

Permalink
installer: Finally make CopyWriter and SymlinkWriter privileged
Browse files Browse the repository at this point in the history
  • Loading branch information
livingsilver94 committed Nov 4, 2023
1 parent 4eba176 commit 565f423
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
33 changes: 14 additions & 19 deletions installer/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,26 @@ import (
)

func init() {
privilege.RegisterInterfaceImpl(SymlinkWriter{})
privilege.RegisterInterfaceImpl(CopyWriter{})
privilege.RegisterInterfaceImpl(&SymlinkWriter{})
privilege.RegisterInterfaceImpl(&CopyWriter{})
privilege.RegisterInterfaceImpl(privilegedPathWriter{})
}

type FileWriter interface {
loadSource(src string) error
writeDestination(dst string) error
}

type PrivilegedFileWriter interface {
FileWriter
gob.GobEncoder
LoadSource(src string) error
WriteDestination(dst string) error
}

func WritePath(dst service.FilePath, src string, wr FileWriter) error {
err := wr.loadSource(src)
err := wr.LoadSource(src)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Dir(dst.Path), 0755)
if err != nil {
return err
}
err = wr.writeDestination(dst.Path)
err = wr.WriteDestination(dst.Path)
if err != nil {
return err
}
Expand All @@ -48,8 +43,8 @@ func WritePath(dst service.FilePath, src string, wr FileWriter) error {
return nil
}

func WritePathPrivileged(dst service.FilePath, src string, wr PrivilegedFileWriter) error {
var r privilege.Runner = privilegedPathWriter{Dst: dst, Src: src, Wr: &wr}
func WritePathPrivileged(dst service.FilePath, src string, wr FileWriter) error {
var r privilege.Runner = privilegedPathWriter{Dst: dst, Src: src, Wr: wr}
return privilege.Run(r)
}

Expand Down Expand Up @@ -80,15 +75,15 @@ func (w *SymlinkWriter) GobDecode(data []byte) error {
return nil
}

func (w *SymlinkWriter) loadSource(src string) error {
func (w *SymlinkWriter) LoadSource(src string) error {
if _, err := os.Stat(src); err != nil { // Check that srcPath exists.
return err
}
w.srcPath = src
return nil
}

func (w *SymlinkWriter) writeDestination(dst string) error {
func (w *SymlinkWriter) WriteDestination(dst string) error {
err := os.Symlink(w.srcPath, dst)
if err != nil {
if !errors.Is(err, fs.ErrExist) {
Expand Down Expand Up @@ -151,13 +146,13 @@ func (w *CopyWriter) GobDecode(data []byte) error {
return nil
}

func (w *CopyWriter) loadSource(src string) error {
func (w *CopyWriter) LoadSource(src string) error {
content, err := os.ReadFile(src)
w.srcContent = string(content)
return err
}

func (w *CopyWriter) writeDestination(dst string) error {
func (w *CopyWriter) WriteDestination(dst string) error {
dstFile, err := os.Create(dst)
if err != nil {
return err
Expand All @@ -174,9 +169,9 @@ func (w *CopyWriter) writeDestination(dst string) error {
type privilegedPathWriter struct {
Dst service.FilePath
Src string
Wr *PrivilegedFileWriter
Wr FileWriter
}

func (p privilegedPathWriter) Run() error {
return WritePath(p.Dst, p.Src, *p.Wr)
return WritePath(p.Dst, p.Src, p.Wr)
}
4 changes: 2 additions & 2 deletions installer/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func runProcess(name string, arg ...string) error {
return cmd.Run()
}

func writeFiles(files map[string]service.FilePath, baseDir string, repl Template, wr PrivilegedFileWriter) error {
func writeFiles(files map[string]service.FilePath, baseDir string, repl Template, wr FileWriter) error {
var resolvedDst strings.Builder
for srcFile, dstFile := range files {
err := repl.ReplaceString(dstFile.Path, &resolvedDst)
Expand All @@ -139,7 +139,7 @@ func writeFiles(files map[string]service.FilePath, baseDir string, repl Template
return nil
}

func writePath(dst service.FilePath, src string, wr PrivilegedFileWriter) error {
func writePath(dst service.FilePath, src string, wr FileWriter) error {
err := WritePath(dst, src, wr)
if err != nil {
if !errors.Is(err, fs.ErrPermission) {
Expand Down

0 comments on commit 565f423

Please sign in to comment.