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

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