| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 bool Directory::Open(const char* path, intptr_t* dir) { | |
| 8 return true; | |
| 9 } | |
| 10 | |
| 11 | |
| 12 bool Directory::Close(intptr_t dir) { | |
| 13 return true; | |
| 14 } | |
| 15 | |
| 16 | |
| 17 static void HandleDir(char* dir_name, | 7 static void HandleDir(char* dir_name, |
| 18 char* path, | 8 char* path, |
| 19 int path_length, | 9 int path_length, |
| 20 Dart_Port dir_handler) { | 10 Dart_Port dir_port) { |
| 21 if (dir_handler != 0 && | 11 if (dir_port != 0 && |
| 22 strcmp(dir_name, ".") != 0 && | 12 strcmp(dir_name, ".") != 0 && |
| 23 strcmp(dir_name, "..") != 0) { | 13 strcmp(dir_name, "..") != 0) { |
| 24 size_t written = snprintf(path + path_length, | 14 size_t written = snprintf(path + path_length, |
| 25 MAX_PATH - path_length, | 15 MAX_PATH - path_length, |
| 26 "%s", | 16 "%s", |
| 27 dir_name); | 17 dir_name); |
| 28 ASSERT(written == strlen(dir_name)); | 18 ASSERT(written == strlen(dir_name)); |
| 29 Dart_Handle name = Dart_NewString(path); | 19 Dart_Handle name = Dart_NewString(path); |
| 30 Dart_Post(dir_handler, name); | 20 Dart_Post(dir_port, name); |
| 31 } | 21 } |
| 32 } | 22 } |
| 33 | 23 |
| 34 | 24 |
| 35 static void HandleFile(char* file_name, | 25 static void HandleFile(char* file_name, |
| 36 char* path, | 26 char* path, |
| 37 int path_length, | 27 int path_length, |
| 38 Dart_Port file_handler) { | 28 Dart_Port file_port) { |
| 39 if (file_handler != 0) { | 29 if (file_port != 0) { |
| 40 size_t written = snprintf(path + path_length, | 30 size_t written = snprintf(path + path_length, |
| 41 MAX_PATH - path_length, | 31 MAX_PATH - path_length, |
| 42 "%s", | 32 "%s", |
| 43 file_name); | 33 file_name); |
| 44 ASSERT(written == strlen(file_name)); | 34 ASSERT(written == strlen(file_name)); |
| 45 Dart_Handle name = Dart_NewString(path); | 35 Dart_Handle name = Dart_NewString(path); |
| 46 Dart_Post(file_handler, name); | 36 Dart_Post(file_port, name); |
| 47 } | 37 } |
| 48 } | 38 } |
| 49 | 39 |
| 50 | 40 |
| 51 static void HandleEntry(LPWIN32_FIND_DATA find_file_data, | 41 static void HandleEntry(LPWIN32_FIND_DATA find_file_data, |
| 52 char* path, | 42 char* path, |
| 53 int path_length, | 43 int path_length, |
| 54 Dart_Port file_handler, | 44 Dart_Port file_port, |
| 55 Dart_Port dir_handler) { | 45 Dart_Port dir_port) { |
| 56 DWORD attributes = find_file_data->dwFileAttributes; | 46 DWORD attributes = find_file_data->dwFileAttributes; |
| 57 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 47 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
| 58 HandleDir(find_file_data->cFileName, path, path_length, dir_handler); | 48 HandleDir(find_file_data->cFileName, path, path_length, dir_port); |
| 59 } else { | 49 } else { |
| 60 HandleFile(find_file_data->cFileName, path, path_length, file_handler); | 50 HandleFile(find_file_data->cFileName, path, path_length, file_port); |
| 61 } | 51 } |
| 62 } | 52 } |
| 63 | 53 |
| 64 | 54 |
| 65 static void ComputeFullSearchPath(const char* dir_name, | 55 static void ComputeFullSearchPath(const char* dir_name, |
| 66 char* path, | 56 char* path, |
| 67 int* path_length) { | 57 int* path_length) { |
| 68 // GetFullPathName only works in a multi-threaded environment if | 58 // GetFullPathName only works in a multi-threaded environment if |
| 69 // SetCurrentDirectory is not used. We currently have no plan for | 59 // SetCurrentDirectory is not used. We currently have no plan for |
| 70 // exposing SetCurrentDirectory. | 60 // exposing SetCurrentDirectory. |
| 71 int written = | 61 int written = |
| 72 GetFullPathName(dir_name, MAX_PATH - *path_length, path, NULL); | 62 GetFullPathName(dir_name, MAX_PATH - *path_length, path, NULL); |
| 73 *path_length += written; | 63 *path_length += written; |
| 74 written = snprintf(path + *path_length, | 64 written = snprintf(path + *path_length, |
| 75 MAX_PATH - *path_length, | 65 MAX_PATH - *path_length, |
| 76 "%s", | 66 "%s", |
| 77 "\\*"); | 67 "\\*"); |
| 78 ASSERT(written == 2); | 68 ASSERT(written == 2); |
| 79 *path_length += written; | 69 *path_length += written; |
| 80 } | 70 } |
| 81 | 71 |
| 82 | 72 |
| 83 void Directory::List(const char* dir_name, | 73 void Directory::List(const char* dir_name, |
| 84 intptr_t dir, | |
| 85 bool recursive, | 74 bool recursive, |
| 86 Dart_Port dir_handler, | 75 Dart_Port dir_port, |
| 87 Dart_Port file_handler, | 76 Dart_Port file_port, |
| 88 Dart_Port done_handler, | 77 Dart_Port done_port, |
| 89 Dart_Port dir_error_handler) { | 78 Dart_Port error_port) { |
| 90 // TODO(ager): Handle recursive listing. | 79 // TODO(ager): Handle recursive listing. |
| 91 char path[MAX_PATH]; | 80 char path[MAX_PATH]; |
| 92 int path_length = 0; | 81 int path_length = 0; |
| 93 ComputeFullSearchPath(dir_name, path, &path_length); | 82 ComputeFullSearchPath(dir_name, path, &path_length); |
| 94 | 83 |
| 95 WIN32_FIND_DATA find_file_data; | 84 WIN32_FIND_DATA find_file_data; |
| 96 HANDLE find_handle = FindFirstFile(path, &find_file_data); | 85 HANDLE find_handle = FindFirstFile(path, &find_file_data); |
| 97 | 86 |
| 98 // Adjust the path by removing the '*' used for the search. | 87 // Adjust the path by removing the '*' used for the search. |
| 99 path_length -= 1; | 88 path_length -= 1; |
| 100 path[path_length] = '\0'; | 89 path[path_length] = '\0'; |
| 101 | 90 |
| 102 if (find_handle == INVALID_HANDLE_VALUE) { | 91 if (find_handle == INVALID_HANDLE_VALUE) { |
| 92 // TODO(ager): Post on error port. |
| 103 Dart_Handle value = Dart_NewBoolean(false); | 93 Dart_Handle value = Dart_NewBoolean(false); |
| 104 Dart_Post(done_handler, value); | 94 Dart_Post(done_port, value); |
| 105 return; | 95 return; |
| 106 } | 96 } |
| 107 | 97 |
| 108 HandleEntry(&find_file_data, path, path_length, file_handler, dir_handler); | 98 HandleEntry(&find_file_data, path, path_length, file_port, dir_port); |
| 109 while (FindNextFile(find_handle, &find_file_data) != 0) { | 99 while (FindNextFile(find_handle, &find_file_data) != 0) { |
| 110 HandleEntry(&find_file_data, path, path_length, file_handler, dir_handler); | 100 HandleEntry(&find_file_data, path, path_length, file_port, dir_port); |
| 111 } | 101 } |
| 112 | 102 |
| 113 bool result = GetLastError() == ERROR_NO_MORE_FILES; | 103 bool result = GetLastError() == ERROR_NO_MORE_FILES; |
| 114 BOOL closed = FindClose(find_handle); | |
| 115 if (!closed) { | |
| 116 result = false; | |
| 117 } | |
| 118 Dart_Handle value = Dart_NewBoolean(result); | 104 Dart_Handle value = Dart_NewBoolean(result); |
| 119 Dart_Post(done_handler, value); | 105 Dart_Post(done_port, value); |
| 106 |
| 107 // TODO(ager): Post on error port. |
| 108 FindClose(find_handle); |
| 120 } | 109 } |
| OLD | NEW |