1 // Copyright 2014 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "store/layout.hpp"
34 #include "store/exceptions.hpp"
35 #include "utils/datetime.hpp"
36 #include "utils/format/macros.hpp"
37 #include "utils/fs/directory.hpp"
38 #include "utils/fs/exceptions.hpp"
39 #include "utils/fs/path.hpp"
40 #include "utils/fs/operations.hpp"
41 #include "utils/logging/macros.hpp"
42 #include "utils/env.hpp"
43 #include "utils/optional.ipp"
44 #include "utils/sanity.hpp"
45 #include "utils/text/exceptions.hpp"
46 #include "utils/text/regex.hpp"
48 namespace datetime = utils::datetime;
49 namespace fs = utils::fs;
50 namespace layout = store::layout;
51 namespace text = utils::text;
53 using utils::optional;
59 /// Finds the results file for the latest run of the given test suite.
61 /// \param test_suite Identifier of the test suite to query.
63 /// \return Path to the located database holding the most recent data for the
66 /// \throw store::error If no previous results file can be found.
68 find_latest(const std::string& test_suite)
70 const fs::path store_dir = layout::query_store_dir();
72 const text::regex preg = text::regex::compile(
73 F("^results.%s.[0-9]{8}-[0-9]{6}-[0-9]{6}.db$") % test_suite, 0);
77 const fs::directory dir(store_dir);
78 for (fs::directory::const_iterator iter = dir.begin();
79 iter != dir.end(); ++iter) {
80 const text::regex_matches matches = preg.match(iter->name);
82 if (latest.empty() || iter->name > latest) {
86 // Not a database file; skip.
92 F("No previous results file found for test suite %s")
95 return store_dir / latest;
96 } catch (const fs::system_error& e) {
97 LW(F("Failed to open store dir %s: %s") % store_dir % e.what());
98 throw store::error(F("No previous results file found for test suite %s")
100 } catch (const text::regex_error& e) {
101 throw store::error(e.what());
106 /// Computes the identifier of a new tests results file.
108 /// \param test_suite Identifier of the test suite.
109 /// \param when Timestamp to attach to the identifier.
111 /// \return Identifier of the file to be created.
113 new_id(const std::string& test_suite, const datetime::timestamp& when)
115 const std::string when_datetime = when.strftime("%Y%m%d-%H%M%S");
116 const int when_ms = static_cast<int>(when.to_microseconds() % 1000000);
117 return F("%s.%s-%06s") % test_suite % when_datetime % when_ms;
121 } // anonymous namespace
124 /// Value to request the creation of a new results file with an automatic name.
126 /// Can be passed to new_db().
127 const char* layout::results_auto_create_name = "NEW";
130 /// Value to request the opening of the latest results file.
132 /// Can be passed to find_results().
133 const char* layout::results_auto_open_name = "LATEST";
136 /// Resolves the results file for the given identifier.
138 /// \param id Identifier of the test suite to open.
140 /// \return Path to the requested file, if any.
142 /// \throw store::error If there is no matching entry.
144 layout::find_results(const std::string& id)
146 LI(F("Searching for a results file with id %s") % id);
148 if (id == results_auto_open_name) {
149 const std::string test_suite = test_suite_for_path(fs::current_path());
150 return find_latest(test_suite);
152 const fs::path id_as_path(id);
154 if (fs::exists(id_as_path) && !fs::is_directory(id_as_path)) {
155 if (id_as_path.is_absolute())
158 return id_as_path.to_absolute();
159 } else if (id.find('/') == std::string::npos) {
160 const fs::path candidate =
161 query_store_dir() / (F("results.%s.db") % id);
162 if (fs::exists(candidate)) {
165 return find_latest(id);
168 INV(id.find('/') != std::string::npos);
169 return find_latest(test_suite_for_path(id_as_path));
175 /// Computes the path to a new database for the given test suite.
177 /// \param id Identifier of the test suite to create.
178 /// \param root Path to the root of the test suite being run, needed to properly
179 /// autogenerate the identifiers.
181 /// \return Identifier of the created results file, if applicable, and the path
183 layout::results_id_file_pair
184 layout::new_db(const std::string& id, const fs::path& root)
186 std::string generated_id;
187 optional< fs::path > path;
189 if (id == results_auto_create_name) {
190 generated_id = new_id(test_suite_for_path(root),
191 datetime::timestamp::now());
192 path = query_store_dir() / (F("results.%s.db") % generated_id);
193 fs::mkdir_p(path.get().branch_path(), 0755);
198 return std::make_pair(generated_id, path.get());
202 /// Computes the path to a new database for the given test suite.
204 /// \param root Path to the root of the test suite being run; needed to properly
205 /// autogenerate the identifiers.
206 /// \param when Timestamp for the test suite being run; needed to properly
207 /// autogenerate the identifiers.
209 /// \return Identifier of the created results file, if applicable, and the path
212 layout::new_db_for_migration(const fs::path& root,
213 const datetime::timestamp& when)
215 const std::string generated_id = new_id(test_suite_for_path(root), when);
216 const fs::path path = query_store_dir() / (
217 F("results.%s.db") % generated_id);
218 fs::mkdir_p(path.branch_path(), 0755);
223 /// Gets the path to the store directory.
225 /// Note that this function does not create the determined directory. It is the
226 /// responsibility of the caller to do so.
228 /// \return Path to the directory holding all the database files.
230 layout::query_store_dir(void)
232 const optional< fs::path > home = utils::get_home();
234 const fs::path& home_path = home.get();
235 if (home_path.is_absolute())
236 return home_path / ".kyua/store";
238 return home_path.to_absolute() / ".kyua/store";
240 LW("HOME not defined; creating store database in current "
242 return fs::current_path();
247 /// Returns the test suite name for the current directory.
249 /// \return The identifier of the current test suite.
251 layout::test_suite_for_path(const fs::path& path)
253 std::string test_suite;
254 if (path.is_absolute())
255 test_suite = path.str();
257 test_suite = path.to_absolute().str();
258 PRE(!test_suite.empty() && test_suite[0] == '/');
260 std::replace(test_suite.begin(), test_suite.end(), '/', '_');
261 test_suite.erase(0, 1);