Not. On POSIX-compatible systems, all locks are advisable anyway, so even if apache gets a read lock, another process may just write the file.
You can determine that with strace :
[pid 7246] open("/var/www/file.json", O_RDONLY|O_CLOEXEC) = 11 [pid 7246] fcntl(11, F_GETFD) = 0x1 (flags FD_CLOEXEC) [pid 7246] mmap(NULL, 20, PROT_READ, MAP_SHARED, 11, 0) = 0x7f53f93da000 [pid 7246] munmap(0x7f53f93da000, 20) = 0 [pid 7246] writev(10, [{"HTTP/1.1 200 OK\r\nDate: Thu, 26 J"}, ...) = 365 [pid 7246] close(11) = 0
Therefore, it may happen that your JSON file is only partially written. To avoid this problem, write your JSON file to a temporary file in the same file system and use atomic rename to overwrite the file.
That way, if open succeeds, apache will continue to serve the old file. If rename ends before open , apache will receive a new completed file.
If you are worried about the sequence (in the event of a power failure or so), you can also call fsync in an application that writes JSON before closing the file.
phihag
source share