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

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

Issue 2573143002: Handle non-regular files in recursive delete (Closed)
Patch Set: Address comments. Small adjustments Created 4 years 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
« no previous file with comments | « runtime/bin/directory_android.cc ('k') | runtime/bin/directory_macos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 return path->Add(dir_name) && DeleteRecursively(path); 250 return path->Add(dir_name) && DeleteRecursively(path);
251 } 251 }
252 252
253 253
254 static bool DeleteRecursively(PathBuffer* path) { 254 static bool DeleteRecursively(PathBuffer* path) {
255 // Do not recurse into links for deletion. Instead delete the link. 255 // Do not recurse into links for deletion. Instead delete the link.
256 // If it's a file, delete it. 256 // If it's a file, delete it.
257 struct stat64 st; 257 struct stat64 st;
258 if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &st)) == -1) { 258 if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &st)) == -1) {
259 return false; 259 return false;
260 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { 260 } else if (!S_ISDIR(st.st_mode)) {
261 return (NO_RETRY_EXPECTED(unlink(path->AsString())) == 0); 261 return (NO_RETRY_EXPECTED(unlink(path->AsString())) == 0);
262 } 262 }
263 263
264 if (!path->Add(File::PathSeparator())) { 264 if (!path->Add(File::PathSeparator())) {
265 return false; 265 return false;
266 } 266 }
267 267
268 // Not a link. Attempt to open as a directory and recurse into the 268 // Not a link. Attempt to open as a directory and recurse into the
269 // directory. 269 // directory.
270 DIR* dir_pointer; 270 DIR* dir_pointer;
(...skipping 23 matching lines...) Expand all
294 } 294 }
295 // End of directory. 295 // End of directory.
296 return (NO_RETRY_EXPECTED(closedir(dir_pointer)) == 0) && 296 return (NO_RETRY_EXPECTED(closedir(dir_pointer)) == 0) &&
297 (NO_RETRY_EXPECTED(remove(path->AsString())) == 0); 297 (NO_RETRY_EXPECTED(remove(path->AsString())) == 0);
298 } 298 }
299 bool ok = false; 299 bool ok = false;
300 switch (entry->d_type) { 300 switch (entry->d_type) {
301 case DT_DIR: 301 case DT_DIR:
302 ok = DeleteDir(entry->d_name, path); 302 ok = DeleteDir(entry->d_name, path);
303 break; 303 break;
304 case DT_BLK:
305 case DT_CHR:
306 case DT_FIFO:
307 case DT_SOCK:
304 case DT_REG: 308 case DT_REG:
305 case DT_LNK: 309 case DT_LNK:
306 // Treat all links as files. This will delete the link which 310 // Treat all links as files. This will delete the link which
307 // is what we want no matter if the link target is a file or a 311 // is what we want no matter if the link target is a file or a
308 // directory. 312 // directory.
309 ok = DeleteFile(entry->d_name, path); 313 ok = DeleteFile(entry->d_name, path);
310 break; 314 break;
311 case DT_UNKNOWN: { 315 case DT_UNKNOWN: {
312 if (!path->Add(entry->d_name)) { 316 if (!path->Add(entry->d_name)) {
313 break; 317 break;
314 } 318 }
315 // On some file systems the entry type is not determined by 319 // On some file systems the entry type is not determined by
316 // readdir. For those we use lstat to determine the entry 320 // readdir. For those we use lstat to determine the entry
317 // type. 321 // type.
318 struct stat64 entry_info; 322 struct stat64 entry_info;
319 if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &entry_info)) == -1) { 323 if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &entry_info)) == -1) {
320 break; 324 break;
321 } 325 }
322 path->Reset(path_length); 326 path->Reset(path_length);
323 if (S_ISDIR(entry_info.st_mode)) { 327 if (S_ISDIR(entry_info.st_mode)) {
324 ok = DeleteDir(entry->d_name, path); 328 ok = DeleteDir(entry->d_name, path);
325 } else if (S_ISREG(entry_info.st_mode) || S_ISLNK(entry_info.st_mode)) { 329 } else {
326 // Treat links as files. This will delete the link which is 330 // Treat links as files. This will delete the link which is
327 // what we want no matter if the link target is a file or a 331 // what we want no matter if the link target is a file or a
328 // directory. 332 // directory.
329 ok = DeleteFile(entry->d_name, path); 333 ok = DeleteFile(entry->d_name, path);
330 } 334 }
331 break; 335 break;
332 } 336 }
333 default: 337 default:
338 // We should have covered all the bases. If not, let's get an error.
339 FATAL1("Unexpected d_type: %d\n", entry->d_type);
334 break; 340 break;
335 } 341 }
336 if (!ok) { 342 if (!ok) {
337 break; 343 break;
338 } 344 }
339 path->Reset(path_length); 345 path->Reset(path_length);
340 } 346 }
341 // Only happens if an error. 347 // Only happens if an error.
342 ASSERT(errno != 0); 348 ASSERT(errno != 0);
343 int err = errno; 349 int err = errno;
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 if (exists != EXISTS) { 477 if (exists != EXISTS) {
472 return false; 478 return false;
473 } 479 }
474 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0); 480 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0);
475 } 481 }
476 482
477 } // namespace bin 483 } // namespace bin
478 } // namespace dart 484 } // namespace dart
479 485
480 #endif // defined(TARGET_OS_LINUX) 486 #endif // defined(TARGET_OS_LINUX)
OLDNEW
« no previous file with comments | « runtime/bin/directory_android.cc ('k') | runtime/bin/directory_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698