Skip to content

Commit b6ca595

Browse files
authored
fix: misc (#9)
1 parent 50787f7 commit b6ca595

10 files changed

Lines changed: 98 additions & 37 deletions

File tree

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
*dist/*
2121
*.exe
2222
*.bat
23-
kcheck
24-
makecheck
23+
/kcheck
24+
/makecheck
2525

2626
# custom
2727
*.xml

cmd/kcheck/command.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package main
33
import (
44
"crypto/md5"
55
"crypto/sha1"
6+
"crypto/sha256"
67
"fmt"
78
"github.com/beevik/etree"
89
"github.com/fatih/color"
910
"github.com/littlecxm/kbinxml-go"
1011
"github.com/littlecxm/kcheck/configs"
1112
"github.com/littlecxm/kcheck/pkg/checksum"
12-
"github.com/littlecxm/kcheck/pkg/filetype"
13+
"github.com/littlecxm/kcheck/pkg/formatType"
1314
"github.com/littlecxm/kcheck/pkg/reporter"
1415
"github.com/littlecxm/kcheck/pkg/utils"
1516
"github.com/urfave/cli/v2"
@@ -26,7 +27,7 @@ func commandHandler(c *cli.Context) error {
2627

2728
var listPath string
2829
if c.NArg() == 0 {
29-
guessListPath, err := filetype.GuessListPath()
30+
guessListPath, err := formatType.GuessListPath()
3031
if err != nil {
3132
fmt.Println("use", color.GreenString("help"), "to get more info")
3233
os.Exit(-1)
@@ -52,7 +53,7 @@ func commandHandler(c *cli.Context) error {
5253
}()
5354

5455
if listType == "" {
55-
listType, err = filetype.GuessType(listPath)
56+
listType, err = formatType.GuessType(listPath)
5657
if err != nil {
5758
log.Fatalf("get list type error: %s", err)
5859
}
@@ -152,11 +153,14 @@ func commandHandler(c *cli.Context) error {
152153
}
153154
metaCreateAt := time.Unix(0, kcheckList.CreatedAt*int64(time.Millisecond))
154155
fCount = len(kcheckList.Files)
155-
h := sha1.New()
156+
h := sha256.New()
156157
fmt.Println("KCheck list created at:", metaCreateAt)
157158
for _, files := range kcheckList.Files {
158-
formatPath := strings.TrimPrefix(filepath.FromSlash(files.Path), string(os.PathSeparator))
159-
if err := checksum.CheckByHash(formatPath, files.SHA1, h); err != nil {
159+
formatPath := filepath.Join(
160+
configs.WorkDir,
161+
strings.TrimPrefix(filepath.FromSlash(files.Path), string(os.PathSeparator)),
162+
)
163+
if err := checksum.CheckByHash(formatPath, files.SHA256, h); err != nil {
160164
failCount++
161165
res <- &reporter.CheckResult{
162166
Error: err,
File renamed without changes.

cmd/makecheck/command.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"crypto/sha1"
4+
"crypto/sha256"
55
"encoding/hex"
66
"fmt"
77
"github.com/littlecxm/kcheck/configs"
@@ -17,7 +17,9 @@ import (
1717
)
1818

1919
func commandHandler(c *cli.Context) error {
20-
fmt.Sprintf("makecheck %s %s (built: %s)", version, commitID, buildDate)
20+
builtVer := fmt.Sprintf("makecheck %s %s (built: %s)", version, commitID, buildDate)
21+
fmt.Println(builtVer)
22+
fmt.Println("--------")
2123

2224
if c.NArg() == 0 {
2325
cli.ShowAppHelpAndExit(c, 0)
@@ -26,56 +28,91 @@ func commandHandler(c *cli.Context) error {
2628
srcDir = c.Args().Get(0)
2729
}
2830

29-
h := sha1.New()
3031
var kcheckList configs.KCheckList
3132
res := make(chan *reporter.CheckResult, 999)
3233

3334
go reporter.Handler("failed_make.list", res)
3435

36+
kcheckList.CreatedAt = time.Now().Unix()
37+
kcheckList.Version = builtVer
38+
39+
// file walk
40+
var fCount, passCount, failCount = 0, 0, 0
3541
err := filepath.Walk(srcDir, func(filePath string, fi os.FileInfo, err error) error {
3642
if err != nil {
43+
failCount++
3744
return err
3845
}
39-
if strings.Contains(strings.ToLower(fi.Name()), ".ds_store") {
46+
if utils.CheckPathBlacklist(filePath) {
4047
return nil
4148
}
4249
srcPath := filepath.ToSlash(strings.TrimPrefix(strings.TrimPrefix(filePath, srcDir), string(filepath.Separator)))
50+
absPath := filepath.Join(srcDir, srcPath)
4351
if !fi.Mode().IsRegular() {
4452
return nil
4553
}
46-
f, err := os.Open(filePath)
47-
_, err = io.Copy(h, f)
54+
fCount++
55+
56+
f, err := os.Open(absPath)
4857
if err != nil {
58+
failCount++
59+
return err
60+
}
61+
defer f.Close()
62+
63+
h := sha256.New()
64+
if _, err := io.Copy(h, f); err != nil {
4965
res <- &reporter.CheckResult{
5066
Error: err,
5167
Path: srcPath,
5268
}
69+
failCount++
5370
utils.PrintStatus(false, srcPath)
5471
return err
5572
} else {
73+
passCount++
5674
utils.PrintStatus(true, srcPath)
5775
}
58-
kcheckList.Files = append(kcheckList.Files, configs.KCheckFiles{Path: srcPath, SHA1: hex.EncodeToString(h.Sum(nil)), Size: fi.Size()})
76+
kcheckList.Files = append(kcheckList.Files,
77+
configs.KCheckFiles{
78+
Path: srcPath,
79+
SHA256: hex.EncodeToString(h.Sum(nil)),
80+
Size: fi.Size(),
81+
},
82+
)
5983
f.Close()
6084
return nil
6185
})
6286
if err != nil {
6387
log.Fatalln("filepath err:", err)
6488
}
89+
90+
// save list
6591
var outBytes []byte
6692
if isIndent {
67-
outBytes, err = json.Marshal(kcheckList)
68-
} else {
6993
outBytes, err = json.MarshalIndent(kcheckList, "", " ")
94+
} else {
95+
outBytes, err = json.Marshal(kcheckList)
7096
}
7197
err = os.WriteFile(filepath.Join(configs.WorkDir, outFilename), outBytes, os.ModePerm)
7298
if err != nil {
7399
log.Fatalln("save list err:", err)
74100
}
101+
102+
if failCount == 0 {
103+
os.Remove(filepath.Join(configs.WorkDir, "failed_make.list"))
104+
}
105+
75106
fmt.Println("--------")
107+
fmt.Printf(
108+
"Finished.\n"+
109+
"ALL: %d / PASS: %d / FAIL: %d\n",
110+
fCount,
111+
passCount,
112+
failCount,
113+
)
76114
fmt.Println("Check list saved:", outFilename)
77-
fmt.Println("Finished.")
78-
fmt.Println("Exit after 2 seconds...")
115+
fmt.Println("Exit after 3 seconds...")
79116
time.Sleep(3 * time.Second)
80117
return nil
81118
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func main() {
3838
},
3939
&cli.BoolFlag{
4040
Name: "indent",
41-
Aliases: []string{"ind"},
41+
Aliases: []string{"idt"},
4242
Value: false,
4343
Usage: "enable indent for output list",
4444
Destination: &isIndent,

configs/type.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ type MetaData struct {
2020

2121
type KCheckList struct {
2222
CreatedAt int64 `json:"createdAt"`
23+
Version string `json:"version"`
2324
Files []KCheckFiles `json:"files"`
2425
}
2526

2627
type KCheckFiles struct {
27-
Path string `json:"path"`
28-
SHA1 string `json:"sha1"`
29-
Size int64 `json:"size"`
28+
Path string `json:"path"`
29+
SHA256 string `json:"sha256"`
30+
Size int64 `json:"size"`
3031
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/hex"
66
"errors"
77
"github.com/littlecxm/kcheck/configs"
8-
"github.com/littlecxm/kcheck/pkg/fileutil"
8+
"github.com/littlecxm/kcheck/pkg/utils"
99
"hash"
1010
"io"
1111
"os"
@@ -18,7 +18,7 @@ func CheckByHash(relativePath, hashStr string, h hash.Hash) error {
1818
}()
1919

2020
fPath := filepath.Join(configs.WorkDir, relativePath)
21-
if !fileutil.FileExists(fPath) {
21+
if !utils.FileExists(fPath) {
2222
return errors.New("NOT FOUND")
2323
}
2424

@@ -35,7 +35,10 @@ func CheckByHash(relativePath, hashStr string, h hash.Hash) error {
3535
return errors.New("failed copy buffer")
3636
}
3737

38-
byteHash, _ := hex.DecodeString(hashStr)
38+
byteHash, err := hex.DecodeString(hashStr)
39+
if err != nil {
40+
return errors.New("cannot decode hash string")
41+
}
3942
if bytes.Compare(h.Sum(nil), byteHash) != 0 {
4043
return errors.New("CHECK FAIL")
4144
}

pkg/fileutil/file.go

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package filetype
1+
package formatType
22

33
import (
44
"bufio"
55
"encoding/binary"
66
"errors"
77
"github.com/littlecxm/kcheck/configs"
8-
"github.com/littlecxm/kcheck/pkg/fileutil"
8+
"github.com/littlecxm/kcheck/pkg/utils"
99
"os"
1010
"path/filepath"
1111
"strings"
@@ -23,7 +23,7 @@ func getDefaultPaths() []string {
2323
// GuessListPath get the relative path of the list file
2424
func GuessListPath() (string, error) {
2525
for _, p := range getDefaultPaths() {
26-
if fileutil.FileExists(p) {
26+
if utils.FileExists(p) {
2727
return p, nil
2828
}
2929
}

pkg/utils/file.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package utils
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
func FileExists(filename string) bool {
9+
info, err := os.Stat(filename)
10+
return !os.IsNotExist(err) && !info.IsDir()
11+
}
12+
13+
func CheckPathBlacklist(path string) bool {
14+
list := []string{
15+
".ds_store",
16+
".git",
17+
}
18+
for _, s := range list {
19+
if strings.Contains(strings.ToLower(strings.ToLower(path)), s) {
20+
return true
21+
}
22+
}
23+
return false
24+
}

0 commit comments

Comments
 (0)