Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split nix-eval-jobs further into smaller files #280

Merged
merged 3 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/buffered-io.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "buffered-io.hh"
#include <string.h>
#include <unistd.h>
#include <nix/signals.hh>

[[nodiscard]] int tryWriteLine(int fd, std::string s) {
s += "\n";
std::string_view sv{s};
while (!sv.empty()) {
nix::checkInterrupt();
ssize_t res = write(fd, sv.data(), sv.size());
if (res == -1 && errno != EINTR) {
return -errno;
}
if (res > 0) {
sv.remove_prefix(res);
}
}
return 0;
}

LineReader::LineReader(int fd) {
stream = fdopen(fd, "r");
if (!stream) {
throw nix::Error("fdopen failed: %s", strerror(errno));
}
}

LineReader::~LineReader() {
fclose(stream);
free(buffer);
}

LineReader::LineReader(LineReader &&other) {
stream = other.stream;
other.stream = nullptr;
buffer = other.buffer;
other.buffer = nullptr;
len = other.len;
other.len = 0;
}

[[nodiscard]] std::string_view LineReader::readLine() {
ssize_t read = getline(&buffer, &len, stream);

if (read == -1) {
return {}; // Return an empty string_view in case of error
}

// Remove trailing newline
return std::string_view(buffer, read - 1);
}
20 changes: 20 additions & 0 deletions src/buffered-io.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <cstdio>
#include <string>
#include <string_view>

[[nodiscard]] int tryWriteLine(int fd, std::string s);

class LineReader {
public:
LineReader(int fd);
~LineReader();

LineReader(LineReader &&other);
[[nodiscard]] std::string_view readLine();

private:
FILE *stream = nullptr;
char *buffer = nullptr;
size_t len = 0;
};
13 changes: 7 additions & 6 deletions src/drv.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "drvs.hh"
#include "drv.hh"
#include <nix/config.h>
#include <nix/path-with-outputs.hh>
#include <nix/store-api.hh>
#include <nix/local-fs-store.hh>
#include <nix/value-to-json.hh>
#include <nix/derivations.hh>


static bool queryIsCached(nix::Store &store,
std::map<std::string, std::string> &outputs) {
std::map<std::string, std::string> &outputs) {
uint64_t downloadSize, narSize;
nix::StorePathSet willBuild, willSubstitute, unknown;

Expand All @@ -23,7 +22,8 @@ static bool queryIsCached(nix::Store &store,
}

/* The fields of a derivation that are printed in json form */
Drv::Drv(std::string &attrPath, nix::EvalState &state, nix::DrvInfo &drvInfo, MyArgs &args) {
Drv::Drv(std::string &attrPath, nix::EvalState &state, nix::DrvInfo &drvInfo,
MyArgs &args) {

auto localStore = state.store.dynamic_pointer_cast<nix::LocalFSStore>();

Expand Down Expand Up @@ -58,8 +58,9 @@ Drv::Drv(std::string &attrPath, nix::EvalState &state, nix::DrvInfo &drvInfo, My
meta = meta_;
}
if (args.checkCacheStatus) {
cacheStatus = queryIsCached(*localStore, outputs) ? Drv::CacheStatus::Cached
: Drv::CacheStatus::Uncached;
cacheStatus = queryIsCached(*localStore, outputs)
? Drv::CacheStatus::Cached
: Drv::CacheStatus::Uncached;
} else {
cacheStatus = Drv::CacheStatus::Unknown;
}
Expand Down
2 changes: 1 addition & 1 deletion src/eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ MyArgs::MyArgs() : MixCommonArgs("nix-eval-jobs") {
expectArg("expr", &releaseExpr);
}

void MyArgs::parseArgs(char** argv, int argc) {
void MyArgs::parseArgs(char **argv, int argc) {
parseCmdline(nix::argvToStrings(argc, argv), 0);
}
4 changes: 3 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
src = [
'nix-eval-jobs.cc',
'eval-args.cc',
'drv.cc'
'drv.cc',
'buffered-io.cc',
'worker.cc'
]

executable('nix-eval-jobs', src,
Expand Down
Loading