#include <sys/stat.h>
#include <unistd.h>

#include <stdplus/fd/atomic.hpp>
#include <stdplus/fd/managed.hpp>
#include <stdplus/util/cexec.hpp>

#include <cstdlib>
#include <filesystem>
#include <format>
#include <system_error>
#include <utility>

namespace stdplus
{
namespace fd
{

static std::string makeTmpName(const std::filesystem::path& filename)
{
    auto name = filename.filename();
    auto path = filename.parent_path() /
                std::format(".{}.XXXXXX", name.native());
    return path.native();
}

static int mktemp(std::string& tmpl)
{
    mode_t old = umask(0077);
    int fd = mkstemp(tmpl.data());
    umask(old);
    return CHECK_ERRNO(fd, [&](int error) {
        throw std::system_error(error, std::generic_category(),
                                std::format("mkstemp({})", tmpl));
    });
}

AtomicWriter::AtomicWriter(const std::filesystem::path& filename, int mode,
                           std::string_view tmpl) :
    filename(filename), mode(mode),
    tmpname(!tmpl.empty() ? std::string(tmpl) : makeTmpName(filename)),
    fd(mktemp(tmpname))
{}

AtomicWriter::AtomicWriter(AtomicWriter&& other) :
    filename(std::move(other.filename)), mode(other.mode),
    tmpname(std::move(other.tmpname)), fd(std::move(other.fd))
{
    // We don't want to cleanup twice
    other.tmpname.clear();
}

AtomicWriter& AtomicWriter::operator=(AtomicWriter&& other)
{
    if (this != &other)
    {
        filename = std::move(other.filename);
        mode = other.mode;
        tmpname = std::move(other.tmpname);
        fd = std::move(other.fd);

        // We don't want to cleanup twice
        other.tmpname.clear();
    }
    return *this;
}

AtomicWriter::~AtomicWriter()
{
    cleanup();
}

void AtomicWriter::commit(bool allow_copy)
{
    try
    {
        CHECK_ERRNO(fsync(get()), "fsync");
        CHECK_ERRNO(fchmod(get(), mode), "fchmod");
        // We want the file to be closed before renaming it
        {
            auto ifd = std::move(fd);
        }
        try
        {
            std::filesystem::rename(tmpname, filename);
            tmpname.clear();
        }
        catch (const std::filesystem::filesystem_error& e)
        {
            if (!allow_copy || e.code() != std::errc::cross_device_link)
            {
                throw;
            }
            std::filesystem::copy(tmpname, filename);
        }
    }
    catch (...)
    {
        // We do this to ensure that any crashes that might happen before the
        // destructor don't cause us to leave stale files.
        cleanup();
        throw;
    }
}

int AtomicWriter::get() const
{
    return fd.get();
}

void AtomicWriter::cleanup() noexcept
{
    if (!tmpname.empty())
    {
        // Ensure the FD is closed prior to removing the file
        {
            auto ifd = std::move(fd);
        }
        std::error_code ec;
        std::filesystem::remove(tmpname, ec);
        tmpname.clear();
    }
}

} // namespace fd
} // namespace stdplus
