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

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
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 #include "../dev/RandomDevice.h"
8 #include "../dev/NullDevice.h"
9
10 DevMount::DevMount() {
11 // RandomDevice* dev_random = new RandomDevice(??);
12 // Attach("/random", dev_random);
13 NullDevice* dev_null = new NullDevice();
14 Attach("/null", dev_null);
15 }
16
17 void DevMount::Attach(std::string path, Device* device) {
18 if (path_to_inode.find(path) == path_to_inode.end()) {
19 int inode = device->GetInode();
20 inode_to_path[inode] = path;
21 inode_to_dev[inode] = device;
22 inode_to_ref[inode] = 1;
23 path_to_inode[path] = inode;
24 }
25 }
26 void DevMount::Ref(ino_t node) {
27 if (inode_to_path.find(slot) != inode_to_path.end()) {
28 inode_to_ref[node]++;
29 }
30 }
31 void DevMount::Unref(ino_t node) {
32 if (inode_to_path.find(slot) != inode_to_path.end()) {
33 inode_to_ref[node]--;
34 }
35 }
36
37 int DevMount::Creat(const std::string& path, mode_t mode, struct stat *buf) {
38 if (path_to_inode.find(path) != path_to_inode.end()) {
39 return Stat(path_to_inode[path], buf);
40 }
41
42 int DevMount::Stat(ino_t node, struct stat *buf) {
43 if (inode_to_path.find(node) == inode_to_path.end())
44 return -1;
45 memset(buf, 0, sizeof(struct stat));
46 buf->st_ino = node;
47 buf->st_mode = S_IFREG | 0777;
48 return 0;
49 }
50
51 int DevMount::Getdents(ino_t slot, off_t offset,
52 struct dirent *dir, unsigned int count) {
53 for (std::map<string, int>::const_iterator it = path_to_inode.begin();
54 it != path_to_inode.end(); ++it) {
55 memset(dir, 0, sizeof(struct dirent));
56 // We want d_ino to be non-zero because readdir()
57 // will return null if d_ino is zero.
58 dir->d_ino = 0x60061E;
59 dir->d_reclen = sizeof(struct dirent);
60 strncpy(dir->d_name, it->first().c_str(), sizeof(dir->d_name));
61 }
62 return -1;
63 }
64
65 ssize_t DevMount::Read(ino_t slot, off_t offset, void *buf, size_t count) {
66 if (inode_to_path.find(slot) != inode_to_path.end()) {
67 return inode_to_dev[device->GetInode()].Read(offset, buf, count);
68 } else {
69 return -1;
70 }
71 }
72
73 ssize_t DevMount::Write(ino_t slot, off_t offset, const void *buf,
74 size_t count) {
75 if (inode_to_path.find(slot) != inode_to_path.end()) {
76 return inode_to_dev[device->GetInode()].Write(offset, buf, count);
77 } else {
78 return -1;
79 }
80 }
81
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698