Skip to content

Commit

Permalink
Use SymWalker (#116)
Browse files Browse the repository at this point in the history
* use symwalker

Signed-off-by: orme292 <orme292@users.noreply.github.com>

* replace MaxGroup return var name

Signed-off-by: orme292 <orme292@users.noreply.github.com>

---------

Signed-off-by: orme292 <orme292@users.noreply.github.com>
  • Loading branch information
orme292 committed Apr 19, 2024
1 parent 295a728 commit 2814ca9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 64 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/orme292/symwalker v0.1.72 // indirect
github.com/sony/gobreaker v0.4.2-0.20210216022020-dd874f9dd33b // indirect
golang.org/x/sys v0.12.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APP
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/oracle/oci-go-sdk/v49 v49.2.0 h1:l4PUk81EKdTDD4mDg5wrELpdWFqYeE9KYejfNgtsyUI=
github.com/oracle/oci-go-sdk/v49 v49.2.0/go.mod h1:E8q2DXmXnSozLdXHUFF+o3L2gzcWbiFIPFYOYWdqOfc=
github.com/orme292/symwalker v0.1.7 h1:db0BmiItUcxq9zj/GZArlzsZgB+/HguGq6hp1/26AUg=
github.com/orme292/symwalker v0.1.7/go.mod h1:VWOAx9WTCecK0Ruf0i3eub53zbB4rg98/CiMZWrbxoU=
github.com/orme292/symwalker v0.1.61 h1:T8ESz1NtXUj6scvhj3TFUpmkMU5F7Ld0uX7sYRhvHVA=
github.com/orme292/symwalker v0.1.61/go.mod h1:VWOAx9WTCecK0Ruf0i3eub53zbB4rg98/CiMZWrbxoU=
github.com/orme292/symwalker v0.1.71 h1:6c5SbmpCjlPARPvQpYQskLVr7/16bBbtcrR/sYYwIEQ=
github.com/orme292/symwalker v0.1.71/go.mod h1:VWOAx9WTCecK0Ruf0i3eub53zbB4rg98/CiMZWrbxoU=
github.com/orme292/symwalker v0.1.72 h1:kQSFqe59eueb+XvcscyeQQyWSh+NBRqyHgyk9gJgnMA=
github.com/orme292/symwalker v0.1.72/go.mod h1:VWOAx9WTCecK0Ruf0i3eub53zbB4rg98/CiMZWrbxoU=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
8 changes: 4 additions & 4 deletions s3packs/objectify/file_obj_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func (fol FileObjList) repairRedundantKeys() {
}
}

func (fol FileObjList) MaxGroup() (max int) {
func (fol FileObjList) MaxGroup() (maxVal int) {
if len(fol) == 0 {
return 0
}

max = fol[0].Group
maxVal = fol[0].Group
for _, fo := range fol {
if fo.Group > max {
max = fo.Group
if fo.Group > maxVal {
maxVal = fo.Group
}
}

Expand Down
74 changes: 17 additions & 57 deletions s3packs/objectify/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"io/fs"
"os"
"path"
"path/filepath"
"strings"

"github.com/orme292/s3packer/conf"
sw "github.com/orme292/symwalker"
)

// getFileSize returns the size of a file in bytes.
Expand All @@ -30,32 +30,17 @@ func getFileSize(ap string) (size int64, err error) {
}

func getFiles(ac *conf.AppConfig, p string) (files []string, err error) {
ap, err := filepath.Abs(filepath.Clean(p))
results, err := sw.SymWalker(sw.NewSymConf(
sw.WithStartPath(p),
sw.WithFollowedSymLinks(),
sw.WithDepth(sw.FLAT),
))
if err != nil {
return nil, errors.New("Error getting absolute path: " + err.Error())
}

ap, err = filepath.EvalSymlinks(ap)
if err != nil {
return nil, errors.New("Error evaluating link: " + err.Error())
return
}

objs, err := os.ReadDir(ap)
if err != nil {
return nil, errors.New("Error reading directory: " + err.Error())
}
for _, file := range objs {
info, err := file.Info()
if err != nil {
ac.Log.Warn("Unable to stat, skipping: %q", filepath.Join(ap, file.Name()))
continue
}
mode := info.Mode()
if mode.IsRegular() && !info.IsDir() {
files = append(files, filepath.Join(ap, file.Name()))
} else if !mode.IsRegular() && !info.IsDir() {
ac.Log.Warn("Skipping non-regular file: %q", filepath.Join(ap, file.Name()))
}
for _, file := range results.Files {
files = append(files, file.Path)
}
return
}
Expand All @@ -69,43 +54,18 @@ func isRegular(ap string) (bool, error) {
}

func getSubDirs(p string) (dirs []string, err error) {
ap, err := filepath.Abs(filepath.Clean(p))
results, err := sw.SymWalker(sw.NewSymConf(
sw.WithStartPath(p),
sw.WithFollowedSymLinks(),
sw.WithoutFiles(),
))
if err != nil {
return nil, errors.New("Error getting absolute path: " + err.Error())
return
}

ap, err = filepath.EvalSymlinks(ap)
if err != nil {
return nil, errors.New("Error evaluating link: " + err.Error())
for _, dir := range results.Dirs {
dirs = append(dirs, dir.Path)
}

err = filepath.WalkDir(ap, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
if d.Type()&fs.ModeSymlink != 0 {
path, err = os.Readlink(path)
if err != nil {
return err
}

rInfo, err := os.Stat(path)
if err != nil {
return err
}

if rInfo.IsDir() {
dirs = append(dirs, path)
}
} else {
dirs = append(dirs, path)
}
}
return nil
})

return
}

Expand Down
4 changes: 1 addition & 3 deletions s3packs/objectify/root_list.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package objectify

import (
"fmt"

"github.com/orme292/s3packer/conf"
)

Expand All @@ -14,7 +12,7 @@ func NewRootList(ac *conf.AppConfig, paths []string) (rl RootList, err error) {
return nil, err
}
for _, d := range dirs {
fmt.Printf("Processing directory: %q\n", d)
ac.Log.Info("Found directory: %q", d)
files, err := getFiles(ac, d)
if err != nil {
return nil, err
Expand Down

0 comments on commit 2814ca9

Please sign in to comment.