I tried intercepting ES_EVENT_TYPE_AUTH_UNLINK events. It worked for me. One thing to note is that, if you use the Finder UI to delete files, you'll not get deletion events. What I mean is, if you right-click on the file and select "Move to Trash", the file doesn't get deleted. Instead, if you use the "rm" command for deletion, you'll see ES_EVENT_TYPE_AUTH_UNLINK event being processed. "Move to Trash" is basically moving the file to the folder "~/.Trash/" so that it could be recovered in future unless you delete the file ("Delete Immediately...") from Trash or "Empty Trash" (which is basically nothing but invoking "rm"). So, "/bin/rm" is actually the binary that would trigger the event ES_EVENT_TYPE_AUTH_UNLINK. And we could subscribe to this event type for authorizing the deletion of a file.
For instance, here's my code:
es_handler_block_t deletion_cbk = ^(es_client_t *client, const es_message_t *msg)
{
log_events(msg); //logging events to console
NSString *path = esstring_to_nsstring(&msg->event.unlink.target->path); //casting es_string_token_t to NSString
es_auth_result_t flag =
([path rangeOfString:@"dont_delete_me"].location == NSNotFound) ? ES_AUTH_RESULT_ALLOW : ES_AUTH_RESULT_DENY;
// checking if the substring "dont_delete_me" is found in the event message
es_respond_result_t res = es_respond_auth_result(client, msg, flag, false);
if(ES_RESPOND_RESULT_SUCCESS != res)
LOG_ERROR("es_respond_auth_result: %d", res);
};
After subscribing to ES_EVENT_TYPE_AUTH_UNLINK with es_new_client(), I am intercepting the file deletion event messages inside this es_handler_block_t callback. The esstring_to_nsstring() function (to cast es_string_token_t to NSString) implementation is similar to: https://gist.github.com/Omar-Ikram/8e6721d8e83a3da69b31d4c2612a68ba#file-endpointsecuritydemo-m-L47-L56
OUTPUT
Uddalak:Desktop qwerty$ echo "this file should be deleted" > delete_me.txt
Uddalak:Desktop qwerty$ echo "this file should not be deleted" > dont_delete_me.txt
Uddalak:Desktop qwerty$ rm delete_me.txt
Uddalak:Desktop qwerty$ rm dont_delete_me.txt
rm: dont_delete_me.txt: Operation not permitted
Uddalak:Desktop qwerty$ ls -ltrh | grep "dont"
-rw-r--r-- 1 qwerty staff 32B May 28 15:01 dont_delete_me.txt
Uddalak:Desktop qwerty$
As you can see the file, "dont_delete_me.txt" doesn't get deleted as I set ES_AUTH_RESULT_DENY for files containing the substring "dont_delete_me". Additionally, once I had selected "Move to Trash" and then deleted the file ("Delete Immediately..." from the Trash) or "Empty Trash", then I was able to intercept the deletion event (like I said, which is basically nothing but invoking "rm").