| 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), node_(node), offs_(0) {} |
| 22 node_(node), | 22 |
| 23 mode_(mode), | 23 Error KernelHandle::Init(int open_mode) { |
| 24 offs_(0) { | 24 if (open_mode & O_APPEND) { |
| 25 if (mode & O_APPEND) offs_ = node->GetSize(); | 25 size_t node_size; |
| 26 Error error = node_->GetSize(&offs_); |
| 27 if (error) |
| 28 return error; |
| 29 } |
| 30 |
| 31 return 0; |
| 26 } | 32 } |
| 27 | 33 |
| 28 off_t KernelHandle::Seek(off_t offset, int whence) { | 34 Error KernelHandle::Seek(off_t offset, int whence, off_t* out_offset) { |
| 35 // By default, don't move the offset. |
| 36 *out_offset = offset; |
| 37 |
| 29 size_t base; | 38 size_t base; |
| 30 size_t node_size = node_->GetSize(); | 39 size_t node_size; |
| 40 Error error = node_->GetSize(&node_size); |
| 41 if (error) |
| 42 return error; |
| 31 | 43 |
| 32 switch (whence) { | 44 switch (whence) { |
| 33 default: return -1; | 45 default: |
| 34 case SEEK_SET: base = 0; break; | 46 return -1; |
| 35 case SEEK_CUR: base = offs_; break; | 47 case SEEK_SET: |
| 36 case SEEK_END: base = node_size; break; | 48 base = 0; |
| 49 break; |
| 50 case SEEK_CUR: |
| 51 base = offs_; |
| 52 break; |
| 53 case SEEK_END: |
| 54 base = node_size; |
| 55 break; |
| 37 } | 56 } |
| 38 | 57 |
| 39 if (base + offset < 0) { | 58 if (base + offset < 0) |
| 40 errno = EINVAL; | 59 return EINVAL; |
| 41 return -1; | |
| 42 } | |
| 43 | 60 |
| 44 offs_ = base + offset; | 61 off_t new_offset = base + offset; |
| 45 | 62 |
| 46 // Seeking past the end of the file will zero out the space between the old | 63 // Seeking past the end of the file will zero out the space between the old |
| 47 // end and the new end. | 64 // end and the new end. |
| 48 if (offs_ > node_size) { | 65 if (new_offset > node_size) { |
| 49 if (node_->FTruncate(offs_) < 0) { | 66 error = node_->FTruncate(new_offset); |
| 50 errno = EINVAL; | 67 if (error) |
| 51 return -1; | 68 return EINVAL; |
| 52 } | |
| 53 } | 69 } |
| 54 | 70 |
| 55 return offs_; | 71 *out_offset = offs_ = new_offset; |
| 72 return 0; |
| 56 } | 73 } |
| OLD | NEW |