| 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_mounts/mount_node_mem.h" |
| 5 | 6 |
| 6 #include <errno.h> | 7 #include <errno.h> |
| 7 #include <sys/stat.h> | 8 #include <string.h> |
| 8 | 9 |
| 9 #include "auto_lock.h" | 10 #include "nacl_mounts/osstat.h" |
| 10 #include "mount_node_mem.h" | 11 #include "utils/auto_lock.h" |
| 11 | 12 |
| 12 #define BLOCK_SIZE (1 << 16) | 13 #define BLOCK_SIZE (1 << 16) |
| 13 #define BLOCK_MASK (BLOCK_SIZE - 1) | 14 #define BLOCK_MASK (BLOCK_SIZE - 1) |
| 14 | 15 |
| 15 MountNodeMem::MountNodeMem(Mount *mount, int ino, int dev) | 16 MountNodeMem::MountNodeMem(Mount *mount, int ino, int dev) |
| 16 : MountNode(mount, ino, dev), | 17 : MountNode(mount, ino, dev), |
| 17 data_(NULL), | 18 data_(NULL), |
| 18 capacity_(0) { | 19 capacity_(0) { |
| 19 } | 20 } |
| 20 | 21 |
| 21 MountNodeMem::~MountNodeMem() { | 22 MountNodeMem::~MountNodeMem() { |
| 22 free(data_); | 23 free(data_); |
| 23 } | 24 } |
| 24 | 25 |
| 25 bool MountNodeMem::Init(int mode, short uid, short gid) { | 26 bool MountNodeMem::Init(int mode, short uid, short gid) { |
| 26 bool ok = MountNode::Init(mode, uid, gid); | 27 bool ok = MountNode::Init(mode, uid, gid); |
| 27 stat_.st_mode |= _S_IFREG; | 28 stat_.st_mode |= S_IFREG; |
| 28 return ok; | 29 return ok; |
| 29 } | 30 } |
| 30 | 31 |
| 31 int MountNodeMem::Read(size_t offs, void *buf, size_t count) { | 32 int MountNodeMem::Read(size_t offs, void *buf, size_t count) { |
| 32 AutoLock lock(&lock_); | 33 AutoLock lock(&lock_); |
| 33 if (count == 0) return 0; | 34 if (count == 0) return 0; |
| 34 if (offs + count > GetSize()) { | 35 if (offs + count > GetSize()) { |
| 35 count = GetSize() - offs; | 36 count = GetSize() - offs; |
| 36 } | 37 } |
| 37 | 38 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 52 memcpy(&data_[offs], buf, count); | 53 memcpy(&data_[offs], buf, count); |
| 53 return static_cast<int>(count); | 54 return static_cast<int>(count); |
| 54 } | 55 } |
| 55 | 56 |
| 56 int MountNodeMem::Truncate(size_t size) { | 57 int MountNodeMem::Truncate(size_t size) { |
| 57 size_t need = (size + BLOCK_MASK) & ~BLOCK_MASK; | 58 size_t need = (size + BLOCK_MASK) & ~BLOCK_MASK; |
| 58 | 59 |
| 59 // If the current capacity is correct, just adjust and return | 60 // If the current capacity is correct, just adjust and return |
| 60 if (need == capacity_) { | 61 if (need == capacity_) { |
| 61 stat_.st_size = static_cast<off_t>(size); | 62 stat_.st_size = static_cast<off_t>(size); |
| 62 0; | 63 return 0; |
| 63 } | 64 } |
| 64 | 65 |
| 65 // Attempt to realloc the block | 66 // Attempt to realloc the block |
| 66 char *newdata = static_cast<char *>(realloc(data_, need)); | 67 char *newdata = static_cast<char *>(realloc(data_, need)); |
| 67 if (newdata != NULL) { | 68 if (newdata != NULL) { |
| 68 data_ = newdata; | 69 data_ = newdata; |
| 69 capacity_ = need; | 70 capacity_ = need; |
| 70 stat_.st_size = static_cast<off_t>(size); | 71 stat_.st_size = static_cast<off_t>(size); |
| 71 return 0; | 72 return 0; |
| 72 } | 73 } |
| 73 | 74 |
| 74 // If we failed, then adjust size according to what we keep | 75 // If we failed, then adjust size according to what we keep |
| 75 if (size > capacity_) size = capacity_; | 76 if (size > capacity_) size = capacity_; |
| 76 | 77 |
| 77 // Update the size and return the new size | 78 // Update the size and return the new size |
| 78 stat_.st_size = static_cast<off_t>(size); | 79 stat_.st_size = static_cast<off_t>(size); |
| 79 errno = EIO; | 80 errno = EIO; |
| 80 return -1; | 81 return -1; |
| 81 } | 82 } |
| OLD | NEW |