Merge branch 'development' into wwlog-newlines

This commit is contained in:
Christian Goll
2022-10-13 14:46:03 +02:00
committed by GitHub
36 changed files with 1804 additions and 391 deletions

View File

@@ -260,34 +260,17 @@ func FindFilterFiles(
defer func() {
err = FirstError(err, os.Chdir(cwd))
}()
err = os.Chdir(path)
if err != nil {
return ofiles, errors.Wrapf(err, "Failed to change path: %s", path)
}
files := []string{}
for _, pattern := range include {
_files, err := filepath.Glob(pattern)
if err != nil {
return ofiles, errors.Wrapf(err, "Failed to apply pattern: %s", pattern)
}
wwlog.Debug("Including pattern: %s -> %d matches", pattern, len(_files))
files = append(files, _files...)
}
for i, pattern := range(ignore) {
if strings.HasPrefix(pattern, "./") {
ignore[i] = pattern[2:]
}
for i := range(ignore) {
ignore[i] = strings.TrimLeft(ignore[i], "/")
ignore[i] = strings.TrimPrefix(ignore[i], "./")
wwlog.Debug("Ignore pattern (%d): %s", i, ignore[i])
}
if ignore_xdev {
wwlog.Debug("Ignoring cross-device (xdev) files")
}
@@ -299,66 +282,72 @@ func FindFilterFiles(
dev := path_stat.Sys().(*syscall.Stat_t).Dev
for _, ifile := range files {
stat, err := os.Stat(ifile)
includeDirs := []string{}
ignoreDirs := []string{}
err = filepath.Walk(".", func(location string, info os.FileInfo, err error) error {
if err != nil {
return ofiles, err
return err
}
if stat.IsDir() {
// recursivly include from the matched directory
if location == "." {
return nil
}
num_init := len(ofiles)
err = filepath.Walk(ifile, func(location string, info os.FileInfo, err error) error {
var file string
var file string
if info.IsDir() {
file = location + "/"
} else {
file = location
}
if err != nil {
return err
}
if location == "." {
return nil
}
if info.IsDir() {
file = location + "/"
} else {
file = location
}
if ignore_xdev && info.Sys().(*syscall.Stat_t).Dev != dev {
wwlog.Debug("Ignored (cross-device): %s", file)
return nil
}
for i, pattern := range(ignore) {
m, err := filepath.Match(pattern, location)
if err != nil {
return err
}
if m {
wwlog.Debug("Ignored (%d): %s", i, file)
return nil
}
}
ofiles = append(ofiles, file)
if ignore_xdev && info.Sys().(*syscall.Stat_t).Dev != dev {
wwlog.Debug("Ignored (cross-device): %s", file)
return nil
}
for _, ignoreDir := range(ignoreDirs) {
if strings.HasPrefix(location, ignoreDir) {
wwlog.Debug("Ignored (dir): %s", file)
return nil
})
num_final := len(ofiles)
wwlog.Debug("Included: %s -> %d files", ifile, num_final-num_init)
if err != nil {
return ofiles, err
}
}else{
wwlog.Debug("Included: %s", ifile)
ofiles = append(ofiles, ifile)
}
}
for i, pattern := range(ignore) {
m, err := filepath.Match(pattern, location)
if err != nil {
return err
} else if m {
wwlog.Debug("Ignored (%d): %s", i, file)
if info.IsDir() {
ignoreDirs = append(ignoreDirs, file)
}
return nil
}
}
for _, includeDir := range(includeDirs) {
if strings.HasPrefix(location, includeDir) {
wwlog.Debug("Included (dir): %s", file)
ofiles = append(ofiles, location)
return nil
}
}
for i, pattern := range(include) {
m, err := filepath.Match(pattern, location)
if err != nil {
return err
} else if m {
wwlog.Debug("Included (%d): %s", i, file)
ofiles = append(ofiles, location)
if info.IsDir() {
includeDirs = append(includeDirs, file)
}
return nil
}
}
return nil
})
return ofiles, err
}
@@ -715,3 +704,36 @@ func RunWWCTL(args ...string) (out []byte, err error) {
return out, err
}
/*
Get size of given directory in bytes
*/
func DirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
/*
Convert bytes to human friendly format
*/
func ByteToString(b int64) string {
const base = 1024
if b < base {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(base), 0
for n := b / base; n >= base; n /= base {
div *= base
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
}