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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « libraries/nacl-mounts/dev/DevMount.h ('k') | libraries/nacl-mounts/dev/Device.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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,137 @@
+/*
+ * 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"
+#ifdef __native_client__
+#include <irt.h>
+#endif
+#include <sys/stat.h>
+#include <cstring>
+#include "../dev/NullDevice.h"
+#ifdef __native_client__
+#include "../dev/RandomDevice.h"
+#endif
+
+DevMount::DevMount() {
+ max_inode = 2;
+ // 1 and 2 are reserved for / and /fd, respectively
+ NullDevice* dev_null = new NullDevice();
+ if (dev_null == NULL) {
+ throw 1;
+ }
+ Attach("/null", dev_null);
+#ifdef __native_client__
+ 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);
+ if (dev_random == NULL) {
+ throw 1;
+ }
+ Attach("/random", dev_random);
+ }
+#endif
+}
+
+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;
+ path_to_inode[path] = inode;
+ }
+}
+
+int DevMount::Mkdir(const std::string& path, mode_t mode,
+ struct stat *st) {
+ if (path == "/fd" || path == "fd") {
+ return Stat(2, st);
+ }
+ errno = ENOENT;
+ return -1;
+}
+
+int DevMount::GetNode(const std::string& path, struct stat *buf) {
+ if (path == "/") {
+ return Stat(1, buf);
+ };
+ if (path == "/fd" || path == "fd") {
+ return Stat(2, buf);
+ };
+ if (path_to_inode.find(path) != path_to_inode.end()) {
+ return Stat(path_to_inode[path], buf);
+ } else {
+ errno = ENOENT;
+ return -1;
+ }
+}
+
+int DevMount::Stat(ino_t node, struct stat *buf) {
+ if (inode_to_path.find(node) == inode_to_path.end() && node > 2) {
+ errno = ENOENT;
+ return -1;
+ }
+ if (buf == NULL)
+ return 0;
+ memset(buf, 0, sizeof(struct stat));
+ buf->st_ino = node;
+ if (node > 2) {
+ buf->st_mode = S_IFREG | 0777;
+ } else {
+ buf->st_mode = S_IFDIR | 0777;
+ };
+ return 0;
+}
+
+int DevMount::Getdents(ino_t slot, off_t offset,
+ struct dirent *dir, unsigned int count) {
+ if (dir == NULL || slot != 1) {
+ errno = ENOENT;
+ return -1;
+ };
+ int cnt = 0;
+ for (std::map<std::string, int>::const_iterator it = path_to_inode.begin();
+ it != path_to_inode.end(); ++it) {
+ cnt += sizeof(struct dirent);
+ if (cnt > static_cast<int>(count)) {
+ cnt -= sizeof(struct dirent);
+ break;
+ }
+ 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 cnt;
+}
+
+ssize_t DevMount::Read(ino_t slot, off_t offset, void *buf, size_t count) {
+ if (buf == NULL) {
+ errno = ENOENT;
+ return -1;
+ };
+ if (inode_to_path.find(slot) != inode_to_path.end()) {
+ return inode_to_dev[slot]->Read(offset, buf, count);
+ } else {
+ errno = ENOENT;
+ return -1;
+ }
+}
+
+ssize_t DevMount::Write(ino_t slot, off_t offset, const void *buf,
+ size_t count) {
+ if (buf == NULL) {
+ errno = ENOENT;
+ return -1;
+ };
+ if (inode_to_path.find(slot) != inode_to_path.end()) {
+ return inode_to_dev[slot]->Write(offset, buf, count);
+ } else {
+ errno = ENOENT;
+ return -1;
+ }
+}
+
« 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