Chromium Code Reviews| 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 <errno.h> | 8 #include <errno.h> |
| 8 #include <fcntl.h> | 9 #include <fcntl.h> |
| 9 #include <fnmatch.h> | 10 #include <fnmatch.h> |
| 10 #include <fts.h> | 11 #include <fts.h> |
| 11 #include <libgen.h> | 12 #include <libgen.h> |
| 12 #include <stdio.h> | 13 #include <stdio.h> |
| 13 #include <string.h> | 14 #include <string.h> |
| 14 #include <sys/errno.h> | 15 #include <sys/errno.h> |
| 15 #include <sys/mman.h> | 16 #include <sys/mman.h> |
| 16 #include <sys/stat.h> | 17 #include <sys/stat.h> |
| 18 #include <sys/types.h> | |
| 17 #include <time.h> | 19 #include <time.h> |
| 20 #include <unistd.h> | |
| 18 | 21 |
| 19 #include <fstream> | 22 #include <fstream> |
| 20 | 23 |
| 21 #include "base/basictypes.h" | 24 #include "base/basictypes.h" |
| 22 #include "base/file_path.h" | 25 #include "base/file_path.h" |
| 23 #include "base/logging.h" | 26 #include "base/logging.h" |
| 24 #include "base/string_util.h" | 27 #include "base/string_util.h" |
| 28 #include "base/time.h" | |
| 25 | 29 |
| 26 namespace file_util { | 30 namespace file_util { |
| 27 | 31 |
| 28 #if defined(GOOGLE_CHROME_BUILD) | 32 #if defined(GOOGLE_CHROME_BUILD) |
| 29 static const char* kTempFileName = "com.google.chrome.XXXXXX"; | 33 static const char* kTempFileName = "com.google.chrome.XXXXXX"; |
| 30 #else | 34 #else |
| 31 static const char* kTempFileName = "org.chromium.XXXXXX"; | 35 static const char* kTempFileName = "org.chromium.XXXXXX"; |
| 32 #endif | 36 #endif |
| 33 | 37 |
| 34 std::wstring GetDirectoryFromPath(const std::wstring& path) { | 38 std::wstring GetDirectoryFromPath(const std::wstring& path) { |
| 35 if (EndsWithSeparator(path)) { | 39 if (EndsWithSeparator(path)) { |
| 36 std::wstring dir = path; | 40 std::wstring dir = path; |
| 37 TrimTrailingSeparator(&dir); | 41 TrimTrailingSeparator(&dir); |
| 38 return dir; | 42 return dir; |
| 39 } else { | 43 } else { |
| 40 char full_path[PATH_MAX]; | 44 char full_path[PATH_MAX]; |
| 41 base::strlcpy(full_path, WideToUTF8(path).c_str(), arraysize(full_path)); | 45 base::strlcpy(full_path, WideToUTF8(path).c_str(), arraysize(full_path)); |
| 42 return UTF8ToWide(dirname(full_path)); | 46 return UTF8ToWide(dirname(full_path)); |
| 43 } | 47 } |
| 44 } | 48 } |
| 45 | 49 |
| 46 bool AbsolutePath(FilePath* path) { | 50 bool AbsolutePath(FilePath* path) { |
| 47 char full_path[PATH_MAX]; | 51 char full_path[PATH_MAX]; |
| 48 if (realpath(path->value().c_str(), full_path) == NULL) | 52 if (realpath(path->value().c_str(), full_path) == NULL) |
| 49 return false; | 53 return false; |
| 50 *path = FilePath(full_path); | 54 *path = FilePath(full_path); |
| 51 return true; | 55 return true; |
| 52 } | 56 } |
| 53 | 57 |
| 58 int CountFilesCreatedAfter(const FilePath& path, | |
| 59 const base::Time& comparison_time) { | |
| 60 int file_count = 0; | |
| 61 | |
| 62 DIR* dir = opendir(path.value().c_str()); | |
| 63 if (dir) { | |
| 64 struct dirent* ent; | |
| 65 while ((ent = readdir(dir)) != NULL) { | |
|
Erik does not do reviews
2009/04/21 16:10:21
lint says to use readdir_r instead of readdir (see
| |
| 66 if ((strcmp(ent->d_name, ".") == 0) || | |
| 67 (strcmp(ent->d_name, "..") == 0)) | |
| 68 continue; | |
| 69 | |
| 70 struct stat64 st; | |
| 71 int test = stat64(path.Append(ent->d_name).value().c_str(), &st); | |
| 72 if (test != 0) { | |
| 73 LOG(ERROR) << "stat64 failed: " << strerror(errno); | |
| 74 continue; | |
| 75 } | |
| 76 if (st.st_ctime >= comparison_time.ToTimeT()) | |
|
Hironori Bono
2009/04/22 04:46:27
nit: If I recall correctly, base::Time::ToTimeT()
hamaji
2009/04/22 05:12:23
Good point. However, if we use Time::DoubleT(), I
Hironori Bono
2009/04/22 05:35:58
I'm wondering why you did not write this comment i
| |
| 77 ++file_count; | |
| 78 } | |
| 79 closedir(dir); | |
| 80 } | |
| 81 return file_count; | |
| 82 } | |
| 83 | |
| 54 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" | 84 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" |
| 55 // which works both with and without the recursive flag. I'm not sure we need | 85 // which works both with and without the recursive flag. I'm not sure we need |
| 56 // that functionality. If not, remove from file_util_win.cc, otherwise add it | 86 // that functionality. If not, remove from file_util_win.cc, otherwise add it |
| 57 // here. | 87 // here. |
| 58 bool Delete(const FilePath& path, bool recursive) { | 88 bool Delete(const FilePath& path, bool recursive) { |
| 59 const char* path_str = path.value().c_str(); | 89 const char* path_str = path.value().c_str(); |
| 60 struct stat64 file_info; | 90 struct stat64 file_info; |
| 61 int test = stat64(path_str, &file_info); | 91 int test = stat64(path_str, &file_info); |
| 62 if (test != 0) { | 92 if (test != 0) { |
| 63 // The Windows version defines this condition as success. | 93 // The Windows version defines this condition as success. |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 584 munmap(data_, length_); | 614 munmap(data_, length_); |
| 585 if (file_ != -1) | 615 if (file_ != -1) |
| 586 close(file_); | 616 close(file_); |
| 587 | 617 |
| 588 data_ = NULL; | 618 data_ = NULL; |
| 589 length_ = 0; | 619 length_ = 0; |
| 590 file_ = -1; | 620 file_ = -1; |
| 591 } | 621 } |
| 592 | 622 |
| 593 } // namespace file_util | 623 } // namespace file_util |
| OLD | NEW |