2015-07-27 18:00:22 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-07-27 18:00:22 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2021-03-17 21:22:49 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
|
|
|
)
|
2015-07-27 18:00:22 +00:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
period := flag.Duration("period", 200*time.Millisecond, "Sleep period between checks")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
file := flag.Arg(0)
|
|
|
|
|
|
|
|
if file == "" {
|
|
|
|
fmt.Println("Expects a path as an argument")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exists := true
|
|
|
|
size := int64(0)
|
|
|
|
mtime := time.Time{}
|
2021-03-17 21:22:49 +00:00
|
|
|
var hash [sha256.Size]byte
|
2015-07-27 18:00:22 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(*period)
|
|
|
|
|
|
|
|
newExists := true
|
|
|
|
fi, err := os.Stat(file)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
newExists = false
|
|
|
|
} else if err != nil {
|
|
|
|
fmt.Println("stat:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if newExists != exists {
|
|
|
|
exists = newExists
|
|
|
|
if !newExists {
|
|
|
|
fmt.Println(file, "does not exist")
|
|
|
|
} else {
|
|
|
|
fmt.Println(file, "appeared")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
size = 0
|
|
|
|
mtime = time.Time{}
|
2021-03-17 21:22:49 +00:00
|
|
|
hash = [sha256.Size]byte{}
|
2015-07-27 18:00:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
fmt.Println(file, "is directory")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newSize := fi.Size()
|
|
|
|
newMtime := fi.ModTime()
|
|
|
|
|
2021-03-17 21:22:49 +00:00
|
|
|
newHash, err := sha256file(file)
|
2015-07-27 18:00:22 +00:00
|
|
|
if err != nil {
|
2021-03-17 21:22:49 +00:00
|
|
|
fmt.Println("sha256file:", err)
|
2015-07-27 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 21:22:49 +00:00
|
|
|
if newSize != size || newMtime != mtime || newHash != hash {
|
2015-07-27 18:00:22 +00:00
|
|
|
fmt.Println(file, "Size:", newSize, "Mtime:", newMtime, "Hash:", fmt.Sprintf("%x", newHash))
|
|
|
|
hash = newHash
|
|
|
|
size = newSize
|
|
|
|
mtime = newMtime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 21:22:49 +00:00
|
|
|
|
|
|
|
func sha256file(fname string) (hash [sha256.Size]byte, err error) {
|
|
|
|
f, err := os.Open(fname)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
io.Copy(h, f)
|
|
|
|
hb := h.Sum(nil)
|
|
|
|
copy(hash[:], hb)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|