Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Side by Side Diff: runtime/bin/directory_linux.cc

Issue 165723007: Move signal_blocker to platform and use it by default in TEMP_FAILURE_RETRY. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_LINUX) 6 #if defined(TARGET_OS_LINUX)
7 7
8 #include "bin/directory.h" 8 #include "bin/directory.h"
9 9
10 #include <dirent.h> // NOLINT 10 #include <dirent.h> // NOLINT
11 #include <errno.h> // NOLINT 11 #include <errno.h> // NOLINT
12 #include <stdlib.h> // NOLINT 12 #include <stdlib.h> // NOLINT
13 #include <string.h> // NOLINT 13 #include <string.h> // NOLINT
14 #include <sys/param.h> // NOLINT 14 #include <sys/param.h> // NOLINT
15 #include <sys/stat.h> // NOLINT 15 #include <sys/stat.h> // NOLINT
16 #include <unistd.h> // NOLINT 16 #include <unistd.h> // NOLINT
17 17
18 #include "platform/signal_blocker.h"
18 #include "bin/file.h" 19 #include "bin/file.h"
19 #include "bin/platform.h" 20 #include "bin/platform.h"
20 21
21 22
22 namespace dart { 23 namespace dart {
23 namespace bin { 24 namespace bin {
24 25
25 26
26 PathBuffer::PathBuffer() : length_(0) { 27 PathBuffer::PathBuffer() : length_(0) {
27 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT 28 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 } 99 }
99 // Reset. 100 // Reset.
100 listing->path_buffer().Reset(path_length_); 101 listing->path_buffer().Reset(path_length_);
101 ResetLink(); 102 ResetLink();
102 103
103 // Iterate the directory and post the directories and files to the 104 // Iterate the directory and post the directories and files to the
104 // ports. 105 // ports.
105 int status = 0; 106 int status = 0;
106 dirent entry; 107 dirent entry;
107 dirent* result; 108 dirent* result;
108 if ((status = TEMP_FAILURE_RETRY(readdir_r(reinterpret_cast<DIR*>(lister_), 109 if ((status = readdir_r(reinterpret_cast<DIR*>(lister_),
109 &entry, 110 &entry,
110 &result))) == 0 && 111 &result)) == 0 &&
111 result != NULL) { 112 result != NULL) {
112 if (!listing->path_buffer().Add(entry.d_name)) { 113 if (!listing->path_buffer().Add(entry.d_name)) {
113 done_ = true; 114 done_ = true;
114 return kListError; 115 return kListError;
115 } 116 }
116 switch (entry.d_type) { 117 switch (entry.d_type) {
117 case DT_DIR: 118 case DT_DIR:
118 if (strcmp(entry.d_name, ".") == 0) return Next(listing); 119 if (strcmp(entry.d_name, ".") == 0) return Next(listing);
119 if (strcmp(entry.d_name, "..") == 0) return Next(listing); 120 if (strcmp(entry.d_name, "..") == 0) return Next(listing);
120 return kListDirectory; 121 return kListDirectory;
121 case DT_REG: 122 case DT_REG:
122 return kListFile; 123 return kListFile;
123 case DT_LNK: 124 case DT_LNK:
124 if (!listing->follow_links()) { 125 if (!listing->follow_links()) {
125 return kListLink; 126 return kListLink;
126 } 127 }
127 // Else fall through to next case. 128 // Else fall through to next case.
128 // Fall through. 129 // Fall through.
129 case DT_UNKNOWN: { 130 case DT_UNKNOWN: {
130 // On some file systems the entry type is not determined by 131 // On some file systems the entry type is not determined by
131 // readdir_r. For those and for links we use stat to determine 132 // readdir_r. For those and for links we use stat to determine
132 // the actual entry type. Notice that stat returns the type of 133 // the actual entry type. Notice that stat returns the type of
133 // the file pointed to. 134 // the file pointed to.
134 struct stat64 entry_info; 135 struct stat64 entry_info;
135 int stat_success; 136 int stat_success;
136 stat_success = TEMP_FAILURE_RETRY( 137 stat_success = lstat64(listing->path_buffer().AsString(), &entry_info);
137 lstat64(listing->path_buffer().AsString(), &entry_info));
138 if (stat_success == -1) { 138 if (stat_success == -1) {
139 return kListError; 139 return kListError;
140 } 140 }
141 if (listing->follow_links() && S_ISLNK(entry_info.st_mode)) { 141 if (listing->follow_links() && S_ISLNK(entry_info.st_mode)) {
142 // Check to see if we are in a loop created by a symbolic link. 142 // Check to see if we are in a loop created by a symbolic link.
143 LinkList current_link = { entry_info.st_dev, 143 LinkList current_link = { entry_info.st_dev,
144 entry_info.st_ino, 144 entry_info.st_ino,
145 link_ }; 145 link_ };
146 LinkList* previous = link_; 146 LinkList* previous = link_;
147 while (previous != NULL) { 147 while (previous != NULL) {
148 if (previous->dev == current_link.dev && 148 if (previous->dev == current_link.dev &&
149 previous->ino == current_link.ino) { 149 previous->ino == current_link.ino) {
150 // Report the looping link as a link, rather than following it. 150 // Report the looping link as a link, rather than following it.
151 return kListLink; 151 return kListLink;
152 } 152 }
153 previous = previous->next; 153 previous = previous->next;
154 } 154 }
155 stat_success = TEMP_FAILURE_RETRY( 155 stat_success = stat64(listing->path_buffer().AsString(), &entry_info);
156 stat64(listing->path_buffer().AsString(), &entry_info));
157 if (stat_success == -1) { 156 if (stat_success == -1) {
158 // Report a broken link as a link, even if follow_links is true. 157 // Report a broken link as a link, even if follow_links is true.
159 return kListLink; 158 return kListLink;
160 } 159 }
161 if (S_ISDIR(entry_info.st_mode)) { 160 if (S_ISDIR(entry_info.st_mode)) {
162 // Recurse into the subdirectory with current_link added to the 161 // Recurse into the subdirectory with current_link added to the
163 // linked list of seen file system links. 162 // linked list of seen file system links.
164 link_ = new LinkList(current_link); 163 link_ = new LinkList(current_link);
165 if (strcmp(entry.d_name, ".") == 0) return Next(listing); 164 if (strcmp(entry.d_name, ".") == 0) return Next(listing);
166 if (strcmp(entry.d_name, "..") == 0) return Next(listing); 165 if (strcmp(entry.d_name, "..") == 0) return Next(listing);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 if (strcmp(dir_name, ".") == 0) return true; 225 if (strcmp(dir_name, ".") == 0) return true;
227 if (strcmp(dir_name, "..") == 0) return true; 226 if (strcmp(dir_name, "..") == 0) return true;
228 return path->Add(dir_name) && DeleteRecursively(path); 227 return path->Add(dir_name) && DeleteRecursively(path);
229 } 228 }
230 229
231 230
232 static bool DeleteRecursively(PathBuffer* path) { 231 static bool DeleteRecursively(PathBuffer* path) {
233 // Do not recurse into links for deletion. Instead delete the link. 232 // Do not recurse into links for deletion. Instead delete the link.
234 // If it's a file, delete it. 233 // If it's a file, delete it.
235 struct stat64 st; 234 struct stat64 st;
236 if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &st)) == -1) { 235 if (lstat64(path->AsString(), &st) == -1) {
237 return false; 236 return false;
238 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { 237 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
239 return (unlink(path->AsString()) == 0); 238 return (unlink(path->AsString()) == 0);
240 } 239 }
241 240
242 if (!path->Add(File::PathSeparator())) return false; 241 if (!path->Add(File::PathSeparator())) return false;
243 242
244 // Not a link. Attempt to open as a directory and recurse into the 243 // Not a link. Attempt to open as a directory and recurse into the
245 // directory. 244 // directory.
246 DIR* dir_pointer; 245 DIR* dir_pointer;
247 do { 246 do {
248 dir_pointer = opendir(path->AsString()); 247 dir_pointer = opendir(path->AsString());
249 } while (dir_pointer == NULL && errno == EINTR); 248 } while (dir_pointer == NULL && errno == EINTR);
250 249
251 if (dir_pointer == NULL) { 250 if (dir_pointer == NULL) {
252 return false; 251 return false;
253 } 252 }
254 253
255 // Iterate the directory and delete all files and directories. 254 // Iterate the directory and delete all files and directories.
256 int path_length = path->length(); 255 int path_length = path->length();
257 int read = 0; 256 int read = 0;
258 bool success = true; 257 bool success = true;
259 dirent entry; 258 dirent entry;
260 dirent* result; 259 dirent* result;
261 while ((read = TEMP_FAILURE_RETRY(readdir_r(dir_pointer, 260 while ((read = readdir_r(dir_pointer, &entry, &result)) == 0 &&
262 &entry,
263 &result))) == 0 &&
264 result != NULL && 261 result != NULL &&
265 success) { 262 success) {
266 switch (entry.d_type) { 263 switch (entry.d_type) {
267 case DT_DIR: 264 case DT_DIR:
268 success = success && DeleteDir(entry.d_name, path); 265 success = success && DeleteDir(entry.d_name, path);
269 break; 266 break;
270 case DT_REG: 267 case DT_REG:
271 case DT_LNK: 268 case DT_LNK:
272 // Treat all links as files. This will delete the link which 269 // Treat all links as files. This will delete the link which
273 // is what we want no matter if the link target is a file or a 270 // is what we want no matter if the link target is a file or a
274 // directory. 271 // directory.
275 success = success && DeleteFile(entry.d_name, path); 272 success = success && DeleteFile(entry.d_name, path);
276 break; 273 break;
277 case DT_UNKNOWN: { 274 case DT_UNKNOWN: {
278 if (!path->Add(entry.d_name)) { 275 if (!path->Add(entry.d_name)) {
279 success = false; 276 success = false;
280 break; 277 break;
281 } 278 }
282 // On some file systems the entry type is not determined by 279 // On some file systems the entry type is not determined by
283 // readdir_r. For those we use lstat to determine the entry 280 // readdir_r. For those we use lstat to determine the entry
284 // type. 281 // type.
285 struct stat64 entry_info; 282 struct stat64 entry_info;
286 int lstat_success = TEMP_FAILURE_RETRY( 283 int lstat_success = lstat64(path->AsString(), &entry_info);
287 lstat64(path->AsString(), &entry_info));
288 if (lstat_success == -1) { 284 if (lstat_success == -1) {
289 success = false; 285 success = false;
290 break; 286 break;
291 } 287 }
292 path->Reset(path_length); 288 path->Reset(path_length);
293 if (S_ISDIR(entry_info.st_mode)) { 289 if (S_ISDIR(entry_info.st_mode)) {
294 success = success && DeleteDir(entry.d_name, path); 290 success = success && DeleteDir(entry.d_name, path);
295 } else if (S_ISREG(entry_info.st_mode) || S_ISLNK(entry_info.st_mode)) { 291 } else if (S_ISREG(entry_info.st_mode) || S_ISLNK(entry_info.st_mode)) {
296 // Treat links as files. This will delete the link which is 292 // Treat links as files. This will delete the link which is
297 // what we want no matter if the link target is a file or a 293 // what we want no matter if the link target is a file or a
(...skipping 12 matching lines...) Expand all
310 (closedir(dir_pointer) == -1) || 306 (closedir(dir_pointer) == -1) ||
311 (remove(path->AsString()) == -1)) { 307 (remove(path->AsString()) == -1)) {
312 return false; 308 return false;
313 } 309 }
314 return success; 310 return success;
315 } 311 }
316 312
317 313
318 Directory::ExistsResult Directory::Exists(const char* dir_name) { 314 Directory::ExistsResult Directory::Exists(const char* dir_name) {
319 struct stat64 entry_info; 315 struct stat64 entry_info;
320 int success = TEMP_FAILURE_RETRY(stat64(dir_name, &entry_info)); 316 int success = stat64(dir_name, &entry_info);
321 if (success == 0) { 317 if (success == 0) {
322 if (S_ISDIR(entry_info.st_mode)) { 318 if (S_ISDIR(entry_info.st_mode)) {
323 return EXISTS; 319 return EXISTS;
324 } else { 320 } else {
325 return DOES_NOT_EXIST; 321 return DOES_NOT_EXIST;
326 } 322 }
327 } else { 323 } else {
328 if (errno == EACCES || 324 if (errno == EACCES ||
329 errno == EBADF || 325 errno == EBADF ||
330 errno == EFAULT || 326 errno == EFAULT ||
(...skipping 23 matching lines...) Expand all
354 result = getcwd(buffer, size); 350 result = getcwd(buffer, size);
355 if (result == NULL && errno != ERANGE) { 351 if (result == NULL && errno != ERANGE) {
356 return NULL; 352 return NULL;
357 } 353 }
358 } 354 }
359 return buffer; 355 return buffer;
360 } 356 }
361 357
362 358
363 bool Directory::SetCurrent(const char* path) { 359 bool Directory::SetCurrent(const char* path) {
364 int result = TEMP_FAILURE_RETRY(chdir(path)); 360 return chdir(path) == 0;
365 return result == 0;
366 } 361 }
367 362
368 363
369 bool Directory::Create(const char* dir_name) { 364 bool Directory::Create(const char* dir_name) {
370 // Create the directory with the permissions specified by the 365 // Create the directory with the permissions specified by the
371 // process umask. 366 // process umask.
372 int result = TEMP_FAILURE_RETRY(mkdir(dir_name, 0777)); 367 int result = mkdir(dir_name, 0777);
373 // If the directory already exists, treat it as a success. 368 // If the directory already exists, treat it as a success.
374 if (result == -1 && errno == EEXIST) { 369 if (result == -1 && errno == EEXIST) {
375 return (Exists(dir_name) == EXISTS); 370 return (Exists(dir_name) == EXISTS);
376 } 371 }
377 return (result == 0); 372 return (result == 0);
378 } 373 }
379 374
380 375
381 char* Directory::SystemTemp() { 376 char* Directory::SystemTemp() {
382 const char* temp_dir = getenv("TMPDIR"); 377 const char* temp_dir = getenv("TMPDIR");
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 return NULL; 410 return NULL;
416 } 411 }
417 return strdup(result); 412 return strdup(result);
418 } 413 }
419 414
420 415
421 bool Directory::Delete(const char* dir_name, bool recursive) { 416 bool Directory::Delete(const char* dir_name, bool recursive) {
422 if (!recursive) { 417 if (!recursive) {
423 if (File::GetType(dir_name, false) == File::kIsLink && 418 if (File::GetType(dir_name, false) == File::kIsLink &&
424 File::GetType(dir_name, true) == File::kIsDirectory) { 419 File::GetType(dir_name, true) == File::kIsDirectory) {
425 return (TEMP_FAILURE_RETRY(unlink(dir_name)) == 0); 420 return unlink(dir_name) == 0;
426 } 421 }
427 return (TEMP_FAILURE_RETRY(rmdir(dir_name)) == 0); 422 return rmdir(dir_name) == 0;
428 } else { 423 } else {
429 PathBuffer path; 424 PathBuffer path;
430 if (!path.Add(dir_name)) { 425 if (!path.Add(dir_name)) {
431 return false; 426 return false;
432 } 427 }
433 return DeleteRecursively(&path); 428 return DeleteRecursively(&path);
434 } 429 }
435 } 430 }
436 431
437 432
438 bool Directory::Rename(const char* path, const char* new_path) { 433 bool Directory::Rename(const char* path, const char* new_path) {
439 ExistsResult exists = Exists(path); 434 ExistsResult exists = Exists(path);
440 if (exists != EXISTS) return false; 435 if (exists != EXISTS) return false;
441 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); 436 return rename(path, new_path) == 0;
442 } 437 }
443 438
444 } // namespace bin 439 } // namespace bin
445 } // namespace dart 440 } // namespace dart
446 441
447 #endif // defined(TARGET_OS_LINUX) 442 #endif // defined(TARGET_OS_LINUX)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698