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/lib/directory_wrapper.h" | |
6 | |
7 #include <errno.h> | |
8 | |
9 #include "files/public/c/lib/errno_impl.h" | |
10 #include "files/public/c/lib/file_fd_impl.h" | |
11 #include "files/public/c/lib/template_util.h" | |
12 #include "files/public/c/lib/util.h" | |
13 #include "files/public/c/mojio_fcntl.h" | |
14 #include "files/public/interfaces/file.mojom.h" | |
15 #include "files/public/interfaces/types.mojom.h" | |
16 #include "mojo/public/cpp/bindings/interface_request.h" | |
17 #include "mojo/public/cpp/environment/logging.h" | |
18 | |
19 namespace mojio { | |
20 | |
21 DirectoryWrapper::DirectoryWrapper(ErrnoImpl* errno_impl, | |
22 mojo::files::DirectoryPtr directory) | |
23 : errno_impl_(errno_impl), directory_(directory.Pass()) { | |
24 MOJO_DCHECK(directory_); | |
25 } | |
26 | |
27 DirectoryWrapper::~DirectoryWrapper() { | |
28 } | |
29 | |
30 // TODO(vtl): This doesn't currently support opening non-files (in particular, | |
31 // directories), but it should. | |
32 std::unique_ptr<FDImpl> DirectoryWrapper::Open(const char* path, | |
33 int oflag, | |
34 mojio_mode_t mode) { | |
35 ErrnoImpl::Setter errno_setter(errno_impl_); | |
36 | |
37 if (!path) { | |
38 errno_setter.Set(EFAULT); | |
39 return nullptr; | |
40 } | |
41 | |
42 uint32_t mojo_open_flags = 0; | |
43 switch (oflag & MOJIO_O_ACCMODE) { | |
44 case MOJIO_O_RDONLY: | |
45 mojo_open_flags = mojo::files::kOpenFlagRead; | |
46 break; | |
47 case MOJIO_O_RDWR: | |
48 mojo_open_flags = | |
49 mojo::files::kOpenFlagRead | mojo::files::kOpenFlagWrite; | |
50 break; | |
51 case MOJIO_O_WRONLY: | |
52 mojo_open_flags = mojo::files::kOpenFlagWrite; | |
53 break; | |
54 default: | |
55 errno_setter.Set(EINVAL); | |
56 return nullptr; | |
57 } | |
58 | |
59 if ((oflag & MOJIO_O_CREAT)) | |
60 mojo_open_flags |= mojo::files::kOpenFlagCreate; | |
61 if ((oflag & MOJIO_O_EXCL)) | |
62 mojo_open_flags |= mojo::files::kOpenFlagExclusive; | |
63 if ((oflag & MOJIO_O_TRUNC)) | |
64 mojo_open_flags |= mojo::files::kOpenFlagTruncate; | |
65 if ((oflag & MOJIO_O_APPEND)) | |
66 mojo_open_flags |= mojo::files::kOpenFlagAppend; | |
67 // TODO(vtl): What should we do we open flags we don't support? And invalid | |
68 // flags? | |
69 | |
70 // TODO(vtl): We currently totally ignore |mode|; maybe we should do something | |
71 // with it? | |
72 | |
73 mojo::files::FilePtr file; | |
74 mojo::files::Error error = mojo::files::Error::INTERNAL; | |
75 directory_->OpenFile(path, mojo::GetProxy(&file), mojo_open_flags, | |
76 Capture(&error)); | |
77 if (!directory_.WaitForIncomingResponse()) { | |
78 // This may be somewhat surprising. The implication is that the CWD is | |
79 // stale, which may be a little strange. | |
80 errno_setter.Set(ESTALE); | |
81 return nullptr; | |
82 } | |
83 if (!errno_setter.Set(ErrorToErrno(error))) | |
84 return nullptr; | |
85 // C++11, why don't you have make_unique? | |
86 return std::unique_ptr<FDImpl>(new FileFDImpl(errno_impl_, file.Pass())); | |
87 } | |
88 | |
89 bool DirectoryWrapper::Chdir(const char* path) { | |
90 ErrnoImpl::Setter errno_setter(errno_impl_); | |
91 | |
92 if (!path) | |
93 return errno_setter.Set(EFAULT); | |
94 | |
95 mojo::files::DirectoryPtr new_directory; | |
96 mojo::files::Error error = mojo::files::Error::INTERNAL; | |
97 directory_->OpenDirectory( | |
98 path, mojo::GetProxy(&new_directory), | |
99 mojo::files::kOpenFlagRead | mojo::files::kOpenFlagWrite, | |
100 Capture(&error)); | |
101 if (!directory_.WaitForIncomingResponse()) { | |
102 // This may be somewhat surprising. The implication is that the CWD is | |
103 // stale, which may be a little strange. | |
104 return errno_setter.Set(ESTALE); | |
105 } | |
106 if (!errno_setter.Set(ErrorToErrno(error))) | |
107 return false; | |
108 directory_ = new_directory.Pass(); | |
109 return true; | |
110 } | |
111 | |
112 } // namespace mojio | |
OLD | NEW |