Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/mount_node_dir.cc

Issue 16232016: [NaCl SDK] nacl_io: big refactor to return error value (errno). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge master, fix windows Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "sdk_util/auto_lock.h" 12 #include "sdk_util/auto_lock.h"
13 #include "sdk_util/macros.h" 13 #include "sdk_util/macros.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() {
20 for (MountNodeMap_t::iterator it = map_.begin(); it != map_.end(); ++it) {
21 it->second->Unlink();
22 }
22 free(cache_); 23 free(cache_);
23 } 24 }
24 25
25 int MountNodeDir::Read(size_t offs, void *buf, size_t count) { 26 Error MountNodeDir::Read(size_t offs, void* buf, size_t count, int* out_bytes) {
26 errno = EISDIR; 27 *out_bytes = 0;
27 return -1; 28 return EISDIR;
28 } 29 }
29 30
30 int MountNodeDir::FTruncate(off_t size) { 31 Error MountNodeDir::FTruncate(off_t size) { return EISDIR; }
31 errno = EISDIR; 32
32 return -1; 33 Error MountNodeDir::Write(size_t offs,
34 void* buf,
35 size_t count,
36 int* out_bytes) {
37 *out_bytes = 0;
38 return EISDIR;
33 } 39 }
34 40
35 int MountNodeDir::Write(size_t offs, void *buf, size_t count) { 41 Error MountNodeDir::GetDents(size_t offs,
36 errno = EISDIR; 42 struct dirent* pdir,
37 return -1; 43 size_t size,
38 } 44 int* out_bytes) {
45 *out_bytes = 0;
39 46
40 int MountNodeDir::GetDents(size_t offs, struct dirent* pdir, size_t size) {
41 AutoLock lock(&lock_); 47 AutoLock lock(&lock_);
42 48
43 // If the buffer pointer is invalid, fail 49 // If the buffer pointer is invalid, fail
44 if (NULL == pdir) { 50 if (NULL == pdir)
45 errno = EINVAL; 51 return EINVAL;
46 return -1;
47 }
48 52
49 // If the buffer is too small, fail 53 // If the buffer is too small, fail
50 if (size < sizeof(struct dirent)) { 54 if (size < sizeof(struct dirent))
51 errno = EINVAL; 55 return EINVAL;
52 return -1;
53 }
54 56
55 // Force size to a multiple of dirent 57 // Force size to a multiple of dirent
56 size -= size % sizeof(struct dirent); 58 size -= size % sizeof(struct dirent);
57 size_t max = map_.size() * sizeof(struct dirent); 59 size_t max = map_.size() * sizeof(struct dirent);
58 if (cache_ == NULL) BuildCache(); 60 if (cache_ == NULL)
61 BuildCache();
59 62
60 if (offs >= max) return 0; 63 if (offs >= max) {
61 if (offs + size >= max) size = max - offs; 64 // OK, trying to read past the end.
65 return 0;
66 }
62 67
63 memcpy(pdir, ((char *) cache_) + offs, size); 68 if (offs + size >= max)
64 return size; 69 size = max - offs;
70
71 memcpy(pdir, ((char*)cache_) + offs, size);
72 *out_bytes = size;
73 return 0;
65 } 74 }
66 75
67 int MountNodeDir::AddChild(const std::string& name, MountNode* node) { 76 Error MountNodeDir::AddChild(const std::string& name, MountNode* node) {
68 AutoLock lock(&lock_); 77 AutoLock lock(&lock_);
69 78
70 if (name.empty()) { 79 if (name.empty())
71 errno = ENOENT; 80 return ENOENT;
72 return -1; 81
73 } 82 if (name.length() >= MEMBER_SIZE(struct dirent, d_name))
74 if (name.length() >= MEMBER_SIZE(struct dirent, d_name)) { 83 return ENAMETOOLONG;
75 errno = ENAMETOOLONG;
76 return -1;
77 }
78 84
79 MountNodeMap_t::iterator it = map_.find(name); 85 MountNodeMap_t::iterator it = map_.find(name);
80 if (it != map_.end()) { 86 if (it != map_.end())
81 errno = EEXIST; 87 return EEXIST;
82 return -1;
83 }
84 88
85 node->Link(); 89 node->Link();
86 map_[name] = node; 90 map_[name] = node;
87 ClearCache(); 91 ClearCache();
88 return 0; 92 return 0;
89 } 93 }
90 94
91 int MountNodeDir::RemoveChild(const std::string& name) { 95 Error MountNodeDir::RemoveChild(const std::string& name) {
92 AutoLock lock(&lock_); 96 AutoLock lock(&lock_);
93 MountNodeMap_t::iterator it = map_.find(name); 97 MountNodeMap_t::iterator it = map_.find(name);
94 if (it != map_.end()) { 98 if (it != map_.end()) {
95 it->second->Unlink(); 99 it->second->Unlink();
96 map_.erase(it); 100 map_.erase(it);
97 ClearCache(); 101 ClearCache();
98 return 0; 102 return 0;
99 } 103 }
100 errno = ENOENT; 104 return ENOENT;
101 return -1;
102 } 105 }
103 106
104 MountNode* MountNodeDir::FindChild(const std::string& name) { 107 Error MountNodeDir::FindChild(const std::string& name, MountNode** out_node) {
108 *out_node = NULL;
109
105 AutoLock lock(&lock_); 110 AutoLock lock(&lock_);
106 MountNodeMap_t::iterator it = map_.find(name); 111 MountNodeMap_t::iterator it = map_.find(name);
107 if (it != map_.end()) { 112 if (it == map_.end())
108 return it->second; 113 return ENOENT;
109 } 114
110 errno = ENOENT; 115 *out_node = it->second;
111 return NULL; 116 return 0;
112 } 117 }
113 118
114 int MountNodeDir::ChildCount() { 119 int MountNodeDir::ChildCount() {
115 AutoLock lock(&lock_); 120 AutoLock lock(&lock_);
116 return map_.size(); 121 return map_.size();
117 } 122 }
118 123
119 void MountNodeDir::ClearCache() { 124 void MountNodeDir::ClearCache() {
120 free(cache_); 125 free(cache_);
121 cache_ = NULL; 126 cache_ = NULL;
122 } 127 }
123 128
124 void MountNodeDir::BuildCache() { 129 void MountNodeDir::BuildCache() {
125 if (map_.size()) { 130 if (map_.size()) {
126 cache_ = (struct dirent *) malloc(sizeof(struct dirent) * map_.size()); 131 cache_ = (struct dirent*)malloc(sizeof(struct dirent) * map_.size());
127 MountNodeMap_t::iterator it = map_.begin(); 132 MountNodeMap_t::iterator it = map_.begin();
128 for (size_t index = 0; it != map_.end(); it++, index++) { 133 for (size_t index = 0; it != map_.end(); it++, index++) {
129 MountNode* node = it->second; 134 MountNode* node = it->second;
130 size_t len = it->first.length(); 135 size_t len = it->first.length();
131 cache_[index].d_ino = node->stat_.st_ino; 136 cache_[index].d_ino = node->stat_.st_ino;
132 cache_[index].d_off = sizeof(struct dirent); 137 cache_[index].d_off = sizeof(struct dirent);
133 cache_[index].d_reclen = sizeof(struct dirent); 138 cache_[index].d_reclen = sizeof(struct dirent);
134 cache_[index].d_name[len] = 0; 139 cache_[index].d_name[len] = 0;
135 strncpy(cache_[index].d_name, &it->first[0], len); 140 strncpy(cache_[index].d_name, &it->first[0], len);
136 } 141 }
137 } 142 }
138 } 143 }
OLDNEW
« no previous file with comments | « native_client_sdk/src/libraries/nacl_io/mount_node_dir.h ('k') | native_client_sdk/src/libraries/nacl_io/mount_node_html5fs.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698