1// Copyright 2014 The Kyua Authors.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
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.
16//
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.
28
29#include "store/layout.hpp"
30
31#include <algorithm>
32#include <cstring>
33
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"
47
48namespace datetime = utils::datetime;
49namespace fs = utils::fs;
50namespace layout = store::layout;
51namespace text = utils::text;
52
53using utils::optional;
54
55
56namespace {
57
58
59/// Finds the results file for the latest run of the given test suite.
60///
61/// \param test_suite Identifier of the test suite to query.
62///
63/// \return Path to the located database holding the most recent data for the
64/// given test suite.
65///
66/// \throw store::error If no previous results file can be found.
67static fs::path
68find_latest(const std::string& test_suite)
69{
70    const fs::path store_dir = layout::query_store_dir();
71    try {
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);
74
75        std::string latest;
76
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);
81            if (matches) {
82                if (latest.empty() || iter->name > latest) {
83                    latest = iter->name;
84                }
85            } else {
86                // Not a database file; skip.
87            }
88        }
89
90        if (latest.empty())
91            throw store::error(
92                F("No previous results file found for test suite %s")
93                % test_suite);
94
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")
99                           % test_suite);
100    } catch (const text::regex_error& e) {
101        throw store::error(e.what());
102    }
103}
104
105
106/// Computes the identifier of a new tests results file.
107///
108/// \param test_suite Identifier of the test suite.
109/// \param when Timestamp to attach to the identifier.
110///
111/// \return Identifier of the file to be created.
112static std::string
113new_id(const std::string& test_suite, const datetime::timestamp& when)
114{
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;
118}
119
120
121}  // anonymous namespace
122
123
124/// Value to request the creation of a new results file with an automatic name.
125///
126/// Can be passed to new_db().
127const char* layout::results_auto_create_name = "NEW";
128
129
130/// Value to request the opening of the latest results file.
131///
132/// Can be passed to find_results().
133const char* layout::results_auto_open_name = "LATEST";
134
135
136/// Resolves the results file for the given identifier.
137///
138/// \param id Identifier of the test suite to open.
139///
140/// \return Path to the requested file, if any.
141///
142/// \throw store::error If there is no matching entry.
143fs::path
144layout::find_results(const std::string& id)
145{
146    LI(F("Searching for a results file with id %s") % id);
147
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);
151    } else {
152        const fs::path id_as_path(id);
153
154        if (fs::exists(id_as_path) && !fs::is_directory(id_as_path)) {
155            if (id_as_path.is_absolute())
156                return id_as_path;
157            else
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)) {
163                return candidate;
164            } else {
165                return find_latest(id);
166            }
167        } else {
168            INV(id.find('/') != std::string::npos);
169            return find_latest(test_suite_for_path(id_as_path));
170        }
171    }
172}
173
174
175/// Computes the path to a new database for the given test suite.
176///
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.
180///
181/// \return Identifier of the created results file, if applicable, and the path
182/// to such file.
183layout::results_id_file_pair
184layout::new_db(const std::string& id, const fs::path& root)
185{
186    std::string generated_id;
187    optional< fs::path > path;
188
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);
194    } else {
195        path = fs::path(id);
196    }
197
198    return std::make_pair(generated_id, path.get());
199}
200
201
202/// Computes the path to a new database for the given test suite.
203///
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.
208///
209/// \return Identifier of the created results file, if applicable, and the path
210/// to such file.
211fs::path
212layout::new_db_for_migration(const fs::path& root,
213                             const datetime::timestamp& when)
214{
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);
219    return path;
220}
221
222
223/// Gets the path to the store directory.
224///
225/// Note that this function does not create the determined directory.  It is the
226/// responsibility of the caller to do so.
227///
228/// \return Path to the directory holding all the database files.
229fs::path
230layout::query_store_dir(void)
231{
232    const optional< fs::path > home = utils::get_home();
233    if (home) {
234        const fs::path& home_path = home.get();
235        if (home_path.is_absolute())
236            return home_path / ".kyua/store";
237        else
238            return home_path.to_absolute() / ".kyua/store";
239    } else {
240        LW("HOME not defined; creating store database in current "
241           "directory");
242        return fs::current_path();
243    }
244}
245
246
247/// Returns the test suite name for the current directory.
248///
249/// \return The identifier of the current test suite.
250std::string
251layout::test_suite_for_path(const fs::path& path)
252{
253    std::string test_suite;
254    if (path.is_absolute())
255        test_suite = path.str();
256    else
257        test_suite = path.to_absolute().str();
258    PRE(!test_suite.empty() && test_suite[0] == '/');
259
260    std::replace(test_suite.begin(), test_suite.end(), '/', '_');
261    test_suite.erase(0, 1);
262
263    return test_suite;
264}
265