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

Side by Side Diff: libraries/nacl-mounts/dev/DevMount.cc

Issue 10377102: Added simplest device mount implementation (includes NullDevice and RandomDevice) (Closed) Base URL: http://naclports.googlecode.com/svn/trunk/src/
Patch Set: Created 8 years, 7 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
« no previous file with comments | « libraries/nacl-mounts/dev/DevMount.h ('k') | libraries/nacl-mounts/dev/Device.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6 #include "../dev/DevMount.h"
7 #ifdef __native_client__
8 #include <irt.h>
9 #endif
10 #include <sys/stat.h>
11 #include <cstring>
12 #include "../dev/NullDevice.h"
13 #ifdef __native_client__
14 #include "../dev/RandomDevice.h"
15 #endif
16
17 DevMount::DevMount() {
18 max_inode = 2;
19 // 1 and 2 are reserved for / and /fd, respectively
20 NullDevice* dev_null = new NullDevice();
21 if (dev_null == NULL) {
22 throw 1;
23 }
24 Attach("/null", dev_null);
25 #ifdef __native_client__
26 nacl_irt_random random;
27 if (nacl_interface_query(NACL_IRT_RANDOM_v0_1, &random, sizeof(random))) {
28 RandomDevice* dev_random = new RandomDevice(random.get_random_bytes);
29 if (dev_random == NULL) {
30 throw 1;
31 }
32 Attach("/random", dev_random);
33 }
34 #endif
35 }
36
37 void DevMount::Attach(std::string path, Device* device) {
38 if (path_to_inode.find(path) == path_to_inode.end()) {
39 int inode = ++this->max_inode;
40 inode_to_path[inode] = path;
41 inode_to_dev[inode] = device;
42 path_to_inode[path] = inode;
43 }
44 }
45
46 int DevMount::Mkdir(const std::string& path, mode_t mode,
47 struct stat *st) {
48 if (path == "/fd" || path == "fd") {
49 return Stat(2, st);
50 }
51 errno = ENOENT;
52 return -1;
53 }
54
55 int DevMount::GetNode(const std::string& path, struct stat *buf) {
56 if (path == "/") {
57 return Stat(1, buf);
58 };
59 if (path == "/fd" || path == "fd") {
60 return Stat(2, buf);
61 };
62 if (path_to_inode.find(path) != path_to_inode.end()) {
63 return Stat(path_to_inode[path], buf);
64 } else {
65 errno = ENOENT;
66 return -1;
67 }
68 }
69
70 int DevMount::Stat(ino_t node, struct stat *buf) {
71 if (inode_to_path.find(node) == inode_to_path.end() && node > 2) {
72 errno = ENOENT;
73 return -1;
74 }
75 if (buf == NULL)
76 return 0;
77 memset(buf, 0, sizeof(struct stat));
78 buf->st_ino = node;
79 if (node > 2) {
80 buf->st_mode = S_IFREG | 0777;
81 } else {
82 buf->st_mode = S_IFDIR | 0777;
83 };
84 return 0;
85 }
86
87 int DevMount::Getdents(ino_t slot, off_t offset,
88 struct dirent *dir, unsigned int count) {
89 if (dir == NULL || slot != 1) {
90 errno = ENOENT;
91 return -1;
92 };
93 int cnt = 0;
94 for (std::map<std::string, int>::const_iterator it = path_to_inode.begin();
95 it != path_to_inode.end(); ++it) {
96 cnt += sizeof(struct dirent);
97 if (cnt > static_cast<int>(count)) {
98 cnt -= sizeof(struct dirent);
99 break;
100 }
101 memset(dir, 0, sizeof(struct dirent));
102 // We want d_ino to be non-zero because readdir()
103 // will return null if d_ino is zero.
104 dir->d_ino = 0x60061E;
105 dir->d_reclen = sizeof(struct dirent);
106 strncpy(dir->d_name, it->first.c_str(), sizeof(dir->d_name));
107 }
108 return cnt;
109 }
110
111 ssize_t DevMount::Read(ino_t slot, off_t offset, void *buf, size_t count) {
112 if (buf == NULL) {
113 errno = ENOENT;
114 return -1;
115 };
116 if (inode_to_path.find(slot) != inode_to_path.end()) {
117 return inode_to_dev[slot]->Read(offset, buf, count);
118 } else {
119 errno = ENOENT;
120 return -1;
121 }
122 }
123
124 ssize_t DevMount::Write(ino_t slot, off_t offset, const void *buf,
125 size_t count) {
126 if (buf == NULL) {
127 errno = ENOENT;
128 return -1;
129 };
130 if (inode_to_path.find(slot) != inode_to_path.end()) {
131 return inode_to_dev[slot]->Write(offset, buf, count);
132 } else {
133 errno = ENOENT;
134 return -1;
135 }
136 }
137
OLDNEW
« no previous file with comments | « libraries/nacl-mounts/dev/DevMount.h ('k') | libraries/nacl-mounts/dev/Device.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698