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

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

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

Powered by Google App Engine
This is Rietveld 408576698