package collector import ( "hash" "io" "os" "strings" ) // ----------------------------------------------------------------------------------- // cleanName 函数清理输入的字符串,通过替换或删除特定字符来规范化它。 // 参数: // s: 需要处理的输入字符串 // 返回: // 返回经过清理和规范化后的字符串 func cleanName(s string) string { // 将字符串中的所有空格替换为下划线 s = strings.ReplaceAll(s, " ", "_") // Remove spaces // 将字符串中的所有连字符(-)替换为下划线 s = strings.ReplaceAll(s, "-", "_") // Remove hyphens // 将字符串中的所有左括号(()移除 s = strings.ReplaceAll(s, "(", "") // Remove open parenthesis // 将字符串中的所有右括号())移除 s = strings.ReplaceAll(s, ")", "") // Remove close parenthesis // 将字符串中的所有正斜杠(/)移除 s = strings.ReplaceAll(s, "/", "") // Remove forward slashes // 将字符串中的所有星号(*)移除 s = strings.ReplaceAll(s, "*", "") // Remove asterisks // 将整个字符串转换为小写字母 s = strings.ToLower(s) // 返回处理后的字符串 return s } // hashFile 函数用于计算指定文件的哈希值。 // 参数: // h: 一个实现了 hash.Hash 接口的哈希对象(如 sha256.New()),用于存储文件的哈希值。 // fn: 要计算哈希值的文件路径。 // 返回: // 如果计算过程中出现错误,返回该错误;否则返回 nil。 func hashFile(h hash.Hash, fn string) error { // 打开指定的文件 f, err := os.Open(fn) if err != nil { // 如果文件打开失败,返回错误 return err } defer f.Close() // 确保函数结束时关闭文件 // 将文件内容复制到哈希计算器 h 中 if _, err := io.Copy(h, f); err != nil { // 如果复制过程中发生错误,返回错误 return err } // 返回 nil,表示没有发生错误 return nil }