OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <fnmatch.h> | 10 #include <fnmatch.h> |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 #include <fstream> | 22 #include <fstream> |
23 | 23 |
24 #include "base/basictypes.h" | 24 #include "base/basictypes.h" |
25 #include "base/eintr_wrapper.h" | 25 #include "base/eintr_wrapper.h" |
26 #include "base/file_path.h" | 26 #include "base/file_path.h" |
27 #include "base/logging.h" | 27 #include "base/logging.h" |
28 #include "base/string_util.h" | 28 #include "base/string_util.h" |
29 #include "base/time.h" | 29 #include "base/time.h" |
30 | 30 |
| 31 namespace { |
| 32 |
| 33 bool IsDirectory(const FTSENT* file) { |
| 34 switch (file->fts_info) { |
| 35 case FTS_D: |
| 36 case FTS_DC: |
| 37 case FTS_DNR: |
| 38 case FTS_DOT: |
| 39 case FTS_DP: |
| 40 return true; |
| 41 default: |
| 42 return false; |
| 43 } |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
31 namespace file_util { | 48 namespace file_util { |
32 | 49 |
33 #if defined(GOOGLE_CHROME_BUILD) | 50 #if defined(GOOGLE_CHROME_BUILD) |
34 static const char* kTempFileName = "com.google.chrome.XXXXXX"; | 51 static const char* kTempFileName = "com.google.chrome.XXXXXX"; |
35 #else | 52 #else |
36 static const char* kTempFileName = "org.chromium.XXXXXX"; | 53 static const char* kTempFileName = "org.chromium.XXXXXX"; |
37 #endif | 54 #endif |
38 | 55 |
39 std::wstring GetDirectoryFromPath(const std::wstring& path) { | 56 std::wstring GetDirectoryFromPath(const std::wstring& path) { |
40 if (EndsWithSeparator(path)) { | 57 if (EndsWithSeparator(path)) { |
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 DCHECK(info); | 570 DCHECK(info); |
554 | 571 |
555 if (!is_in_find_op_) | 572 if (!is_in_find_op_) |
556 return; | 573 return; |
557 | 574 |
558 memcpy(&(info->stat), fts_ent_->fts_statp, sizeof(info->stat)); | 575 memcpy(&(info->stat), fts_ent_->fts_statp, sizeof(info->stat)); |
559 info->filename.assign(fts_ent_->fts_name); | 576 info->filename.assign(fts_ent_->fts_name); |
560 } | 577 } |
561 | 578 |
562 int CompareFiles(const FTSENT** a, const FTSENT** b) { | 579 int CompareFiles(const FTSENT** a, const FTSENT** b) { |
563 // Order lexicographically, ignoring case and whether they are files or | 580 // Order lexicographically with directories before other files. |
564 // directories. | 581 const bool a_is_dir = IsDirectory(*a); |
565 // TODO(yuzo): make this case-sensitive, directories-then-files, and | 582 const bool b_is_dir = IsDirectory(*b); |
566 // internationalized. | 583 if (a_is_dir != b_is_dir) |
| 584 return a_is_dir ? -1 : 1; |
| 585 |
| 586 // TODO(yuzo): make this internationalized when encoding detection function |
| 587 // becomes available. |
567 return base::strcasecmp((*a)->fts_name, (*b)->fts_name); | 588 return base::strcasecmp((*a)->fts_name, (*b)->fts_name); |
568 } | 589 } |
569 | 590 |
570 // As it stands, this method calls itself recursively when the next item of | 591 // As it stands, this method calls itself recursively when the next item of |
571 // the fts enumeration doesn't match (type, pattern, etc.). In the case of | 592 // the fts enumeration doesn't match (type, pattern, etc.). In the case of |
572 // large directories with many files this can be quite deep. | 593 // large directories with many files this can be quite deep. |
573 // TODO(erikkay) - get rid of this recursive pattern | 594 // TODO(erikkay) - get rid of this recursive pattern |
574 FilePath FileEnumerator::Next() { | 595 FilePath FileEnumerator::Next() { |
575 if (!is_in_find_op_) { | 596 if (!is_in_find_op_) { |
576 if (pending_paths_.empty()) | 597 if (pending_paths_.empty()) |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 munmap(data_, length_); | 693 munmap(data_, length_); |
673 if (file_ != -1) | 694 if (file_ != -1) |
674 close(file_); | 695 close(file_); |
675 | 696 |
676 data_ = NULL; | 697 data_ = NULL; |
677 length_ = 0; | 698 length_ = 0; |
678 file_ = -1; | 699 file_ = -1; |
679 } | 700 } |
680 | 701 |
681 } // namespace file_util | 702 } // namespace file_util |
OLD | NEW |