OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "files/public/c/mojio_unistd.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "files/public/c/lib/directory_wrapper.h" | |
11 #include "files/public/c/lib/fd_impl.h" | |
12 #include "files/public/c/lib/fd_table.h" | |
13 #include "files/public/c/lib/singletons.h" | |
14 | |
15 namespace mojio { | |
16 namespace { | |
17 | |
18 int ChdirImpl(const char* path) { | |
19 DirectoryWrapper* cwd = singletons::GetCurrentWorkingDirectory(); | |
20 if (!cwd) | |
21 return -1; | |
22 | |
23 return cwd->Chdir(path) ? 0 : -1; | |
24 } | |
25 | |
26 int CloseImpl(int fd) { | |
27 std::unique_ptr<FDImpl> fd_impl(singletons::GetFDTable()->Remove(fd)); | |
28 if (!fd_impl) | |
29 return -1; | |
30 | |
31 return fd_impl->Close() ? 0 : -1; | |
32 } | |
33 | |
34 int DupImpl(int fd) { | |
35 FDImpl* fd_impl = singletons::GetFDTable()->Get(fd); | |
36 if (!fd_impl) | |
37 return -1; | |
38 | |
39 std::unique_ptr<FDImpl> new_fd_impl(fd_impl->Dup()); | |
40 if (!new_fd_impl) | |
41 return -1; | |
42 | |
43 return singletons::GetFDTable()->Add(std::move(new_fd_impl)); | |
44 } | |
45 | |
46 int FtruncateImpl(int fd, mojio_off_t length) { | |
47 FDImpl* fd_impl = singletons::GetFDTable()->Get(fd); | |
48 if (!fd_impl) | |
49 return -1; | |
50 | |
51 return fd_impl->Ftruncate(length) ? 0 : -1; | |
52 } | |
53 | |
54 mojio_off_t LseekImpl(int fd, mojio_off_t offset, int whence) { | |
55 FDImpl* fd_impl = singletons::GetFDTable()->Get(fd); | |
56 if (!fd_impl) | |
57 return -1; | |
58 | |
59 return fd_impl->Lseek(offset, whence); | |
60 } | |
61 | |
62 mojio_ssize_t ReadImpl(int fd, void* buf, size_t count) { | |
63 FDImpl* fd_impl = singletons::GetFDTable()->Get(fd); | |
64 if (!fd_impl) | |
65 return -1; | |
66 | |
67 return fd_impl->Read(buf, count); | |
68 } | |
69 | |
70 mojio_ssize_t WriteImpl(int fd, const void* buf, size_t count) { | |
71 FDImpl* fd_impl = singletons::GetFDTable()->Get(fd); | |
72 if (!fd_impl) | |
73 return -1; | |
74 | |
75 return fd_impl->Write(buf, count); | |
76 } | |
77 | |
78 } // namespace | |
79 } // namespace mojio | |
80 | |
81 extern "C" { | |
82 | |
83 int mojio_chdir(const char* path) { | |
84 return mojio::ChdirImpl(path); | |
85 } | |
86 | |
87 int mojio_close(int fd) { | |
88 return mojio::CloseImpl(fd); | |
89 } | |
90 | |
91 int mojio_dup(int fd) { | |
92 return mojio::DupImpl(fd); | |
93 } | |
94 | |
95 int mojio_ftruncate(int fd, mojio_off_t length) { | |
96 return mojio::FtruncateImpl(fd, length); | |
97 } | |
98 | |
99 mojio_off_t mojio_lseek(int fd, mojio_off_t offset, int whence) { | |
100 return mojio::LseekImpl(fd, offset, whence); | |
101 } | |
102 | |
103 mojio_ssize_t mojio_read(int fd, void* buf, size_t count) { | |
104 return mojio::ReadImpl(fd, buf, count); | |
105 } | |
106 | |
107 mojio_ssize_t mojio_write(int fd, const void* buf, size_t count) { | |
108 return mojio::WriteImpl(fd, buf, count); | |
109 } | |
110 | |
111 } // extern "C" | |
OLD | NEW |