|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/md5" |
| 5 | + "crypto/sha1" |
| 6 | + "fmt" |
| 7 | + "github.com/beevik/etree" |
| 8 | + "github.com/fatih/color" |
| 9 | + jsoniter "github.com/json-iterator/go" |
| 10 | + "github.com/littlecxm/kbinxml-go" |
| 11 | + "github.com/littlecxm/kcheck/configs" |
| 12 | + "github.com/littlecxm/kcheck/pkg/checksum" |
| 13 | + "github.com/urfave/cli/v2" |
| 14 | + "io/ioutil" |
| 15 | + "log" |
| 16 | + "os" |
| 17 | + "path/filepath" |
| 18 | + "sort" |
| 19 | + "strings" |
| 20 | + "time" |
| 21 | +) |
| 22 | + |
| 23 | +var json = jsoniter.ConfigCompatibleWithStandardLibrary |
| 24 | +var ( |
| 25 | + workDir string |
| 26 | + version, buildDate, commitID string |
| 27 | + listType, listPath string |
| 28 | +) |
| 29 | + |
| 30 | +func main() { |
| 31 | + fmt.Printf("kcheck %s\n", version) |
| 32 | + configs.WorkDir, _ = os.Getwd() |
| 33 | + app := &cli.App{ |
| 34 | + Name: "kcheck", |
| 35 | + Usage: "check files through list", |
| 36 | + Version: fmt.Sprintf("%s (built: %s)", commitID, buildDate), |
| 37 | + Flags: []cli.Flag{ |
| 38 | + &cli.StringFlag{ |
| 39 | + Name: "type", |
| 40 | + Aliases: []string{"t"}, |
| 41 | + Usage: "input list `TYPE`, support: `kbin`,`xml`,`metadata`,`kcheck`", |
| 42 | + Destination: &listType, |
| 43 | + }, |
| 44 | + }, |
| 45 | + Action: func(c *cli.Context) error { |
| 46 | + if c.NArg() == 0 { |
| 47 | + listPath = guessListPath() |
| 48 | + if listPath == "" { |
| 49 | + fmt.Println("failed to guess input list, please specify the path") |
| 50 | + fmt.Fprintln(color.Output, "use", |
| 51 | + color.GreenString("help"), |
| 52 | + "to get more info", |
| 53 | + ) |
| 54 | + os.Exit(-1) |
| 55 | + } |
| 56 | + } |
| 57 | + if c.NArg() > 0 { |
| 58 | + manualPath := c.Args().Get(0) |
| 59 | + listPath = filepath.Base(manualPath) |
| 60 | + configs.WorkDir = filepath.Dir(manualPath) |
| 61 | + } |
| 62 | + log.Println("current work dir:", configs.WorkDir) |
| 63 | + log.Println("list path:", listPath) |
| 64 | + |
| 65 | + f, err := os.Open(filepath.Join(configs.WorkDir, listPath)) |
| 66 | + inByte, err := ioutil.ReadAll(f) |
| 67 | + if err != nil { |
| 68 | + log.Fatalf("load file error: %s", err) |
| 69 | + } |
| 70 | + if listType == "" { |
| 71 | + listType, err = guessType() |
| 72 | + if err != nil { |
| 73 | + log.Fatalf("get list type error: %s", err) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // report handler |
| 78 | + res := make(chan *CheckResult, 999) |
| 79 | + go handler(res) |
| 80 | + var fCount, passCount, failCount = 0, 0, 0 |
| 81 | + switch listType { |
| 82 | + case configs.KBinType: |
| 83 | + kxml, _, err := kbinxml.DeserializeKbin(inByte) |
| 84 | + if err != nil { |
| 85 | + log.Fatalf("cannot unmarshal kbin: %s", err) |
| 86 | + } |
| 87 | + inByte = kxml |
| 88 | + fallthrough |
| 89 | + case configs.XMLType: |
| 90 | + doc := etree.NewDocument() |
| 91 | + if err := doc.ReadFromBytes(inByte); err != nil { |
| 92 | + log.Fatal("load xml list err:", err) |
| 93 | + } |
| 94 | + // check |
| 95 | + listNode := doc.FindElement("list") |
| 96 | + fCount = len(listNode.ChildElements()) |
| 97 | + for _, fNode := range listNode.SelectElements("file") { |
| 98 | + dstPath := fNode.SelectElement("dst_path").Text() |
| 99 | + dstMd5 := fNode.SelectElement("dst_md5").Text() |
| 100 | + formatPath := strings.TrimPrefix(filepath.FromSlash(dstPath), string(os.PathSeparator)) |
| 101 | + if err := checksum.CheckByHash(formatPath, dstMd5, md5.New()); err != nil { |
| 102 | + failCount++ |
| 103 | + res <- &CheckResult{ |
| 104 | + false, |
| 105 | + err, |
| 106 | + formatPath, |
| 107 | + } |
| 108 | + printStatus(false, formatPath) |
| 109 | + } else { |
| 110 | + passCount++ |
| 111 | + printStatus(true, formatPath) |
| 112 | + } |
| 113 | + } |
| 114 | + close(res) |
| 115 | + case configs.MetadataType: |
| 116 | + var metaStruct configs.MetaData |
| 117 | + err = json.Unmarshal(inByte, &metaStruct) |
| 118 | + if err != nil { |
| 119 | + log.Fatal("load metadata list err:", err) |
| 120 | + } |
| 121 | + metaCreateAt := time.Unix(0, metaStruct.CreatedAt*int64(time.Millisecond)) |
| 122 | + fCount = len(metaStruct.Files) |
| 123 | + log.Println("metadata created at:", metaCreateAt) |
| 124 | + for _, files := range metaStruct.Files { |
| 125 | + var ( |
| 126 | + fileSHA1 = files.SHA1 |
| 127 | + filePath = files.Path |
| 128 | + ) |
| 129 | + if fileSHA1 == "" { |
| 130 | + fileSHA1 = files.SSHA1 |
| 131 | + } |
| 132 | + if filePath == "" { |
| 133 | + filePath = files.SPath |
| 134 | + } |
| 135 | + formatPath := filepath.Join( |
| 136 | + "data", |
| 137 | + strings.TrimPrefix(filepath.FromSlash(filePath), string(os.PathSeparator)), |
| 138 | + ) |
| 139 | + if err := checksum.CheckByHash(formatPath, fileSHA1, sha1.New()); err != nil { |
| 140 | + failCount++ |
| 141 | + res <- &CheckResult{ |
| 142 | + false, |
| 143 | + err, |
| 144 | + formatPath, |
| 145 | + } |
| 146 | + printStatus(false, formatPath) |
| 147 | + } else { |
| 148 | + passCount++ |
| 149 | + printStatus(true, formatPath) |
| 150 | + } |
| 151 | + } |
| 152 | + case configs.KCheckType: |
| 153 | + var kcheckList configs.KCheckList |
| 154 | + err = json.Unmarshal(inByte, &kcheckList) |
| 155 | + if err != nil { |
| 156 | + log.Fatal("load KCheck list err:", err) |
| 157 | + } |
| 158 | + metaCreateAt := time.Unix(0, kcheckList.CreatedAt*int64(time.Millisecond)) |
| 159 | + fCount = len(kcheckList.Files) |
| 160 | + fmt.Println("KCheck list created at:", metaCreateAt) |
| 161 | + for _, files := range kcheckList.Files { |
| 162 | + formatPath := strings.TrimPrefix(filepath.FromSlash(files.Path), string(os.PathSeparator)) |
| 163 | + if err := checksum.CheckByHash(formatPath, files.SHA1, sha1.New()); err != nil { |
| 164 | + failCount++ |
| 165 | + res <- &CheckResult{ |
| 166 | + false, |
| 167 | + err, |
| 168 | + formatPath, |
| 169 | + } |
| 170 | + printStatus(false, formatPath) |
| 171 | + } else { |
| 172 | + passCount++ |
| 173 | + printStatus(true, formatPath) |
| 174 | + } |
| 175 | + } |
| 176 | + default: |
| 177 | + log.Fatalf("unknown type: %s", listType) |
| 178 | + } |
| 179 | + fmt.Println("Finished.") |
| 180 | + fmt.Println("ALL:", fCount, "/", "PASS:", passCount, "/", "FAIL:", failCount) |
| 181 | + fmt.Println("Exit after 5 seconds...") |
| 182 | + time.Sleep(5 * time.Second) |
| 183 | + return nil |
| 184 | + }, |
| 185 | + } |
| 186 | + |
| 187 | + sort.Sort(cli.FlagsByName(app.Flags)) |
| 188 | + sort.Sort(cli.CommandsByName(app.Commands)) |
| 189 | + |
| 190 | + err := app.Run(os.Args) |
| 191 | + if err != nil { |
| 192 | + log.Println(err) |
| 193 | + os.Exit(1) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +func printStatus(isSuccess bool, path string) { |
| 198 | + successDisp := color.New(color.Bold, color.FgWhite, color.BgGreen).FprintfFunc() |
| 199 | + failedDisp := color.New(color.Bold, color.FgWhite, color.BgRed).FprintfFunc() |
| 200 | + if isSuccess { |
| 201 | + successDisp(color.Output, "[PASSED]") |
| 202 | + } else { |
| 203 | + failedDisp(color.Output, "[FAILED]") |
| 204 | + } |
| 205 | + fmt.Println(" ", path) |
| 206 | +} |
0 commit comments