8 changed files with 237 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||
@echo off |
|||
title 文件HASH登记数据库FOR LINUX |
|||
color 16 |
|||
set GOOS=linux |
|||
set GOARCH=amd64 |
|||
set CGO_ENABLED=0 |
|||
|
|||
go build -o fshash main.go |
|||
@ -0,0 +1,9 @@ |
|||
package db |
|||
|
|||
// 定义数据库中用的模型
|
|||
type StFileInfo struct { |
|||
Id string `json:"id"` |
|||
Fpath string `json:"fpath"` |
|||
Fhash string `json:"fhash"` |
|||
Fname string `json:"fname"` |
|||
} |
|||
@ -0,0 +1,97 @@ |
|||
package db |
|||
|
|||
import ( |
|||
"database/sql" |
|||
"fmt" |
|||
|
|||
// 导入包,导入前缀为下划线,则init函数被执行,然后注册驱动。
|
|||
_ "github.com/mattn/go-sqlite3" |
|||
) |
|||
|
|||
var db *sql.DB |
|||
var err error |
|||
|
|||
func init() { |
|||
fmt.Printf("hey,I am initilize function in SqliteDb\n") |
|||
//
|
|||
// Open() 函数指定驱动名称和数据源名称
|
|||
db, err = sql.Open("sqlite3", "ups.db") |
|||
if err != nil { |
|||
fmt.Printf("Database creation failed: %v\n", err) |
|||
return |
|||
} |
|||
// 调用db.Close() 函数,确保关闭数据库并阻止启动新的查询
|
|||
defer db.Close() |
|||
//
|
|||
connectDB() |
|||
} |
|||
|
|||
// 连接数据库
|
|||
func ConnectDB() { |
|||
var version string |
|||
// QueryRow() 执行查询,返回版本号
|
|||
err = db.QueryRow("SELECT SQLITE_VERSION()").Scan(&version) |
|||
if err != nil { |
|||
fmt.Printf("Database creation failed: %v\n", err) |
|||
return |
|||
} |
|||
// 连接成功,打印出"database connected:版本号"
|
|||
fmt.Printf("Database creation successful: %v\n", version) |
|||
} |
|||
|
|||
// 创建数据库表
|
|||
func createTable() { |
|||
// 建表语句
|
|||
sts := ` |
|||
CREATE TABLE f_info ( |
|||
id INTEGER PRIMARY KEY NOT NULL, |
|||
fname TEXT NOT NULL, |
|||
fpath TEXT NOT NULL, |
|||
fhash TEXT NOT NULL |
|||
);` |
|||
|
|||
// 使用db.Exec() 函数来执行 SQL 语句
|
|||
_, err = db.Exec(sts) |
|||
if err != nil { |
|||
fmt.Printf("Failed to create database table: %v\n", err) |
|||
return |
|||
} |
|||
fmt.Printf("Successfully created database table! \n") |
|||
} |
|||
|
|||
// 插入数据
|
|||
func InsertStf(sf StFileInfo) { |
|||
// 插入语句
|
|||
res, err := db.Exec("INSERT INTO f_info(fname, fpath,fhash) VALUES(?,?,?)", sf.Fname, sf.Fpath, sf.Fhash) |
|||
if err != nil { |
|||
fmt.Printf("Insert data failed: %v\n", err) |
|||
return |
|||
} |
|||
|
|||
// 获取自增ID
|
|||
lastInsertId, _ := res.LastInsertId() |
|||
fmt.Printf("Successfully inserted data, lastInsertId = %v\n", lastInsertId) |
|||
} |
|||
|
|||
// 查询数据
|
|||
func query(uid int) (weightList []float64) { |
|||
// 结果重量集合
|
|||
weightList = make([]float64, 0) |
|||
|
|||
// 查询语句
|
|||
rows, err := db.Query("SELECT weight FROM express_orders WHERE uid = ?", uid) |
|||
if err != nil { |
|||
fmt.Printf("Failed to query data: %v\n", err) |
|||
return |
|||
} |
|||
for rows.Next() { |
|||
var weight float64 |
|||
err = rows.Scan(&weight) |
|||
if err != nil { |
|||
fmt.Printf("Failed to read data: %v\n", err) |
|||
continue |
|||
} |
|||
weightList = append(weightList, weight) |
|||
} |
|||
return weightList |
|||
} |
|||
@ -0,0 +1,5 @@ |
|||
module fstool |
|||
|
|||
go 1.22.1 |
|||
|
|||
require github.com/mattn/go-sqlite3 v1.14.22 |
|||
@ -0,0 +1,2 @@ |
|||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= |
|||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= |
|||
@ -0,0 +1,57 @@ |
|||
package main |
|||
|
|||
import ( |
|||
// 导入包,导入前缀为下划线,则init函数被执行,然后注册驱动。
|
|||
"fmt" |
|||
"fstool/db" |
|||
"fstool/util" |
|||
"os" |
|||
"path/filepath" |
|||
|
|||
_ "github.com/mattn/go-sqlite3" |
|||
) |
|||
|
|||
func main() { |
|||
// 获取信息
|
|||
args := os.Args |
|||
// no input
|
|||
if args == nil || len(args) < 2 { |
|||
fmt.Printf("input your real path, like /home etc.\n") |
|||
os.Exit(1) |
|||
} |
|||
|
|||
// 第一个是输入的参数
|
|||
// 路径
|
|||
path := args[1] |
|||
//fmt.Printf("your path is :%s\n", path)
|
|||
|
|||
// open datebase
|
|||
db.ConnectDB() |
|||
//遍历
|
|||
// var ftree []string
|
|||
ftree, err := util.GetDirFilePaths(path, false) |
|||
if err != nil { |
|||
fmt.Printf("scan error is :%v", err) |
|||
} |
|||
//计算hash,然后存放到数据库
|
|||
for _, v := range ftree { |
|||
//fmt.Printf("%v\n", v)
|
|||
// 计算结果
|
|||
//fmt.Printf("%v,%s\n", filepath.Dir(v), filepath.Base(v))
|
|||
relpath, err := filepath.Rel(path, v) |
|||
if err != nil { |
|||
fmt.Printf("relpath error :%v", err) |
|||
} |
|||
fmt.Printf("relpath:%s\n", filepath.Dir(relpath)) |
|||
// 拼装文件信息插入数据库
|
|||
var stf db.StFileInfo |
|||
stf.Fhash = util.CalacHash(v) |
|||
stf.Fname = filepath.Base(v) |
|||
stf.Fpath = filepath.Dir(relpath) |
|||
// insert into db
|
|||
db.InsertStf(stf) |
|||
} |
|||
// fmt.Printf("scan is :%v", ftree)
|
|||
// connectDB()
|
|||
// insert(1, 0.81192)
|
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
package util |
|||
|
|||
import ( |
|||
"crypto/sha256" |
|||
"fmt" |
|||
"io" |
|||
"os" |
|||
"path/filepath" |
|||
) |
|||
|
|||
// 遍历目录下的文件
|
|||
// 是否只扫相对路径下
|
|||
func GetDirFilePaths(dirPath string, relativeOnly bool) ([]string, error) { |
|||
var filePaths []string |
|||
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { |
|||
if err != nil { |
|||
return err |
|||
} |
|||
if !info.IsDir() { |
|||
if relativeOnly { |
|||
fileName := filepath.Base(path) |
|||
relativePath := filepath.ToSlash(filepath.Join(filepath.Base(dirPath), fileName)) |
|||
filePaths = append(filePaths, relativePath) |
|||
} else { |
|||
filePaths = append(filePaths, filepath.ToSlash(path)) |
|||
} |
|||
} |
|||
return nil |
|||
}) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
return filePaths, nil |
|||
} |
|||
|
|||
// 计算文件的hash
|
|||
func CalacHash(rfile string) string { |
|||
// 获取到真实地址
|
|||
// rpath := filepath.Join(config.G.FilePath, rfile)
|
|||
//
|
|||
file, err := os.Open(rfile) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
defer file.Close() |
|||
|
|||
// initlize hash object
|
|||
hash := sha256.New() |
|||
|
|||
// write file into hash object
|
|||
if _, err := io.Copy(hash, file); err != nil { |
|||
panic(err) |
|||
} |
|||
// get hash value
|
|||
hashBytes := hash.Sum(nil) |
|||
//converto to hash string
|
|||
hastString := fmt.Sprintf("%x", hashBytes) |
|||
return hastString |
|||
} |
|||
Loading…
Reference in new issue