I try to create some deamon which look the folde and if we insert new file open this file and do some magic, i use this (https://github.com/howeyc/fsnotify) FS notificator , but if i open file i have error "The process cannot access the file because it is being used by another process." i find a dirty solution whith time sleep 100 ms , but i think this is not clear way
func main(){
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
done := make(chan bool)
// Process events
go func() {
for {
select {
case ev := <-watcher.Event:
if ev.IsCreate() == true {
time.Sleep(100 * time.Millisecond)
file,err := os.OpenFile(path+ev.Name,os.O_RDWR|os.O_EXCL,0755)
if err !=nil{
panic(err)
}
//some code
file.Close()
}
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}()
err = watcher.Watch("test")
if err != nil {
log.Fatal(err)
}
<-done
watcher.Close()
}
UPD: Why do I have to do this ? I have a task to make the database Webm files , in order to determine if I have already such a file in the database I should get the hash of a file , but in the specification of this format (https://matroska.org/technical/specs/index.html) is the global bit Void block which stores some information , due to the fact that this unit can store different information from the same file will have different hash values , for this I use my function to find this bit block and reset it.