| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/test/mini_installer_test/installer_path_provider.h" | 5 #include "chrome/test/mini_installer_test/installer_path_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file_enumerator.h" | |
| 12 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 13 #include "base/process_util.h" | 12 #include "base/process_util.h" |
| 14 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 15 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 16 #include "chrome/test/mini_installer_test/mini_installer_test_constants.h" | 15 #include "chrome/test/mini_installer_test/mini_installer_test_constants.h" |
| 17 #include "chrome/test/mini_installer_test/installer_test_util.h" | 16 #include "chrome/test/mini_installer_test/installer_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 struct FilePathInfo { | 21 struct FilePathInfo { |
| 23 base::FileEnumerator::FileInfo info; | 22 file_util::FileEnumerator::FindInfo info; |
| 24 base::FilePath path; | 23 base::FilePath path; |
| 25 }; | 24 }; |
| 26 | 25 |
| 27 bool CompareDate(const FilePathInfo& a, const FilePathInfo& b) { | 26 bool CompareDate(const FilePathInfo& a, |
| 28 return a.info.GetLastModifiedTime() > b.info.GetLastModifiedTime(); | 27 const FilePathInfo& b) { |
| 28 #if defined(OS_POSIX) |
| 29 return a.info.stat.st_mtime > b.info.stat.st_mtime; |
| 30 #elif defined(OS_WIN) |
| 31 if (a.info.ftLastWriteTime.dwHighDateTime == |
| 32 b.info.ftLastWriteTime.dwHighDateTime) { |
| 33 return a.info.ftLastWriteTime.dwLowDateTime > |
| 34 b.info.ftLastWriteTime.dwLowDateTime; |
| 35 } else { |
| 36 return a.info.ftLastWriteTime.dwHighDateTime > |
| 37 b.info.ftLastWriteTime.dwHighDateTime; |
| 38 } |
| 39 #endif |
| 29 } | 40 } |
| 30 | 41 |
| 31 // Get list of file |type| matching |pattern| in |root|. | 42 // Get list of file |type| matching |pattern| in |root|. |
| 32 // The list is sorted in last modified date order. | 43 // The list is sorted in last modified date order. |
| 33 // Return true if files/directories are found. | 44 // Return true if files/directories are found. |
| 34 bool FindMatchingFiles(const base::FilePath& root, | 45 bool FindMatchingFiles(const base::FilePath& root, |
| 35 const std::string& pattern, | 46 const std::string& pattern, |
| 36 base::FileEnumerator::FileType type, | 47 file_util::FileEnumerator::FileType type, |
| 37 std::vector<base::FilePath>* paths) { | 48 std::vector<base::FilePath>* paths) { |
| 38 base::FileEnumerator files(root, false, type, | 49 file_util::FileEnumerator files(root, false, type, |
| 39 base::FilePath().AppendASCII(pattern).value()); | 50 base::FilePath().AppendASCII(pattern).value()); |
| 40 std::vector<FilePathInfo> matches; | 51 std::vector<FilePathInfo> matches; |
| 41 for (base::FilePath current = files.Next(); !current.empty(); | 52 for (base::FilePath current = files.Next(); !current.empty(); |
| 42 current = files.Next()) { | 53 current = files.Next()) { |
| 43 FilePathInfo entry; | 54 FilePathInfo entry; |
| 44 entry.info = files.GetInfo(); | 55 files.GetFindInfo(&entry.info); |
| 45 entry.path = current; | 56 entry.path = current; |
| 46 matches.push_back(entry); | 57 matches.push_back(entry); |
| 47 } | 58 } |
| 48 | 59 |
| 49 if (matches.empty()) | 60 if (matches.empty()) |
| 50 return false; | 61 return false; |
| 51 | 62 |
| 52 std::sort(matches.begin(), matches.end(), CompareDate); | 63 std::sort(matches.begin(), matches.end(), CompareDate); |
| 53 std::vector<FilePathInfo>::iterator current; | 64 std::vector<FilePathInfo>::iterator current; |
| 54 for (current = matches.begin(); current != matches.end(); ++current) { | 65 for (current = matches.begin(); current != matches.end(); ++current) { |
| 55 paths->push_back(current->path); | 66 paths->push_back(current->path); |
| 56 } | 67 } |
| 57 return true; | 68 return true; |
| 58 } | 69 } |
| 59 | 70 |
| 60 bool FindNewestMatchingFile(const base::FilePath& root, | 71 bool FindNewestMatchingFile(const base::FilePath& root, |
| 61 const std::string& pattern, | 72 const std::string& pattern, |
| 62 base::FileEnumerator::FileType type, | 73 file_util::FileEnumerator::FileType type, |
| 63 base::FilePath* path) { | 74 base::FilePath* path) { |
| 64 std::vector<base::FilePath> paths; | 75 std::vector<base::FilePath> paths; |
| 65 if (FindMatchingFiles(root, pattern, type, &paths)) { | 76 if (FindMatchingFiles(root, pattern, type, &paths)) { |
| 66 *path = paths[0]; | 77 *path = paths[0]; |
| 67 return true; | 78 return true; |
| 68 } | 79 } |
| 69 return false; | 80 return false; |
| 70 } | 81 } |
| 71 | 82 |
| 72 } // namespace | 83 } // namespace |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 return false; | 137 return false; |
| 127 | 138 |
| 128 base::FilePath previous_installer; | 139 base::FilePath previous_installer; |
| 129 std::vector<std::string> tokenized_name; | 140 std::vector<std::string> tokenized_name; |
| 130 Tokenize(diff_installer.BaseName().MaybeAsASCII(), | 141 Tokenize(diff_installer.BaseName().MaybeAsASCII(), |
| 131 "_", &tokenized_name); | 142 "_", &tokenized_name); |
| 132 std::string build_pattern = base::StringPrintf( | 143 std::string build_pattern = base::StringPrintf( |
| 133 "*%s", tokenized_name[2].c_str()); | 144 "*%s", tokenized_name[2].c_str()); |
| 134 std::vector<base::FilePath> previous_build; | 145 std::vector<base::FilePath> previous_build; |
| 135 if (FindMatchingFiles(diff_installer.DirName().DirName().DirName(), | 146 if (FindMatchingFiles(diff_installer.DirName().DirName().DirName(), |
| 136 build_pattern, base::FileEnumerator::DIRECTORIES, | 147 build_pattern, file_util::FileEnumerator::DIRECTORIES, |
| 137 &previous_build)) { | 148 &previous_build)) { |
| 138 base::FilePath windir = previous_build.at(0).Append( | 149 base::FilePath windir = previous_build.at(0).Append( |
| 139 mini_installer_constants::kWinFolder); | 150 mini_installer_constants::kWinFolder); |
| 140 FindNewestMatchingFile(windir, full_installer_pattern, | 151 FindNewestMatchingFile(windir, full_installer_pattern, |
| 141 base::FileEnumerator::FILES, &previous_installer); | 152 file_util::FileEnumerator::FILES, &previous_installer); |
| 142 } | 153 } |
| 143 | 154 |
| 144 if (previous_installer.empty()) | 155 if (previous_installer.empty()) |
| 145 return false; | 156 return false; |
| 146 *path = previous_installer; | 157 *path = previous_installer; |
| 147 return true; | 158 return true; |
| 148 } | 159 } |
| 149 | 160 |
| 150 bool InstallerPathProvider::GetStandaloneInstaller(base::FilePath* path) { | 161 bool InstallerPathProvider::GetStandaloneInstaller(base::FilePath* path) { |
| 151 // Get standalone installer. | 162 // Get standalone installer. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 return path; | 208 return path; |
| 198 } | 209 } |
| 199 | 210 |
| 200 bool InstallerPathProvider::GetInstaller(const std::string& pattern, | 211 bool InstallerPathProvider::GetInstaller(const std::string& pattern, |
| 201 base::FilePath* path) { | 212 base::FilePath* path) { |
| 202 base::FilePath installer; | 213 base::FilePath installer; |
| 203 // Search filer for installer binary. | 214 // Search filer for installer binary. |
| 204 base::FilePath root(mini_installer_constants::kChromeInstallersLocation); | 215 base::FilePath root(mini_installer_constants::kChromeInstallersLocation); |
| 205 std::vector<base::FilePath> paths; | 216 std::vector<base::FilePath> paths; |
| 206 if (!FindMatchingFiles(root, current_build_, | 217 if (!FindMatchingFiles(root, current_build_, |
| 207 base::FileEnumerator::DIRECTORIES, &paths)) { | 218 file_util::FileEnumerator::DIRECTORIES, &paths)) { |
| 208 return false; | 219 return false; |
| 209 } | 220 } |
| 210 | 221 |
| 211 std::vector<base::FilePath>::const_iterator dir; | 222 std::vector<base::FilePath>::const_iterator dir; |
| 212 for (dir = paths.begin(); dir != paths.end(); ++dir) { | 223 for (dir = paths.begin(); dir != paths.end(); ++dir) { |
| 213 base::FilePath windir = dir->Append( | 224 base::FilePath windir = dir->Append( |
| 214 mini_installer_constants::kWinFolder); | 225 mini_installer_constants::kWinFolder); |
| 215 if (FindNewestMatchingFile(windir, pattern, base::FileEnumerator::FILES, | 226 if (FindNewestMatchingFile(windir, pattern, |
| 216 &installer)) { | 227 file_util::FileEnumerator::FILES, &installer)) { |
| 217 break; | 228 break; |
| 218 } | 229 } |
| 219 } | 230 } |
| 220 | 231 |
| 221 if (installer.empty()) { | 232 if (installer.empty()) { |
| 222 LOG(WARNING) << "Failed to find installer with pattern: " << pattern; | 233 LOG(WARNING) << "Failed to find installer with pattern: " << pattern; |
| 223 return false; | 234 return false; |
| 224 } | 235 } |
| 225 | 236 |
| 226 *path = installer; | 237 *path = installer; |
| 227 return true; | 238 return true; |
| 228 } | 239 } |
| 229 | 240 |
| 230 std::string InstallerPathProvider::GetCurrentBuild() { | 241 std::string InstallerPathProvider::GetCurrentBuild() { |
| 231 return current_build_; | 242 return current_build_; |
| 232 } | 243 } |
| 233 | 244 |
| 234 std::string InstallerPathProvider::GetPreviousBuild() { | 245 std::string InstallerPathProvider::GetPreviousBuild() { |
| 235 return previous_build_; | 246 return previous_build_; |
| 236 } | 247 } |
| 237 | 248 |
| 238 } // namespace | 249 } // namespace |
| OLD | NEW |