| 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 <dirent.h> | 5 #include <dirent.h> |
| 6 #include <errno.h> | 6 #include <errno.h> |
| 7 #include <libgen.h> | 7 #include <libgen.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <sys/param.h> | 9 #include <sys/param.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 PATH_MAX - path_length, | 80 PATH_MAX - path_length, |
| 81 "%s", | 81 "%s", |
| 82 file_name); | 82 file_name); |
| 83 ASSERT(written == strlen(file_name)); | 83 ASSERT(written == strlen(file_name)); |
| 84 Dart_Handle name = Dart_NewString(path); | 84 Dart_Handle name = Dart_NewString(path); |
| 85 Dart_Post(file_port, name); | 85 Dart_Post(file_port, name); |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 | 89 |
| 90 static void PostError(Dart_Port error_port, | |
| 91 const char* prefix, | |
| 92 const char* suffix, | |
| 93 int error_code) { | |
| 94 if (error_port != 0) { | |
| 95 // Extract the errno string. | |
| 96 static int kBufferSize = 1024; | |
| 97 char* error_buffer = static_cast<char*>(malloc(kBufferSize)); | |
| 98 error_buffer[0] = '\0'; | |
| 99 char* error_str = strerror_r(error_code, error_buffer, kBufferSize); | |
| 100 // Compose the error message from the parts. | |
| 101 int error_message_size = | |
| 102 strlen(prefix) + strlen(suffix) + strlen(error_str) + 3; | |
| 103 char* message = static_cast<char*>(malloc(error_message_size + 1)); | |
| 104 int written = snprintf(message, | |
| 105 error_message_size + 1, | |
| 106 "%s%s (%s)", | |
| 107 prefix, | |
| 108 suffix, | |
| 109 error_str); | |
| 110 ASSERT(written == error_message_size); | |
| 111 free(error_buffer); | |
| 112 Dart_Post(error_port, Dart_NewString(message)); | |
| 113 free(message); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 | |
| 118 static bool ListRecursively(const char* dir_name, | 90 static bool ListRecursively(const char* dir_name, |
| 119 bool recursive, | 91 bool recursive, |
| 120 Dart_Port dir_port, | 92 Dart_Port dir_port, |
| 121 Dart_Port file_port, | 93 Dart_Port file_port, |
| 122 Dart_Port done_port, | 94 Dart_Port done_port, |
| 123 Dart_Port error_port) { | 95 Dart_Port error_port) { |
| 124 DIR* dir_pointer = opendir(dir_name); | 96 DIR* dir_pointer = opendir(dir_name); |
| 125 if (dir_pointer == NULL) { | 97 if (dir_pointer == NULL) { |
| 126 PostError(error_port, "Directory listing failed for: ", dir_name, errno); | 98 // TODO(ager): post something on the error port. |
| 127 return false; | 99 return false; |
| 128 } | 100 } |
| 129 | 101 |
| 130 // Compute full path for the directory currently being listed. | 102 // Compute full path for the directory currently being listed. |
| 131 char *path = static_cast<char*>(malloc(PATH_MAX)); | 103 char *path = static_cast<char*>(malloc(PATH_MAX)); |
| 132 ASSERT(path != NULL); | 104 ASSERT(path != NULL); |
| 133 int path_length = 0; | 105 int path_length = 0; |
| 134 ComputeFullPath(dir_name, path, &path_length); | 106 ComputeFullPath(dir_name, path, &path_length); |
| 135 | 107 |
| 136 // Iterated the directory and post the directories and files to the | 108 // Iterated the directory and post the directories and files to the |
| 137 // ports. | 109 // ports. |
| 138 int success = 0; | 110 int success = 0; |
| 139 bool listing_error = false; | 111 bool completed = true; |
| 140 dirent entry; | 112 dirent entry; |
| 141 dirent* result; | 113 dirent* result; |
| 142 while ((success = readdir_r(dir_pointer, &entry, &result)) == 0 && | 114 while ((success = readdir_r(dir_pointer, &entry, &result)) == 0 && |
| 143 result != NULL && | 115 result != NULL) { |
| 144 !listing_error) { | |
| 145 switch (entry.d_type) { | 116 switch (entry.d_type) { |
| 146 case DT_DIR: | 117 case DT_DIR: |
| 147 listing_error = listing_error || !HandleDir(entry.d_name, | 118 completed = completed && HandleDir(entry.d_name, |
| 148 path, | 119 path, |
| 149 path_length, | 120 path_length, |
| 150 recursive, | 121 recursive, |
| 151 dir_port, | 122 dir_port, |
| 152 file_port, | 123 file_port, |
| 153 done_port, | 124 done_port, |
| 154 error_port); | 125 error_port); |
| 155 break; | 126 break; |
| 156 case DT_REG: | 127 case DT_REG: |
| 157 HandleFile(entry.d_name, path, path_length, file_port); | 128 HandleFile(entry.d_name, path, path_length, file_port); |
| 158 break; | 129 break; |
| 159 case DT_UNKNOWN: { | 130 case DT_UNKNOWN: { |
| 160 // On some file systems the entry type is not determined by | 131 // On some file systems the entry type is not determined by |
| 161 // readdir_r. For those we use lstat to determine the entry | 132 // readdir_r. For those we use lstat to determine the entry |
| 162 // type. | 133 // type. |
| 163 struct stat entry_info; | 134 struct stat entry_info; |
| 164 size_t written = snprintf(path + path_length, | 135 size_t written = snprintf(path + path_length, |
| 165 PATH_MAX - path_length, | 136 PATH_MAX - path_length, |
| 166 "%s", | 137 "%s", |
| 167 entry.d_name); | 138 entry.d_name); |
| 168 ASSERT(written == strlen(entry.d_name)); | 139 ASSERT(written == strlen(entry.d_name)); |
| 169 int lstat_success = lstat(path, &entry_info); | 140 int lstat_success = lstat(path, &entry_info); |
| 170 if (lstat_success == -1) { | 141 if (lstat_success != 0) { |
| 171 listing_error = true; | 142 completed = false; |
| 172 PostError(error_port, "Directory listing failed for: ", path, errno); | |
| 173 break; | 143 break; |
| 174 } | 144 } |
| 175 if ((entry_info.st_mode & S_IFMT) == S_IFDIR) { | 145 if ((entry_info.st_mode & S_IFMT) == S_IFDIR) { |
| 176 listing_error = listing_error || !HandleDir(entry.d_name, | 146 HandleDir(entry.d_name, |
| 177 path, | 147 path, |
| 178 path_length, | 148 path_length, |
| 179 recursive, | 149 recursive, |
| 180 dir_port, | 150 dir_port, |
| 181 file_port, | 151 file_port, |
| 182 done_port, | 152 done_port, |
| 183 error_port); | 153 error_port); |
| 184 } else if ((entry_info.st_mode & S_IFMT) == S_IFREG) { | 154 } else if ((entry_info.st_mode & S_IFMT) == S_IFREG) { |
| 185 HandleFile(entry.d_name, path, path_length, file_port); | 155 HandleFile(entry.d_name, path, path_length, file_port); |
| 186 } | 156 } |
| 187 break; | 157 break; |
| 188 } | 158 } |
| 189 default: | 159 default: |
| 190 break; | 160 break; |
| 191 } | 161 } |
| 192 } | 162 } |
| 163 completed = completed && (success == 0); |
| 193 | 164 |
| 194 if (success != 0) { | 165 // TODO(ager): Post on error port if closing fails. |
| 195 listing_error = true; | 166 closedir(dir_pointer); |
| 196 PostError(error_port, "Directory listing failed", "", success); | |
| 197 } | |
| 198 | |
| 199 if (closedir(dir_pointer) == -1) { | |
| 200 PostError(error_port, "Failed to close directory", "", errno); | |
| 201 } | |
| 202 free(path); | 167 free(path); |
| 203 | 168 |
| 204 return !listing_error; | 169 return completed; |
| 205 } | 170 } |
| 206 | 171 |
| 207 | 172 |
| 208 void Directory::List(const char* dir_name, | 173 void Directory::List(const char* dir_name, |
| 209 bool recursive, | 174 bool recursive, |
| 210 Dart_Port dir_port, | 175 Dart_Port dir_port, |
| 211 Dart_Port file_port, | 176 Dart_Port file_port, |
| 212 Dart_Port done_port, | 177 Dart_Port done_port, |
| 213 Dart_Port error_port) { | 178 Dart_Port error_port) { |
| 214 bool completed = ListRecursively(dir_name, | 179 bool completed = ListRecursively(dir_name, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 bool Directory::Create(const char* dir_name) { | 221 bool Directory::Create(const char* dir_name) { |
| 257 // Create the directory with the permissions specified by the | 222 // Create the directory with the permissions specified by the |
| 258 // process umask. | 223 // process umask. |
| 259 return (mkdir(dir_name, 0777) == 0); | 224 return (mkdir(dir_name, 0777) == 0); |
| 260 } | 225 } |
| 261 | 226 |
| 262 | 227 |
| 263 bool Directory::Delete(const char* dir_name) { | 228 bool Directory::Delete(const char* dir_name) { |
| 264 return (rmdir(dir_name) == 0); | 229 return (rmdir(dir_name) == 0); |
| 265 } | 230 } |
| OLD | NEW |