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

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

Issue 3007703002: [dart:io] Namespaces for file IO (Closed)
Patch Set: Remove namespace_unsupported.cc Created 3 years, 3 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/directory_linux.cc ('k') | runtime/bin/directory_patch.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(HOST_OS_MACOS) 6 #if defined(HOST_OS_MACOS)
7 7
8 #include "bin/directory.h" 8 #include "bin/directory.h"
9 9
10 #include <dirent.h> // NOLINT 10 #include <dirent.h> // NOLINT
11 #include <errno.h> // NOLINT 11 #include <errno.h> // NOLINT
12 #include <string.h> // NOLINT 12 #include <string.h> // NOLINT
13 #include <sys/param.h> // NOLINT 13 #include <sys/param.h> // NOLINT
14 #include <sys/stat.h> // NOLINT 14 #include <sys/stat.h> // NOLINT
15 #include <unistd.h> // NOLINT 15 #include <unistd.h> // NOLINT
16 16
17 #include "bin/dartutils.h" 17 #include "bin/dartutils.h"
18 #include "bin/file.h" 18 #include "bin/file.h"
19 #include "bin/namespace.h"
19 #include "bin/platform.h" 20 #include "bin/platform.h"
20 #include "platform/signal_blocker.h" 21 #include "platform/signal_blocker.h"
21 22
22 namespace dart { 23 namespace dart {
23 namespace bin { 24 namespace bin {
24 25
25 PathBuffer::PathBuffer() : length_(0) { 26 PathBuffer::PathBuffer() : length_(0) {
26 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT 27 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT
27 } 28 }
28 29
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 path->Reset(path_length); 322 path->Reset(path_length);
322 } 323 }
323 // Only happens if an error. 324 // Only happens if an error.
324 ASSERT(errno != 0); 325 ASSERT(errno != 0);
325 int err = errno; 326 int err = errno;
326 VOID_NO_RETRY_EXPECTED(closedir(dir_pointer)); 327 VOID_NO_RETRY_EXPECTED(closedir(dir_pointer));
327 errno = err; 328 errno = err;
328 return false; 329 return false;
329 } 330 }
330 331
331 Directory::ExistsResult Directory::Exists(const char* dir_name) { 332 Directory::ExistsResult Directory::Exists(Namespace* namespc,
333 const char* dir_name) {
332 struct stat entry_info; 334 struct stat entry_info;
333 int success = NO_RETRY_EXPECTED(stat(dir_name, &entry_info)); 335 int success = NO_RETRY_EXPECTED(stat(dir_name, &entry_info));
334 if (success == 0) { 336 if (success == 0) {
335 if (S_ISDIR(entry_info.st_mode)) { 337 if (S_ISDIR(entry_info.st_mode)) {
336 return EXISTS; 338 return EXISTS;
337 } else { 339 } else {
338 // An OSError may be constructed based on the return value of this 340 // An OSError may be constructed based on the return value of this
339 // function, so set errno to something that makes sense. 341 // function, so set errno to something that makes sense.
340 errno = ENOTDIR; 342 errno = ENOTDIR;
341 return DOES_NOT_EXIST; 343 return DOES_NOT_EXIST;
342 } 344 }
343 } else { 345 } else {
344 if ((errno == EACCES) || (errno == EBADF) || (errno == EFAULT) || 346 if ((errno == EACCES) || (errno == EBADF) || (errno == EFAULT) ||
345 (errno == ENOMEM) || (errno == EOVERFLOW)) { 347 (errno == ENOMEM) || (errno == EOVERFLOW)) {
346 // Search permissions denied for one of the directories in the 348 // Search permissions denied for one of the directories in the
347 // path or a low level error occured. We do not know if the 349 // path or a low level error occured. We do not know if the
348 // directory exists. 350 // directory exists.
349 return UNKNOWN; 351 return UNKNOWN;
350 } 352 }
351 ASSERT((errno == ELOOP) || (errno == ENAMETOOLONG) || (errno == ENOENT) || 353 ASSERT((errno == ELOOP) || (errno == ENAMETOOLONG) || (errno == ENOENT) ||
352 (errno == ENOTDIR)); 354 (errno == ENOTDIR));
353 return DOES_NOT_EXIST; 355 return DOES_NOT_EXIST;
354 } 356 }
355 } 357 }
356 358
357 char* Directory::CurrentNoScope() { 359 char* Directory::CurrentNoScope() {
358 return getcwd(NULL, 0); 360 return getcwd(NULL, 0);
359 } 361 }
360 362
361 const char* Directory::Current() { 363 bool Directory::Create(Namespace* namespc, const char* dir_name) {
362 char buffer[PATH_MAX];
363 if (getcwd(buffer, PATH_MAX) == NULL) {
364 return NULL;
365 }
366 return DartUtils::ScopedCopyCString(buffer);
367 }
368
369 bool Directory::SetCurrent(const char* path) {
370 int result = NO_RETRY_EXPECTED(chdir(path));
371 return (result == 0);
372 }
373
374 bool Directory::Create(const char* dir_name) {
375 // Create the directory with the permissions specified by the 364 // Create the directory with the permissions specified by the
376 // process umask. 365 // process umask.
377 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777)); 366 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777));
378 // If the directory already exists, treat it as a success. 367 // If the directory already exists, treat it as a success.
379 if ((result == -1) && (errno == EEXIST)) { 368 if ((result == -1) && (errno == EEXIST)) {
380 return (Exists(dir_name) == EXISTS); 369 return (Exists(namespc, dir_name) == EXISTS);
381 } 370 }
382 return (result == 0); 371 return (result == 0);
383 } 372 }
384 373
385 const char* Directory::SystemTemp() { 374 const char* Directory::SystemTemp(Namespace* namespc) {
386 PathBuffer path; 375 PathBuffer path;
387 const char* temp_dir = getenv("TMPDIR"); 376 const char* temp_dir = getenv("TMPDIR");
388 if (temp_dir == NULL) { 377 if (temp_dir == NULL) {
389 temp_dir = getenv("TMP"); 378 temp_dir = getenv("TMP");
390 } 379 }
391 if (temp_dir == NULL) { 380 if (temp_dir == NULL) {
392 temp_dir = "/tmp"; 381 temp_dir = "/tmp";
393 } 382 }
394 if (!path.Add(temp_dir)) { 383 if (!path.Add(temp_dir)) {
395 return NULL; 384 return NULL;
396 } 385 }
397 // Remove any trailing slash. 386 // Remove any trailing slash.
398 char* result = path.AsString(); 387 char* result = path.AsString();
399 int length = strlen(result); 388 int length = strlen(result);
400 if ((length > 1) && (result[length - 1] == '/')) { 389 if ((length > 1) && (result[length - 1] == '/')) {
401 result[length - 1] = '\0'; 390 result[length - 1] = '\0';
402 } 391 }
403 return path.AsScopedString(); 392 return path.AsScopedString();
404 } 393 }
405 394
406 const char* Directory::CreateTemp(const char* prefix) { 395 const char* Directory::CreateTemp(Namespace* namespc, const char* prefix) {
407 // Returns a new, unused directory name, adding characters to the end 396 // Returns a new, unused directory name, adding characters to the end
408 // of prefix. Creates the directory with the permissions specified 397 // of prefix. Creates the directory with the permissions specified
409 // by the process umask. 398 // by the process umask.
410 // The return value is Dart_ScopeAllocated. 399 // The return value is Dart_ScopeAllocated.
411 PathBuffer path; 400 PathBuffer path;
412 if (!path.Add(prefix)) { 401 if (!path.Add(prefix)) {
413 return NULL; 402 return NULL;
414 } 403 }
415 if (!path.Add("XXXXXX")) { 404 if (!path.Add("XXXXXX")) {
416 // Pattern has overflowed. 405 // Pattern has overflowed.
417 return NULL; 406 return NULL;
418 } 407 }
419 char* result; 408 char* result;
420 do { 409 do {
421 result = mkdtemp(path.AsString()); 410 result = mkdtemp(path.AsString());
422 } while ((result == NULL) && (errno == EINTR)); 411 } while ((result == NULL) && (errno == EINTR));
423 if (result == NULL) { 412 if (result == NULL) {
424 return NULL; 413 return NULL;
425 } 414 }
426 return path.AsScopedString(); 415 return path.AsScopedString();
427 } 416 }
428 417
429 bool Directory::Delete(const char* dir_name, bool recursive) { 418 bool Directory::Delete(Namespace* namespc,
419 const char* dir_name,
420 bool recursive) {
430 if (!recursive) { 421 if (!recursive) {
431 if ((File::GetType(dir_name, false) == File::kIsLink) && 422 if ((File::GetType(namespc, dir_name, false) == File::kIsLink) &&
432 (File::GetType(dir_name, true) == File::kIsDirectory)) { 423 (File::GetType(namespc, dir_name, true) == File::kIsDirectory)) {
433 return (NO_RETRY_EXPECTED(unlink(dir_name)) == 0); 424 return (NO_RETRY_EXPECTED(unlink(dir_name)) == 0);
434 } 425 }
435 return (NO_RETRY_EXPECTED(rmdir(dir_name)) == 0); 426 return (NO_RETRY_EXPECTED(rmdir(dir_name)) == 0);
436 } else { 427 } else {
437 PathBuffer path; 428 PathBuffer path;
438 if (!path.Add(dir_name)) { 429 if (!path.Add(dir_name)) {
439 return false; 430 return false;
440 } 431 }
441 return DeleteRecursively(&path); 432 return DeleteRecursively(&path);
442 } 433 }
443 } 434 }
444 435
445 bool Directory::Rename(const char* path, const char* new_path) { 436 bool Directory::Rename(Namespace* namespc,
446 ExistsResult exists = Exists(path); 437 const char* path,
438 const char* new_path) {
439 ExistsResult exists = Exists(namespc, path);
447 if (exists != EXISTS) { 440 if (exists != EXISTS) {
448 return false; 441 return false;
449 } 442 }
450 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0); 443 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0);
451 } 444 }
452 445
453 } // namespace bin 446 } // namespace bin
454 } // namespace dart 447 } // namespace dart
455 448
456 #endif // defined(HOST_OS_MACOS) 449 #endif // defined(HOST_OS_MACOS)
OLDNEW
« no previous file with comments | « runtime/bin/directory_linux.cc ('k') | runtime/bin/directory_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698