summaryrefslogtreecommitdiff
path: root/src/Output.cc
blob: 1f3fd2c24782d578110dcf00d2a972e18f6fff7f (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef _MSC_VER
#include <unistd.h>
#else
#include <io.h>
#endif
#include "Async.h"
#include "Output.h"
#include "Task.h"
#include "TaskMan.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

namespace {

struct cbResults_t {
    Balau::Events::Custom evt;
    ssize_t result;
    int errorno;
    struct stat statdata;
    enum { NONE, OPEN, STAT, CLOSE, WRITE } type;
};

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() {
#ifdef _MSC_VER
        const ssize_t r = m_results->result = _open(m_path, O_WRONLY | O_CREAT | (m_truncate ? O_TRUNC : 0) | O_BINARY, 0755);
#else
        const ssize_t r = m_results->result = open(m_path, O_WRONLY | O_CREAT | (m_truncate ? O_TRUNC : 0), 0755);
#endif
        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;
};

class AsyncOpStat : public Balau::AsyncOperation {
  public:
      AsyncOpStat(int fd, cbResults_t * results) : m_fd(fd), m_results(results) { }
    virtual void run() {
        const ssize_t 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;
    cbResults_t * m_results;
};

};

Balau::Output::Output(const char * fname) {
    m_name.set("Output(%s)", fname);
    m_fname = fname;
}

bool Balau::Output::isPendingComplete() {
    if (!m_pendingOp)
        return true;
    return reinterpret_cast<cbResults_t *>(m_pendingOp)->evt.gotSignal();
}

void Balau::Output::open(bool truncate) throw (GeneralException) {
    AAssert(isClosed() || m_pendingOp, "Can't open a file twice.");
    Printer::elog(E_OUTPUT, "Opening file %s", m_fname.to_charp());

    cbResults_t * cbResults;

    if (!m_pendingOp) {
        m_pendingOp = cbResults = new cbResults_t();
        cbResults->type = cbResults_t::NONE;
    } else {
        cbResults = (cbResults_t *) m_pendingOp;
    }

    try {
        switch (cbResults->type) {
        case cbResults_t::NONE:
            cbResults->type = cbResults_t::OPEN;
            createAsyncOp(new AsyncOpOpen(m_fname.to_charp(), truncate, cbResults));
            Task::operationYield(&cbResults->evt, Task::INTERRUPTIBLE);
        case cbResults_t::OPEN:
            AAssert(isPendingComplete(), "Don't call open again without checking isPendingComplete.");
            if (cbResults->result < 0) {
                if (cbResults->errorno == ENOENT) {
                    throw ENoEnt(m_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 {
                m_fd = (int) cbResults->result;
            }

            delete cbResults;
            m_pendingOp = cbResults = new cbResults_t();
            cbResults->type = cbResults_t::STAT;
            createAsyncOp(new AsyncOpStat(m_fd, cbResults));
            Task::operationYield(&cbResults->evt, Task::INTERRUPTIBLE);
        case cbResults_t::STAT:
            if (cbResults->result == 0) {
                m_size = cbResults->statdata.st_size;
                m_mtime = cbResults->statdata.st_mtime;
            }
            delete cbResults;
            m_pendingOp = NULL;
            break;
        default:
            AAssert(false, "Don't switch operations while one is still not complete.");
        }
    }
    catch (Balau::TaskSwitch) {
        throw;
    }
    catch (Balau::EAgain) {
        throw;
    }
    catch (...) {
        delete cbResults;
        m_pendingOp = NULL;
        throw;
    }
}

namespace {

class AsyncOpClose : public Balau::AsyncOperation {
  public:
      AsyncOpClose(int fd, cbResults_t * results) : m_fd(fd), m_results(results) { }
    virtual void run() {
        const ssize_t 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) && !m_pendingOp)
        return;

    cbResults_t * cbResults;

    if (!m_pendingOp) {
        m_pendingOp = cbResults = new cbResults_t;
        cbResults->type = cbResults_t::NONE;
    } else {
        cbResults = (cbResults_t *) m_pendingOp;
    }

    try {
        switch (cbResults->type) {
        case cbResults_t::NONE:
            cbResults->type = cbResults_t::CLOSE;
            createAsyncOp(new AsyncOpClose(m_fd, cbResults));
            Task::operationYield(&cbResults->evt, Task::INTERRUPTIBLE);
        case cbResults_t::CLOSE:
            m_fd = -1;
            if (cbResults->result < 0) {
                char buf[4096];
                const char * str = strerror_r(cbResults->errorno, buf, sizeof(buf));
                throw GeneralException(String("Unable to close file ") + m_name + ": " + str);
            }
            delete cbResults;
            m_pendingOp = NULL;
            break;
        default:
            AAssert(false, "Don't switch operations while one is still not complete.");
        }
    }
    catch (Balau::TaskSwitch) {
        throw;
    }
    catch (Balau::EAgain) {
        throw;
    }
    catch (...) {
        delete cbResults;
        m_pendingOp = NULL;
        throw;
    }
}

namespace {

class AsyncOpWrite : public Balau::AsyncOperation {
  public:
      AsyncOpWrite(int fd, const void * buf, size_t count, off64_t offset, cbResults_t * results) : m_fd(fd), m_buf(buf), m_count(count), m_offset(offset), m_results(results) { }
    virtual void run() {
#ifdef _MSC_VER
        off64_t offset = _lseeki64(m_fd, m_offset, SEEK_SET);
        if (offset < 0) {
            m_results->result = -1;
            m_results->errorno = errno;
            return;
        }
        const ssize_t r = m_results->result = write(m_fd, m_buf, m_count);
#else
        const ssize_t r = m_results->result = pwrite(m_fd, m_buf, m_count, m_offset);
#endif
        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;
    off64_t m_offset;
    cbResults_t * m_results;
};

};

ssize_t Balau::Output::write(const void * buf, size_t count) throw (GeneralException) {
    AAssert(!isClosed(), "Can't write a closed file");
    ssize_t result;

    cbResults_t * cbResults;

    if (!m_pendingOp) {
        m_pendingOp = cbResults = new cbResults_t;
        cbResults->type = cbResults_t::NONE;
    } else {
        cbResults = (cbResults_t *) m_pendingOp;
    }

    try {
        switch (cbResults->type) {
        case cbResults_t::NONE:
            cbResults->type = cbResults_t::WRITE;
            createAsyncOp(new AsyncOpWrite(m_fd, buf, count, getWOffset(), cbResults));
            Task::operationYield(&cbResults->evt, Task::INTERRUPTIBLE);
        case cbResults_t::WRITE:
            result = cbResults->result;
            if (result > 0) {
                wseek(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 + ")");
            }
            delete cbResults;
            m_pendingOp = NULL;
            return result;
        default:
            AAssert(false, "Don't switch operations while one is still not complete.");
        }
    }
    catch (Balau::TaskSwitch) {
        throw;
    }
    catch (Balau::EAgain) {
        throw;
    }
    catch (...) {
        delete cbResults;
        m_pendingOp = NULL;
        throw;
    }

    IAssert(false, "Shouldn't end up there.");

    return -1;
}

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

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

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

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

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