OLD | NEW |
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 #include "nacl_io/mount_passthrough.h" | 5 #include "nacl_io/mount_passthrough.h" |
6 #include <errno.h> | 6 #include <errno.h> |
7 #include "nacl_io/kernel_wrap_real.h" | 7 #include "nacl_io/kernel_wrap_real.h" |
8 | 8 |
9 class MountNodePassthrough : public MountNode { | 9 class MountNodePassthrough : public MountNode { |
10 public: | 10 public: |
11 explicit MountNodePassthrough(Mount* mount, int real_fd) | 11 explicit MountNodePassthrough(Mount* mount, int real_fd) |
12 : MountNode(mount), | 12 : MountNode(mount), real_fd_(real_fd) {} |
13 real_fd_(real_fd) { | |
14 } | |
15 | 13 |
16 protected: | 14 protected: |
17 virtual bool Init(int flags) { | 15 virtual Error Init(int flags) { return 0; } |
18 return true; | |
19 } | |
20 | 16 |
21 virtual void Destroy() { | 17 virtual void Destroy() { |
22 if (real_fd_) | 18 if (real_fd_) |
23 _real_close(real_fd_); | 19 _real_close(real_fd_); |
24 real_fd_ = 0; | 20 real_fd_ = 0; |
25 } | 21 } |
26 | 22 |
27 public: | 23 public: |
28 // Normal read/write operations on a file | 24 // Normal read/write operations on a file |
29 virtual int Read(size_t offs, void* buf, size_t count) { | 25 virtual Error Read(size_t offs, void* buf, size_t count, int* out_bytes) { |
| 26 *out_bytes = 0; |
| 27 |
30 off_t new_offset; | 28 off_t new_offset; |
31 int err = _real_lseek(real_fd_, offs, 0, &new_offset); | 29 int err = _real_lseek(real_fd_, offs, 0, &new_offset); |
32 if (err) { | 30 if (err) |
33 errno = err; | 31 return err; |
34 return -1; | |
35 } | |
36 | 32 |
37 size_t nread; | 33 size_t nread; |
38 err = _real_read(real_fd_, buf, count, &nread); | 34 err = _real_read(real_fd_, buf, count, &nread); |
39 if (err) { | 35 if (err) |
40 errno = err; | 36 return err; |
41 return -1; | |
42 } | |
43 | 37 |
44 return static_cast<int>(nread); | 38 *out_bytes = static_cast<int>(nread); |
| 39 return 0; |
45 } | 40 } |
46 | 41 |
47 virtual int Write(size_t offs, const void* buf, size_t count) { | 42 virtual Error Write(size_t offs, |
| 43 const void* buf, |
| 44 size_t count, |
| 45 int* out_bytes) { |
| 46 *out_bytes = 0; |
| 47 |
48 off_t new_offset; | 48 off_t new_offset; |
49 int err = _real_lseek(real_fd_, offs, 0, &new_offset); | 49 int err = _real_lseek(real_fd_, offs, 0, &new_offset); |
50 if (err) { | 50 if (err) |
51 errno = err; | 51 return err; |
52 return -1; | |
53 } | |
54 | 52 |
55 size_t nwrote; | 53 size_t nwrote; |
56 err = _real_write(real_fd_, buf, count, &nwrote); | 54 err = _real_write(real_fd_, buf, count, &nwrote); |
57 if (err) { | 55 if (err) |
58 errno = err; | 56 return err; |
59 return -1; | |
60 } | |
61 | 57 |
62 return static_cast<int>(nwrote); | 58 *out_bytes = static_cast<int>(nwrote); |
| 59 return 0; |
63 } | 60 } |
64 | 61 |
65 virtual int FTruncate(off_t size) { | 62 virtual Error FTruncate(off_t size) { |
66 // TODO(binji): what to do here? | 63 // TODO(binji): what to do here? |
67 return -1; | 64 return ENOSYS; |
68 } | 65 } |
69 | 66 |
70 virtual int GetDents(size_t offs, struct dirent* pdir, size_t count) { | 67 virtual Error GetDents(size_t offs, struct dirent* pdir, size_t count) { |
71 size_t nread; | 68 size_t nread; |
72 int err = _real_getdents(real_fd_, pdir, count, &nread); | 69 int err = _real_getdents(real_fd_, pdir, count, &nread); |
73 if (err) { | 70 if (err) |
74 errno = err; | 71 return err; |
75 return -1; | |
76 } | |
77 | |
78 return nread; | 72 return nread; |
79 } | 73 } |
80 | 74 |
81 virtual int GetStat(struct stat* stat) { | 75 virtual Error GetStat(struct stat* stat) { |
82 int err = _real_fstat(real_fd_, stat); | 76 int err = _real_fstat(real_fd_, stat); |
83 if (err) { | 77 if (err) |
84 errno = err; | 78 return err; |
85 return -1; | |
86 } | |
87 | |
88 return 0; | 79 return 0; |
89 } | 80 } |
90 | 81 |
91 void* MMap(void* addr, size_t length, int prot, int flags, size_t offset) { | 82 Error MMap(void* addr, |
92 void* new_addr = addr; | 83 size_t length, |
93 int err = _real_mmap(&new_addr, length, prot, flags, real_fd_, offset); | 84 int prot, |
94 if (err) { | 85 int flags, |
95 errno = err; | 86 size_t offset, |
96 return (void*)-1; | 87 void** out_addr) { |
97 } | 88 *out_addr = addr; |
98 | 89 int err = _real_mmap(out_addr, length, prot, flags, real_fd_, offset); |
99 return new_addr; | 90 if (err) |
| 91 return err; |
| 92 return 0; |
100 } | 93 } |
101 | 94 |
102 private: | 95 private: |
103 friend class MountPassthrough; | 96 friend class MountPassthrough; |
104 | 97 |
105 int real_fd_; | 98 int real_fd_; |
106 }; | 99 }; |
107 | 100 |
108 MountPassthrough::MountPassthrough() { | 101 MountPassthrough::MountPassthrough() {} |
| 102 |
| 103 Error MountPassthrough::Init(int dev, |
| 104 StringMap_t& args, |
| 105 PepperInterface* ppapi) { |
| 106 return Mount::Init(dev, args, ppapi); |
109 } | 107 } |
110 | 108 |
111 bool MountPassthrough::Init(int dev, StringMap_t& args, | 109 void MountPassthrough::Destroy() {} |
112 PepperInterface* ppapi) { | |
113 Mount::Init(dev, args, ppapi); | |
114 return true; | |
115 } | |
116 | 110 |
117 void MountPassthrough::Destroy() { | 111 Error MountPassthrough::Open(const Path& path, int mode, MountNode** out_node) { |
118 } | 112 *out_node = NULL; |
119 | 113 |
120 MountNode *MountPassthrough::Open(const Path& path, int mode) { | |
121 int real_fd; | 114 int real_fd; |
122 int err = _real_open(path.Join().c_str(), mode, 0666, &real_fd); | 115 int error = _real_open(path.Join().c_str(), mode, 0666, &real_fd); |
123 if (err) { | 116 if (error) |
124 errno = err; | 117 return error; |
125 return NULL; | |
126 } | |
127 | 118 |
128 MountNodePassthrough* node = new MountNodePassthrough(this, real_fd); | 119 MountNodePassthrough* node = new MountNodePassthrough(this, real_fd); |
129 return node; | 120 *out_node = node; |
130 } | |
131 | |
132 MountNode *MountPassthrough::OpenResource(const Path& path) { | |
133 int real_fd; | |
134 int err = _real_open_resource(path.Join().c_str(), &real_fd); | |
135 if (err) { | |
136 errno = err; | |
137 return NULL; | |
138 } | |
139 | |
140 MountNodePassthrough* node = new MountNodePassthrough(this, real_fd); | |
141 return node; | |
142 } | |
143 | |
144 int MountPassthrough::Unlink(const Path& path) { | |
145 // Not implemented by NaCl. | |
146 errno = ENOSYS; | |
147 return -1; | |
148 } | |
149 | |
150 int MountPassthrough::Mkdir(const Path& path, int perm) { | |
151 int err = _real_mkdir(path.Join().c_str(), perm); | |
152 if (err) { | |
153 errno = err; | |
154 return -1; | |
155 } | |
156 | |
157 return 0; | 121 return 0; |
158 } | 122 } |
159 | 123 |
160 int MountPassthrough::Rmdir(const Path& path) { | 124 Error MountPassthrough::OpenResource(const Path& path, MountNode** out_node) { |
161 int err = _real_rmdir(path.Join().c_str()); | 125 *out_node = NULL; |
162 if (err) { | |
163 errno = err; | |
164 return -1; | |
165 } | |
166 | 126 |
| 127 int real_fd; |
| 128 int error = _real_open_resource(path.Join().c_str(), &real_fd); |
| 129 if (error) |
| 130 return error; |
| 131 |
| 132 MountNodePassthrough* node = new MountNodePassthrough(this, real_fd); |
| 133 *out_node = node; |
167 return 0; | 134 return 0; |
168 } | 135 } |
169 | 136 |
170 int MountPassthrough::Remove(const Path& path) { | 137 Error MountPassthrough::Unlink(const Path& path) { |
171 // Not implemented by NaCl. | 138 // Not implemented by NaCl. |
172 errno = ENOSYS; | 139 return ENOSYS; |
173 return -1; | |
174 } | 140 } |
| 141 |
| 142 Error MountPassthrough::Mkdir(const Path& path, int perm) { |
| 143 return _real_mkdir(path.Join().c_str(), perm); |
| 144 } |
| 145 |
| 146 Error MountPassthrough::Rmdir(const Path& path) { |
| 147 return _real_rmdir(path.Join().c_str()); |
| 148 } |
| 149 |
| 150 Error MountPassthrough::Remove(const Path& path) { |
| 151 // Not implemented by NaCl. |
| 152 return ENOSYS; |
| 153 } |
OLD | NEW |