Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to wait until a copy is complete #97

Open
jbryanmiller opened this issue Feb 27, 2020 · 2 comments
Open

how to wait until a copy is complete #97

jbryanmiller opened this issue Feb 27, 2020 · 2 comments

Comments

@jbryanmiller
Copy link

when a file is copied into the watched location, the event fires immediately. I need to wait until the file is completely copied before calling another function from within the event firing. How might i do this? Is it possible?

@jbryanmiller
Copy link
Author

Just an update if it helps anyone else that encounter this issue. I the following code allows one to wait for a file copy to complete so that other operations may be performed on it afterwards:
`w := watcher.New()

w.FilterOps(watcher.Create, watcher.Remove, watcher.Write)

go func() {
for {
timer := time.NewTimer(5 * time.Second)

	select {
	case event := <-w.Event:
		if event.Op == watcher.Remove {
			fmt.Println("Delete was fired")
		} else if (event.Op == watcher.Write || event.Op == watcher.Create) && event.IsDir() == false {
			fmt.Println(event)
			copying = true
		}
	case err := <-w.Error:
		log.Fatalln(err)
	case <-w.Closed:
		return
	case <-timer.C:
		if copying {
			fmt.Println("Done with Copy")
			copying = false
		}
	}
	timer.Stop()
}

}()`

@technoweenie
Copy link

One other option is writing to another directory on the same hard drive volume outside of the watcher, and then call os.Rename() to move it to where it should go. Renames should be atomic and fast, regardless of the size of the file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants