Commit 8af1aa8a authored by jan.koester's avatar jan.koester
Browse files

serverconfig/editorconfig: Windows switches for file handling

serverconfig.cpp wrote config files with POSIX open/write/fsync/
fchmod/chown/rename/unlink and <unistd.h>, which do not exist on
Windows. Route them through small file* wrappers: POSIX keeps the
old calls; on Windows _open/_write/_commit/_close/_unlink are used,
chmod/chown are no-ops, and rename becomes MoveFileEx with
MOVEFILE_REPLACE_EXISTING because rename() fails on an existing
target there.

editorconfig.cpp: isDir() uses std::filesystem instead of stat/S_ISDIR.

The Windows wrappers were built with MinGW and run under wine
(write, replace over an existing file, unlink).

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent d99cbe08
Loading
Loading
Loading
Loading
+3 −3
Changes for editor/src/editorconfig.cpp: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -10,10 +10,10 @@
#include <json-c/json.h>

#include <algorithm>
#include <filesystem>
#include <netplus/socket.h>
#include <cctype>
#include <sstream>
#include <sys/stat.h>

namespace webedit::editorconfig {

@@ -128,8 +128,8 @@ bool validAddress(const std::string &a) {
}

bool isDir(const std::string &p) {
    struct stat sb;
    return ::stat(p.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode);
    std::error_code ec;
    return std::filesystem::is_directory(p, ec);
}

} // namespace
+60 −16
Changes for src/serverconfig.cpp: 60 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -21,7 +21,18 @@
#include <netplus/socket.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <io.h>
#if defined(_MSC_VER)
typedef int mode_t;
#endif
#else
#include <unistd.h>
#endif

namespace blogi::serverconfig {

@@ -379,19 +390,52 @@ bool validAddress(const std::string &a) {
    return netplus::isIPAddr(a) || validHost(a);
}

// Thin wrappers so the write/replace logic below stays the same on POSIX and Windows.
#ifdef _WIN32
int fileOpen(const std::string &p, int flags, mode_t mode = 0) {
    return ::_open(p.c_str(), flags | _O_BINARY, mode ? (_S_IREAD | _S_IWRITE) : 0);
}
long fileWrite(int fd, const std::string &c) {
    return ::_write(fd, c.data(), static_cast<unsigned int>(c.size()));
}
int fileSync(int fd) { return ::_commit(fd); }
int fileClose(int fd) { return ::_close(fd); }
int fileUnlink(const std::string &p) { return ::_unlink(p.c_str()); }
void fileChmod(int, mode_t) {}                       // no POSIX permissions on Windows
void fileChownLike(const std::string &, const struct stat &) {}
// rename() on Windows fails when the target exists; MoveFileEx replaces it.
int fileReplace(const std::string &from, const std::string &to) {
    if (::MoveFileExA(from.c_str(), to.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH))
        return 0;
    errno = ::GetLastError() == ERROR_SHARING_VIOLATION ? EBUSY : EACCES;
    return -1;
}
#else
int fileOpen(const std::string &p, int flags, mode_t mode = 0) { return ::open(p.c_str(), flags, mode); }
long fileWrite(int fd, const std::string &c) { return ::write(fd, c.data(), c.size()); }
int fileSync(int fd) { return ::fsync(fd); }
int fileClose(int fd) { return ::close(fd); }
int fileUnlink(const std::string &p) { return ::unlink(p.c_str()); }
void fileChmod(int fd, mode_t mode) { ::fchmod(fd, mode); }
void fileChownLike(const std::string &p, const struct stat &sb) {
    if (::chown(p.c_str(), sb.st_uid, sb.st_gid) != 0) {}   // keep the "blogi" group
}
int fileReplace(const std::string &from, const std::string &to) { return ::rename(from.c_str(), to.c_str()); }
#endif

bool writeRaw(const std::string &path, const std::string &content, mode_t mode, std::string &err) {
    int fd = ::open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, mode);
    int fd = fileOpen(path, O_CREAT | O_TRUNC | O_WRONLY, mode);
    if (fd < 0) {
        err = "cannot write " + path + ": " + std::strerror(errno);
        return false;
    }
    ::fchmod(fd, mode);
    ssize_t n = ::write(fd, content.data(), content.size());
    bool ok = n == static_cast<ssize_t>(content.size()) && ::fsync(fd) == 0;
    ::close(fd);
    fileChmod(fd, mode);
    long n = fileWrite(fd, content);
    bool ok = n == static_cast<long>(content.size()) && fileSync(fd) == 0;
    fileClose(fd);
    if (!ok) {
        err = "cannot write " + path + ": " + std::strerror(errno);
        ::unlink(path.c_str());
        fileUnlink(path);
    }
    return ok;
}
@@ -707,21 +751,21 @@ std::string patch(const std::string &content, const Settings &s) {
}

bool replaceFile(const std::string &tmp, const std::string &path, const std::string &content, std::string &err) {
    if (::rename(tmp.c_str(), path.c_str()) == 0) return true;
    if (fileReplace(tmp, path) == 0) return true;
    if (errno != EBUSY && errno != EXDEV) {
        err = "cannot replace " + path + ": " + std::strerror(errno);
        ::unlink(tmp.c_str());
        fileUnlink(tmp);
        return false;
    }
    ::unlink(tmp.c_str());
    int fd = ::open(path.c_str(), O_WRONLY | O_TRUNC);
    fileUnlink(tmp);
    int fd = fileOpen(path, O_WRONLY | O_TRUNC);
    if (fd < 0) {
        err = "cannot write " + path + ": " + std::strerror(errno);
        return false;
    }
    ssize_t n = ::write(fd, content.data(), content.size());
    bool ok = n == static_cast<ssize_t>(content.size()) && ::fsync(fd) == 0;
    ::close(fd);
    long n = fileWrite(fd, content);
    bool ok = n == static_cast<long>(content.size()) && fileSync(fd) == 0;
    fileClose(fd);
    if (!ok) err = "cannot write " + path + ": " + std::strerror(errno);
    return ok;
}
@@ -744,7 +788,7 @@ bool writeChecked(const std::string &path, const std::string &content,
    const mode_t mode = existed ? (sb.st_mode & 0777) : 0640;
    if (!writeRaw(tmp, content, mode, err)) return false;
    if (existed) {
        if (::chown(tmp.c_str(), sb.st_uid, sb.st_gid) != 0) {}   // keep the "blogi" group
        fileChownLike(tmp, sb);
    }
    std::string bad;
    try {
@@ -754,7 +798,7 @@ bool writeChecked(const std::string &path, const std::string &content,
    }
    if (!bad.empty()) {
        err = std::string("the new configuration does not parse: ") + bad;
        ::unlink(tmp.c_str());
        fileUnlink(tmp);
        return false;
    }
    if (existed) {
@@ -762,7 +806,7 @@ bool writeChecked(const std::string &path, const std::string &content,
        std::stringstream ss;
        ss << in.rdbuf();
        if (!writeRaw(path + ".bak", ss.str(), mode, err)) {
            ::unlink(tmp.c_str());
            fileUnlink(tmp);
            return false;
        }
    }