| 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 #include "nacl_io/mount.h" | 11 #include "nacl_io/mount.h" |
| 12 #include "nacl_io/mount_node.h" | 12 #include "nacl_io/mount_node.h" |
| 13 #include "nacl_io/osunistd.h" | 13 #include "nacl_io/osunistd.h" |
| 14 | 14 |
| 15 #include "sdk_util/auto_lock.h" |
| 16 |
| 15 // It is only legal to construct a handle while the kernel lock is held. | 17 // It is only legal to construct a handle while the kernel lock is held. |
| 16 KernelHandle::KernelHandle() | 18 KernelHandle::KernelHandle() |
| 17 : mount_(NULL), node_(NULL), offs_(0) {} | 19 : mount_(NULL), node_(NULL), offs_(0) {} |
| 18 | 20 |
| 19 KernelHandle::KernelHandle(const ScopedMount& mnt, const ScopedMountNode& node) | 21 KernelHandle::KernelHandle(const ScopedMount& mnt, const ScopedMountNode& node) |
| 20 : mount_(mnt), node_(node), offs_(0) {} | 22 : mount_(mnt), node_(node), offs_(0) {} |
| 21 | 23 |
| 22 Error KernelHandle::Init(int open_mode) { | 24 Error KernelHandle::Init(int open_mode) { |
| 23 if (open_mode & O_APPEND) { | 25 if (open_mode & O_APPEND) { |
| 24 size_t node_size; | 26 size_t node_size; |
| 25 Error error = node_->GetSize(&offs_); | 27 Error error = node_->GetSize(&offs_); |
| 26 if (error) | 28 if (error) |
| 27 return error; | 29 return error; |
| 28 } | 30 } |
| 29 | 31 |
| 30 return 0; | 32 return 0; |
| 31 } | 33 } |
| 32 | 34 |
| 33 Error KernelHandle::Seek(off_t offset, int whence, off_t* out_offset) { | 35 Error KernelHandle::Seek(off_t offset, int whence, off_t* out_offset) { |
| 34 // By default, don't move the offset. | 36 // By default, don't move the offset. |
| 35 *out_offset = offset; | 37 *out_offset = offset; |
| 36 | |
| 37 size_t base; | 38 size_t base; |
| 38 size_t node_size; | 39 size_t node_size; |
| 40 |
| 41 AUTO_LOCK(offs_lock_); |
| 39 Error error = node_->GetSize(&node_size); | 42 Error error = node_->GetSize(&node_size); |
| 40 if (error) | 43 if (error) |
| 41 return error; | 44 return error; |
| 42 | 45 |
| 43 switch (whence) { | 46 switch (whence) { |
| 44 default: | 47 default: |
| 45 return -1; | 48 return -1; |
| 46 case SEEK_SET: | 49 case SEEK_SET: |
| 47 base = 0; | 50 base = 0; |
| 48 break; | 51 break; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 64 if (new_offset > node_size) { | 67 if (new_offset > node_size) { |
| 65 error = node_->FTruncate(new_offset); | 68 error = node_->FTruncate(new_offset); |
| 66 if (error) | 69 if (error) |
| 67 return EINVAL; | 70 return EINVAL; |
| 68 } | 71 } |
| 69 | 72 |
| 70 *out_offset = offs_ = new_offset; | 73 *out_offset = offs_ = new_offset; |
| 71 return 0; | 74 return 0; |
| 72 } | 75 } |
| 73 | 76 |
| 77 Error KernelHandle::Read(void* buf, size_t nbytes, int* cnt) { |
| 78 AUTO_LOCK(offs_lock_); |
| 79 Error error = node_->Read(offs_, buf, nbytes, cnt); |
| 80 if (0 == error) |
| 81 offs_ += *cnt; |
| 82 return error; |
| 83 } |
| 84 |
| 85 Error KernelHandle::Write(const void* buf, size_t nbytes, int* cnt) { |
| 86 AUTO_LOCK(offs_lock_); |
| 87 Error error = node_->Write(offs_, buf, nbytes, cnt); |
| 88 if (0 == error) |
| 89 offs_ += *cnt; |
| 90 return error; |
| 91 } |
| 92 |
| 93 Error KernelHandle::GetDents(struct dirent* pdir, size_t nbytes, int* cnt) { |
| 94 AUTO_LOCK(offs_lock_); |
| 95 Error error = node_->GetDents(offs_, pdir, nbytes, cnt); |
| 96 if (0 == error) |
| 97 offs_ += *cnt; |
| 98 return error; |
| 99 } |
| 100 |
| 101 const ScopedRef<MountNode>& KernelHandle::node() { return node_; } |
| 102 const ScopedRef<Mount>& KernelHandle::mount() { return mount_; } |
| OLD | NEW |