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

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

Issue 2593913003: Make File.create() fail when a directory exists at the same path (Closed)
Patch Set: Fix test for Windows 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
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_LINUX) 6 #if defined(TARGET_OS_LINUX)
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 #include "platform/signal_blocker.h" 22 #include "platform/signal_blocker.h"
22 #include "platform/utils.h" 23 #include "platform/utils.h"
23 24
24 namespace dart { 25 namespace dart {
25 namespace bin { 26 namespace bin {
26 27
27 class FileHandle { 28 class FileHandle {
28 public: 29 public:
29 explicit FileHandle(int fd) : fd_(fd) {} 30 explicit FileHandle(int fd) : fd_(fd) {}
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 221
221 222
222 File* File::OpenStdio(int fd) { 223 File* File::OpenStdio(int fd) {
223 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd)); 224 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd));
224 } 225 }
225 226
226 227
227 bool File::Exists(const char* name) { 228 bool File::Exists(const char* name) {
228 struct stat64 st; 229 struct stat64 st;
229 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) { 230 if (TEMP_FAILURE_RETRY(stat64(name, &st)) == 0) {
230 return S_ISREG(st.st_mode); 231 // Everything but a directory and a link is a file to Dart.
232 return !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode);
231 } else { 233 } else {
232 return false; 234 return false;
233 } 235 }
234 } 236 }
235 237
236 238
237 bool File::Create(const char* name) { 239 bool File::Create(const char* name) {
238 int fd = 240 int fd =
239 TEMP_FAILURE_RETRY(open64(name, O_RDONLY | O_CREAT | O_CLOEXEC, 0666)); 241 TEMP_FAILURE_RETRY(open64(name, O_RDONLY | O_CREAT | O_CLOEXEC, 0666));
240 if (fd < 0) { 242 if (fd < 0) {
241 return false; 243 return false;
242 } 244 }
243 return (TEMP_FAILURE_RETRY(close(fd)) == 0); 245 // File.create returns a File, so we shouldn't be giving the illusion that the
246 // call has created a file or that a file already exists if there is already
247 // an entity at the same path that is a directory or a link.
248 bool is_file = true;
249 struct stat64 st;
250 if (TEMP_FAILURE_RETRY(fstat64(fd, &st)) == 0) {
251 if (S_ISDIR(st.st_mode)) {
252 errno = EISDIR;
253 is_file = false;
254 } else if (S_ISLNK(st.st_mode)) {
255 errno = ENOENT;
256 is_file = false;
257 }
258 }
259 FDUtils::SaveErrorAndClose(fd);
260 return is_file;
244 } 261 }
245 262
246 263
247 bool File::CreateLink(const char* name, const char* target) { 264 bool File::CreateLink(const char* name, const char* target) {
248 return NO_RETRY_EXPECTED(symlink(target, name)) == 0; 265 return NO_RETRY_EXPECTED(symlink(target, name)) == 0;
249 } 266 }
250 267
251 268
252 bool File::Delete(const char* name) { 269 bool File::Delete(const char* name) {
253 File::Type type = File::GetType(name, true); 270 File::Type type = File::GetType(name, true);
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 return ((file_1_info.st_ino == file_2_info.st_ino) && 536 return ((file_1_info.st_ino == file_2_info.st_ino) &&
520 (file_1_info.st_dev == file_2_info.st_dev)) 537 (file_1_info.st_dev == file_2_info.st_dev))
521 ? File::kIdentical 538 ? File::kIdentical
522 : File::kDifferent; 539 : File::kDifferent;
523 } 540 }
524 541
525 } // namespace bin 542 } // namespace bin
526 } // namespace dart 543 } // namespace dart
527 544
528 #endif // defined(TARGET_OS_LINUX) 545 #endif // defined(TARGET_OS_LINUX)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698