1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "nacl_io/dir_node.h"
10 #include "nacl_io/log.h"
11 #include "nacl_io/osdirent.h"
12 #include "nacl_io/osinttypes.h"
13 #include "nacl_io/osstat.h"
14 #include "sdk_util/auto_lock.h"
15 #include "sdk_util/macros.h"
21 // TODO(binji): For now, just use a dummy value for the parent ino.
22 const ino_t kParentDirIno = -1;
25 DirNode::DirNode(Filesystem* filesystem, mode_t mode)
27 cache_(stat_.st_ino, kParentDirIno),
34 for (NodeMap_t::iterator it = map_.begin(); it != map_.end(); ++it) {
39 Error DirNode::Read(const HandleAttr& attr,
44 LOG_TRACE("Can't read a directory.");
48 Error DirNode::FTruncate(off_t size) {
49 LOG_TRACE("Can't truncate a directory.");
53 Error DirNode::Write(const HandleAttr& attr,
58 LOG_TRACE("Can't write to a directory.");
62 Error DirNode::GetDents(size_t offs,
66 AUTO_LOCK(node_lock_);
68 return cache_.GetDents(offs, pdir, size, out_bytes);
71 Error DirNode::Fchmod(mode_t mode) {
72 AUTO_LOCK(node_lock_);
77 Error DirNode::AddChild(const std::string& name, const ScopedNode& node) {
78 AUTO_LOCK(node_lock_);
81 LOG_ERROR("Can't add child with no name.");
85 if (name.length() >= MEMBER_SIZE(dirent, d_name)) {
86 LOG_ERROR("Child name is too long: %" PRIuS " >= %" PRIuS,
88 MEMBER_SIZE(dirent, d_name));
92 NodeMap_t::iterator it = map_.find(name);
93 if (it != map_.end()) {
94 LOG_TRACE("Can't add child \"%s\", it already exists.", name.c_str());
104 Error DirNode::RemoveChild(const std::string& name) {
105 AUTO_LOCK(node_lock_);
106 NodeMap_t::iterator it = map_.find(name);
107 if (it != map_.end()) {
108 it->second->Unlink();
116 Error DirNode::FindChild(const std::string& name, ScopedNode* out_node) {
117 out_node->reset(NULL);
119 AUTO_LOCK(node_lock_);
120 NodeMap_t::iterator it = map_.find(name);
121 if (it == map_.end())
124 *out_node = it->second;
128 int DirNode::ChildCount() {
129 AUTO_LOCK(node_lock_);
133 void DirNode::BuildCache_Locked() {
137 for (NodeMap_t::iterator it = map_.begin(), end = map_.end(); it != end;
139 const std::string& name = it->first;
140 ino_t ino = it->second->stat_.st_ino;
141 cache_.AddDirent(ino, name.c_str(), name.length());
147 void DirNode::ClearCache_Locked() {
148 cache_built_ = false;
152 } // namespace nacl_io