| 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 "bin/directory.h" | 5 #include "bin/directory.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 | 9 |
| 10 #include "bin/platform.h" | 10 #include "bin/platform.h" |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 if ((attributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) { | 215 if ((attributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) { |
| 216 attributes &= ~FILE_ATTRIBUTE_READONLY; | 216 attributes &= ~FILE_ATTRIBUTE_READONLY; |
| 217 | 217 |
| 218 if (SetFileAttributes(path, attributes) == 0) { | 218 if (SetFileAttributes(path, attributes) == 0) { |
| 219 return false; | 219 return false; |
| 220 } | 220 } |
| 221 | 221 |
| 222 return DeleteFile(path) != 0; | 222 return DeleteFile(path) != 0; |
| 223 } | 223 } |
| 224 } | 224 } |
| 225 |
| 226 return false; |
| 225 } | 227 } |
| 226 | 228 |
| 227 | 229 |
| 228 static bool DeleteDir(char* dir_name, | 230 static bool DeleteDir(char* dir_name, |
| 229 char* path, | 231 char* path, |
| 230 int path_length) { | 232 int path_length) { |
| 231 if (strcmp(dir_name, ".") != 0 && | 233 if (strcmp(dir_name, ".") != 0 && |
| 232 strcmp(dir_name, "..") != 0) { | 234 strcmp(dir_name, "..") != 0) { |
| 233 size_t written = snprintf(path + path_length, | 235 size_t written = snprintf(path + path_length, |
| 234 MAX_PATH - path_length, | 236 MAX_PATH - path_length, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 313 |
| 312 bool Directory::List(const char* dir_name, | 314 bool Directory::List(const char* dir_name, |
| 313 bool recursive, | 315 bool recursive, |
| 314 DirectoryListing* listing) { | 316 DirectoryListing* listing) { |
| 315 bool completed = ListRecursively(dir_name, recursive, listing); | 317 bool completed = ListRecursively(dir_name, recursive, listing); |
| 316 return completed; | 318 return completed; |
| 317 } | 319 } |
| 318 | 320 |
| 319 | 321 |
| 320 Directory::ExistsResult Directory::Exists(const char* dir_name) { | 322 Directory::ExistsResult Directory::Exists(const char* dir_name) { |
| 321 struct stat entry_info; | 323 DWORD attributes = GetFileAttributes(dir_name); |
| 322 int stat_success = stat(dir_name, &entry_info); | 324 if (attributes == INVALID_FILE_ATTRIBUTES) { |
| 323 if (stat_success == 0) { | 325 DWORD last_error = GetLastError(); |
| 324 if ((entry_info.st_mode & S_IFMT) == S_IFDIR) { | 326 if (last_error == ERROR_FILE_NOT_FOUND || |
| 325 return EXISTS; | 327 last_error == ERROR_PATH_NOT_FOUND) { |
| 328 return DOES_NOT_EXIST; |
| 326 } else { | 329 } else { |
| 327 return DOES_NOT_EXIST; | 330 // We might not be able to get the file attributes for other |
| 328 } | 331 // reasons such as lack of permissions. In that case we do |
| 329 } else { | 332 // not know if the directory exists. |
| 330 if (errno == EACCES || | |
| 331 errno == EBADF || | |
| 332 errno == EFAULT || | |
| 333 errno == ENOMEM) { | |
| 334 // Search permissions denied for one of the directories in the | |
| 335 // path or a low level error occured. We do not know if the | |
| 336 // directory exists. | |
| 337 return UNKNOWN; | 333 return UNKNOWN; |
| 338 } | 334 } |
| 339 ASSERT(errno == ENAMETOOLONG || | |
| 340 errno == ENOENT || | |
| 341 errno == ENOTDIR); | |
| 342 return DOES_NOT_EXIST; | |
| 343 } | 335 } |
| 336 bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 337 return exists ? EXISTS : DOES_NOT_EXIST; |
| 344 } | 338 } |
| 345 | 339 |
| 346 | 340 |
| 347 char* Directory::Current() { | 341 char* Directory::Current() { |
| 348 char* result; | 342 char* result; |
| 349 int length = GetCurrentDirectory(0, NULL); | 343 int length = GetCurrentDirectory(0, NULL); |
| 350 result = reinterpret_cast<char*>(malloc(length + 1)); | 344 result = reinterpret_cast<char*>(malloc(length + 1)); |
| 351 GetCurrentDirectory(length + 1, result); | 345 GetCurrentDirectory(length + 1, result); |
| 352 return result; | 346 return result; |
| 353 } | 347 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 // MoveFile does not allow replacing exising directories. Therefore, | 418 // MoveFile does not allow replacing exising directories. Therefore, |
| 425 // if the new_path is currently a directory we need to delete it | 419 // if the new_path is currently a directory we need to delete it |
| 426 // first. | 420 // first. |
| 427 if (new_exists == EXISTS) { | 421 if (new_exists == EXISTS) { |
| 428 bool success = DeleteRecursively(new_path); | 422 bool success = DeleteRecursively(new_path); |
| 429 if (!success) return false; | 423 if (!success) return false; |
| 430 } | 424 } |
| 431 DWORD flags = MOVEFILE_WRITE_THROUGH; | 425 DWORD flags = MOVEFILE_WRITE_THROUGH; |
| 432 return (MoveFileEx(path, new_path, flags) != 0); | 426 return (MoveFileEx(path, new_path, flags) != 0); |
| 433 } | 427 } |
| OLD | NEW |