mirror of
https://github.com/Zygo/bees.git
synced 2025-05-17 21:35:45 +02:00
crucible: remove unused execpipe
Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
parent
4a9f26d12e
commit
cf04fb17de
@ -1,28 +0,0 @@
|
||||
#ifndef CRUCIBLE_EXECPIPE_H
|
||||
#define CRUCIBLE_EXECPIPE_H
|
||||
|
||||
#include "crucible/fd.h"
|
||||
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
namespace crucible {
|
||||
using namespace std;
|
||||
|
||||
void redirect_stdin(const Fd &child_fd);
|
||||
void redirect_stdin_stdout(const Fd &child_fd);
|
||||
void redirect_stdin_stdout_stderr(const Fd &child_fd);
|
||||
void redirect_stdout(const Fd &child_fd);
|
||||
void redirect_stdout_stderr(const Fd &child_fd);
|
||||
|
||||
// Open a pipe (actually socketpair) to child process, then execute code in that process.
|
||||
// e.g. popen([] () { system("echo Hello, World!"); });
|
||||
// Forked process will exit when function returns.
|
||||
Fd popen(function<int()> f, function<void(const Fd &child_fd)> import_fd_fn = redirect_stdin_stdout);
|
||||
|
||||
// Read all the data from fd into a string
|
||||
string read_all(Fd fd, size_t max_bytes = numeric_limits<size_t>::max(), size_t chunk_bytes = 4096);
|
||||
};
|
||||
|
||||
#endif // CRUCIBLE_EXECPIPE_H
|
@ -4,7 +4,6 @@ OBJS = \
|
||||
crc64.o \
|
||||
chatter.o \
|
||||
error.o \
|
||||
execpipe.o \
|
||||
extentwalker.o \
|
||||
fd.o \
|
||||
fs.o \
|
||||
|
100
lib/execpipe.cc
100
lib/execpipe.cc
@ -1,100 +0,0 @@
|
||||
#include "crucible/execpipe.h"
|
||||
|
||||
#include "crucible/chatter.h"
|
||||
#include "crucible/error.h"
|
||||
#include "crucible/process.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace crucible {
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
redirect_stdin(const Fd &child_fd)
|
||||
{
|
||||
dup2_or_die(child_fd, STDIN_FILENO);
|
||||
}
|
||||
|
||||
void
|
||||
redirect_stdin_stdout(const Fd &child_fd)
|
||||
{
|
||||
dup2_or_die(child_fd, STDOUT_FILENO);
|
||||
dup2_or_die(child_fd, STDIN_FILENO);
|
||||
}
|
||||
|
||||
void
|
||||
redirect_stdin_stdout_stderr(const Fd &child_fd)
|
||||
{
|
||||
dup2_or_die(child_fd, STDERR_FILENO);
|
||||
dup2_or_die(child_fd, STDOUT_FILENO);
|
||||
dup2_or_die(child_fd, STDIN_FILENO);
|
||||
}
|
||||
|
||||
void
|
||||
redirect_stdout_stderr(const Fd &child_fd)
|
||||
{
|
||||
dup2_or_die(child_fd, STDERR_FILENO);
|
||||
dup2_or_die(child_fd, STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void
|
||||
redirect_stdout(const Fd &child_fd)
|
||||
{
|
||||
dup2_or_die(child_fd, STDOUT_FILENO);
|
||||
}
|
||||
|
||||
void
|
||||
redirect_stderr(const Fd &child_fd)
|
||||
{
|
||||
dup2_or_die(child_fd, STDERR_FILENO);
|
||||
}
|
||||
|
||||
Fd popen(function<int()> f, function<void(const Fd &child_fd)> import_fd_fn)
|
||||
{
|
||||
Fd parent_fd, child_fd;
|
||||
{
|
||||
pair<Fd, Fd> fd_pair = socketpair_or_die();
|
||||
parent_fd = fd_pair.first;
|
||||
child_fd = fd_pair.second;
|
||||
}
|
||||
|
||||
pid_t fv;
|
||||
DIE_IF_MINUS_ONE(fv = fork());
|
||||
|
||||
if (fv) {
|
||||
child_fd->close();
|
||||
return parent_fd;
|
||||
} else {
|
||||
int rv = EXIT_FAILURE;
|
||||
catch_all([&]() {
|
||||
parent_fd->close();
|
||||
import_fd_fn(child_fd);
|
||||
|
||||
rv = f();
|
||||
});
|
||||
_exit(rv);
|
||||
}
|
||||
}
|
||||
|
||||
string
|
||||
read_all(Fd fd, size_t max_bytes, size_t chunk_bytes)
|
||||
{
|
||||
char buf[chunk_bytes];
|
||||
string str;
|
||||
size_t rv;
|
||||
while (1) {
|
||||
read_partial_or_die(fd, static_cast<void *>(buf), chunk_bytes, rv);
|
||||
if (rv == 0) {
|
||||
break;
|
||||
}
|
||||
if (max_bytes - str.size() < rv) {
|
||||
THROW_ERROR(out_of_range, "Output size limit " << max_bytes << " exceeded by appending " << rv << " bytes read to " << str.size() << " already in string");
|
||||
}
|
||||
str.append(buf, rv);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
PROGRAMS = \
|
||||
chatter \
|
||||
crc64 \
|
||||
execpipe \
|
||||
fd \
|
||||
limits \
|
||||
path \
|
||||
|
@ -1,64 +0,0 @@
|
||||
#include "tests.h"
|
||||
|
||||
#include "crucible/execpipe.h"
|
||||
|
||||
#include <ios>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace crucible;
|
||||
using namespace std;
|
||||
|
||||
#if 1 // Needs rework
|
||||
static inline
|
||||
void
|
||||
test_hello_world()
|
||||
{
|
||||
// alarm(9);
|
||||
Fd fd = popen([]() { return system("echo Hello, World!"); });
|
||||
char buf[1024];
|
||||
size_t rv = -1;
|
||||
read_partial_or_die(fd, buf, rv);
|
||||
assert(rv > 0);
|
||||
string b(buf, buf + rv - 1);
|
||||
// cerr << "hello_world says: '" << b << "'" << endl;
|
||||
assert(b == "Hello, World!");
|
||||
}
|
||||
|
||||
static inline
|
||||
void
|
||||
test_read_limit(size_t limit = 4096)
|
||||
{
|
||||
alarm(9);
|
||||
Fd fd = popen([]() { return system("yes Hello!"); });
|
||||
try {
|
||||
string b = read_all(fd, limit);
|
||||
} catch (out_of_range &re) {
|
||||
return;
|
||||
}
|
||||
assert(!"no exception thrown by read_all");
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace crucible {
|
||||
extern bool assert_no_leaked_fds();
|
||||
};
|
||||
|
||||
int
|
||||
main(int, char**)
|
||||
{
|
||||
#if 1
|
||||
RUN_A_TEST(test_hello_world());
|
||||
assert(assert_no_leaked_fds());
|
||||
RUN_A_TEST(test_read_limit(4095));
|
||||
RUN_A_TEST(test_read_limit(4096));
|
||||
RUN_A_TEST(test_read_limit(4097));
|
||||
assert(assert_no_leaked_fds());
|
||||
#endif
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user