/*
  ISC License

  Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>

  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "fuse_rmdir.hpp"

#include "config.hpp"
#include "errno.hpp"
#include "fs_path.hpp"
#include "fs_rmdir.hpp"
#include "fs_unlink.hpp"

#include "fuse.h"

#include <string>

#include <unistd.h>

// 1. First error sets the initial value
// 2. If we get -EEXIST or -ENOTEMPTY, it takes priority (overwrites anything)
// 3. If we already have -EEXIST or -ENOTEMPTY, keep them (don't overwrite)
// 4. Success (>= 0) only sets to 0 if we don't have a priority error
// 5. Any other error only overwrites if we don't have a priority error
struct RmdirErr
{
private:
  std::optional<int> _err;

public:
  RmdirErr()
  {
  }

  operator int()
  {
    return (_err.has_value() ? _err.value() : -ENOENT);
  }

  RmdirErr&
  operator=(int v_)
  {
    if(!_err.has_value())
      _err = ((v_ >= 0) ? 0 : v_);
    else if((v_ == -EEXIST) || (v_ == -ENOTEMPTY))
      _err = v_;
    else if((*_err == -EEXIST) || (*_err == -ENOTEMPTY))
      ;
    else if(v_ >= 0)
      _err = 0;
    else
      _err = v_;

    return *this;
  }

  bool
  operator==(int v_)
  {
    if(_err.has_value())
      return (_err.value() == v_);
    return false;
  }
};

static
int
_should_unlink(int            rv_,
               FollowSymlinks followsymlinks_)
{
  return ((rv_ == -ENOTDIR) &&
          (followsymlinks_ != FollowSymlinks::ENUM::NEVER));
}

static
int
_rmdir_core(const fs::path       &basepath_,
            const fs::path       &fusepath_,
            const FollowSymlinks  followsymlinks_)
{
  int rv;
  fs::path fullpath;

  fullpath = basepath_ / fusepath_;

  rv = fs::rmdir(fullpath);
  if(::_should_unlink(rv,followsymlinks_))
    rv = fs::unlink(fullpath);

  return rv;
}

static
int
_rmdir_loop(const std::vector<Branch*> &branches_,
            const fs::path             &fusepath_,
            const FollowSymlinks        followsymlinks_)
{
  RmdirErr err;

  for(const auto &branch : branches_)
    {
      err = ::_rmdir_core(branch->path,fusepath_,followsymlinks_);
    }

  return err;
}

static
int
_rmdir(const Policy::Action &actionFunc_,
       const Branches::Ptr   branches_,
       const FollowSymlinks  followsymlinks_,
       const fs::path       &fusepath_)
{
  int rv;
  std::vector<Branch*> branches;

  rv = actionFunc_(branches_,fusepath_,branches);
  if(rv < 0)
    return rv;

  return ::_rmdir_loop(branches,fusepath_,followsymlinks_);
}

int
FUSE::rmdir(const fuse_req_ctx_t *ctx_,
            const char           *fusepath_)
{
  const fs::path  fusepath{fusepath_};

  return ::_rmdir(cfg.func.rmdir.policy,
                  cfg.branches,
                  cfg.follow_symlinks,
                  fusepath);
}
