Fix build errors on Windows

This commit is contained in:
John Ericson 2024-08-19 11:02:46 -04:00
parent 67a54d47c5
commit 84ea12ad7f
5 changed files with 31 additions and 32 deletions

View File

@ -86,7 +86,7 @@ fs::path ExecutablePath::findPath(const fs::path & exe, std::function<bool(const
if (resOpt) if (resOpt)
return *resOpt; return *resOpt;
else else
throw ExecutableLookupError("Could not find executable '%s'", exe.native()); throw ExecutableLookupError("Could not find executable '%s'", exe.string());
} else { } else {
return exe; return exe;
} }

View File

@ -23,9 +23,9 @@ bool dryRun = false;
void removeOldGenerations(std::filesystem::path dir) void removeOldGenerations(std::filesystem::path dir)
{ {
if (access(dir.c_str(), R_OK) != 0) return; if (access(dir.string().c_str(), R_OK) != 0) return;
bool canWrite = access(dir.c_str(), W_OK) == 0; bool canWrite = access(dir.string().c_str(), W_OK) == 0;
for (auto & i : std::filesystem::directory_iterator{dir}) { for (auto & i : std::filesystem::directory_iterator{dir}) {
checkInterrupt(); checkInterrupt();

View File

@ -87,12 +87,8 @@ struct CmdEval : MixJSON, InstallableValueCommand, MixReadOnlyOption
// FIXME: disallow strings with contexts? // FIXME: disallow strings with contexts?
writeFile(path.string(), v.string_view()); writeFile(path.string(), v.string_view());
else if (v.type() == nAttrs) { else if (v.type() == nAttrs) {
if (mkdir(path.c_str() // TODO abstract mkdir perms for Windows
#ifndef _WIN32 // TODO abstract mkdir perms for Windows createDir(path.string(), 0777);
, 0777
#endif
) == -1)
throw SysError("creating directory '%s'", path);
for (auto & attr : *v.attrs()) { for (auto & attr : *v.attrs()) {
std::string_view name = state->symbols[attr.name]; std::string_view name = state->symbols[attr.name];
try { try {

View File

@ -25,6 +25,8 @@
#include "strings-inline.hh" #include "strings-inline.hh"
namespace fs = std::filesystem;
using namespace nix; using namespace nix;
using namespace nix::flake; using namespace nix::flake;
using json = nlohmann::json; using json = nlohmann::json;
@ -897,25 +899,26 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
"If you've set '%s' to a string, try using a path instead.", "If you've set '%s' to a string, try using a path instead.",
templateDir, templateDirAttr->getAttrPathStr()).debugThrow(); templateDir, templateDirAttr->getAttrPathStr()).debugThrow();
std::vector<std::filesystem::path> changedFiles; std::vector<fs::path> changedFiles;
std::vector<std::filesystem::path> conflictedFiles; std::vector<fs::path> conflictedFiles;
std::function<void(const std::filesystem::path & from, const std::filesystem::path & to)> copyDir; std::function<void(const fs::path & from, const fs::path & to)> copyDir;
copyDir = [&](const std::filesystem::path & from, const std::filesystem::path & to) copyDir = [&](const fs::path & from, const fs::path & to)
{ {
createDirs(to); fs::create_directories(to);
for (auto & entry : std::filesystem::directory_iterator{from}) { for (auto & entry : fs::directory_iterator{from}) {
checkInterrupt(); checkInterrupt();
auto from2 = entry.path(); auto from2 = entry.path();
auto to2 = to / entry.path().filename(); auto to2 = to / entry.path().filename();
auto st = entry.symlink_status(); auto st = entry.symlink_status();
if (std::filesystem::is_directory(st)) auto to_st = fs::symlink_status(to2);
if (fs::is_directory(st))
copyDir(from2, to2); copyDir(from2, to2);
else if (std::filesystem::is_regular_file(st)) { else if (fs::is_regular_file(st)) {
auto contents = readFile(from2); auto contents = readFile(from2.string());
if (pathExists(to2)) { if (fs::exists(to_st)) {
auto contents2 = readFile(to2); auto contents2 = readFile(to2.string());
if (contents != contents2) { if (contents != contents2) {
printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2.string()); printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2.string());
conflictedFiles.push_back(to2); conflictedFiles.push_back(to2);
@ -924,12 +927,12 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
} }
continue; continue;
} else } else
writeFile(to2, contents); writeFile(to2.string(), contents);
} }
else if (std::filesystem::is_symlink(st)) { else if (fs::is_symlink(st)) {
auto target = readLink(from2); auto target = fs::read_symlink(from2);
if (pathExists(to2)) { if (fs::exists(to_st)) {
if (readLink(to2) != target) { if (fs::read_symlink(to2) != target) {
printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2.string()); printError("refusing to overwrite existing file '%s'\n please merge it manually with '%s'", to2.string(), from2.string());
conflictedFiles.push_back(to2); conflictedFiles.push_back(to2);
} else { } else {
@ -937,7 +940,7 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
} }
continue; continue;
} else } else
createSymlink(target, to2); fs::create_symlink(target, to2);
} }
else else
throw Error("file '%s' has unsupported type", from2); throw Error("file '%s' has unsupported type", from2);
@ -948,9 +951,9 @@ struct CmdFlakeInitCommon : virtual Args, EvalCommand
copyDir(templateDir, flakeDir); copyDir(templateDir, flakeDir);
if (!changedFiles.empty() && pathExists(flakeDir + "/.git")) { if (!changedFiles.empty() && fs::exists(std::filesystem::path{flakeDir} / ".git")) {
Strings args = { "-C", flakeDir, "add", "--intent-to-add", "--force", "--" }; Strings args = { "-C", flakeDir, "add", "--intent-to-add", "--force", "--" };
for (auto & s : changedFiles) args.push_back(s); for (auto & s : changedFiles) args.emplace_back(s.string());
runProgram("git", true, args); runProgram("git", true, args);
} }
auto welcomeText = cursor->maybeGetAttr("welcomeText"); auto welcomeText = cursor->maybeGetAttr("welcomeText");
@ -1275,7 +1278,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
if (auto aDescription = aMeta->maybeGetAttr(state->sDescription)) if (auto aDescription = aMeta->maybeGetAttr(state->sDescription))
description = aDescription->getString(); description = aDescription->getString();
} }
if (json) { if (json) {
j.emplace("type", "derivation"); j.emplace("type", "derivation");
j.emplace("name", name); j.emplace("name", name);

View File

@ -126,8 +126,8 @@ struct ProfileManifest
{ {
auto manifestPath = profile / "manifest.json"; auto manifestPath = profile / "manifest.json";
if (pathExists(manifestPath)) { if (std::filesystem::exists(manifestPath)) {
auto json = nlohmann::json::parse(readFile(manifestPath)); auto json = nlohmann::json::parse(readFile(manifestPath.string()));
auto version = json.value("version", 0); auto version = json.value("version", 0);
std::string sUrl; std::string sUrl;
@ -176,7 +176,7 @@ struct ProfileManifest
} }
} }
else if (pathExists(profile / "manifest.nix")) { else if (std::filesystem::exists(profile / "manifest.nix")) {
// FIXME: needed because of pure mode; ugly. // FIXME: needed because of pure mode; ugly.
state.allowPath(state.store->followLinksToStore(profile.string())); state.allowPath(state.store->followLinksToStore(profile.string()));
state.allowPath(state.store->followLinksToStore((profile / "manifest.nix").string())); state.allowPath(state.store->followLinksToStore((profile / "manifest.nix").string()));