ready to test
This commit is contained in:
parent
0baac68e24
commit
4f55ed2f10
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/*.exe
|
||||
/notify
|
||||
|
||||
115
notify.go
115
notify.go
@ -4,8 +4,12 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
@ -24,7 +28,9 @@ func main() {
|
||||
if watcher, err = fsnotify.NewWatcher(); err != nil {
|
||||
log.Fatal("Error:", err)
|
||||
}
|
||||
defer watcher.Close()
|
||||
defer func() {
|
||||
_ = watcher.Close()
|
||||
}()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
@ -34,6 +40,12 @@ func main() {
|
||||
log.Fatal("Error:", err)
|
||||
}
|
||||
|
||||
sm := new(sync.Map)
|
||||
go createEmptyAndDelete(ctx, sm, time.Second, dir2Watch)
|
||||
go createEmptyAndDelete(ctx, sm, time.Second+(time.Millisecond*200), dir2Watch)
|
||||
go createEmptyAndDelete(ctx, sm, time.Second+(time.Millisecond*400), dir2Watch)
|
||||
go createEmptyAndDelete(ctx, sm, time.Second+(time.Millisecond*600), dir2Watch)
|
||||
|
||||
done := make(chan os.Signal, 1)
|
||||
signal.Notify(done, os.Interrupt)
|
||||
|
||||
@ -73,5 +85,104 @@ func checkArgs() (string, error) {
|
||||
if len(os.Args) != 2 {
|
||||
return "", fmt.Errorf("invalid arguments")
|
||||
}
|
||||
return "", nil
|
||||
|
||||
fi, err := os.Stat(os.Args[1])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !fi.IsDir() {
|
||||
return "", fmt.Errorf("not a directory")
|
||||
}
|
||||
|
||||
return os.Args[1], nil
|
||||
}
|
||||
|
||||
func genUniqueRandomString(sm *sync.Map, len uint) string {
|
||||
for {
|
||||
rs := genRandomString(len)
|
||||
if _, ok := sm.LoadOrStore(rs, nil); !ok {
|
||||
return rs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func genRandomString(len uint) string {
|
||||
if len == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
b := make([]byte, len)
|
||||
var i uint
|
||||
for i = 0; i < len; i++ {
|
||||
b[i] = byte(rand.Intn(52))
|
||||
if b[i] >= 26 {
|
||||
b[i] += 97 - 26
|
||||
} else {
|
||||
b[i] += 65
|
||||
}
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func createEmptyAndDelete(ctx context.Context, sm *sync.Map, initialWait time.Duration, dir string) {
|
||||
if initialWait < 0 {
|
||||
log.Println("initialWait cannot be less than 0")
|
||||
return
|
||||
}
|
||||
|
||||
tick := time.NewTicker(initialWait)
|
||||
defer tick.Stop()
|
||||
|
||||
const (
|
||||
createFile = iota
|
||||
deleteFile
|
||||
|
||||
iterWait time.Duration = time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
next int = createFile
|
||||
nextFileName string
|
||||
nextFile string
|
||||
f *os.File
|
||||
err error
|
||||
)
|
||||
|
||||
nextFile = genUniqueRandomString(sm, 6)
|
||||
nextFileName = nextFile + ".txt"
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
tick.Stop()
|
||||
switch next {
|
||||
case createFile:
|
||||
nextFileName = filepath.Join(dir, nextFileName)
|
||||
if f, err = os.OpenFile(nextFileName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666); err != nil {
|
||||
log.Printf("Error creating file %s: %+v", nextFileName, err)
|
||||
return
|
||||
}
|
||||
_ = f.Close()
|
||||
next = deleteFile
|
||||
case deleteFile:
|
||||
if err = os.Remove(nextFileName); err != nil {
|
||||
log.Printf("Error deleting file %s: %+v", nextFileName, err)
|
||||
return
|
||||
}
|
||||
next = createFile
|
||||
|
||||
sm.Delete(nextFile)
|
||||
nextFile = genUniqueRandomString(sm, 6)
|
||||
nextFileName = nextFile + ".txt"
|
||||
}
|
||||
tick.Reset(iterWait)
|
||||
case <-ctx.Done():
|
||||
if next == deleteFile {
|
||||
_ = os.Remove(nextFileName)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user