Skip to content

Commit

Permalink
Installer: SymlinkWriter: Make it gob-encodable
Browse files Browse the repository at this point in the history
  • Loading branch information
livingsilver94 committed Nov 2, 2023
1 parent 018b114 commit e2208c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
42 changes: 36 additions & 6 deletions installer/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/gob"
"errors"
"io/fs"
"log/slog"
"os"
"path/filepath"

Expand Down Expand Up @@ -48,19 +47,36 @@ func WritePathPrivileged(dst service.FilePath, src string, wr PrivilegedFileWrit
}

func RegisterPrivilegedTypes() {
privilege.RegisterInterfaceImpl(SymlinkWriter{})
privilege.RegisterInterfaceImpl(CopyWriter{})
privilege.RegisterInterfaceImpl(privilegedPathWriter{})
}

type SymlinkWriter struct {
log *slog.Logger
srcPath string
}

func NewSymlinkWriter(log *slog.Logger) *SymlinkWriter {
return &SymlinkWriter{
log: log,
func NewSymlinkWriter() *SymlinkWriter {
return &SymlinkWriter{}
}

func (w *SymlinkWriter) GobEncode() ([]byte, error) {
buf := &bytes.Buffer{}
enc := gob.NewEncoder(buf)
err := enc.Encode(w.srcPath)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (w *SymlinkWriter) GobDecode(data []byte) error {
dec := gob.NewDecoder(bytes.NewReader(data))
err := dec.Decode(&w.srcPath)
if err != nil {
return err
}
return nil
}

func (w *SymlinkWriter) loadSource(src string) error {
Expand All @@ -77,11 +93,25 @@ func (w *SymlinkWriter) writeDestination(dst string) error {
if !errors.Is(err, fs.ErrExist) {
return err
}
w.log.Info("Already exists", "path", dst)
eq, errEq := w.isSymlinkEqual(dst)
if errEq != nil {
return errEq
}
if !eq {
return err
}
}
return nil
}

func (w *SymlinkWriter) isSymlinkEqual(dst string) (bool, error) {
eq, err := filepath.EvalSymlinks(dst)
if err != nil {
return false, err
}
return eq == w.srcPath, nil
}

type CopyWriter struct {
repl Template
srcContent string
Expand Down
2 changes: 1 addition & 1 deletion installer/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s Symlinks) Run(log *slog.Logger, srv *service.Service) error {
if err != nil {
return err
}
return writeFiles(srv.Links, linkDir, s.repl, NewSymlinkWriter(log))
return writeFiles(srv.Links, linkDir, s.repl, NewSymlinkWriter())
}

type Copies struct {
Expand Down

0 comments on commit e2208c3

Please sign in to comment.