Chromium Code Reviews| 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 if (error_port != 0) { | |
| 94 int error_message_size = strlen(prefix) + strlen(suffix); | |
| 95 char* buffer = static_cast<char*>(malloc(error_message_size + 1)); | |
| 96 int written = snprintf(buffer, | |
| 97 error_message_size + 1, | |
| 98 "%s%s", | |
| 99 prefix, | |
| 100 suffix); | |
| 101 ASSERT(written == error_message_size); | |
| 102 Dart_Post(error_port, Dart_NewString(buffer)); | |
| 103 free(buffer); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 | |
| 90 static bool ListRecursively(const char* dir_name, | 108 static bool ListRecursively(const char* dir_name, |
| 91 bool recursive, | 109 bool recursive, |
| 92 Dart_Port dir_port, | 110 Dart_Port dir_port, |
| 93 Dart_Port file_port, | 111 Dart_Port file_port, |
| 94 Dart_Port done_port, | 112 Dart_Port done_port, |
| 95 Dart_Port error_port) { | 113 Dart_Port error_port) { |
| 96 DIR* dir_pointer = opendir(dir_name); | 114 DIR* dir_pointer = opendir(dir_name); |
| 97 if (dir_pointer == NULL) { | 115 if (dir_pointer == NULL) { |
| 98 // TODO(ager): post something on the error port. | 116 PostError(error_port, "Directory listing failed for: ", dir_name); |
|
Søren Gjesse
2011/11/08 16:56:16
This does not say anything about the actual error.
Mads Ager (google)
2011/11/09 08:07:30
Yes. Refactored so PostError always extracts the e
| |
| 99 return false; | 117 return false; |
| 100 } | 118 } |
| 101 | 119 |
| 102 // Compute full path for the directory currently being listed. | 120 // Compute full path for the directory currently being listed. |
| 103 char *path = static_cast<char*>(malloc(PATH_MAX)); | 121 char *path = static_cast<char*>(malloc(PATH_MAX)); |
| 104 ASSERT(path != NULL); | 122 ASSERT(path != NULL); |
| 105 int path_length = 0; | 123 int path_length = 0; |
| 106 ComputeFullPath(dir_name, path, &path_length); | 124 ComputeFullPath(dir_name, path, &path_length); |
| 107 | 125 |
| 108 // Iterated the directory and post the directories and files to the | 126 // Iterated the directory and post the directories and files to the |
| 109 // ports. | 127 // ports. |
| 110 int success = 0; | 128 int success = 0; |
| 111 bool completed = true; | 129 bool completed = true; |
| 112 dirent entry; | 130 dirent entry; |
| 113 dirent* result; | 131 dirent* result; |
| 114 while ((success = readdir_r(dir_pointer, &entry, &result)) == 0 && | 132 while ((success = readdir_r(dir_pointer, &entry, &result)) == 0 && |
| 115 result != NULL) { | 133 result != NULL && |
| 134 completed) { | |
|
Søren Gjesse
2011/11/08 16:56:16
Maybe it is just the naming, but "while (completed
Mads Ager (google)
2011/11/09 08:07:30
I agree. Renamed to listing_error and updated the
| |
| 116 switch (entry.d_type) { | 135 switch (entry.d_type) { |
| 117 case DT_DIR: | 136 case DT_DIR: |
| 118 completed = completed && HandleDir(entry.d_name, | 137 completed = completed && HandleDir(entry.d_name, |
| 119 path, | 138 path, |
| 120 path_length, | 139 path_length, |
| 121 recursive, | 140 recursive, |
| 122 dir_port, | 141 dir_port, |
| 123 file_port, | 142 file_port, |
| 124 done_port, | 143 done_port, |
| 125 error_port); | 144 error_port); |
| 126 break; | 145 break; |
| 127 case DT_REG: | 146 case DT_REG: |
| 128 HandleFile(entry.d_name, path, path_length, file_port); | 147 HandleFile(entry.d_name, path, path_length, file_port); |
| 129 break; | 148 break; |
| 130 case DT_UNKNOWN: { | 149 case DT_UNKNOWN: { |
| 131 // On some file systems the entry type is not determined by | 150 // On some file systems the entry type is not determined by |
| 132 // readdir_r. For those we use lstat to determine the entry | 151 // readdir_r. For those we use lstat to determine the entry |
| 133 // type. | 152 // type. |
| 134 struct stat entry_info; | 153 struct stat entry_info; |
| 135 size_t written = snprintf(path + path_length, | 154 size_t written = snprintf(path + path_length, |
| 136 PATH_MAX - path_length, | 155 PATH_MAX - path_length, |
| 137 "%s", | 156 "%s", |
| 138 entry.d_name); | 157 entry.d_name); |
| 139 ASSERT(written == strlen(entry.d_name)); | 158 ASSERT(written == strlen(entry.d_name)); |
| 140 int lstat_success = lstat(path, &entry_info); | 159 int lstat_success = lstat(path, &entry_info); |
| 141 if (lstat_success != 0) { | 160 if (lstat_success != 0) { |
| 142 completed = false; | 161 completed = false; |
| 162 PostError(error_port, "Directory listing failed for: ", path); | |
| 143 break; | 163 break; |
| 144 } | 164 } |
| 145 if ((entry_info.st_mode & S_IFMT) == S_IFDIR) { | 165 if ((entry_info.st_mode & S_IFMT) == S_IFDIR) { |
| 146 HandleDir(entry.d_name, | 166 HandleDir(entry.d_name, |
|
Søren Gjesse
2011/11/08 16:56:16
Why is completed not updated here?
Mads Ager (google)
2011/11/09 08:07:30
Whoops, good catch. Done!
| |
| 147 path, | 167 path, |
| 148 path_length, | 168 path_length, |
| 149 recursive, | 169 recursive, |
| 150 dir_port, | 170 dir_port, |
| 151 file_port, | 171 file_port, |
| 152 done_port, | 172 done_port, |
| 153 error_port); | 173 error_port); |
| 154 } else if ((entry_info.st_mode & S_IFMT) == S_IFREG) { | 174 } else if ((entry_info.st_mode & S_IFMT) == S_IFREG) { |
| 155 HandleFile(entry.d_name, path, path_length, file_port); | 175 HandleFile(entry.d_name, path, path_length, file_port); |
| 156 } | 176 } |
| 157 break; | 177 break; |
| 158 } | 178 } |
| 159 default: | 179 default: |
| 160 break; | 180 break; |
| 161 } | 181 } |
| 162 } | 182 } |
| 163 completed = completed && (success == 0); | |
| 164 | 183 |
| 165 // TODO(ager): Post on error port if closing fails. | 184 if (success != 0) { |
| 166 closedir(dir_pointer); | 185 completed = false; |
| 186 PostError(error_port, "Directory listing failed", ""); | |
| 187 } | |
| 188 | |
| 189 if (closedir(dir_pointer) == -1) { | |
| 190 static int kBufferSize = 1024; | |
| 191 char* buffer = static_cast<char*>(malloc(kBufferSize)); | |
| 192 buffer[0] = '\0'; | |
| 193 strerror_r(errno, buffer, kBufferSize); | |
| 194 PostError(error_port, "Failed to close directory: ", buffer); | |
| 195 free(buffer); | |
| 196 } | |
| 167 free(path); | 197 free(path); |
| 168 | 198 |
| 169 return completed; | 199 return completed; |
| 170 } | 200 } |
| 171 | 201 |
| 172 | 202 |
| 173 void Directory::List(const char* dir_name, | 203 void Directory::List(const char* dir_name, |
| 174 bool recursive, | 204 bool recursive, |
| 175 Dart_Port dir_port, | 205 Dart_Port dir_port, |
| 176 Dart_Port file_port, | 206 Dart_Port file_port, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 bool Directory::Create(const char* dir_name) { | 251 bool Directory::Create(const char* dir_name) { |
| 222 // Create the directory with the permissions specified by the | 252 // Create the directory with the permissions specified by the |
| 223 // process umask. | 253 // process umask. |
| 224 return (mkdir(dir_name, 0777) == 0); | 254 return (mkdir(dir_name, 0777) == 0); |
| 225 } | 255 } |
| 226 | 256 |
| 227 | 257 |
| 228 bool Directory::Delete(const char* dir_name) { | 258 bool Directory::Delete(const char* dir_name) { |
| 229 return (rmdir(dir_name) == 0); | 259 return (rmdir(dir_name) == 0); |
| 230 } | 260 } |
| OLD | NEW |