Skip to content

Commit

Permalink
修复 merge 时处理文件顺序不对的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
letmestudy committed Oct 21, 2024
1 parent 2773d19 commit ce58e2c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.0

require (
github.com/dustin/go-humanize v1.0.1
github.com/spf13/cast v1.7.0
github.com/spf13/cobra v1.8.1
)

Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
11 changes: 10 additions & 1 deletion merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strings"

"github.com/spf13/cast"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -42,6 +45,12 @@ func mergeFiles(outputFilePath string, inputDir string) error {
return errors.New("no files to merge")
}

sort.Slice(files, func(i, j int) bool {
iext := strings.Replace(filepath.Ext(files[i]), ".part", "", -1)
jext := strings.Replace(filepath.Ext(files[j]), ".part", "", -1)
return cast.ToInt(iext) < cast.ToInt(jext)
})

fmt.Println("output to: ", outputFilePath)
outputFile, err := os.Create(outputFilePath)
if err != nil {
Expand All @@ -50,7 +59,7 @@ func mergeFiles(outputFilePath string, inputDir string) error {
defer outputFile.Close()

for idx, filePath := range files {
fmt.Printf("start progress: %d/%d\n", idx+1, len(files))
fmt.Printf("start progress: %d/%d, file %s\n", idx+1, len(files), filePath)
inputFile, err := os.Open(filePath)
if err != nil {
return err
Expand Down
45 changes: 31 additions & 14 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strings"

"github.com/spf13/cast"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -100,23 +103,37 @@ func calculateMD5(filePath string) (string, error) {
func computeDirectoryMD5(dirPath string) (string, error) {
hasher := md5.New()

err := filepath.Walk(dirPath, func(filePath string, info os.FileInfo, err error) error {
matchers, err := filepath.Glob(filepath.Join(dirPath, "*"))
if err != nil {
return "", err
}

sort.Slice(matchers, func(i, j int) bool {
iext := strings.Replace(filepath.Ext(matchers[i]), ".part", "", -1)
jext := strings.Replace(filepath.Ext(matchers[j]), ".part", "", -1)
return cast.ToInt(iext) < cast.ToInt(jext)
})

for _, m := range matchers {
fmt.Printf("start to deal file %s\n", m)
fi, err := os.Stat(m)
if err != nil {
return err
return "", err
}
if !info.IsDir() { // 仅处理文件
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()

if _, err := io.Copy(hasher, file); err != nil {
return err
}
if fi.IsDir() {
fmt.Printf("path %s is directory, skip ...\n", m)
continue
}
return nil
})
file, err := os.Open(m)
if err != nil {
return "", err
}
defer file.Close()
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}
}

if err != nil {
return "", err
}
Expand Down

0 comments on commit ce58e2c

Please sign in to comment.