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

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

Issue 2974233002: VM: Re-format to use at most one newline between functions (Closed)
Patch Set: Rebase and merge Created 3 years, 5 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_test.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(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/platform.h" 19 #include "bin/platform.h"
20 #include "platform/signal_blocker.h" 20 #include "platform/signal_blocker.h"
21 21
22 namespace dart { 22 namespace dart {
23 namespace bin { 23 namespace bin {
24 24
25 PathBuffer::PathBuffer() : length_(0) { 25 PathBuffer::PathBuffer() : length_(0) {
26 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT 26 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT
27 } 27 }
28 28
29
30 PathBuffer::~PathBuffer() { 29 PathBuffer::~PathBuffer() {
31 free(data_); 30 free(data_);
32 } 31 }
33 32
34
35 bool PathBuffer::AddW(const wchar_t* name) { 33 bool PathBuffer::AddW(const wchar_t* name) {
36 UNREACHABLE(); 34 UNREACHABLE();
37 return false; 35 return false;
38 } 36 }
39 37
40
41 char* PathBuffer::AsString() const { 38 char* PathBuffer::AsString() const {
42 return reinterpret_cast<char*>(data_); 39 return reinterpret_cast<char*>(data_);
43 } 40 }
44 41
45
46 wchar_t* PathBuffer::AsStringW() const { 42 wchar_t* PathBuffer::AsStringW() const {
47 UNREACHABLE(); 43 UNREACHABLE();
48 return NULL; 44 return NULL;
49 } 45 }
50 46
51
52 const char* PathBuffer::AsScopedString() const { 47 const char* PathBuffer::AsScopedString() const {
53 return DartUtils::ScopedCopyCString(AsString()); 48 return DartUtils::ScopedCopyCString(AsString());
54 } 49 }
55 50
56
57 bool PathBuffer::Add(const char* name) { 51 bool PathBuffer::Add(const char* name) {
58 char* data = AsString(); 52 char* data = AsString();
59 int written = snprintf(data + length_, PATH_MAX - length_, "%s", name); 53 int written = snprintf(data + length_, PATH_MAX - length_, "%s", name);
60 data[PATH_MAX] = '\0'; 54 data[PATH_MAX] = '\0';
61 if ((written <= PATH_MAX - length_) && (written >= 0) && 55 if ((written <= PATH_MAX - length_) && (written >= 0) &&
62 (static_cast<size_t>(written) == strlen(name))) { 56 (static_cast<size_t>(written) == strlen(name))) {
63 length_ += written; 57 length_ += written;
64 return true; 58 return true;
65 } else { 59 } else {
66 errno = ENAMETOOLONG; 60 errno = ENAMETOOLONG;
67 return false; 61 return false;
68 } 62 }
69 } 63 }
70 64
71
72 void PathBuffer::Reset(intptr_t new_length) { 65 void PathBuffer::Reset(intptr_t new_length) {
73 length_ = new_length; 66 length_ = new_length;
74 AsString()[length_] = '\0'; 67 AsString()[length_] = '\0';
75 } 68 }
76 69
77
78 // A linked list of symbolic links, with their unique file system identifiers. 70 // A linked list of symbolic links, with their unique file system identifiers.
79 // These are scanned to detect loops while doing a recursive directory listing. 71 // These are scanned to detect loops while doing a recursive directory listing.
80 struct LinkList { 72 struct LinkList {
81 dev_t dev; 73 dev_t dev;
82 ino_t ino; 74 ino_t ino;
83 LinkList* next; 75 LinkList* next;
84 }; 76 };
85 77
86
87 ListType DirectoryListingEntry::Next(DirectoryListing* listing) { 78 ListType DirectoryListingEntry::Next(DirectoryListing* listing) {
88 if (done_) { 79 if (done_) {
89 return kListDone; 80 return kListDone;
90 } 81 }
91 82
92 if (lister_ == 0) { 83 if (lister_ == 0) {
93 do { 84 do {
94 lister_ = reinterpret_cast<intptr_t>( 85 lister_ = reinterpret_cast<intptr_t>(
95 opendir(listing->path_buffer().AsString())); 86 opendir(listing->path_buffer().AsString()));
96 } while ((lister_ == 0) && (errno == EINTR)); 87 } while ((lister_ == 0) && (errno == EINTR));
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 done_ = true; 201 done_ = true;
211 202
212 if (status != 0) { 203 if (status != 0) {
213 errno = status; 204 errno = status;
214 return kListError; 205 return kListError;
215 } 206 }
216 207
217 return kListDone; 208 return kListDone;
218 } 209 }
219 210
220
221 DirectoryListingEntry::~DirectoryListingEntry() { 211 DirectoryListingEntry::~DirectoryListingEntry() {
222 ResetLink(); 212 ResetLink();
223 if (lister_ != 0) { 213 if (lister_ != 0) {
224 closedir(reinterpret_cast<DIR*>(lister_)); 214 closedir(reinterpret_cast<DIR*>(lister_));
225 } 215 }
226 } 216 }
227 217
228
229 void DirectoryListingEntry::ResetLink() { 218 void DirectoryListingEntry::ResetLink() {
230 if ((link_ != NULL) && ((parent_ == NULL) || (parent_->link_ != link_))) { 219 if ((link_ != NULL) && ((parent_ == NULL) || (parent_->link_ != link_))) {
231 delete link_; 220 delete link_;
232 link_ = NULL; 221 link_ = NULL;
233 } 222 }
234 if (parent_ != NULL) { 223 if (parent_ != NULL) {
235 link_ = parent_->link_; 224 link_ = parent_->link_;
236 } 225 }
237 } 226 }
238 227
239
240 static bool DeleteRecursively(PathBuffer* path); 228 static bool DeleteRecursively(PathBuffer* path);
241 229
242
243 static bool DeleteFile(char* file_name, PathBuffer* path) { 230 static bool DeleteFile(char* file_name, PathBuffer* path) {
244 return path->Add(file_name) && (unlink(path->AsString()) == 0); 231 return path->Add(file_name) && (unlink(path->AsString()) == 0);
245 } 232 }
246 233
247
248 static bool DeleteDir(char* dir_name, PathBuffer* path) { 234 static bool DeleteDir(char* dir_name, PathBuffer* path) {
249 if ((strcmp(dir_name, ".") == 0) || (strcmp(dir_name, "..") == 0)) { 235 if ((strcmp(dir_name, ".") == 0) || (strcmp(dir_name, "..") == 0)) {
250 return true; 236 return true;
251 } 237 }
252 return path->Add(dir_name) && DeleteRecursively(path); 238 return path->Add(dir_name) && DeleteRecursively(path);
253 } 239 }
254 240
255
256 static bool DeleteRecursively(PathBuffer* path) { 241 static bool DeleteRecursively(PathBuffer* path) {
257 // Do not recurse into links for deletion. Instead delete the link. 242 // Do not recurse into links for deletion. Instead delete the link.
258 // If it's a file, delete it. 243 // If it's a file, delete it.
259 struct stat st; 244 struct stat st;
260 if (NO_RETRY_EXPECTED(lstat(path->AsString(), &st)) == -1) { 245 if (NO_RETRY_EXPECTED(lstat(path->AsString(), &st)) == -1) {
261 return false; 246 return false;
262 } else if (!S_ISDIR(st.st_mode)) { 247 } else if (!S_ISDIR(st.st_mode)) {
263 return (unlink(path->AsString()) == 0); 248 return (unlink(path->AsString()) == 0);
264 } 249 }
265 250
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 path->Reset(path_length); 321 path->Reset(path_length);
337 } 322 }
338 // Only happens if an error. 323 // Only happens if an error.
339 ASSERT(errno != 0); 324 ASSERT(errno != 0);
340 int err = errno; 325 int err = errno;
341 VOID_NO_RETRY_EXPECTED(closedir(dir_pointer)); 326 VOID_NO_RETRY_EXPECTED(closedir(dir_pointer));
342 errno = err; 327 errno = err;
343 return false; 328 return false;
344 } 329 }
345 330
346
347 Directory::ExistsResult Directory::Exists(const char* dir_name) { 331 Directory::ExistsResult Directory::Exists(const char* dir_name) {
348 struct stat entry_info; 332 struct stat entry_info;
349 int success = NO_RETRY_EXPECTED(stat(dir_name, &entry_info)); 333 int success = NO_RETRY_EXPECTED(stat(dir_name, &entry_info));
350 if (success == 0) { 334 if (success == 0) {
351 if (S_ISDIR(entry_info.st_mode)) { 335 if (S_ISDIR(entry_info.st_mode)) {
352 return EXISTS; 336 return EXISTS;
353 } else { 337 } else {
354 // An OSError may be constructed based on the return value of this 338 // An OSError may be constructed based on the return value of this
355 // function, so set errno to something that makes sense. 339 // function, so set errno to something that makes sense.
356 errno = ENOTDIR; 340 errno = ENOTDIR;
357 return DOES_NOT_EXIST; 341 return DOES_NOT_EXIST;
358 } 342 }
359 } else { 343 } else {
360 if ((errno == EACCES) || (errno == EBADF) || (errno == EFAULT) || 344 if ((errno == EACCES) || (errno == EBADF) || (errno == EFAULT) ||
361 (errno == ENOMEM) || (errno == EOVERFLOW)) { 345 (errno == ENOMEM) || (errno == EOVERFLOW)) {
362 // Search permissions denied for one of the directories in the 346 // Search permissions denied for one of the directories in the
363 // path or a low level error occured. We do not know if the 347 // path or a low level error occured. We do not know if the
364 // directory exists. 348 // directory exists.
365 return UNKNOWN; 349 return UNKNOWN;
366 } 350 }
367 ASSERT((errno == ELOOP) || (errno == ENAMETOOLONG) || (errno == ENOENT) || 351 ASSERT((errno == ELOOP) || (errno == ENAMETOOLONG) || (errno == ENOENT) ||
368 (errno == ENOTDIR)); 352 (errno == ENOTDIR));
369 return DOES_NOT_EXIST; 353 return DOES_NOT_EXIST;
370 } 354 }
371 } 355 }
372 356
373
374 char* Directory::CurrentNoScope() { 357 char* Directory::CurrentNoScope() {
375 return getcwd(NULL, 0); 358 return getcwd(NULL, 0);
376 } 359 }
377 360
378
379 const char* Directory::Current() { 361 const char* Directory::Current() {
380 char buffer[PATH_MAX]; 362 char buffer[PATH_MAX];
381 if (getcwd(buffer, PATH_MAX) == NULL) { 363 if (getcwd(buffer, PATH_MAX) == NULL) {
382 return NULL; 364 return NULL;
383 } 365 }
384 return DartUtils::ScopedCopyCString(buffer); 366 return DartUtils::ScopedCopyCString(buffer);
385 } 367 }
386 368
387
388 bool Directory::SetCurrent(const char* path) { 369 bool Directory::SetCurrent(const char* path) {
389 int result = NO_RETRY_EXPECTED(chdir(path)); 370 int result = NO_RETRY_EXPECTED(chdir(path));
390 return (result == 0); 371 return (result == 0);
391 } 372 }
392 373
393
394 bool Directory::Create(const char* dir_name) { 374 bool Directory::Create(const char* dir_name) {
395 // Create the directory with the permissions specified by the 375 // Create the directory with the permissions specified by the
396 // process umask. 376 // process umask.
397 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777)); 377 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777));
398 // If the directory already exists, treat it as a success. 378 // If the directory already exists, treat it as a success.
399 if ((result == -1) && (errno == EEXIST)) { 379 if ((result == -1) && (errno == EEXIST)) {
400 return (Exists(dir_name) == EXISTS); 380 return (Exists(dir_name) == EXISTS);
401 } 381 }
402 return (result == 0); 382 return (result == 0);
403 } 383 }
404 384
405
406 const char* Directory::SystemTemp() { 385 const char* Directory::SystemTemp() {
407 PathBuffer path; 386 PathBuffer path;
408 const char* temp_dir = getenv("TMPDIR"); 387 const char* temp_dir = getenv("TMPDIR");
409 if (temp_dir == NULL) { 388 if (temp_dir == NULL) {
410 temp_dir = getenv("TMP"); 389 temp_dir = getenv("TMP");
411 } 390 }
412 if (temp_dir == NULL) { 391 if (temp_dir == NULL) {
413 temp_dir = "/tmp"; 392 temp_dir = "/tmp";
414 } 393 }
415 if (!path.Add(temp_dir)) { 394 if (!path.Add(temp_dir)) {
416 return NULL; 395 return NULL;
417 } 396 }
418 // Remove any trailing slash. 397 // Remove any trailing slash.
419 char* result = path.AsString(); 398 char* result = path.AsString();
420 int length = strlen(result); 399 int length = strlen(result);
421 if ((length > 1) && (result[length - 1] == '/')) { 400 if ((length > 1) && (result[length - 1] == '/')) {
422 result[length - 1] = '\0'; 401 result[length - 1] = '\0';
423 } 402 }
424 return path.AsScopedString(); 403 return path.AsScopedString();
425 } 404 }
426 405
427
428 const char* Directory::CreateTemp(const char* prefix) { 406 const char* Directory::CreateTemp(const char* prefix) {
429 // Returns a new, unused directory name, adding characters to the end 407 // Returns a new, unused directory name, adding characters to the end
430 // of prefix. Creates the directory with the permissions specified 408 // of prefix. Creates the directory with the permissions specified
431 // by the process umask. 409 // by the process umask.
432 // The return value is Dart_ScopeAllocated. 410 // The return value is Dart_ScopeAllocated.
433 PathBuffer path; 411 PathBuffer path;
434 if (!path.Add(prefix)) { 412 if (!path.Add(prefix)) {
435 return NULL; 413 return NULL;
436 } 414 }
437 if (!path.Add("XXXXXX")) { 415 if (!path.Add("XXXXXX")) {
438 // Pattern has overflowed. 416 // Pattern has overflowed.
439 return NULL; 417 return NULL;
440 } 418 }
441 char* result; 419 char* result;
442 do { 420 do {
443 result = mkdtemp(path.AsString()); 421 result = mkdtemp(path.AsString());
444 } while ((result == NULL) && (errno == EINTR)); 422 } while ((result == NULL) && (errno == EINTR));
445 if (result == NULL) { 423 if (result == NULL) {
446 return NULL; 424 return NULL;
447 } 425 }
448 return path.AsScopedString(); 426 return path.AsScopedString();
449 } 427 }
450 428
451
452 bool Directory::Delete(const char* dir_name, bool recursive) { 429 bool Directory::Delete(const char* dir_name, bool recursive) {
453 if (!recursive) { 430 if (!recursive) {
454 if ((File::GetType(dir_name, false) == File::kIsLink) && 431 if ((File::GetType(dir_name, false) == File::kIsLink) &&
455 (File::GetType(dir_name, true) == File::kIsDirectory)) { 432 (File::GetType(dir_name, true) == File::kIsDirectory)) {
456 return (NO_RETRY_EXPECTED(unlink(dir_name)) == 0); 433 return (NO_RETRY_EXPECTED(unlink(dir_name)) == 0);
457 } 434 }
458 return (NO_RETRY_EXPECTED(rmdir(dir_name)) == 0); 435 return (NO_RETRY_EXPECTED(rmdir(dir_name)) == 0);
459 } else { 436 } else {
460 PathBuffer path; 437 PathBuffer path;
461 if (!path.Add(dir_name)) { 438 if (!path.Add(dir_name)) {
462 return false; 439 return false;
463 } 440 }
464 return DeleteRecursively(&path); 441 return DeleteRecursively(&path);
465 } 442 }
466 } 443 }
467 444
468
469 bool Directory::Rename(const char* path, const char* new_path) { 445 bool Directory::Rename(const char* path, const char* new_path) {
470 ExistsResult exists = Exists(path); 446 ExistsResult exists = Exists(path);
471 if (exists != EXISTS) { 447 if (exists != EXISTS) {
472 return false; 448 return false;
473 } 449 }
474 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0); 450 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0);
475 } 451 }
476 452
477 } // namespace bin 453 } // namespace bin
478 } // namespace dart 454 } // namespace dart
479 455
480 #endif // defined(HOST_OS_MACOS) 456 #endif // defined(HOST_OS_MACOS)
OLDNEW
« no previous file with comments | « runtime/bin/directory_linux.cc ('k') | runtime/bin/directory_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698