#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define USE_OPEN 1
// disabling &#39;USE_FCLONEFILEAT&#39; will cause repro to stop
#define USE_FCLONEFILEAT 1

static void LOG(const char *fmt, ...)
{
  static dispatch_queue_t queue;
  static dispatch_once_t onceToken;
  dispatch_once(&amp;onceToken, ^{
    queue = dispatch_queue_create(&#34;com.acronis.log&#34;, DISPATCH_QUEUE_SERIAL);
  });

  va_list ap;
  va_start(ap, fmt);
  char* line = nullptr;
  vasprintf(&amp;line, fmt, ap);
  if (line)
  {
    dispatch_async(queue, ^{
      printf(&#34;%s\n&#34;, line);
      free(line);
    });
  }
  va_end(ap);
}

static std::string volfsPath(uint64_t devId, uint64_t vnodeId)
{
  return &#34;/.vol/&#34; + std::to_string(devId) + &#34;/&#34; + std::to_string(vnodeId);
}

static void cloneAndScheduleDelete(const std::string&amp; sourcePath, dispatch_queue_t queue, uint64_t devId, uint64_t vnodeId)
{
  struct stat st;
  if (stat(sourcePath.c_str(), &amp;st) != 0 || !S_ISREG(st.st_mode))
  {
    return;
  }

#ifdef USE_OPEN
  int srcFd = open(sourcePath.c_str(), O_RDONLY);
  if (srcFd &lt; 0)
  {
    LOG(&#34;Failed to open source file: %s&#34;, sourcePath.c_str());
    return;
  }
#endif

  const char* cloneDir = &#34;/Users/admin/Downloads/_clone&#34;;
  mkdir(cloneDir, 0755);

  const char* filename = strrchr(sourcePath.c_str(), &#39;/&#39;);
  filename = filename ? filename + 1 : sourcePath.c_str();
  
  std::string cloneFilename = std::string(filename) + &#34;.clone.&#34; + std::to_string(time(nullptr)) + &#34;.&#34; + std::to_string(getpid());
  std::string clonePath = std::string(cloneDir) + &#34;/&#34; + cloneFilename;

#ifdef USE_FCLONEFILEAT
  if (fclonefileat(srcFd, AT_FDCWD, clonePath.c_str(), 0) == 0)
  {
    LOG(&#34;Cloned file: %s -&gt; %s&#34;, sourcePath.c_str(), clonePath.c_str());
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), queue, ^{
      if (unlink(clonePath.c_str()) == 0)
      {
        LOG(&#34;Deleted clone: %s&#34;, clonePath.c_str());
      }
      else
      {
        LOG(&#34;Failed to delete clone: %s&#34;, clonePath.c_str());
      }
    });
  }
  else
  {
    LOG(&#34;Failed to clone file: %s&#34;, sourcePath.c_str());
  }
  
#else
  std::string sourcePathVolfs = volfsPath(devId, vnodeId);
  if (clonefile(sourcePathVolfs.c_str(), clonePath.c_str(), 0) == 0)
  {
    LOG(&#34;Cloned file: %s -&gt; %s&#34;, sourcePathVolfs.c_str(), clonePath.c_str());
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), queue, ^{
      if (unlink(clonePath.c_str()) == 0)
      {
        LOG(&#34;Deleted clone: %s&#34;, clonePath.c_str());
      }
      else
      {
        LOG(&#34;Failed to delete clone: %s&#34;, clonePath.c_str());
      }
    });
  }
  else
  {
    LOG(&#34;Failed to clone file: %s&#34;, sourcePath.c_str());
  }
#endif

#ifdef USE_OPEN
  close(srcFd);
#endif
}

static void respond(es_client_t* client, const es_message_t* msg, bool cache)
{
  if (msg-&gt;action_type == ES_ACTION_TYPE_AUTH)
  {
    if (msg-&gt;event_type == ES_EVENT_TYPE_AUTH_OPEN)
    {
      es_respond_flags_result(client, msg, ~0, cache);
    }
    else
    {
      es_respond_auth_result(client, msg, ES_AUTH_RESULT_ALLOW, cache);
    }
  }
}

static const es_file_t* file(const es_message_t* msg)
{
  switch (msg-&gt;event_type)
  {
    case ES_EVENT_TYPE_AUTH_OPEN:
      return msg-&gt;event.open.file;
    case ES_EVENT_TYPE_AUTH_EXEC:
      return msg-&gt;event.exec.target-&gt;executable;
    case ES_EVENT_TYPE_AUTH_RENAME:
      return msg-&gt;event.rename.source;
  }
    
  return nullptr;
}

int main(int argc, const char * argv[])
{
  dispatch_queue_t esQueue    = dispatch_queue_create(&#34;com.acronis.es&#34;,      DISPATCH_QUEUE_CONCURRENT);
  dispatch_queue_t cloneQueue = dispatch_queue_create(&#34;com.acronis.clone&#34;,   DISPATCH_QUEUE_CONCURRENT);

  es_client_t* cli;
  auto ret = es_new_client(&amp;cli, ^(es_client_t* client, const es_message_t * msgc)
  {
    if (msgc-&gt;process-&gt;is_es_client)
    {
      es_mute_process(client, &amp;msgc-&gt;process-&gt;audit_token);
      return respond(client, msgc, true);
    }

    dispatch_async(esQueue, ^{
      bool shouldClone = false;
      if (msgc-&gt;event_type == ES_EVENT_TYPE_AUTH_OPEN)
      {
        auto&amp; ev = msgc-&gt;event.open;
        if (ev.fflag &amp; (FWRITE | O_RDWR | O_WRONLY | O_TRUNC | O_APPEND))
        {
          shouldClone = true;
        }
      }
      else if (msgc-&gt;event_type == ES_EVENT_TYPE_AUTH_UNLINK || msgc-&gt;event_type == ES_EVENT_TYPE_AUTH_RENAME)
      {
        shouldClone = true;
      }

      if (shouldClone)
      {
        if (auto f = ::file(msgc))
          cloneAndScheduleDelete(f-&gt;path.data, cloneQueue, f-&gt;stat.st_dev, f-&gt;stat.st_ino);
      }

      respond(client, msgc, true);
    });
  });
  LOG(&#34;es_new_client -&gt; %d&#34;, ret);

  es_mute_path(cli, &#34;/usr/sbin/notifyd&#34;, ES_MUTE_PATH_TYPE_LITERAL);
  es_mute_path(cli, &#34;/usr/sbin/spindump&#34;, ES_MUTE_PATH_TYPE_LITERAL);
  es_mute_path(cli, &#34;/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal&#34;, ES_MUTE_PATH_TYPE_LITERAL);

  es_event_type_t events[] = {
    ES_EVENT_TYPE_AUTH_OPEN,
    ES_EVENT_TYPE_AUTH_EXEC,
    ES_EVENT_TYPE_AUTH_RENAME,
    ES_EVENT_TYPE_AUTH_UNLINK,
  };
  es_subscribe(cli, events, sizeof(events) / sizeof(*events));
  dispatch_main();
}
