-
Notifications
You must be signed in to change notification settings - Fork 33
/
pepper_file.cc
359 lines (316 loc) · 9.01 KB
/
pepper_file.cc
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2012 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pepper_file.h"
#include <assert.h>
#include "nacl_io/pepper_interface.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_file_io.h"
#include "ppapi/cpp/file_ref.h"
#include "file_system.h"
const size_t PepperFile::kBufSize;
PepperFileHandler::PepperFileHandler(pp::FileSystem* file_system)
: ref_(1), file_system_(file_system) {
assert(file_system);
}
PepperFileHandler::~PepperFileHandler() {
assert(!ref_);
}
void PepperFileHandler::addref() {
++ref_;
}
void PepperFileHandler::release() {
if (!--ref_)
delete this;
}
FileStream* PepperFileHandler::open(int fd, const char* pathname, int oflag,
int* err) {
PepperFile* file = new PepperFile(fd, oflag, file_system_);
int32_t ret = file->open(pathname);
if (ret == 0) {
return file;
} else {
*err = nacl_io::PPErrorToErrno(ret);
file->release();
return NULL;
}
}
int PepperFileHandler::stat(const char* pathname, nacl_abi_stat* out) {
memset(out, 0, sizeof(nacl_abi_stat));
return 0;
}
//------------------------------------------------------------------------------
PepperFile::PepperFile(int fd, int oflag, pp::FileSystem* file_system)
: ref_(1), fd_(fd), oflag_(oflag), factory_(this), file_system_(file_system),
file_io_(NULL), offset_(0), file_info_(), write_sent_(false) {
}
PepperFile::~PepperFile() {
assert(!ref_);
}
void PepperFile::addref() {
++ref_;
}
void PepperFile::release() {
if (!--ref_)
delete this;
}
FileStream* PepperFile::dup(int fd) {
assert(0);
return NULL;
}
int32_t PepperFile::open(const char* pathname) {
int32_t result = PP_OK_COMPLETIONPENDING;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&PepperFile::Open, pathname, &result));
FileSystem* sys = FileSystem::GetFileSystem();
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
return result;
}
void PepperFile::close() {
int32_t result = PP_OK_COMPLETIONPENDING;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&PepperFile::Close, &result));
FileSystem* sys = FileSystem::GetFileSystem();
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
}
int PepperFile::read(char* buf, size_t count, size_t* nread) {
if (!is_open())
return EIO;
FileSystem* sys = FileSystem::GetFileSystem();
if (is_block() && in_buf_.empty()) {
int32_t result = PP_OK_COMPLETIONPENDING;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&PepperFile::Read, count, &result));
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
if (result < 0) {
*nread = -1;
return EIO;
}
}
*nread = 0;
while (*nread < count) {
if (in_buf_.empty())
break;
buf[(*nread)++] = in_buf_.front();
offset_++;
in_buf_.pop_front();
}
return 0;
}
int PepperFile::write(const char* buf, size_t count, size_t* nwrote) {
if (!is_open())
return EIO;
out_buf_.insert(out_buf_.end(), buf, buf + count);
if (is_block()) {
int32_t result = PP_OK_COMPLETIONPENDING;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&PepperFile::Write, &result));
FileSystem* sys = FileSystem::GetFileSystem();
while (result == PP_OK_COMPLETIONPENDING)
sys->cond().wait(sys->mutex());
if ((size_t)result != count) {
*nwrote = -1;
return EIO;
} else {
*nwrote = count;
return 0;
}
} else {
if (!write_sent_) {
write_sent_ = true;
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&PepperFile::Write, (int32_t*)NULL));
}
*nwrote = count;
return 0;
}
}
int PepperFile::seek(nacl_abi_off_t offset, int whence,
nacl_abi_off_t* new_offset) {
switch (whence) {
case SEEK_SET:
offset_ = offset;
if (new_offset)
*new_offset = offset_;
return 0;
case SEEK_CUR:
offset_ += offset;
if (new_offset)
*new_offset = offset_;
return 0;
case SEEK_END:
offset_ = file_info_.size + offset;
if (new_offset)
*new_offset = offset_;
return 0;
default:
if (new_offset)
*new_offset = -1;
return EINVAL;
}
}
int PepperFile::fstat(nacl_abi_stat* out) {
memset(out, 0, sizeof(nacl_abi_stat));
out->nacl_abi_st_size = file_info_.size;
return 0;
}
int PepperFile::fcntl(int cmd, va_list ap) {
if (cmd == F_GETFL) {
return oflag_;
} else if (cmd == F_SETFL) {
int oflag = va_arg(ap, long);
if (is_block() && (oflag & O_NONBLOCK)) {
pp::Module::Get()->core()->CallOnMainThread(0,
factory_.NewCallback(&PepperFile::Read,
kBufSize, (int32_t*)NULL));
}
oflag_ = oflag;
return 0;
} else {
return -1;
}
}
bool PepperFile::is_read_ready() {
return !in_buf_.empty();
}
bool PepperFile::is_write_ready() {
return out_buf_.size() < kBufSize;
}
bool PepperFile::is_exception() {
return !is_open();
}
void PepperFile::Open(int32_t result, const char* pathname, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
pp::FileRef file_ref(*file_system_, pathname);
file_io_ = new pp::FileIO(sys->instance());
int open_flags;
if ((oflag_ & O_ACCMODE) == O_WRONLY)
open_flags = PP_FILEOPENFLAG_WRITE;
else if ((oflag_ & O_ACCMODE) == O_RDONLY)
open_flags = PP_FILEOPENFLAG_READ;
else
open_flags = PP_FILEOPENFLAG_READ | PP_FILEOPENFLAG_WRITE;
if (oflag_ & O_CREAT)
open_flags |= PP_FILEOPENFLAG_CREATE;
if (oflag_ & O_TRUNC)
open_flags |= PP_FILEOPENFLAG_TRUNCATE;
*pres = file_io_->Open(file_ref, open_flags,
factory_.NewCallback(&PepperFile::OnOpen, pres));
if (*pres != PP_OK_COMPLETIONPENDING)
sys->cond().broadcast();
}
void PepperFile::OnOpen(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
if (result == PP_OK) {
result = file_io_->Query(&file_info_,
factory_.NewCallback(&PepperFile::OnQuery, pres));
if (result == PP_OK_COMPLETIONPENDING)
return;
}
delete file_io_;
file_io_ = NULL;
*pres = result;
sys->cond().broadcast();
}
void PepperFile::OnQuery(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
if (result == PP_OK) {
if (oflag_ & O_APPEND) {
offset_ = file_info_.size;
} else {
if (!is_block())
Read(PP_OK, kBufSize, NULL);
}
} else {
delete file_io_;
file_io_ = NULL;
}
*pres = result;
sys->cond().broadcast();
}
void PepperFile::Read(int32_t result, size_t count, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
assert(file_io_);
read_buf_.resize(count);
result = file_io_->Read(offset_, &read_buf_[0], read_buf_.size(),
factory_.NewCallback(&PepperFile::OnRead, pres));
if (result != PP_OK_COMPLETIONPENDING) {
delete file_io_;
file_io_ = NULL;
if (pres)
*pres = result;
sys->cond().broadcast();
}
}
void PepperFile::OnRead(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
if (result >= 0) {
in_buf_.insert(in_buf_.end(), &read_buf_[0], &read_buf_[0] + result);
if (result && !is_block() && in_buf_.size() < kBufSize)
Read(PP_OK, kBufSize, NULL);
} else {
delete file_io_;
file_io_ = NULL;
}
if (pres)
*pres = result;
sys->cond().broadcast();
}
void PepperFile::Write(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
assert(file_io_);
if (result == PP_OK) {
if (write_buf_.size()) {
// Previous write operation is in progress.
pp::Module::Get()->core()->CallOnMainThread(1,
factory_.NewCallback(&PepperFile::Write, &result));
return;
}
assert(out_buf_.size());
write_buf_.swap(out_buf_);
result = file_io_->Write(offset_, &write_buf_[0], write_buf_.size(),
factory_.NewCallback(&PepperFile::OnWrite, pres));
write_sent_ = false;
} else {
result = PP_ERROR_FAILED;
}
if (result != PP_OK_COMPLETIONPENDING) {
delete file_io_;
file_io_ = NULL;
if (pres)
*pres = result;
sys->cond().broadcast();
}
}
void PepperFile::OnWrite(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
if ((size_t)result != write_buf_.size()) {
delete file_io_;
file_io_ = NULL;
} else {
offset_ += result;
}
if (pres)
*pres = result;
write_buf_.clear();
sys->cond().broadcast();
}
void PepperFile::Close(int32_t result, int32_t* pres) {
FileSystem* sys = FileSystem::GetFileSystem();
Mutex::Lock lock(sys->mutex());
delete file_io_;
file_io_ = NULL;
if (pres)
*pres = PP_OK;
sys->cond().broadcast();
}