makeFileSystemObjectSource does not monitor changes made with nano

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()

Accepted Reply

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"

Replies

you're not getting events when you are editing a file using the nano terminal command. And This is not a bug , but it might be due to how nano saves files.

just try this it may solve your problem. Step1. nano filename.txt step2.change what u want to change step3.replace the Original File After saving your changes in the temporary file, you can replace the original file with the modified temporary file using the mv command. mv /path/to/tempfile.txt /path/to/originalfile.txt Well This Might work I guess

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"