From: Paul Archer Date: 2009-07-17T11:10:51+09:00 Subject: Solved: Re: making sure a file isn't being written/copied before moving I think I solved my problem. I was looking at inotify in order to avoid having the script have to check the directories on a regular basis. Turns out that it can report when a file is closed for writing, *and* return the path and basename of the file. The only downside is that ruby-inotify doesn't (as far as I can tell) do recursive checks of the directory, so I'm using Open3 to call inotifywait, and parsing its output. Here's my test program: #!/usr/bin/ruby -w require 'open3' require 'ftools' def inwait(path) Open3.popen3("inotifywait -m -r #{path}"){ |stdin, stdout, stderr| while line = stdout.gets next unless line.include?("CLOSE_WRITE") yield line end } end inwait("/tmp") do |line| path, action, file = line.split puts "path: \t #{path}" puts "action: \t #{action}" puts "file: \t #{file}" File.move(path+file, "/tmp") end Paul Tomorrow, Paul Archer wrote: > So, I'm (still) working on some scripts to rename and reorganize my image > files. > Right now, I have a script on my laptop that pulls images off the CF card and > stores them locally. I have another script that rsync's the images to my > server at home whenever there's a connection available. These scripts don't > step on each other because they look at the process list for instances of > rsync. > > But I need to write two more scripts that take the image files from an > incoming directory, rename them, and drop them into a directory for me to > work on them; then once I've done whatever I'm going to do (cull, keyword, > etc), then I put them into a directory for archiving. The images are going to > be pulled from that directory, put in an archive directory, and have the > immutable extended attribute (xattr) set. > > My problem/issue is that I don't want to do anything with a file that is in > the process of being moved into one of these directories. I have to be sure > that the file is not still being moved/copied. Now, these directories are all > on the same filesystem, so it *should* be an atomic change by the filesystem > (technically a rename of the file from one directory/file name to another). > But I want to be sure--plus I may be dropping files into the incoming > directory from elsewhere from time to time. (I plan on going through my > backlog of old untagged files eventually.) > > Can someone suggest an easy (or at least reliable) way to make sure that any > file I'm about to modify isn't being touched by another program? > > Paul >