Skip to content

Commit

Permalink
Merge pull request #43 from jxsl13/fix-close-sort
Browse files Browse the repository at this point in the history
Fix-close-sort
  • Loading branch information
jxsl13 authored Oct 22, 2024
2 parents 46fde3a + 2646ed2 commit 93d2a61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
30 changes: 18 additions & 12 deletions fs_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,7 @@ func copyFile(fs FS, name string, info fs.FileInfo, sourceFile File) (err error)
//
targetMode := info.Mode()

// same as create but with custom permissions
file, err := fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, targetMode.Perm())
if err != nil {
return err
}

_, err = io.Copy(file, sourceFile)
if err != nil {
return err
}

err = file.Close()
err = writeFile(fs, name, targetMode.Perm(), sourceFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -218,6 +207,23 @@ func copyFile(fs FS, name string, info fs.FileInfo, sourceFile File) (err error)
return nil
}

func writeFile(fs FS, name string, perm fs.FileMode, content io.Reader) (err error) {
// same as create but with custom permissions
file, err := fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm.Perm())
if err != nil {
return err
}
defer func() {
err = errors.Join(err, file.Close())
}()

_, err = io.Copy(file, content)
if err != nil {
return err
}
return nil
}

func copySymlink(source, target FS, name string, info fs.FileInfo) (err error) {
defer func() {
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func (a ByLeastFilePathSeparators) Less(i, j int) bool {
// LessFilePathSeparators compares two file paths by the number of file path separators
// returns true if a has less file path separators than b
func LessFilePathSeparators(a, b string) bool {
ai := TrimVolume(a)
aj := TrimVolume(a)
aa := TrimVolume(a)
ab := TrimVolume(b)

ca := strings.Count(ai, separator)
cb := strings.Count(aj, separator)
ca := strings.Count(aa, separator)
cb := strings.Count(ab, separator)

/*
Edge case where the root path is compared to a file in the root path.
Expand All @@ -47,11 +47,11 @@ func LessFilePathSeparators(a, b string) bool {
*/

// root = smallest number of separators
if ca == 1 && ai == separator {
if ca == 1 && aa == separator {
ca = -1
}

if cb == 1 && aj == separator {
if cb == 1 && ab == separator {
cb = -1
}

Expand Down

0 comments on commit 93d2a61

Please sign in to comment.