Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.
Prev Previous commit
Next Next commit
resolve observations
  • Loading branch information
karchx committed Jun 29, 2022
commit 72c120cda08a4435531a9462ab7f78edf9285ed5
42 changes: 23 additions & 19 deletions jar/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,36 @@ import (
"io"
"os"
"path/filepath"
"strings"
)

func BackupFiles(jar string) {
pathdst := filepath.Join(".", "backup")
if _, err := os.Stat(pathdst); os.IsNotExist(err) {
err := os.Mkdir(pathdst, os.ModePerm)
if err != nil {
fmt.Errorf("create backup %v", err)
}
}
// backupFile makes copies JARs file in the backup folder
func backupFile(jarPath string, jarSource string, jarFile string) error {
Comment thread
karchx marked this conversation as resolved.
Outdated
pathdst := filepath.Join(jarPath, "backup")
Comment thread
karchx marked this conversation as resolved.
Outdated

jarSplit := strings.Split(jar, "/")

if len(jarSplit) == 3 {
pathdst = pathdst + "/" + jarSplit[2] + ".bak"
err := makeDirIfNotExist(pathdst)
if err != nil {
return err
}

if len(jarSplit) == 4 {
pathdst = pathdst + "/" + jarSplit[3] + ".bak"
}
jarDst := pathdst + "/" + jarFile + ".bak"

_, err := copyJars(jar, pathdst)
_, err = copyJars(jarSource, jarDst)
if err != nil {
fmt.Errorf("copy file %v ", err)
return err
}
return nil
}

// makeDirIfNotExist make folder to copies file from
// directory to scan
func makeDirIfNotExist(src string) error {
if _, err := os.Stat(src); os.IsNotExist(err) {
err := os.Mkdir(src, os.ModePerm)
if err != nil {
return fmt.Errorf("creating backup directory: %v", err)
}
}
return nil
}

func copyJars(src, dst string) (int64, error) {
Expand All @@ -53,7 +57,7 @@ func copyJars(src, dst string) (int64, error) {
}

if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
return 0, fmt.Errorf("%s is not a regular file:", src)
Comment thread
karchx marked this conversation as resolved.
Outdated
}

source, err := os.Open(src)
Expand Down
12 changes: 8 additions & 4 deletions jar/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Walker struct {
// Rewrite indicates if the Walker should rewrite JARs in place as it
// iterates through the filesystem.
Rewrite bool

// Backup indicates if the Walker should create a backups from JARs
Comment thread
karchx marked this conversation as resolved.
Outdated
Backup bool
// SkipDir, if provided, allows the walker to skip certain directories
// as it scans.
Expand Down Expand Up @@ -135,9 +135,6 @@ func (w *walker) skipDir(path string, d fs.DirEntry) bool {
}

func (w *walker) visit(p string, d fs.DirEntry) error {
if w.Backup {
BackupFiles(w.filepath(p))
}

if d.IsDir() || !d.Type().IsRegular() {
return nil
Expand Down Expand Up @@ -184,6 +181,13 @@ func (w *walker) visit(p string, d fs.DirEntry) error {
return nil
}

if w.Backup {
err := backupFile(w.dir, w.filepath(p), p)
if err != nil {
return fmt.Errorf("backup: %v", err)
}
}

dest := w.filepath(p)
// Ensure temp file is created in the same directory as the file we want to
// rewrite to improve the chances of ending up on the same filesystem. On
Expand Down