package Filesystem
|
|
|
|
import (
|
|
"PackageManager/Client/Database"
|
|
"PackageManager/Variables"
|
|
"crypto/md5"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func HashFile(path string) (string, error) {
|
|
var (
|
|
Md5Hash string
|
|
hashBytes [16]byte
|
|
file *os.File
|
|
e error
|
|
)
|
|
|
|
file, e = os.Open(path)
|
|
if e != nil {
|
|
panic(e)
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
body, e := ioutil.ReadAll(file)
|
|
if e != nil {
|
|
panic(e)
|
|
}
|
|
|
|
//Get the 16 bytes hash
|
|
hashBytes = md5.Sum(body)
|
|
|
|
//Convert the bytes to a string
|
|
Md5Hash = fmt.Sprintf("%x", hashBytes)
|
|
|
|
return Md5Hash, nil
|
|
}
|
|
|
|
func UpdateFilesystemHash() error {
|
|
var (
|
|
fileHash string
|
|
rows []Database.FilesystemHashRow
|
|
dir string
|
|
e error
|
|
)
|
|
|
|
for _, dir = range Variables.InstallDirs {
|
|
_, e = os.Stat(dir)
|
|
if os.IsNotExist(e) {
|
|
continue
|
|
}
|
|
e = filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
if e != nil {
|
|
return e
|
|
}
|
|
|
|
// Ignore hidden files
|
|
if strings.HasPrefix(info.Name(), ".") || strings.HasPrefix(path, ".") {
|
|
return nil
|
|
}
|
|
|
|
// Ignore directories
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
fileHash, e = HashFile(path)
|
|
|
|
if e != nil {
|
|
return e
|
|
}
|
|
|
|
rows = append(rows, Database.FilesystemHashRow{
|
|
Path: path,
|
|
Hash: fileHash,
|
|
UpdatedAt: info.ModTime(),
|
|
})
|
|
|
|
// TODO: If len(rows) > x, update the db and clear out rows, and continue
|
|
|
|
return nil
|
|
})
|
|
|
|
if e != nil {
|
|
panic(e)
|
|
}
|
|
}
|
|
|
|
return Database.FindOrCreateFileHash(rows)
|
|
}
|
|
|
|
func GetFilesystemDiff() (map[int]string, map[int]string, error) {
|
|
var (
|
|
fileHash string
|
|
hashes []string = []string{}
|
|
lastUpdatedAt time.Time
|
|
dirtyFiles map[int]string
|
|
newFiles map[int]string = make(map[int]string)
|
|
newFilesTmp map[string]string = make(map[string]string)
|
|
counter int
|
|
ok bool
|
|
e error
|
|
)
|
|
|
|
lastUpdatedAt, e = Database.GetMostRecentTimestamp()
|
|
if e != nil {
|
|
return dirtyFiles, newFiles, e
|
|
}
|
|
|
|
e = filepath.Walk(".", func(path string, info os.FileInfo, e error) error {
|
|
if e != nil {
|
|
return e
|
|
}
|
|
|
|
// Ignore hidden files
|
|
if strings.HasPrefix(info.Name(), ".") || strings.HasPrefix(path, ".") {
|
|
return nil
|
|
}
|
|
|
|
// Ignore directories
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
fileHash, e = HashFile(path)
|
|
|
|
if e != nil {
|
|
return e
|
|
}
|
|
|
|
hashes = append(hashes, fileHash)
|
|
|
|
if info.ModTime().After(lastUpdatedAt) {
|
|
newFilesTmp[path] = path
|
|
}
|
|
|
|
// TODO: If len(rows) > x, update the db and clear out rows, and continue
|
|
|
|
return nil
|
|
})
|
|
if e != nil {
|
|
return dirtyFiles, newFiles, e
|
|
}
|
|
|
|
dirtyFiles, e = Database.FindModifiedFiles(hashes)
|
|
if e != nil {
|
|
return dirtyFiles, newFiles, e
|
|
}
|
|
|
|
for _, file := range dirtyFiles {
|
|
_, ok = newFilesTmp[file]
|
|
if ok {
|
|
delete(newFilesTmp, file)
|
|
}
|
|
}
|
|
|
|
counter = len(dirtyFiles)
|
|
|
|
for _, file := range newFilesTmp {
|
|
newFiles[counter] = file
|
|
counter++
|
|
}
|
|
|
|
return dirtyFiles, newFiles, e
|
|
}
|