Chromium Code Reviews| Index: libraries/nacl-mounts/dev/DevMount.cc |
| =================================================================== |
| --- libraries/nacl-mounts/dev/DevMount.cc (revision 0) |
| +++ libraries/nacl-mounts/dev/DevMount.cc (revision 0) |
| @@ -0,0 +1,92 @@ |
| +/* |
| + * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| +#include "../dev/DevMount.h" |
| +#include <irt.h> |
| +#include <cstring> |
| +#include "../dev/NullDevice.h" |
| +#include "../dev/RandomDevice.h" |
| + |
| +DevMount::DevMount() { |
| + max_inode = 0; |
| + NullDevice* dev_null = new NullDevice(); |
| + Attach("/null", dev_null); |
| + nacl_irt_random random; |
| + if (nacl_interface_query(NACL_IRT_RANDOM_v0_1, &random, sizeof(random))) { |
| + RandomDevice* dev_random = new RandomDevice(random.get_random_bytes); |
| + Attach("/random", dev_random); |
| + } |
| +} |
| + |
| +void DevMount::Attach(std::string path, Device* device) { |
| + if (path_to_inode.find(path) == path_to_inode.end()) { |
| + int inode = ++this->max_inode; |
| + inode_to_path[inode] = path; |
| + inode_to_dev[inode] = device; |
| + inode_to_ref[inode] = 1; |
| + path_to_inode[path] = inode; |
| + } |
| +} |
| +void DevMount::Ref(ino_t node) { |
| + if (inode_to_path.find(node) != inode_to_path.end()) { |
| + inode_to_ref[node]++; |
| + } |
| +} |
| +void DevMount::Unref(ino_t node) { |
| + if (inode_to_path.find(node) != inode_to_path.end()) { |
| + 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.
|
| + } |
| +} |
| + |
| +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.
|
| + if (path_to_inode.find(path) != path_to_inode.end()) { |
| + return Stat(path_to_inode[path], buf); |
| + } |
| +} |
| + |
| +int DevMount::Stat(ino_t node, struct stat *buf) { |
| + if (inode_to_path.find(node) == inode_to_path.end()) { |
| + errno = EIO; |
|
Evgeniy Stepanov
2012/05/11 10:47:34
ENOENT
vissi
2012/05/11 11:49:19
Done.
|
| + return -1; |
| + } |
| + memset(buf, 0, sizeof(struct stat)); |
| + buf->st_ino = node; |
| + buf->st_mode = S_IFREG | 0777; |
| + return 0; |
| +} |
| + |
| +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
|
| + struct dirent *dir, unsigned int count) { |
| + for (std::map<std::string, int>::const_iterator it = path_to_inode.begin(); |
| + it != path_to_inode.end(); ++it) { |
| + memset(dir, 0, sizeof(struct dirent)); |
| + // We want d_ino to be non-zero because readdir() |
| + // will return null if d_ino is zero. |
| + dir->d_ino = 0x60061E; |
| + dir->d_reclen = sizeof(struct dirent); |
| + strncpy(dir->d_name, it->first.c_str(), sizeof(dir->d_name)); |
| + } |
| + return 0; |
| +} |
| + |
| +ssize_t DevMount::Read(ino_t slot, off_t offset, void *buf, size_t count) { |
| + if (inode_to_path.find(slot) != inode_to_path.end()) { |
| + return inode_to_dev[slot]->Read(offset, buf, count); |
| + } else { |
| + errno = EIO; |
| + return -1; |
| + } |
| +} |
| + |
| +ssize_t DevMount::Write(ino_t slot, off_t offset, const void *buf, |
| + size_t count) { |
| + if (inode_to_path.find(slot) != inode_to_path.end()) { |
| + return inode_to_dev[slot]->Write(offset, buf, count); |
| + } else { |
| + errno = EIO; |
| + return -1; |
| + } |
| +} |
| + |