summaryrefslogtreecommitdiff
path: root/src/Output.cc
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2012-09-01 23:16:23 -0700
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2012-09-01 23:16:23 -0700
commit7afdc95421086920ec83ff9b782b7d997b1d79a6 (patch)
treec22266556a36bdb3c8f43d6322a18e13fffef280 /src/Output.cc
parent0de06b7c51d77ef18897cf2410515aeeaf5bda49 (diff)
Effectively tossing libeio away.
Diffstat (limited to 'src/Output.cc')
-rw-r--r--src/Output.cc124
1 files changed, 94 insertions, 30 deletions
diff --git a/src/Output.cc b/src/Output.cc
index dd0a588..1f4c36f 100644
--- a/src/Output.cc
+++ b/src/Output.cc
@@ -3,9 +3,10 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
-#include "eio.h"
+#include "Async.h"
#include "Output.h"
#include "Task.h"
+#include "TaskMan.h"
#include "Printer.h"
#ifdef _WIN32
@@ -19,33 +20,53 @@ static const char * strerror_r(int errorno, char * buf, size_t bufsize) {
}
#endif
+namespace {
+
struct cbResults_t {
Balau::Events::Custom evt;
int result, errorno;
};
-static int eioDone(eio_req * req) {
- cbResults_t * cbResults = (cbResults_t *) req->data;
- cbResults->result = req->result;
- cbResults->errorno = req->errorno;
- cbResults->evt.doSignal();
- return 0;
-}
+class AsyncOpOpen : public Balau::AsyncOperation {
+ public:
+ AsyncOpOpen(const char * path, bool truncate, cbResults_t * results) : m_path(path), m_truncate(truncate), m_results(results) { }
+ virtual void run() {
+ int r = m_results->result = open(m_path, O_WRONLY | O_CREAT | (m_truncate ? O_TRUNC : 0), 0755);
+ m_results->errorno = r < 0 ? errno : 0;
+ }
+ virtual void done() {
+ m_results->evt.doSignal();
+ delete this;
+ }
+ private:
+ const char * m_path;
+ bool m_truncate;
+ cbResults_t * m_results;
+};
struct cbStatsResults_t {
Balau::Events::Custom evt;
int result, errorno;
- EIO_STRUCT_STAT statdata;
+ struct stat statdata;
};
-static int eioStatsDone(eio_req * req) {
- cbStatsResults_t * cbStatsResults = (cbStatsResults_t *) req->data;
- cbStatsResults->result = req->result;
- cbStatsResults->errorno = req->errorno;
- cbStatsResults->statdata = *(EIO_STRUCT_STAT *) req->ptr2;
- cbStatsResults->evt.doSignal();
- return 0;
-}
+class AsyncOpStat : public Balau::AsyncOperation {
+ public:
+ AsyncOpStat(int fd, cbStatsResults_t * results) : m_fd(fd), m_results(results) { }
+ virtual void run() {
+ int r = m_results->result = fstat(m_fd, &m_results->statdata);
+ m_results->errorno = r < 0 ? errno : 0;
+ }
+ virtual void done() {
+ m_results->evt.doSignal();
+ delete this;
+ }
+ private:
+ int m_fd;
+ cbStatsResults_t * m_results;
+};
+
+};
Balau::Output::Output(const char * fname, bool truncate) throw (GeneralException) {
m_name.set("Output(%s)", fname);
@@ -54,14 +75,13 @@ Balau::Output::Output(const char * fname, bool truncate) throw (GeneralException
Printer::elog(E_OUTPUT, "Opening file %s", fname);
cbResults_t cbResults;
- eio_req * r = eio_open(fname, O_WRONLY | O_CREAT | (truncate ? O_TRUNC : 0), 0755, 0, eioDone, &cbResults);
- EAssert(r != NULL, "eio_open returned a NULL eio_req");
+ createAsyncOp(new AsyncOpOpen(fname, truncate, &cbResults));
Task::operationYield(&cbResults.evt);
if (cbResults.result < 0) {
- char str[4096];
if (cbResults.errorno == ENOENT) {
throw ENoEnt(fname);
} else {
+ char str[4096];
throw GeneralException(String("Unable to open file ") + m_name + " for reading: " + strerror_r(cbResults.errorno, str, sizeof(str)) + " (err#" + cbResults.errorno + ")");
}
} else {
@@ -69,8 +89,7 @@ Balau::Output::Output(const char * fname, bool truncate) throw (GeneralException
}
cbStatsResults_t cbStatsResults;
- r = eio_fstat(m_fd, 0, eioStatsDone, &cbStatsResults);
- EAssert(r != NULL, "eio_fstat returned a NULL eio_req");
+ createAsyncOp(new AsyncOpStat(m_fd, &cbStatsResults));
Task::operationYield(&cbStatsResults.evt);
if (cbStatsResults.result == 0) {
m_size = cbStatsResults.statdata.st_size;
@@ -78,27 +97,72 @@ Balau::Output::Output(const char * fname, bool truncate) throw (GeneralException
}
}
+namespace {
+
+class AsyncOpClose : public Balau::AsyncOperation {
+ public:
+ AsyncOpClose(int fd, cbResults_t * results) : m_fd(fd), m_results(results) { }
+ virtual void run() {
+ int r = m_results->result = close(m_fd);
+ m_results->errorno = r < 0 ? errno : 0;
+ }
+ virtual void done() {
+ m_results->evt.doSignal();
+ delete this;
+ }
+ private:
+ int m_fd;
+ cbResults_t * m_results;
+};
+
+};
+
void Balau::Output::close() throw (GeneralException) {
if (m_fd < 0)
return;
cbResults_t cbResults;
- eio_req * r = eio_close(m_fd, 0, eioDone, &cbResults);
- EAssert(r != NULL, "eio_close returned a NULL eio_req");
- m_fd = -1;
+ createAsyncOp(new AsyncOpClose(m_fd, &cbResults));
Task::operationYield(&cbResults.evt);
+ m_fd = -1;
if (cbResults.result < 0) {
char str[4096];
strerror_r(cbResults.errorno, str, sizeof(str));
throw GeneralException(String("Unable to close file ") + m_name + ": " + str);
- } else {
- m_fd = cbResults.result;
}
}
+namespace {
+
+struct cbWriteResults_t {
+ Balau::Events::Custom evt;
+ ssize_t result;
+ int errorno;
+};
+
+class AsyncOpWrite : public Balau::AsyncOperation {
+ public:
+ AsyncOpWrite(int fd, const void * buf, size_t count, off_t offset, cbWriteResults_t * results) : m_fd(fd), m_buf(buf), m_count(count), m_offset(offset), m_results(results) { }
+ virtual void run() {
+ int r = m_results->result = pwrite(m_fd, m_buf, m_count, m_offset);
+ m_results->errorno = r < 0 ? errno : 0;
+ }
+ virtual void done() {
+ m_results->evt.doSignal();
+ delete this;
+ }
+ private:
+ int m_fd;
+ const void * m_buf;
+ size_t m_count;
+ off_t m_offset;
+ cbWriteResults_t * m_results;
+};
+
+};
+
ssize_t Balau::Output::write(const void * buf, size_t count) throw (GeneralException) {
- cbResults_t cbResults;
- eio_req * r = eio_write(m_fd, const_cast<void *>(buf), count, getWOffset(), 0, eioDone, &cbResults);
- EAssert(r != NULL, "eio_write returned a NULL eio_req");
+ cbWriteResults_t cbResults;
+ createAsyncOp(new AsyncOpWrite(m_fd, buf, count, getWOffset(), &cbResults));
Task::operationYield(&cbResults.evt);
if (cbResults.result > 0) {
wseek(cbResults.result, SEEK_CUR);