OLD | NEW |
---|---|
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 #include "nacl_io/kernel_handle.h" | 5 #include "nacl_io/kernel_handle.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <pthread.h> | 9 #include <pthread.h> |
10 | 10 |
11 #ifndef WIN32 | 11 #ifndef WIN32 |
12 // Needed for SEEK_SET/SEEK_CUR/SEEK_END. | 12 // Needed for SEEK_SET/SEEK_CUR/SEEK_END. |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 #endif | 14 #endif |
15 | 15 |
16 #include "nacl_io/mount.h" | 16 #include "nacl_io/mount.h" |
17 #include "nacl_io/mount_node.h" | 17 #include "nacl_io/mount_node.h" |
18 | 18 |
19 // It is only legal to construct a handle while the kernel lock is held. | 19 // It is only legal to construct a handle while the kernel lock is held. |
20 KernelHandle::KernelHandle(Mount* mnt, MountNode* node, int mode) | 20 KernelHandle::KernelHandle(Mount* mnt, MountNode* node) |
21 : mount_(mnt), | 21 : mount_(mnt), |
22 node_(node), | 22 node_(node), |
23 mode_(mode), | |
24 offs_(0) { | 23 offs_(0) { |
25 if (mode & O_APPEND) offs_ = node->GetSize(); | |
26 } | 24 } |
27 | 25 |
28 off_t KernelHandle::Seek(off_t offset, int whence) { | 26 Error KernelHandle::Init(int open_mode) { |
27 if (open_mode & O_APPEND) { | |
28 size_t node_size; | |
29 Error error = node_->GetSize(&offs_); | |
30 if (error) | |
31 return error; | |
32 } | |
33 | |
34 return 0; | |
Sam Clegg
2013/05/31 18:40:29
Should you return a special value here, or 0 OK as
binji
2013/06/03 17:16:46
Yes, 0 is OK. AFAIK, there is no EOK, value and I
noelallen1
2013/06/07 21:48:43
If we do end up doing something interesting then
binji
2013/06/07 23:23:11
OK, I'll do that as part of the next CL.
| |
35 } | |
36 | |
37 Error KernelHandle::Seek(off_t offset, int whence, off_t* out_offset) { | |
38 // By default, don't move the offset. | |
39 *out_offset = offset; | |
40 | |
29 size_t base; | 41 size_t base; |
30 size_t node_size = node_->GetSize(); | 42 size_t node_size; |
43 Error error = node_->GetSize(&node_size); | |
44 if (error) | |
45 return error; | |
31 | 46 |
32 switch (whence) { | 47 switch (whence) { |
33 default: return -1; | 48 default: return -1; |
34 case SEEK_SET: base = 0; break; | 49 case SEEK_SET: base = 0; break; |
35 case SEEK_CUR: base = offs_; break; | 50 case SEEK_CUR: base = offs_; break; |
36 case SEEK_END: base = node_size; break; | 51 case SEEK_END: base = node_size; break; |
37 } | 52 } |
38 | 53 |
39 if (base + offset < 0) { | 54 if (base + offset < 0) |
40 errno = EINVAL; | 55 return EINVAL; |
41 return -1; | |
42 } | |
43 | 56 |
44 offs_ = base + offset; | 57 off_t new_offset = base + offset; |
45 | 58 |
46 // Seeking past the end of the file will zero out the space between the old | 59 // Seeking past the end of the file will zero out the space between the old |
47 // end and the new end. | 60 // end and the new end. |
48 if (offs_ > node_size) { | 61 if (new_offset > node_size) { |
49 if (node_->FTruncate(offs_) < 0) { | 62 error = node_->FTruncate(new_offset); |
50 errno = EINVAL; | 63 if (error) |
51 return -1; | 64 return EINVAL; |
52 } | |
53 } | 65 } |
54 | 66 |
55 return offs_; | 67 *out_offset = offs_ = new_offset; |
68 return 0; | |
56 } | 69 } |
OLD | NEW |