OLD | NEW |
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_ANDROID) | 6 #if defined(HOST_OS_ANDROID) |
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) == strnlen(name, PATH_MAX + 1))) { | 56 (static_cast<size_t>(written) == strnlen(name, PATH_MAX + 1))) { |
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 uint64_t dev; | 73 uint64_t dev; |
82 uint64_t ino; | 74 uint64_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 Loading... |
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 Loading... |
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 // Android's getcwd adheres closely to the POSIX standard. It won't | 358 // Android's getcwd adheres closely to the POSIX standard. It won't |
376 // allocate memory. We need to make our own copy. | 359 // allocate memory. We need to make our own copy. |
377 char buffer[PATH_MAX]; | 360 char buffer[PATH_MAX]; |
378 if (getcwd(buffer, PATH_MAX) == NULL) { | 361 if (getcwd(buffer, PATH_MAX) == NULL) { |
379 return NULL; | 362 return NULL; |
380 } | 363 } |
381 | 364 |
382 return strdup(buffer); | 365 return strdup(buffer); |
383 } | 366 } |
384 | 367 |
385 | |
386 const char* Directory::Current() { | 368 const char* Directory::Current() { |
387 char buffer[PATH_MAX]; | 369 char buffer[PATH_MAX]; |
388 if (getcwd(buffer, PATH_MAX) == NULL) { | 370 if (getcwd(buffer, PATH_MAX) == NULL) { |
389 return NULL; | 371 return NULL; |
390 } | 372 } |
391 return DartUtils::ScopedCopyCString(buffer); | 373 return DartUtils::ScopedCopyCString(buffer); |
392 } | 374 } |
393 | 375 |
394 | |
395 bool Directory::SetCurrent(const char* path) { | 376 bool Directory::SetCurrent(const char* path) { |
396 int result = NO_RETRY_EXPECTED(chdir(path)); | 377 int result = NO_RETRY_EXPECTED(chdir(path)); |
397 return (result == 0); | 378 return (result == 0); |
398 } | 379 } |
399 | 380 |
400 | |
401 bool Directory::Create(const char* dir_name) { | 381 bool Directory::Create(const char* dir_name) { |
402 // Create the directory with the permissions specified by the | 382 // Create the directory with the permissions specified by the |
403 // process umask. | 383 // process umask. |
404 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777)); | 384 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777)); |
405 // If the directory already exists, treat it as a success. | 385 // If the directory already exists, treat it as a success. |
406 if ((result == -1) && (errno == EEXIST)) { | 386 if ((result == -1) && (errno == EEXIST)) { |
407 return (Exists(dir_name) == EXISTS); | 387 return (Exists(dir_name) == EXISTS); |
408 } | 388 } |
409 return (result == 0); | 389 return (result == 0); |
410 } | 390 } |
411 | 391 |
412 | |
413 const char* Directory::SystemTemp() { | 392 const char* Directory::SystemTemp() { |
414 if (Directory::system_temp_path_override_ != NULL) { | 393 if (Directory::system_temp_path_override_ != NULL) { |
415 return DartUtils::ScopedCopyCString(Directory::system_temp_path_override_); | 394 return DartUtils::ScopedCopyCString(Directory::system_temp_path_override_); |
416 } | 395 } |
417 // Android does not have a /tmp directory. A partial substitute, | 396 // Android does not have a /tmp directory. A partial substitute, |
418 // suitable for bring-up work and tests, is to create a tmp | 397 // suitable for bring-up work and tests, is to create a tmp |
419 // directory in /data/local/tmp. | 398 // directory in /data/local/tmp. |
420 // | 399 // |
421 // TODO(4413): In the long run, when running in an application we should | 400 // TODO(4413): In the long run, when running in an application we should |
422 // probably use the appropriate directory from the Android API, | 401 // probably use the appropriate directory from the Android API, |
423 // probably what File.createTempFile uses. | 402 // probably what File.createTempFile uses. |
424 #define ANDROID_TEMP_DIR "/data/local/tmp" | 403 #define ANDROID_TEMP_DIR "/data/local/tmp" |
425 struct stat st; | 404 struct stat st; |
426 if (stat(ANDROID_TEMP_DIR, &st) != 0) { | 405 if (stat(ANDROID_TEMP_DIR, &st) != 0) { |
427 mkdir(ANDROID_TEMP_DIR, 0777); | 406 mkdir(ANDROID_TEMP_DIR, 0777); |
428 } | 407 } |
429 return ANDROID_TEMP_DIR; | 408 return ANDROID_TEMP_DIR; |
430 } | 409 } |
431 | 410 |
432 | |
433 const char* Directory::CreateTemp(const char* prefix) { | 411 const char* Directory::CreateTemp(const char* prefix) { |
434 // Returns a new, unused directory name, adding characters to the end | 412 // Returns a new, unused directory name, adding characters to the end |
435 // of prefix. Creates the directory with the permissions specified | 413 // of prefix. Creates the directory with the permissions specified |
436 // by the process umask. | 414 // by the process umask. |
437 // The return value is Dart_ScopeAllocated. | 415 // The return value is Dart_ScopeAllocated. |
438 PathBuffer path; | 416 PathBuffer path; |
439 if (!path.Add(prefix)) { | 417 if (!path.Add(prefix)) { |
440 return NULL; | 418 return NULL; |
441 } | 419 } |
442 if (!path.Add("XXXXXX")) { | 420 if (!path.Add("XXXXXX")) { |
443 // Pattern has overflowed. | 421 // Pattern has overflowed. |
444 return NULL; | 422 return NULL; |
445 } | 423 } |
446 char* result; | 424 char* result; |
447 do { | 425 do { |
448 result = mkdtemp(path.AsString()); | 426 result = mkdtemp(path.AsString()); |
449 } while ((result == NULL) && (errno == EINTR)); | 427 } while ((result == NULL) && (errno == EINTR)); |
450 if (result == NULL) { | 428 if (result == NULL) { |
451 return NULL; | 429 return NULL; |
452 } | 430 } |
453 return path.AsScopedString(); | 431 return path.AsScopedString(); |
454 } | 432 } |
455 | 433 |
456 | |
457 bool Directory::Delete(const char* dir_name, bool recursive) { | 434 bool Directory::Delete(const char* dir_name, bool recursive) { |
458 if (!recursive) { | 435 if (!recursive) { |
459 if ((File::GetType(dir_name, false) == File::kIsLink) && | 436 if ((File::GetType(dir_name, false) == File::kIsLink) && |
460 (File::GetType(dir_name, true) == File::kIsDirectory)) { | 437 (File::GetType(dir_name, true) == File::kIsDirectory)) { |
461 return (NO_RETRY_EXPECTED(unlink(dir_name)) == 0); | 438 return (NO_RETRY_EXPECTED(unlink(dir_name)) == 0); |
462 } | 439 } |
463 return (NO_RETRY_EXPECTED(rmdir(dir_name)) == 0); | 440 return (NO_RETRY_EXPECTED(rmdir(dir_name)) == 0); |
464 } else { | 441 } else { |
465 PathBuffer path; | 442 PathBuffer path; |
466 if (!path.Add(dir_name)) { | 443 if (!path.Add(dir_name)) { |
467 return false; | 444 return false; |
468 } | 445 } |
469 return DeleteRecursively(&path); | 446 return DeleteRecursively(&path); |
470 } | 447 } |
471 } | 448 } |
472 | 449 |
473 | |
474 bool Directory::Rename(const char* path, const char* new_path) { | 450 bool Directory::Rename(const char* path, const char* new_path) { |
475 ExistsResult exists = Exists(path); | 451 ExistsResult exists = Exists(path); |
476 if (exists != EXISTS) { | 452 if (exists != EXISTS) { |
477 return false; | 453 return false; |
478 } | 454 } |
479 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0); | 455 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0); |
480 } | 456 } |
481 | 457 |
482 } // namespace bin | 458 } // namespace bin |
483 } // namespace dart | 459 } // namespace dart |
484 | 460 |
485 #endif // defined(HOST_OS_ANDROID) | 461 #endif // defined(HOST_OS_ANDROID) |
OLD | NEW |