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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 DirectoryListing* listing) { | 46 DirectoryListing* listing) { |
47 if (strcmp(dir_name, ".") != 0 && | 47 if (strcmp(dir_name, ".") != 0 && |
48 strcmp(dir_name, "..") != 0) { | 48 strcmp(dir_name, "..") != 0) { |
49 size_t written = snprintf(path + path_length, | 49 size_t written = snprintf(path + path_length, |
50 MAX_PATH - path_length, | 50 MAX_PATH - path_length, |
51 "%s", | 51 "%s", |
52 dir_name); | 52 dir_name); |
53 if (written != strlen(dir_name)) { | 53 if (written != strlen(dir_name)) { |
54 return false; | 54 return false; |
55 } | 55 } |
56 bool ok = listing->HandleDirectory(path); | 56 char* utf8_path = StringUtils::SystemStringToUtf8(path); |
| 57 bool ok = listing->HandleDirectory(utf8_path); |
| 58 free(utf8_path); |
57 if (!ok) return ok; | 59 if (!ok) return ok; |
58 if (recursive) { | 60 if (recursive) { |
59 return ListRecursively(path, recursive, listing); | 61 return ListRecursively(path, recursive, listing); |
60 } | 62 } |
61 } | 63 } |
62 return true; | 64 return true; |
63 } | 65 } |
64 | 66 |
65 | 67 |
66 static bool HandleFile(char* file_name, | 68 static bool HandleFile(char* file_name, |
67 char* path, | 69 char* path, |
68 int path_length, | 70 int path_length, |
69 DirectoryListing* listing) { | 71 DirectoryListing* listing) { |
70 size_t written = snprintf(path + path_length, | 72 size_t written = snprintf(path + path_length, |
71 MAX_PATH - path_length, | 73 MAX_PATH - path_length, |
72 "%s", | 74 "%s", |
73 file_name); | 75 file_name); |
74 if (written != strlen(file_name)) { | 76 if (written != strlen(file_name)) { |
75 return false; | 77 return false; |
76 }; | 78 }; |
77 return listing->HandleFile(path); | 79 char* utf8_path = StringUtils::SystemStringToUtf8(path); |
| 80 bool ok = listing->HandleFile(utf8_path); |
| 81 free(utf8_path); |
| 82 return ok; |
78 } | 83 } |
79 | 84 |
80 | 85 |
81 static bool HandleEntry(LPWIN32_FIND_DATA find_file_data, | 86 static bool HandleEntry(LPWIN32_FIND_DATA find_file_data, |
82 char* path, | 87 char* path, |
83 int path_length, | 88 int path_length, |
84 bool recursive, | 89 bool recursive, |
85 DirectoryListing* listing) { | 90 DirectoryListing* listing) { |
86 DWORD attributes = find_file_data->dwFileAttributes; | 91 DWORD attributes = find_file_data->dwFileAttributes; |
87 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 92 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
(...skipping 30 matching lines...) Expand all Loading... |
118 "\\*"); | 123 "\\*"); |
119 if (written != 2) { | 124 if (written != 2) { |
120 return false; | 125 return false; |
121 } | 126 } |
122 *path_length += written; | 127 *path_length += written; |
123 return true; | 128 return true; |
124 } | 129 } |
125 | 130 |
126 static void PostError(DirectoryListing* listing, | 131 static void PostError(DirectoryListing* listing, |
127 const char* dir_name) { | 132 const char* dir_name) { |
128 listing->HandleError(dir_name); | 133 const char* utf8_path = StringUtils::SystemStringToUtf8(dir_name); |
| 134 listing->HandleError(utf8_path); |
| 135 free(const_cast<char*>(utf8_path)); |
129 } | 136 } |
130 | 137 |
131 | 138 |
132 static bool ListRecursively(const char* dir_name, | 139 static bool ListRecursively(const char* dir_name, |
133 bool recursive, | 140 bool recursive, |
134 DirectoryListing* listing) { | 141 DirectoryListing* listing) { |
135 // Compute full path for the directory currently being listed. The | 142 // Compute full path for the directory currently being listed. The |
136 // path buffer will be used to construct the current path in the | 143 // path buffer will be used to construct the current path in the |
137 // recursive traversal. path_length does not always equal | 144 // recursive traversal. path_length does not always equal |
138 // strlen(path) but indicates the current prefix of path that is the | 145 // strlen(path) but indicates the current prefix of path that is the |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 return false; | 314 return false; |
308 } | 315 } |
309 | 316 |
310 return success; | 317 return success; |
311 } | 318 } |
312 | 319 |
313 | 320 |
314 bool Directory::List(const char* dir_name, | 321 bool Directory::List(const char* dir_name, |
315 bool recursive, | 322 bool recursive, |
316 DirectoryListing* listing) { | 323 DirectoryListing* listing) { |
317 bool completed = ListRecursively(dir_name, recursive, listing); | 324 const char* system_name = StringUtils::Utf8ToSystemString(dir_name); |
| 325 bool completed = ListRecursively(system_name, recursive, listing); |
| 326 free(const_cast<char*>(system_name)); |
318 return completed; | 327 return completed; |
319 } | 328 } |
320 | 329 |
321 | 330 |
322 Directory::ExistsResult Directory::Exists(const char* dir_name) { | 331 static Directory::ExistsResult ExistsHelper(const char* dir_name) { |
323 DWORD attributes = GetFileAttributes(dir_name); | 332 DWORD attributes = GetFileAttributes(dir_name); |
324 if (attributes == INVALID_FILE_ATTRIBUTES) { | 333 if (attributes == INVALID_FILE_ATTRIBUTES) { |
325 DWORD last_error = GetLastError(); | 334 DWORD last_error = GetLastError(); |
326 if (last_error == ERROR_FILE_NOT_FOUND || | 335 if (last_error == ERROR_FILE_NOT_FOUND || |
327 last_error == ERROR_PATH_NOT_FOUND) { | 336 last_error == ERROR_PATH_NOT_FOUND) { |
328 return DOES_NOT_EXIST; | 337 return Directory::DOES_NOT_EXIST; |
329 } else { | 338 } else { |
330 // We might not be able to get the file attributes for other | 339 // We might not be able to get the file attributes for other |
331 // reasons such as lack of permissions. In that case we do | 340 // reasons such as lack of permissions. In that case we do |
332 // not know if the directory exists. | 341 // not know if the directory exists. |
333 return UNKNOWN; | 342 return Directory::UNKNOWN; |
334 } | 343 } |
335 } | 344 } |
336 bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | 345 bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
337 return exists ? EXISTS : DOES_NOT_EXIST; | 346 return exists ? Directory::EXISTS : Directory::DOES_NOT_EXIST; |
| 347 } |
| 348 |
| 349 |
| 350 Directory::ExistsResult Directory::Exists(const char* dir_name) { |
| 351 const char* system_name = StringUtils::Utf8ToSystemString(dir_name); |
| 352 Directory::ExistsResult result = ExistsHelper(system_name); |
| 353 free(const_cast<char*>(system_name)); |
| 354 return result; |
338 } | 355 } |
339 | 356 |
340 | 357 |
341 char* Directory::Current() { | 358 char* Directory::Current() { |
342 char* result; | |
343 int length = GetCurrentDirectory(0, NULL); | 359 int length = GetCurrentDirectory(0, NULL); |
344 result = reinterpret_cast<char*>(malloc(length + 1)); | 360 char* current = reinterpret_cast<char*>(malloc(length + 1)); |
345 GetCurrentDirectory(length + 1, result); | 361 GetCurrentDirectory(length + 1, current); |
| 362 char* result = StringUtils::SystemStringToUtf8(current); |
| 363 free(current); |
346 return result; | 364 return result; |
347 } | 365 } |
348 | 366 |
349 | 367 |
350 bool Directory::Create(const char* dir_name) { | 368 bool Directory::Create(const char* dir_name) { |
| 369 const char* system_name = StringUtils::Utf8ToSystemString(dir_name); |
351 // If the directory already exists and is a directory do not | 370 // If the directory already exists and is a directory do not |
352 // attempt to create it again and treat it as a success. | 371 // attempt to create it again and treat it as a success. |
353 if (Exists(dir_name) == EXISTS) return true; | 372 if (ExistsHelper(system_name) == EXISTS) { |
354 return (CreateDirectory(dir_name, NULL) != 0); | 373 free(const_cast<char*>(system_name)); |
| 374 return true; |
| 375 } |
| 376 int create_status = CreateDirectory(system_name, NULL); |
| 377 free(const_cast<char*>(system_name)); |
| 378 return (create_status != 0); |
355 } | 379 } |
356 | 380 |
357 | 381 |
358 char* Directory::CreateTemp(const char* const_template) { | 382 char* Directory::CreateTemp(const char* const_template) { |
359 // Returns a new, unused directory name, modifying the contents of | 383 // Returns a new, unused directory name, modifying the contents of |
360 // dir_template. Creates this directory, with a default security | 384 // dir_template. Creates this directory, with a default security |
361 // descriptor inherited from its parent directory. | 385 // descriptor inherited from its parent directory. |
362 // The return value must be freed by the caller. | 386 // The return value must be freed by the caller. |
363 char* path = static_cast<char*>(malloc(MAX_PATH)); | 387 char* path = static_cast<char*>(malloc(MAX_PATH)); |
364 int path_length; | 388 int path_length; |
365 if (0 == strncmp(const_template, "", 1)) { | 389 if (0 == strncmp(const_template, "", 1)) { |
366 path_length = GetTempPath(MAX_PATH, path); | 390 path_length = GetTempPath(MAX_PATH, path); |
367 if (path_length == 0) { | 391 if (path_length == 0) { |
368 free(path); | 392 free(path); |
369 return NULL; | 393 return NULL; |
370 } | 394 } |
371 } else { | 395 } else { |
372 snprintf(path, MAX_PATH, "%s", const_template); | 396 const char* system_template = |
| 397 StringUtils::Utf8ToSystemString(const_template); |
| 398 snprintf(path, MAX_PATH, "%s", system_template); |
| 399 free(const_cast<char*>(system_template)); |
373 path_length = strlen(path); | 400 path_length = strlen(path); |
374 } | 401 } |
375 // Length of tempdir-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is 44. | 402 // Length of tempdir-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is 44. |
376 if (path_length > MAX_PATH - 44) { | 403 if (path_length > MAX_PATH - 44) { |
377 free(path); | 404 free(path); |
378 return NULL; | 405 return NULL; |
379 } | 406 } |
380 if ((path)[path_length - 1] == '\\') { | 407 if ((path)[path_length - 1] == '\\') { |
381 // No base name for the directory - use "tempdir". | 408 // No base name for the directory - use "tempdir". |
382 snprintf(path + path_length, MAX_PATH - path_length, "tempdir"); | 409 snprintf(path + path_length, MAX_PATH - path_length, "tempdir"); |
(...skipping 11 matching lines...) Expand all Loading... |
394 if (status != RPC_S_OK) { | 421 if (status != RPC_S_OK) { |
395 free(path); | 422 free(path); |
396 return NULL; | 423 return NULL; |
397 } | 424 } |
398 | 425 |
399 snprintf(path + path_length, MAX_PATH - path_length, "-%s", uuid_string); | 426 snprintf(path + path_length, MAX_PATH - path_length, "-%s", uuid_string); |
400 if (!CreateDirectory(path, NULL)) { | 427 if (!CreateDirectory(path, NULL)) { |
401 free(path); | 428 free(path); |
402 return NULL; | 429 return NULL; |
403 } | 430 } |
404 return path; | 431 char* result = StringUtils::SystemStringToUtf8(path); |
| 432 free(path); |
| 433 return result; |
405 } | 434 } |
406 | 435 |
407 | 436 |
408 bool Directory::Delete(const char* dir_name, bool recursive) { | 437 bool Directory::Delete(const char* dir_name, bool recursive) { |
| 438 bool result = false; |
| 439 const char* system_dir_name = |
| 440 StringUtils::Utf8ToSystemString(dir_name); |
409 if (!recursive) { | 441 if (!recursive) { |
410 return (RemoveDirectory(dir_name) != 0); | 442 result = (RemoveDirectory(system_dir_name) != 0); |
411 } else { | 443 } else { |
412 return DeleteRecursively(dir_name); | 444 result = DeleteRecursively(system_dir_name); |
413 } | 445 } |
| 446 free(const_cast<char*>(system_dir_name)); |
| 447 return result; |
414 } | 448 } |
415 | 449 |
416 | 450 |
417 bool Directory::Rename(const char* path, const char* new_path) { | 451 bool Directory::Rename(const char* path, const char* new_path) { |
418 ExistsResult exists = Exists(path); | 452 const char* system_path = StringUtils::Utf8ToSystemString(path); |
| 453 const char* system_new_path = |
| 454 StringUtils::Utf8ToSystemString(new_path); |
| 455 ExistsResult exists = ExistsHelper(system_path); |
419 if (exists != EXISTS) return false; | 456 if (exists != EXISTS) return false; |
420 ExistsResult new_exists = Exists(new_path); | 457 ExistsResult new_exists = ExistsHelper(system_new_path); |
421 // MoveFile does not allow replacing exising directories. Therefore, | 458 // MoveFile does not allow replacing exising directories. Therefore, |
422 // if the new_path is currently a directory we need to delete it | 459 // if the new_path is currently a directory we need to delete it |
423 // first. | 460 // first. |
424 if (new_exists == EXISTS) { | 461 if (new_exists == EXISTS) { |
425 bool success = DeleteRecursively(new_path); | 462 bool success = DeleteRecursively(system_new_path); |
426 if (!success) return false; | 463 if (!success) return false; |
427 } | 464 } |
428 DWORD flags = MOVEFILE_WRITE_THROUGH; | 465 DWORD flags = MOVEFILE_WRITE_THROUGH; |
429 return (MoveFileEx(path, new_path, flags) != 0); | 466 int move_status = |
| 467 MoveFileEx(system_path, system_new_path, flags); |
| 468 free(const_cast<char*>(system_path)); |
| 469 free(const_cast<char*>(system_new_path)); |
| 470 return (move_status != 0); |
430 } | 471 } |
OLD | NEW |