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

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

Issue 2594413002: Reland: Make File.create() fail when a directory exists at the same path (Closed)
Patch Set: Created 4 years 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 | « runtime/bin/file_linux.cc ('k') | tests/standalone/io/file_create_test.dart » ('j') | no next file with comments »
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_MACOS) 6 #if defined(TARGET_OS_MACOS)
7 7
8 #include "bin/file.h" 8 #include "bin/file.h"
9 9
10 #include <copyfile.h> // NOLINT 10 #include <copyfile.h> // NOLINT
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 223
224 224
225 File* File::OpenStdio(int fd) { 225 File* File::OpenStdio(int fd) {
226 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd)); 226 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd));
227 } 227 }
228 228
229 229
230 bool File::Exists(const char* name) { 230 bool File::Exists(const char* name) {
231 struct stat st; 231 struct stat st;
232 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { 232 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) {
233 return S_ISREG(st.st_mode); 233 // Everything but a directory and a link is a file to Dart.
234 return !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode);
234 } else { 235 } else {
235 return false; 236 return false;
236 } 237 }
237 } 238 }
238 239
239 240
240 bool File::Create(const char* name) { 241 bool File::Create(const char* name) {
241 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666)); 242 int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CREAT, 0666));
242 if (fd < 0) { 243 if (fd < 0) {
243 return false; 244 return false;
244 } 245 }
245 return (close(fd) == 0); 246 // File.create returns a File, so we shouldn't be giving the illusion that the
247 // call has created a file or that a file already exists if there is already
248 // an entity at the same path that is a directory or a link.
249 bool is_file = true;
250 struct stat st;
251 if (NO_RETRY_EXPECTED(fstat(fd, &st)) == 0) {
252 if (S_ISDIR(st.st_mode)) {
253 errno = EISDIR;
254 is_file = false;
255 } else if (S_ISLNK(st.st_mode)) {
256 errno = ENOENT;
257 is_file = false;
258 }
259 }
260 FDUtils::SaveErrorAndClose(fd);
261 return is_file;
246 } 262 }
247 263
248 264
249 bool File::CreateLink(const char* name, const char* target) { 265 bool File::CreateLink(const char* name, const char* target) {
250 int status = NO_RETRY_EXPECTED(symlink(target, name)); 266 int status = NO_RETRY_EXPECTED(symlink(target, name));
251 return (status == 0); 267 return (status == 0);
252 } 268 }
253 269
254 270
255 bool File::Delete(const char* name) { 271 bool File::Delete(const char* name) {
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 return ((file_1_info.st_ino == file_2_info.st_ino) && 499 return ((file_1_info.st_ino == file_2_info.st_ino) &&
484 (file_1_info.st_dev == file_2_info.st_dev)) 500 (file_1_info.st_dev == file_2_info.st_dev))
485 ? File::kIdentical 501 ? File::kIdentical
486 : File::kDifferent; 502 : File::kDifferent;
487 } 503 }
488 504
489 } // namespace bin 505 } // namespace bin
490 } // namespace dart 506 } // namespace dart
491 507
492 #endif // defined(TARGET_OS_MACOS) 508 #endif // defined(TARGET_OS_MACOS)
OLDNEW
« no previous file with comments | « runtime/bin/file_linux.cc ('k') | tests/standalone/io/file_create_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698