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 | 5 |
| 6 #include <dirent.h> |
6 #include <errno.h> | 7 #include <errno.h> |
7 #include <sys/stat.h> | 8 #include <sys/stat.h> |
8 | 9 |
| 10 #include "macros.h" |
9 #include "auto_lock.h" | 11 #include "auto_lock.h" |
10 #include "mount_node_dir.h" | 12 #include "mount_node_dir.h" |
11 | 13 |
12 MountNodeDir::MountNodeDir(Mount* mount, int ino, int dev) : | 14 MountNodeDir::MountNodeDir(Mount* mount, int ino, int dev) |
13 MountNode(mount, ino, dev), | 15 : MountNode(mount, ino, dev), |
14 cache_(NULL) { | 16 cache_(NULL) { |
15 } | 17 } |
16 | 18 |
17 MountNodeDir::~MountNodeDir() { | 19 MountNodeDir::~MountNodeDir() { |
18 free(cache_); | 20 free(cache_); |
19 } | 21 } |
20 | 22 |
21 bool MountNodeDir::Init(int mode, short uid, short gid) { | 23 bool MountNodeDir::Init(int mode, short uid, short gid) { |
22 bool ok = MountNode::Init(mode, uid, gid); | 24 bool ok = MountNode::Init(mode, uid, gid); |
23 stat_.st_mode |= _S_IFDIR; | 25 stat_.st_mode |= _S_IFDIR; |
24 return ok; | 26 return ok; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 return size; | 68 return size; |
67 } | 69 } |
68 | 70 |
69 int MountNodeDir:: AddChild(const std::string& name, MountNode* node) { | 71 int MountNodeDir:: AddChild(const std::string& name, MountNode* node) { |
70 AutoLock lock(&lock_); | 72 AutoLock lock(&lock_); |
71 | 73 |
72 if (name.empty()) { | 74 if (name.empty()) { |
73 errno = ENOENT; | 75 errno = ENOENT; |
74 return -1; | 76 return -1; |
75 } | 77 } |
76 | |
77 if (name.length() >= MEMBER_SIZE(struct dirent, d_name)) { | 78 if (name.length() >= MEMBER_SIZE(struct dirent, d_name)) { |
78 errno = ENAMETOOLONG; | 79 errno = ENAMETOOLONG; |
79 return -1; | 80 return -1; |
80 } | 81 } |
81 | 82 |
82 MountNodeMap_t::iterator it = map_.find(name); | 83 MountNodeMap_t::iterator it = map_.find(name); |
83 if (it != map_.end()) { | 84 if (it != map_.end()) { |
84 errno = EEXIST; | 85 errno = EEXIST; |
85 return -1; | 86 return -1; |
86 } | 87 } |
(...skipping 20 matching lines...) Expand all Loading... |
107 MountNode* MountNodeDir::FindChild(const std::string& name) { | 108 MountNode* MountNodeDir::FindChild(const std::string& name) { |
108 AutoLock lock(&lock_); | 109 AutoLock lock(&lock_); |
109 MountNodeMap_t::iterator it = map_.find(name); | 110 MountNodeMap_t::iterator it = map_.find(name); |
110 if (it != map_.end()) { | 111 if (it != map_.end()) { |
111 return it->second; | 112 return it->second; |
112 } | 113 } |
113 errno = ENOENT; | 114 errno = ENOENT; |
114 return NULL; | 115 return NULL; |
115 } | 116 } |
116 | 117 |
| 118 int MountNodeDir::ChildCount() { |
| 119 AutoLock lock(&lock_); |
| 120 return map_.size(); |
| 121 } |
| 122 |
117 void MountNodeDir::ClearCache() { | 123 void MountNodeDir::ClearCache() { |
118 free(cache_); | 124 free(cache_); |
119 cache_ = NULL; | 125 cache_ = NULL; |
120 } | 126 } |
121 | 127 |
122 void MountNodeDir::BuildCache() { | 128 void MountNodeDir::BuildCache() { |
123 if (map_.size()) { | 129 if (map_.size()) { |
124 cache_ = (struct dirent *) malloc(sizeof(struct dirent) * map_.size()); | 130 cache_ = (struct dirent *) malloc(sizeof(struct dirent) * map_.size()); |
125 MountNodeMap_t::iterator it = map_.begin(); | 131 MountNodeMap_t::iterator it = map_.begin(); |
126 for (size_t index = 0; it != map_.end(); it++, index++) { | 132 for (size_t index = 0; it != map_.end(); it++, index++) { |
127 MountNode* node = it->second; | 133 MountNode* node = it->second; |
128 size_t len = it->first.length(); | 134 size_t len = it->first.length(); |
129 cache_[index].d_ino = node->stat_.st_ino; | 135 cache_[index].d_ino = node->stat_.st_ino; |
130 cache_[index].d_reclen = sizeof(struct dirent); | 136 cache_[index].d_reclen = sizeof(struct dirent); |
131 cache_[index].d_name[len] = 0; | 137 cache_[index].d_name[len] = 0; |
132 strncpy(cache_[index].d_name, &it->first[0], len); | 138 strncpy(cache_[index].d_name, &it->first[0], len); |
133 } | 139 } |
134 } | 140 } |
135 } | 141 } |
OLD | NEW |