|
Re: File Locking without using flock
Depends what you're trying to achieve.
One option would be to generate a temporary file name using a process that guarantees a unique name so it is only known to the process/script that generates it. Then rename/move the file you want to that temporary location. The file is effectively locked, as (in the eyes of any other processes) it no longer exists. Then do all your work on the temporary file. When the work is done rename/move the temporary file back to the original location. Depending on needs of the application, check if the original file has been recreated while work has been done on the temporary file -- and respond in a manner that makes sense to your script (eg append the original file to the temporary and delete it before copying the whole lot back, etc)
Another option is to use specific features of your operating system to enforce locks in different ways. The trade-off there is that the script is no longer portable.
Yet another option is to implement your system in a client/server arrangement, and (as far as your script is concerned) do without files completely. The script communicates with some back-end server (eg a database) which mediates access to the data via (say) a transactional approach.
|