summaryrefslogtreecommitdiff
path: root/platform/default/file_request_libuv.cpp
blob: f874bed8d220d7c8f56cfb0d7260604c6a16df28 (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
#include <mbgl/storage/file_request.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/std.hpp>

#include <uv.h>

#include <limits>

namespace mbgl {

struct FileRequestBaton {
    FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop);
    ~FileRequestBaton();

    void cancel();
    static void file_opened(uv_fs_t *req);
    static void file_stated(uv_fs_t *req);
    static void file_read(uv_fs_t *req);
    static void file_closed(uv_fs_t *req);
    static void notify_error(uv_fs_t *req);
    static void cleanup(uv_fs_t *req);

    const std::thread::id thread_id;
    FileRequest *request = nullptr;
    uv_fs_t req;
    uv_file fd = -1;
    bool canceled = false;
    std::string body;
    uv_buf_t buffer;
};

FileRequestBaton::FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop)
    : thread_id(std::this_thread::get_id()), request(request_) {
    req.data = this;
    uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, file_opened);
}

FileRequestBaton::~FileRequestBaton() {
}

void FileRequestBaton::cancel() {
    canceled = true;

    // uv_cancel fails frequently when the request has already been started.
    // In that case, we have to let it complete and check the canceled bool
    // instead.
    uv_cancel((uv_req_t *)&req);
}

void FileRequestBaton::notify_error(uv_fs_t *req) {
    FileRequestBaton *ptr = (FileRequestBaton *)req->data;
    assert(ptr->thread_id == std::this_thread::get_id());

    if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) {
        ptr->request->response = util::make_unique<Response>();
        ptr->request->response->code = req->result == UV_ENOENT ? 404 : 500;
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
        ptr->request->response->message = uv_strerror(uv_last_error(req->loop));
#else
        ptr->request->response->message = uv_strerror(int(req->result));
#endif
        ptr->request->notify();
    }
}

void FileRequestBaton::file_opened(uv_fs_t *req) {
    FileRequestBaton *ptr = (FileRequestBaton *)req->data;
    assert(ptr->thread_id == std::this_thread::get_id());

    if (req->result < 0) {
        // Opening failed or was canceled. There isn't much left we can do.
        notify_error(req);
        cleanup(req);
    } else {
        const uv_file fd = uv_file(req->result);

        // We're going to reuse this handle, so we need to cleanup first.
        uv_fs_req_cleanup(req);

        if (ptr->canceled || !ptr->request) {
            // Either the FileRequest object has been destructed, or the
            // request was canceled.
            uv_fs_close(req->loop, req, fd, file_closed);
        } else {
            ptr->fd = fd;
            uv_fs_fstat(req->loop, req, fd, file_stated);
        }
    }
}

void FileRequestBaton::file_stated(uv_fs_t *req) {
    FileRequestBaton *ptr = (FileRequestBaton *)req->data;
    assert(ptr->thread_id == std::this_thread::get_id());

    if (req->result != 0 || ptr->canceled || !ptr->request) {
        // Stating failed or was canceled. We already have an open file handle
        // though, which we'll have to close.
        notify_error(req);

        uv_fs_req_cleanup(req);
        uv_fs_close(req->loop, req, ptr->fd, file_closed);
    } else {
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
        const uv_statbuf_t *stat = static_cast<const uv_statbuf_t *>(req->ptr);
#else
        const uv_stat_t *stat = static_cast<const uv_stat_t *>(req->ptr);
#endif
        if (stat->st_size > std::numeric_limits<int>::max()) {
            // File is too large for us to open this way because uv_buf's only support unsigned
            // ints as maximum size.
            if (ptr->request) {
                ptr->request->response = util::make_unique<Response>();
                ptr->request->response->code = UV_EFBIG;
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
                ptr->request->response->message = uv_strerror(uv_err_t {UV_EFBIG, 0});
#else
                ptr->request->response->message = uv_strerror(UV_EFBIG);
#endif
                ptr->request->notify();
            }

            uv_fs_req_cleanup(req);
            uv_fs_close(req->loop, req, ptr->fd, file_closed);
        } else {
            const unsigned int size = (unsigned int)(stat->st_size);
            ptr->body.resize(size);
            ptr->buffer = uv_buf_init(const_cast<char *>(ptr->body.data()), size);
            uv_fs_req_cleanup(req);
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
            uv_fs_read(req->loop, req, ptr->fd, ptr->buffer.base, ptr->buffer.len, -1, file_read);
#else
            uv_fs_read(req->loop, req, ptr->fd, &ptr->buffer, 1, 0, file_read);
#endif
        }
    }
}

void FileRequestBaton::file_read(uv_fs_t *req) {
    FileRequestBaton *ptr = (FileRequestBaton *)req->data;
    assert(ptr->thread_id == std::this_thread::get_id());

    if (req->result < 0 || ptr->canceled || !ptr->request) {
        // Stating failed or was canceled. We already have an open file handle
        // though, which we'll have to close.
        notify_error(req);
    } else {
        // File was successfully read.
        if (ptr->request) {
            ptr->request->response = util::make_unique<Response>();
            ptr->request->response->code = 200;
            ptr->request->response->data = std::move(ptr->body);
            ptr->request->notify();
        }
    }

    uv_fs_req_cleanup(req);
    uv_fs_close(req->loop, req, ptr->fd, file_closed);
}

void FileRequestBaton::file_closed(uv_fs_t *req) {
    assert(((FileRequestBaton *)req->data)->thread_id == std::this_thread::get_id());

    if (req->result < 0) {
        // Closing the file failed. But there isn't anything we can do.
    }

    cleanup(req);
}

void FileRequestBaton::cleanup(uv_fs_t *req) {
    FileRequestBaton *ptr = (FileRequestBaton *)req->data;
    assert(ptr->thread_id == std::this_thread::get_id());

    if (ptr->request) {
        ptr->request->ptr = nullptr;
    }

    uv_fs_req_cleanup(req);
    delete ptr;
}


FileRequest::FileRequest(const std::string &path_, uv_loop_t *loop)
    : BaseRequest(path_), ptr(new FileRequestBaton(this, path, loop)) {
}

void FileRequest::cancel() {
    assert(thread_id == std::this_thread::get_id());

    if (ptr) {
        ptr->cancel();

        // When deleting a FileRequest object with a uv_fs_* call is in progress, we are making sure
        // that the callback doesn't accidentally reference this object again.
        ptr->request = nullptr;
        ptr = nullptr;
    }

    notify();
}

FileRequest::~FileRequest() {
    assert(thread_id == std::this_thread::get_id());
    cancel();

    // Note: The FileRequestBaton object is deleted in FileRequestBaton::cleanup().
}

}