OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "services/files/directory_impl.h" | 5 #include "services/files/directory_impl.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 108 |
109 void DirectoryImpl::Read(const ReadCallback& callback) { | 109 void DirectoryImpl::Read(const ReadCallback& callback) { |
110 static const size_t kMaxReadCount = 1000; | 110 static const size_t kMaxReadCount = 1000; |
111 | 111 |
112 DCHECK(dir_fd_.is_valid()); | 112 DCHECK(dir_fd_.is_valid()); |
113 | 113 |
114 // |fdopendir()| takes ownership of the FD (giving it to the |DIR| -- | 114 // |fdopendir()| takes ownership of the FD (giving it to the |DIR| -- |
115 // |closedir()| will close the FD)), so we need to |dup()| ours. | 115 // |closedir()| will close the FD)), so we need to |dup()| ours. |
116 base::ScopedFD fd(dup(dir_fd_.get())); | 116 base::ScopedFD fd(dup(dir_fd_.get())); |
117 if (!fd.is_valid()) { | 117 if (!fd.is_valid()) { |
118 callback.Run(ErrnoToError(errno), Array<DirectoryEntryPtr>()); | 118 callback.Run(ErrnoToError(errno), nullptr); |
119 return; | 119 return; |
120 } | 120 } |
121 | 121 |
122 ScopedDIR dir(fdopendir(fd.release())); | 122 ScopedDIR dir(fdopendir(fd.release())); |
123 if (!dir) { | 123 if (!dir) { |
124 callback.Run(ErrnoToError(errno), Array<DirectoryEntryPtr>()); | 124 callback.Run(ErrnoToError(errno), nullptr); |
125 return; | 125 return; |
126 } | 126 } |
127 | 127 |
128 auto result = Array<DirectoryEntryPtr>::New(0); | 128 auto result = Array<DirectoryEntryPtr>::New(0); |
129 | 129 |
130 // Warning: This is not portable (per POSIX.1 -- |buffer| may not be large | 130 // Warning: This is not portable (per POSIX.1 -- |buffer| may not be large |
131 // enough), but it's fine for Linux. | 131 // enough), but it's fine for Linux. |
132 #if !defined(OS_ANDROID) && !defined(OS_LINUX) | 132 #if !defined(OS_ANDROID) && !defined(OS_LINUX) |
133 #error "Use of struct dirent for readdir_r() buffer not portable; please check." | 133 #error "Use of struct dirent for readdir_r() buffer not portable; please check." |
134 #endif | 134 #endif |
135 struct dirent buffer; | 135 struct dirent buffer; |
136 for (size_t n = 0;;) { | 136 for (size_t n = 0;;) { |
137 struct dirent* entry = nullptr; | 137 struct dirent* entry = nullptr; |
138 if (int error = readdir_r(dir.get(), &buffer, &entry)) { | 138 if (int error = readdir_r(dir.get(), &buffer, &entry)) { |
139 // |error| is effectively an errno (for |readdir_r()|), AFAICT. | 139 // |error| is effectively an errno (for |readdir_r()|), AFAICT. |
140 callback.Run(ErrnoToError(error), Array<DirectoryEntryPtr>()); | 140 callback.Run(ErrnoToError(error), nullptr); |
141 return; | 141 return; |
142 } | 142 } |
143 | 143 |
144 if (!entry) | 144 if (!entry) |
145 break; | 145 break; |
146 | 146 |
147 n++; | 147 n++; |
148 if (n > kMaxReadCount) { | 148 if (n > kMaxReadCount) { |
149 LOG(WARNING) << "Directory contents truncated"; | 149 LOG(WARNING) << "Directory contents truncated"; |
150 callback.Run(Error::OUT_OF_RANGE, result.Pass()); | 150 callback.Run(Error::OUT_OF_RANGE, result.Pass()); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 if (unlinkat(dir_fd_.get(), path.get().c_str(), AT_REMOVEDIR) == 0) { | 353 if (unlinkat(dir_fd_.get(), path.get().c_str(), AT_REMOVEDIR) == 0) { |
354 callback.Run(Error::OK); | 354 callback.Run(Error::OK); |
355 return; | 355 return; |
356 } | 356 } |
357 | 357 |
358 callback.Run(ErrnoToError(errno)); | 358 callback.Run(ErrnoToError(errno)); |
359 } | 359 } |
360 | 360 |
361 } // namespace files | 361 } // namespace files |
362 } // namespace mojo | 362 } // namespace mojo |
OLD | NEW |