in reply to making sure a file is not currently being written to

You can try to use flock() to lock file.