summaryrefslogtreecommitdiff
path: root/src/Output.cc
blob: 3f66a7c756892094ed7e9d8e78d8aedd2136448b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "eio.h"
#include "Output.h"
#include "Task.h"
#include "Printer.h"

#ifdef _WIN32
static const char * strerror_r(int errorno, char * buf, size_t bufsize) {
#ifdef _MSVC
    strerror_s(buf, bufsize, errorno);
    return buf;
#else
    return strerror(errorno);
#endif
}
#endif

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;
}

struct cbStatsResults_t {
    Balau::Events::Custom evt;
    int result, errorno;
    EIO_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;
}

Balau::Output::Output(const char * fname, bool truncate) throw (GeneralException) : m_fd(-1), m_size(-1), m_mtime(-1) {
    m_name.set("Output(%s)", fname);
    m_fname = fname;

    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);
    Assert(r != 0);
    Task::yield(&cbResults.evt);
    if (cbResults.result < 0) {
        char str[4096];
        if (cbResults.errorno == ENOENT) {
            throw ENoEnt(fname);
        } else {
            throw GeneralException(String("Unable to open file ") + m_name + " for reading: " + strerror_r(cbResults.errorno, str, sizeof(str)) + " (err#" + cbResults.errorno + ")");
        }
    } else {
        m_fd = cbResults.result;
    }

    cbStatsResults_t cbStatsResults;
    r = eio_fstat(m_fd, 0, eioStatsDone, &cbStatsResults);
    Assert(r != 0);
    Task::yield(&cbStatsResults.evt);
    if (cbStatsResults.result == 0) {
        m_size = cbStatsResults.statdata.st_size;
        m_mtime = cbStatsResults.statdata.st_mtime;
    }
}

void Balau::Output::close() throw (GeneralException) {
    if (m_fd < 0)
        return;
    cbResults_t cbResults;
    eio_req * r = eio_close(m_fd, 0, eioDone, &cbResults);
    Assert(r != 0);
    m_fd = -1;
    Task::yield(&cbResults.evt);
    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;
    }
}

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);
    Assert(r != 0);
    Task::yield(&cbResults.evt);
    if (cbResults.result > 0) {
        wseek(cbResults.result, SEEK_CUR);
    } else {
        char str[4096];
        throw GeneralException(String("Unable to write file ") + m_name + ": " + strerror_r(cbResults.errorno, str, sizeof(str)) + " (err#" + cbResults.errorno + ")");
    }
    return cbResults.result;
}

bool Balau::Output::isClosed() {
    return m_fd < 0;
}

const char * Balau::Output::getName() {
    return m_name.to_charp();
}

off_t Balau::Output::getSize() {
    return m_size;
}

time_t Balau::Output::getMTime() {
    return m_mtime;
}

bool Balau::Output::canWrite() {
    return true;
}