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

Side by Side Diff: runtime/bin/file_android.cc

Issue 2593913003: Make File.create() fail when a directory exists at the same path (Closed)
Patch Set: Fix test for Windows Created 3 years, 12 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
« no previous file with comments | « no previous file | runtime/bin/file_fuchsia.cc » ('j') | tests/standalone/io/file_create_test.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_ANDROID) 6 #if defined(TARGET_OS_ANDROID)
7 7
8 #include "bin/file.h" 8 #include "bin/file.h"
9 9
10 #include <errno.h> // NOLINT 10 #include <errno.h> // NOLINT
11 #include <fcntl.h> // NOLINT 11 #include <fcntl.h> // NOLINT
12 #include <libgen.h> // NOLINT 12 #include <libgen.h> // NOLINT
13 #include <sys/mman.h> // NOLINT 13 #include <sys/mman.h> // NOLINT
14 #include <sys/sendfile.h> // NOLINT 14 #include <sys/sendfile.h> // NOLINT
15 #include <sys/stat.h> // NOLINT 15 #include <sys/stat.h> // NOLINT
16 #include <sys/types.h> // NOLINT 16 #include <sys/types.h> // NOLINT
17 #include <unistd.h> // NOLINT 17 #include <unistd.h> // NOLINT
18 18
19 #include "bin/builtin.h" 19 #include "bin/builtin.h"
20 #include "bin/fdutils.h"
20 #include "bin/log.h" 21 #include "bin/log.h"
21
22 #include "platform/signal_blocker.h" 22 #include "platform/signal_blocker.h"
23 #include "platform/utils.h" 23 #include "platform/utils.h"
24 24
25 namespace dart { 25 namespace dart {
26 namespace bin { 26 namespace bin {
27 27
28 class FileHandle { 28 class FileHandle {
29 public: 29 public:
30 explicit FileHandle(int fd) : fd_(fd) {} 30 explicit FileHandle(int fd) : fd_(fd) {}
31 ~FileHandle() {} 31 ~FileHandle() {}
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 220
221 221
222 File* File::OpenStdio(int fd) { 222 File* File::OpenStdio(int fd) {
223 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd)); 223 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd));
224 } 224 }
225 225
226 226
227 bool File::Exists(const char* name) { 227 bool File::Exists(const char* name) {
228 struct stat st; 228 struct stat st;
229 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { 229 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) {
230 return S_ISREG(st.st_mode); 230 // Everything but a directory and a link is a file to Dart.
231 return !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode);
231 } else { 232 } else {
232 return false; 233 return false;
233 } 234 }
234 } 235 }
235 236
236 237
237 bool File::Create(const char* name) { 238 bool File::Create(const char* name) {
238 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT | O_CLOEXEC, 0666)); 239 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT | O_CLOEXEC, 0666));
239 if (fd < 0) { 240 if (fd < 0) {
240 return false; 241 return false;
241 } 242 }
242 return (close(fd) == 0); 243 // File.create returns a File, so we shouldn't be giving the illusion that the
244 // call has created a file or that a file already exists if there is already
245 // an entity at the same path that is a directory or a link.
246 bool is_file = true;
247 struct stat st;
248 if (NO_RETRY_EXPECTED(fstat(fd, &st)) == 0) {
249 if (S_ISDIR(st.st_mode)) {
250 errno = EISDIR;
251 is_file = false;
252 } else if (S_ISLNK(st.st_mode)) {
253 errno = ENOENT;
254 is_file = false;
255 }
256 }
257 FDUtils::SaveErrorAndClose(fd);
258 return is_file;
243 } 259 }
244 260
245 261
246 bool File::CreateLink(const char* name, const char* target) { 262 bool File::CreateLink(const char* name, const char* target) {
247 int status = NO_RETRY_EXPECTED(symlink(target, name)); 263 int status = NO_RETRY_EXPECTED(symlink(target, name));
248 return (status == 0); 264 return (status == 0);
249 } 265 }
250 266
251 267
252 bool File::Delete(const char* name) { 268 bool File::Delete(const char* name) {
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 return ((file_1_info.st_ino == file_2_info.st_ino) && 522 return ((file_1_info.st_ino == file_2_info.st_ino) &&
507 (file_1_info.st_dev == file_2_info.st_dev)) 523 (file_1_info.st_dev == file_2_info.st_dev))
508 ? File::kIdentical 524 ? File::kIdentical
509 : File::kDifferent; 525 : File::kDifferent;
510 } 526 }
511 527
512 } // namespace bin 528 } // namespace bin
513 } // namespace dart 529 } // namespace dart
514 530
515 #endif // defined(TARGET_OS_ANDROID) 531 #endif // defined(TARGET_OS_ANDROID)
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/file_fuchsia.cc » ('j') | tests/standalone/io/file_create_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698