<commit_before>#include "serialise.hh"
#include "util.hh"
#include "remote-store.hh"
#include "worker-protocol.hh"
#include "archive.hh"
#include "globals.hh"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>

#include <iostream>
#include <unistd.h>


namespace nix {


RemoteStore::RemoteStore()
{
    int sockets[2];
    if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1)
        throw SysError("cannot create sockets");

    fdSelf = sockets[0];
    AutoCloseFD fdChild = sockets[1];

    
    /* Start the worker. */
    Path worker = getEnv("NIX_WORKER");
    if (worker == "")
        worker = nixBinDir + "/nix-worker";

    child = fork();
    
    switch (child) {
        
    case -1:
        throw SysError("unable to fork");

    case 0:
        try { /* child */
            
            if (dup2(fdChild, STDOUT_FILENO) == -1)
                throw SysError("dupping write side");

            if (dup2(fdChild, STDIN_FILENO) == -1)
                throw SysError("dupping read side");

            close(fdSelf);
            close(fdChild);

            int fdDebug = open("/tmp/worker-log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
            assert(fdDebug != -1);
            if (dup2(fdDebug, STDERR_FILENO) == -1)
                throw SysError("dupping stderr");
            close(fdDebug);
            
            execlp(worker.c_str(), worker.c_str(),
                "-vvv", "--slave", NULL);

            throw SysError(format("executing `%1%'") % worker);
            
        } catch (std::exception & e) {
            std::cerr << format("child error: %1%\n") % e.what();
        }
        quickExit(1);
    }

    fdChild.close();

    from.fd = fdSelf;
    to.fd = fdSelf;

    
    /* Send the magic greeting, check for the reply. */
    try {
        processStderr();
        writeInt(WORKER_MAGIC_1, to);
        unsigned int magic = readInt(from);<commit_msg>* Pass the verbosity level to the worker.<commit_after>#include "serialise.hh"
#include "util.hh"
#include "remote-store.hh"
#include "worker-protocol.hh"
#include "archive.hh"
#include "globals.hh"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>

#include <iostream>
#include <unistd.h>


namespace nix {


RemoteStore::RemoteStore()
{
    int sockets[2];
    if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1)
        throw SysError("cannot create sockets");

    fdSelf = sockets[0];
    AutoCloseFD fdChild = sockets[1];

    
    /* Start the worker. */
    Path worker = getEnv("NIX_WORKER");
    if (worker == "")
        worker = nixBinDir + "/nix-worker";

    string verbosityArg = "-";
    for (int i = 1; i < verbosity; ++i)
        verbosityArg += "v";

    child = fork();
    
    switch (child) {
        
    case -1:
        throw SysError("unable to fork");

    case 0:
        try { /* child */
            
            if (dup2(fdChild, STDOUT_FILENO) == -1)
                throw SysError("dupping write side");

            if (dup2(fdChild, STDIN_FILENO) == -1)
                throw SysError("dupping read side");

            close(fdSelf);
            close(fdChild);

            int fdDebug = open("/tmp/worker-log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
            assert(fdDebug != -1);
            if (dup2(fdDebug, STDERR_FILENO) == -1)
                throw SysError("dupping stderr");
            close(fdDebug);
            
            execlp(worker.c_str(), worker.c_str(), "--slave",
                /* hacky - must be at the end */
                verbosityArg == "-" ? NULL : verbosityArg.c_str(),
                NULL);

            throw SysError(format("executing `%1%'") % worker);
            
        } catch (std::exception & e) {
            std::cerr << format("child error: %1%\n") % e.what();
        }
        quickExit(1);
    }

    fdChild.close();

    from.fd = fdSelf;
    to.fd = fdSelf;

    
    /* Send the magic greeting, check for the reply. */
    try {
        processStderr();
        writeInt(WORKER_MAGIC_1, to);
        unsigned int magic = readInt(from);
{'fraction_non_alphanumeric': 0.1474811389632514, 'fraction_numerical': 0.010951569724993915, 'mean_word_length': 1.9525862068965518, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '333907', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1324, 'n_words': 311}