OLD | NEW |
---|---|
(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 #include <irt.h> | |
8 #include <cstring> | |
9 #include "../dev/NullDevice.h" | |
10 #include "../dev/RandomDevice.h" | |
11 | |
12 DevMount::DevMount() { | |
13 max_inode = 0; | |
14 NullDevice* dev_null = new NullDevice(); | |
15 Attach("/null", dev_null); | |
16 nacl_irt_random random; | |
17 if (nacl_interface_query(NACL_IRT_RANDOM_v0_1, &random, sizeof(random))) { | |
18 RandomDevice* dev_random = new RandomDevice(random.get_random_bytes); | |
19 Attach("/random", dev_random); | |
20 } | |
21 } | |
22 | |
23 void DevMount::Attach(std::string path, Device* device) { | |
24 if (path_to_inode.find(path) == path_to_inode.end()) { | |
25 int inode = ++this->max_inode; | |
26 inode_to_path[inode] = path; | |
27 inode_to_dev[inode] = device; | |
28 inode_to_ref[inode] = 1; | |
29 path_to_inode[path] = inode; | |
30 } | |
31 } | |
32 void DevMount::Ref(ino_t node) { | |
33 if (inode_to_path.find(node) != inode_to_path.end()) { | |
34 inode_to_ref[node]++; | |
35 } | |
36 } | |
37 void DevMount::Unref(ino_t node) { | |
38 if (inode_to_path.find(node) != inode_to_path.end()) { | |
39 inode_to_ref[node]--; | |
Evgeniy Stepanov
2012/05/11 10:47:34
I suggest dropping all reference counting in DevMo
vissi
2012/05/11 11:49:19
Done.
| |
40 } | |
41 } | |
42 | |
43 int DevMount::Creat(const std::string& path, mode_t mode, struct stat *buf) { | |
Evgeniy Stepanov
2012/05/11 10:47:34
Missing return on one of the code paths.
This func
vissi
2012/05/11 11:49:19
Done.
| |
44 if (path_to_inode.find(path) != path_to_inode.end()) { | |
45 return Stat(path_to_inode[path], buf); | |
46 } | |
47 } | |
48 | |
49 int DevMount::Stat(ino_t node, struct stat *buf) { | |
50 if (inode_to_path.find(node) == inode_to_path.end()) { | |
51 errno = EIO; | |
Evgeniy Stepanov
2012/05/11 10:47:34
ENOENT
vissi
2012/05/11 11:49:19
Done.
| |
52 return -1; | |
53 } | |
54 memset(buf, 0, sizeof(struct stat)); | |
55 buf->st_ino = node; | |
56 buf->st_mode = S_IFREG | 0777; | |
57 return 0; | |
58 } | |
59 | |
60 int DevMount::Getdents(ino_t slot, off_t offset, | |
Evgeniy Stepanov
2012/05/11 10:47:34
You don't have inodes for any directories, so this
vissi
2012/05/11 11:49:19
Should we be able to list existing devices? That's
Evgeniy Stepanov
2012/05/11 12:02:54
Does it work this way? I suspect not. We need a te
| |
61 struct dirent *dir, unsigned int count) { | |
62 for (std::map<std::string, int>::const_iterator it = path_to_inode.begin(); | |
63 it != path_to_inode.end(); ++it) { | |
64 memset(dir, 0, sizeof(struct dirent)); | |
65 // We want d_ino to be non-zero because readdir() | |
66 // will return null if d_ino is zero. | |
67 dir->d_ino = 0x60061E; | |
68 dir->d_reclen = sizeof(struct dirent); | |
69 strncpy(dir->d_name, it->first.c_str(), sizeof(dir->d_name)); | |
70 } | |
71 return 0; | |
72 } | |
73 | |
74 ssize_t DevMount::Read(ino_t slot, off_t offset, void *buf, size_t count) { | |
75 if (inode_to_path.find(slot) != inode_to_path.end()) { | |
76 return inode_to_dev[slot]->Read(offset, buf, count); | |
77 } else { | |
78 errno = EIO; | |
79 return -1; | |
80 } | |
81 } | |
82 | |
83 ssize_t DevMount::Write(ino_t slot, off_t offset, const void *buf, | |
84 size_t count) { | |
85 if (inode_to_path.find(slot) != inode_to_path.end()) { | |
86 return inode_to_dev[slot]->Write(offset, buf, count); | |
87 } else { | |
88 errno = EIO; | |
89 return -1; | |
90 } | |
91 } | |
92 | |
OLD | NEW |