| 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/mount_node_dir.h" | 5 #include "nacl_io/mount_node_dir.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "nacl_io/osdirent.h" | 10 #include "nacl_io/osdirent.h" |
| 11 #include "nacl_io/osstat.h" | 11 #include "nacl_io/osstat.h" |
| 12 #include "utils/macros.h" | 12 #include "utils/macros.h" |
| 13 #include "utils/auto_lock.h" | 13 #include "utils/auto_lock.h" |
| 14 | 14 |
| 15 MountNodeDir::MountNodeDir(Mount* mount) | 15 MountNodeDir::MountNodeDir(Mount* mount) : MountNode(mount), cache_(NULL) { |
| 16 : MountNode(mount), | |
| 17 cache_(NULL) { | |
| 18 stat_.st_mode |= S_IFDIR; | 16 stat_.st_mode |= S_IFDIR; |
| 19 } | 17 } |
| 20 | 18 |
| 21 MountNodeDir::~MountNodeDir() { | 19 MountNodeDir::~MountNodeDir() { free(cache_); } |
| 22 free(cache_); | 20 |
| 21 Error MountNodeDir::Read(size_t offs, void* buf, size_t count, int* out_bytes) { |
| 22 *out_bytes = 0; |
| 23 return EISDIR; |
| 23 } | 24 } |
| 24 | 25 |
| 25 int MountNodeDir::Read(size_t offs, void *buf, size_t count) { | 26 Error MountNodeDir::FTruncate(off_t size) { return EISDIR; } |
| 26 errno = EISDIR; | 27 |
| 27 return -1; | 28 Error MountNodeDir::Write(size_t offs, |
| 29 void* buf, |
| 30 size_t count, |
| 31 int* out_bytes) { |
| 32 *out_bytes = 0; |
| 33 return EISDIR; |
| 28 } | 34 } |
| 29 | 35 |
| 30 int MountNodeDir::FTruncate(off_t size) { | 36 Error MountNodeDir::GetDents(size_t offs, |
| 31 errno = EISDIR; | 37 struct dirent* pdir, |
| 32 return -1; | 38 size_t size, |
| 33 } | 39 int* out_bytes) { |
| 40 *out_bytes = 0; |
| 34 | 41 |
| 35 int MountNodeDir::Write(size_t offs, void *buf, size_t count) { | |
| 36 errno = EISDIR; | |
| 37 return -1; | |
| 38 } | |
| 39 | |
| 40 int MountNodeDir::GetDents(size_t offs, struct dirent* pdir, size_t size) { | |
| 41 AutoLock lock(&lock_); | 42 AutoLock lock(&lock_); |
| 42 | 43 |
| 43 // If the buffer pointer is invalid, fail | 44 // If the buffer pointer is invalid, fail |
| 44 if (NULL == pdir) { | 45 if (NULL == pdir) |
| 45 errno = EINVAL; | 46 return EINVAL; |
| 46 return -1; | |
| 47 } | |
| 48 | 47 |
| 49 // If the buffer is too small, fail | 48 // If the buffer is too small, fail |
| 50 if (size < sizeof(struct dirent)) { | 49 if (size < sizeof(struct dirent)) |
| 51 errno = EINVAL; | 50 return EINVAL; |
| 52 return -1; | |
| 53 } | |
| 54 | 51 |
| 55 // Force size to a multiple of dirent | 52 // Force size to a multiple of dirent |
| 56 size -= size % sizeof(struct dirent); | 53 size -= size % sizeof(struct dirent); |
| 57 size_t max = map_.size() * sizeof(struct dirent); | 54 size_t max = map_.size() * sizeof(struct dirent); |
| 58 if (cache_ == NULL) BuildCache(); | 55 if (cache_ == NULL) |
| 56 BuildCache(); |
| 59 | 57 |
| 60 if (offs >= max) return 0; | 58 if (offs >= max) { |
| 61 if (offs + size >= max) size = max - offs; | 59 // OK, trying to read past the end. |
| 60 return 0; |
| 61 } |
| 62 | 62 |
| 63 memcpy(pdir, ((char *) cache_) + offs, size); | 63 if (offs + size >= max) |
| 64 return size; | 64 size = max - offs; |
| 65 |
| 66 memcpy(pdir, ((char*)cache_) + offs, size); |
| 67 *out_bytes = size; |
| 68 return 0; |
| 65 } | 69 } |
| 66 | 70 |
| 67 int MountNodeDir::AddChild(const std::string& name, MountNode* node) { | 71 Error MountNodeDir::AddChild(const std::string& name, MountNode* node) { |
| 68 AutoLock lock(&lock_); | 72 AutoLock lock(&lock_); |
| 69 | 73 |
| 70 if (name.empty()) { | 74 if (name.empty()) |
| 71 errno = ENOENT; | 75 return ENOENT; |
| 72 return -1; | 76 |
| 73 } | 77 if (name.length() >= MEMBER_SIZE(struct dirent, d_name)) |
| 74 if (name.length() >= MEMBER_SIZE(struct dirent, d_name)) { | 78 return ENAMETOOLONG; |
| 75 errno = ENAMETOOLONG; | |
| 76 return -1; | |
| 77 } | |
| 78 | 79 |
| 79 MountNodeMap_t::iterator it = map_.find(name); | 80 MountNodeMap_t::iterator it = map_.find(name); |
| 80 if (it != map_.end()) { | 81 if (it != map_.end()) |
| 81 errno = EEXIST; | 82 return EEXIST; |
| 82 return -1; | |
| 83 } | |
| 84 | 83 |
| 85 node->Link(); | 84 node->Link(); |
| 86 map_[name] = node; | 85 map_[name] = node; |
| 87 ClearCache(); | 86 ClearCache(); |
| 88 return 0; | 87 return 0; |
| 89 } | 88 } |
| 90 | 89 |
| 91 int MountNodeDir::RemoveChild(const std::string& name) { | 90 Error MountNodeDir::RemoveChild(const std::string& name) { |
| 92 AutoLock lock(&lock_); | 91 AutoLock lock(&lock_); |
| 93 MountNodeMap_t::iterator it = map_.find(name); | 92 MountNodeMap_t::iterator it = map_.find(name); |
| 94 if (it != map_.end()) { | 93 if (it != map_.end()) { |
| 95 it->second->Unlink(); | 94 it->second->Unlink(); |
| 96 map_.erase(it); | 95 map_.erase(it); |
| 97 ClearCache(); | 96 ClearCache(); |
| 98 return 0; | 97 return 0; |
| 99 } | 98 } |
| 100 errno = ENOENT; | 99 return ENOENT; |
| 101 return -1; | |
| 102 } | 100 } |
| 103 | 101 |
| 104 MountNode* MountNodeDir::FindChild(const std::string& name) { | 102 Error MountNodeDir::FindChild(const std::string& name, MountNode** out_node) { |
| 103 *out_node = NULL; |
| 104 |
| 105 AutoLock lock(&lock_); | 105 AutoLock lock(&lock_); |
| 106 MountNodeMap_t::iterator it = map_.find(name); | 106 MountNodeMap_t::iterator it = map_.find(name); |
| 107 if (it != map_.end()) { | 107 if (it == map_.end()) |
| 108 return it->second; | 108 return ENOENT; |
| 109 } | 109 |
| 110 errno = ENOENT; | 110 *out_node = it->second; |
| 111 return NULL; | 111 return 0; |
| 112 } | 112 } |
| 113 | 113 |
| 114 int MountNodeDir::ChildCount() { | 114 int MountNodeDir::ChildCount() { |
| 115 AutoLock lock(&lock_); | 115 AutoLock lock(&lock_); |
| 116 return map_.size(); | 116 return map_.size(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void MountNodeDir::ClearCache() { | 119 void MountNodeDir::ClearCache() { |
| 120 free(cache_); | 120 free(cache_); |
| 121 cache_ = NULL; | 121 cache_ = NULL; |
| 122 } | 122 } |
| 123 | 123 |
| 124 void MountNodeDir::BuildCache() { | 124 void MountNodeDir::BuildCache() { |
| 125 if (map_.size()) { | 125 if (map_.size()) { |
| 126 cache_ = (struct dirent *) malloc(sizeof(struct dirent) * map_.size()); | 126 cache_ = (struct dirent*)malloc(sizeof(struct dirent) * map_.size()); |
| 127 MountNodeMap_t::iterator it = map_.begin(); | 127 MountNodeMap_t::iterator it = map_.begin(); |
| 128 for (size_t index = 0; it != map_.end(); it++, index++) { | 128 for (size_t index = 0; it != map_.end(); it++, index++) { |
| 129 MountNode* node = it->second; | 129 MountNode* node = it->second; |
| 130 size_t len = it->first.length(); | 130 size_t len = it->first.length(); |
| 131 cache_[index].d_ino = node->stat_.st_ino; | 131 cache_[index].d_ino = node->stat_.st_ino; |
| 132 cache_[index].d_off = sizeof(struct dirent); | 132 cache_[index].d_off = sizeof(struct dirent); |
| 133 cache_[index].d_reclen = sizeof(struct dirent); | 133 cache_[index].d_reclen = sizeof(struct dirent); |
| 134 cache_[index].d_name[len] = 0; | 134 cache_[index].d_name[len] = 0; |
| 135 strncpy(cache_[index].d_name, &it->first[0], len); | 135 strncpy(cache_[index].d_name, &it->first[0], len); |
| 136 } | 136 } |
| 137 } | 137 } |
| 138 } | 138 } |
| OLD | NEW |