Skip to content

Commit

Permalink
Merge pull request #147 from nabbar/fix_os_path_operation
Browse files Browse the repository at this point in the history
- Fix local path operations by using a wrong package (diff between package path and path/filepath)
- Bump dependencies
  • Loading branch information
nabbar authored Nov 15, 2022
2 parents c0046ae + eb8097f commit aedad09
Show file tree
Hide file tree
Showing 30 changed files with 178 additions and 98 deletions.
4 changes: 2 additions & 2 deletions archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"

libbz2 "github.com/nabbar/golib/archive/bz2"
libgzp "github.com/nabbar/golib/archive/gzip"
Expand Down Expand Up @@ -187,7 +187,7 @@ func ExtractAll(src libiot.FileProgress, originalName, outputPath string, defaul
}

liblog.DebugLevel.Log("writing original file...")
if dst, err = src.NewFilePathWrite(path.Join(outputPath, originalName), true, true, 0664); err != nil {
if dst, err = src.NewFilePathWrite(filepath.Join(outputPath, originalName), true, true, 0664); err != nil {
return ErrorFileOpen.Error(err)
}

Expand Down
31 changes: 26 additions & 5 deletions archive/archive/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@
package archive

import (
"path"
"os"
"path/filepath"
"regexp"
"strings"
)

const (
pathSeparatorOs = string(os.PathSeparator)
pathSeparatorCommon = "/"
pathParentCommon = ".."
)

type File struct {
Name string
Path string
Expand Down Expand Up @@ -71,15 +78,15 @@ func (a File) RegexFullPath(regex string) bool {
}

func (a File) GetKeyMap() string {
return path.Join(a.Path, a.Name)
return filepath.Join(a.Path, a.Name)
}

func (a File) GetDestFileOnly(baseDestination string) string {
return path.Join(baseDestination, a.Name)
return filepath.Join(baseDestination, a.Name)
}

func (a File) GetDestWithPath(baseDestination string) string {
return path.Join(baseDestination, a.Path, a.Name)
return filepath.Join(baseDestination, a.Path, a.Name)
}

func NewFile(name, path string) File {
Expand All @@ -90,5 +97,19 @@ func NewFile(name, path string) File {
}

func NewFileFullPath(fullpath string) File {
return NewFile(path.Base(fullpath), path.Dir(fullpath))
return NewFile(filepath.Base(fullpath), filepath.Dir(fullpath))
}

func CleanPath(p string) string {
for {
if strings.HasPrefix(p, pathParentCommon) {
p = strings.TrimPrefix(p, pathParentCommon)
} else if strings.HasPrefix(p, pathSeparatorCommon+pathParentCommon) {
p = strings.TrimPrefix(p, pathSeparatorCommon+pathParentCommon)
} else if strings.HasPrefix(p, pathSeparatorOs+pathParentCommon) {
p = strings.TrimPrefix(p, pathSeparatorOs+pathParentCommon)
} else {
return filepath.Clean(p)
}
}
}
23 changes: 11 additions & 12 deletions archive/tar/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ import (
"archive/tar"
"io"
"os"
"path"
"path/filepath"
"runtime"
"strings"

libarc "github.com/nabbar/golib/archive/archive"
liberr "github.com/nabbar/golib/errors"
libiut "github.com/nabbar/golib/ioutils"
libiot "github.com/nabbar/golib/ioutils"
)

func GetFile(src, dst libiut.FileProgress, filenameContain, filenameRegex string) liberr.Error {
func GetFile(src, dst libiot.FileProgress, filenameContain, filenameRegex string) liberr.Error {

if _, e := src.Seek(0, io.SeekStart); e != nil {
return ErrorFileSeek.ErrorParent(e)
Expand Down Expand Up @@ -95,7 +94,7 @@ func GetAll(src io.ReadSeeker, outputFolder string, defaultDirPerm os.FileMode)

//nolint #nosec
/* #nosec */
if err := writeContent(r, h, path.Join(outputFolder, path.Clean(h.Name)), defaultDirPerm); err != nil {
if err := writeContent(r, h, filepath.Join(outputFolder, libarc.CleanPath(h.Name)), defaultDirPerm); err != nil {
return err
}
}
Expand All @@ -104,10 +103,10 @@ func GetAll(src io.ReadSeeker, outputFolder string, defaultDirPerm os.FileMode)
func writeContent(r io.Reader, h *tar.Header, out string, defaultDirPerm os.FileMode) (err liberr.Error) {
var (
inf = h.FileInfo()
dst libiut.FileProgress
dst libiot.FileProgress
)

if e := dirIsExistOrCreate(path.Dir(out), defaultDirPerm); e != nil {
if e := dirIsExistOrCreate(filepath.Dir(out), defaultDirPerm); e != nil {
return e
}

Expand All @@ -126,12 +125,12 @@ func writeContent(r io.Reader, h *tar.Header, out string, defaultDirPerm os.File
} else if err = notDirExistCannotClean(out, h.Typeflag, h.Linkname); err != nil {
return
} else if h.Typeflag&tar.TypeLink == tar.TypeLink {
return createLink(out, path.Clean(h.Linkname), false)
return createLink(out, libarc.CleanPath(h.Linkname), false)
} else if h.Typeflag&tar.TypeSymlink == tar.TypeSymlink {
return createLink(out, path.Clean(h.Linkname), true)
return createLink(out, libarc.CleanPath(h.Linkname), true)
}

if dst, err = libiut.NewFileProgressPathWrite(out, true, true, inf.Mode()); err != nil {
if dst, err = libiot.NewFileProgressPathWrite(out, true, true, inf.Mode()); err != nil {
return ErrorFileOpen.Error(err)
} else if _, e := io.Copy(dst, r); e != nil {
return ErrorIOCopy.ErrorParent(e)
Expand Down Expand Up @@ -207,12 +206,12 @@ func createLink(link, target string, sym bool) liberr.Error {
}

if sym {
err := os.Symlink(path.Clean(target), path.Clean(link))
err := os.Symlink(libarc.CleanPath(target), libarc.CleanPath(link))
if err != nil {
return ErrorLinkCreate.ErrorParent(err)
}
} else {
err := os.Link(path.Clean(target), path.Clean(link))
err := os.Link(libarc.CleanPath(target), libarc.CleanPath(link))
if err != nil {
return ErrorLinkCreate.ErrorParent(err)
}
Expand All @@ -230,5 +229,5 @@ func compareLinkTarget(link, target string) bool {
return false
}

return strings.EqualFold(path.Clean(l), path.Clean(target))
return strings.EqualFold(libarc.CleanPath(l), libarc.CleanPath(target))
}
32 changes: 16 additions & 16 deletions archive/zip/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import (
"archive/zip"
"io"
"os"
"path"
"path/filepath"

"github.com/nabbar/golib/archive/archive"
"github.com/nabbar/golib/errors"
"github.com/nabbar/golib/ioutils"
arcmod "github.com/nabbar/golib/archive/archive"
liberr "github.com/nabbar/golib/errors"
libiot "github.com/nabbar/golib/ioutils"
)

func GetFile(src, dst ioutils.FileProgress, filenameContain, filenameRegex string) errors.Error {
func GetFile(src, dst libiot.FileProgress, filenameContain, filenameRegex string) liberr.Error {
var (
arc *zip.Reader
inf os.FileInfo
Expand All @@ -58,7 +58,7 @@ func GetFile(src, dst ioutils.FileProgress, filenameContain, filenameRegex strin
continue
}

z := archive.NewFileFullPath(f.Name)
z := arcmod.NewFileFullPath(f.Name)

if z.MatchingFullPath(filenameContain) || z.RegexFullPath(filenameRegex) {
if f == nil {
Expand Down Expand Up @@ -98,7 +98,7 @@ func GetFile(src, dst ioutils.FileProgress, filenameContain, filenameRegex strin
return nil
}

func GetAll(src ioutils.FileProgress, outputFolder string, defaultDirPerm os.FileMode) errors.Error {
func GetAll(src libiot.FileProgress, outputFolder string, defaultDirPerm os.FileMode) liberr.Error {
var (
r *zip.Reader
i os.FileInfo
Expand All @@ -120,24 +120,24 @@ func GetAll(src ioutils.FileProgress, outputFolder string, defaultDirPerm os.Fil

//nolint #nosec
/* #nosec */
if err := writeContent(f, path.Join(outputFolder, path.Clean(f.Name)), defaultDirPerm); err != nil {
if err := writeContent(f, filepath.Join(outputFolder, arcmod.CleanPath(f.Name)), defaultDirPerm); err != nil {
return err
}
}

return nil
}

func writeContent(f *zip.File, out string, defaultDirPerm os.FileMode) (err errors.Error) {
func writeContent(f *zip.File, out string, defaultDirPerm os.FileMode) (err liberr.Error) {
var (
dst ioutils.FileProgress
dst libiot.FileProgress
inf = f.FileInfo()

r io.ReadCloser
e error
)

if err = dirIsExistOrCreate(path.Dir(out), defaultDirPerm); err != nil {
if err = dirIsExistOrCreate(filepath.Dir(out), defaultDirPerm); err != nil {
return
}

Expand All @@ -164,7 +164,7 @@ func writeContent(f *zip.File, out string, defaultDirPerm os.FileMode) (err erro
return
}

if dst, err = ioutils.NewFileProgressPathWrite(out, true, true, inf.Mode()); err != nil {
if dst, err = libiot.NewFileProgressPathWrite(out, true, true, inf.Mode()); err != nil {
return ErrorFileOpen.Error(err)
} else if r, e = f.Open(); e != nil {
return ErrorZipFileOpen.ErrorParent(e)
Expand All @@ -181,9 +181,9 @@ func writeContent(f *zip.File, out string, defaultDirPerm os.FileMode) (err erro
return nil
}

func dirIsExistOrCreate(dirname string, dirPerm os.FileMode) errors.Error {
if i, e := os.Stat(path.Dir(dirname)); e != nil && os.IsNotExist(e) {
if e = os.MkdirAll(path.Dir(dirname), dirPerm); e != nil {
func dirIsExistOrCreate(dirname string, dirPerm os.FileMode) liberr.Error {
if i, e := os.Stat(filepath.Dir(dirname)); e != nil && os.IsNotExist(e) {
if e = os.MkdirAll(filepath.Dir(dirname), dirPerm); e != nil {
return ErrorDirCreate.ErrorParent(e)
}
} else if e != nil {
Expand All @@ -195,7 +195,7 @@ func dirIsExistOrCreate(dirname string, dirPerm os.FileMode) errors.Error {
return nil
}

func notDirExistCannotClean(filename string) errors.Error {
func notDirExistCannotClean(filename string) liberr.Error {
if i, e := os.Stat(filename); e != nil && !os.IsNotExist(e) {
return ErrorDestinationStat.ErrorParent(e)
} else if e == nil && i.IsDir() {
Expand Down
4 changes: 2 additions & 2 deletions aws/aws_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
"testing"
Expand Down Expand Up @@ -214,7 +214,7 @@ func LaunchMinio(host, accessKey, secretKey string) {
defer DelTempFolder(tmp)

if _, minio, _, ok := runtime.Caller(0); ok {
if err := exec.CommandContext(ctx, path.Join(path.Dir(minio), "minio"), "server", "--address", host, tmp).Run(); err != nil {
if err := exec.CommandContext(ctx, filepath.Join(filepath.Dir(minio), "minio"), "server", "--address", host, tmp).Run(); err != nil {
if ctx.Err() != nil {
return
}
Expand Down
7 changes: 3 additions & 4 deletions cobra/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -103,21 +102,21 @@ func (c *cobra) ConfigureWriteConfig(basename string, defaultConfig func() io.Re
return err
}

if len(path.Ext(cfgFile)) > 0 && strings.ToLower(path.Ext(cfgFile)) != ".json" {
if len(filepath.Ext(cfgFile)) > 0 && strings.ToLower(filepath.Ext(cfgFile)) != ".json" {
var mod = make(map[string]interface{}, 0)

err = json.Unmarshal(buf, &mod)
if err != nil {
return err
}

switch strings.ToLower(path.Ext(cfgFile)) {
switch strings.ToLower(filepath.Ext(cfgFile)) {
case ".toml":
buf, err = toml.Marshal(mod)
case ".yml", ".yaml":
buf, err = yaml.Marshal(mod)
default:
return fmt.Errorf("extension file '%s' not compatible", path.Ext(cfgFile))
return fmt.Errorf("extension file '%s' not compatible", filepath.Ext(cfgFile))
}
}

Expand Down
6 changes: 3 additions & 3 deletions cobra/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package cobra
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

liblog "github.com/nabbar/golib/logger"
Expand Down Expand Up @@ -142,11 +142,11 @@ func (c *cobra) getLog() liblog.Logger {
}

func (c *cobra) getPackageName() string {
pkg := path.Base(os.Args[0])
pkg := filepath.Base(os.Args[0])

if pkg == "" {
if f, e := os.Executable(); e == nil {
pkg = path.Base(f)
pkg = filepath.Base(f)
} else {
pkg = c.s.GetPackage()
}
Expand Down
31 changes: 23 additions & 8 deletions errors/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,31 @@ package errors

import (
"path"
"path/filepath"
"reflect"
"runtime"
"strings"
)

const (
PathSeparator = "/"
pathVendor = "vendor"
pathMod = "mod"
pathPkg = "pkg"
pkgRuntime = "runtime"
)

var (
currPkgs = path.Base(reflect.TypeOf(UNK_ERROR).PkgPath())
filterPkg = path.Clean(reflect.TypeOf(UNK_ERROR).PkgPath())
filterPkg = path.Clean(ConvPathFromLocal(reflect.TypeOf(UNK_ERROR).PkgPath()))
currPkgs = path.Base(ConvPathFromLocal(filterPkg))
)

func ConvPathFromLocal(str string) string {
return strings.Replace(str, string(filepath.Separator), PathSeparator, -1)
}

func init() {
if i := strings.LastIndex(filterPkg, "/vendor/"); i != -1 {
if i := strings.LastIndex(filterPkg, PathSeparator+pathVendor+PathSeparator); i != -1 {
filterPkg = filterPkg[:i+1]
}
}
Expand Down Expand Up @@ -101,9 +114,9 @@ func getFrameVendor() []runtime.Frame {

if strings.Contains(item.Function, currPkgs) {
continue
} else if strings.Contains(frame.File, "/vendor/") {
} else if strings.Contains(ConvPathFromLocal(frame.File), PathSeparator+pathVendor+PathSeparator) {
continue
} else if strings.HasPrefix(frame.Function, "runtime") {
} else if strings.HasPrefix(frame.Function, pkgRuntime) {
continue
} else if frameInSlice(res, item) {
continue
Expand Down Expand Up @@ -150,10 +163,12 @@ func getNilFrame() runtime.Frame {

func filterPath(pathname string) string {
var (
filterMod = "/pkg/mod/"
filterVendor = "/vendor/"
filterMod = PathSeparator + pathPkg + PathSeparator + pathMod + PathSeparator
filterVendor = PathSeparator + pathVendor + PathSeparator
)

pathname = ConvPathFromLocal(pathname)

if i := strings.LastIndex(pathname, filterMod); i != -1 {
i = i + len(filterMod)
pathname = pathname[i:]
Expand All @@ -171,5 +186,5 @@ func filterPath(pathname string) string {

pathname = path.Clean(pathname)

return strings.Trim(pathname, "/")
return strings.Trim(pathname, PathSeparator)
}
Loading

0 comments on commit aedad09

Please sign in to comment.