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

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,82 @@
+/*
+ * 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 "../dev/NullDevice.h"
+#include "../dev/RandomDevice.h"
+
+DevMount::DevMount() {
+ // RandomDevice* dev_random = new RandomDevice(??);
+ // Attach("/random", dev_random);
+ NullDevice* dev_null = new NullDevice();
+ max_inode = 0;
+ Attach("/null", dev_null);
+}
+
+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(slot) != inode_to_path.end()) {
+ inode_to_ref[node]++;
+ }
+}
+void DevMount::Unref(ino_t node) {
+ if (inode_to_path.find(slot) != inode_to_path.end()) {
+ inode_to_ref[node]--;
+ }
+}
+
+int DevMount::Creat(const std::string& path, mode_t mode, struct stat *buf) {
+ 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())
+ 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,
+ struct dirent *dir, unsigned int count) {
+ for (std::map<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 -1;
+}
+
+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 {
+ return -1;
Evgeniy Stepanov 2012/05/11 08:46:38 Please set errno when returning with an error.
vissi 2012/05/11 08:55:40 Done.
+ }
+}
+
+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[device->GetInode()].Write(offset, buf, count);
+ } else {
+ 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