I have folder monitoring code using makeFileSystemObjectSource. The events are triggered for everything else, but not for when I edit a file with nano terminal command. Am I doing something wrong? Is it a bug? Is this intended and unfixable?
Code sample:
monitoredFolderFileDescriptor = open(currentlyMonitoredPath, O_EVTONLY)
folderMonitorSource = DispatchSource.makeFileSystemObjectSource(fileDescriptor: monitoredFolderFileDescriptor, eventMask: .all, queue: .main)
folderMonitorSource?.setEventHandler {
// ...
}
folderMonitorSource?.setCancelHandler {
// ...
}
folderMonitorSource?.resume()
Consider this sequence:
% ls -li test.txt
213771106 -rw-r--r-- 1 quinn staff 19 7 Nov 09:28 test.txt
%
% # Edit the file with `vi`.
%
% vi test.txt
%
% # The inode number has changed.
%
% ls -li test.txt
213771127 -rw-r--r-- 1 quinn staff 21 7 Nov 09:28 test.txt
%
% # Edit the file with `nano`.
%
% nano test.txt
%
% # The inode number is the same.
%
% ls -li test.txt
213771127 -rw-r--r-- 1 quinn staff 23 7 Nov 09:29 test.txt
Most text editors use a safe save mechanism. This writes the data to a temporary file and then replaces the existing file with the new one. This is atomic, that is, if something goes wrong you end up with either the old file or the new file, not some mixture of the two. It’s clear that vi
uses this mechanism because the file’s inode number changes when you edit it with vi
.
Apparently nano
doesn’t work this way. When you write the file, it just overwrites it in place. You can tell because the inode number doesn’t change.
When you monitor a directory you only hear about changes to the directory. The safe safe mechanism triggers represents a change to the directory because the replace operation effectively removes the old file and replaces it with a new one of the same name. In contrast, rewriting the file itself, as done by nano
, doesn’t modify the directory, it just modifies the file.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"