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