xref: /freebsd/contrib/kyua/store/layout.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis // Copyright 2014 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis #include "store/layout.hpp"
30*b0d29bc4SBrooks Davis 
31*b0d29bc4SBrooks Davis #include <algorithm>
32*b0d29bc4SBrooks Davis #include <cstring>
33*b0d29bc4SBrooks Davis 
34*b0d29bc4SBrooks Davis #include "store/exceptions.hpp"
35*b0d29bc4SBrooks Davis #include "utils/datetime.hpp"
36*b0d29bc4SBrooks Davis #include "utils/format/macros.hpp"
37*b0d29bc4SBrooks Davis #include "utils/fs/directory.hpp"
38*b0d29bc4SBrooks Davis #include "utils/fs/exceptions.hpp"
39*b0d29bc4SBrooks Davis #include "utils/fs/path.hpp"
40*b0d29bc4SBrooks Davis #include "utils/fs/operations.hpp"
41*b0d29bc4SBrooks Davis #include "utils/logging/macros.hpp"
42*b0d29bc4SBrooks Davis #include "utils/env.hpp"
43*b0d29bc4SBrooks Davis #include "utils/optional.ipp"
44*b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
45*b0d29bc4SBrooks Davis #include "utils/text/exceptions.hpp"
46*b0d29bc4SBrooks Davis #include "utils/text/regex.hpp"
47*b0d29bc4SBrooks Davis 
48*b0d29bc4SBrooks Davis namespace datetime = utils::datetime;
49*b0d29bc4SBrooks Davis namespace fs = utils::fs;
50*b0d29bc4SBrooks Davis namespace layout = store::layout;
51*b0d29bc4SBrooks Davis namespace text = utils::text;
52*b0d29bc4SBrooks Davis 
53*b0d29bc4SBrooks Davis using utils::optional;
54*b0d29bc4SBrooks Davis 
55*b0d29bc4SBrooks Davis 
56*b0d29bc4SBrooks Davis namespace {
57*b0d29bc4SBrooks Davis 
58*b0d29bc4SBrooks Davis 
59*b0d29bc4SBrooks Davis /// Finds the results file for the latest run of the given test suite.
60*b0d29bc4SBrooks Davis ///
61*b0d29bc4SBrooks Davis /// \param test_suite Identifier of the test suite to query.
62*b0d29bc4SBrooks Davis ///
63*b0d29bc4SBrooks Davis /// \return Path to the located database holding the most recent data for the
64*b0d29bc4SBrooks Davis /// given test suite.
65*b0d29bc4SBrooks Davis ///
66*b0d29bc4SBrooks Davis /// \throw store::error If no previous results file can be found.
67*b0d29bc4SBrooks Davis static fs::path
find_latest(const std::string & test_suite)68*b0d29bc4SBrooks Davis find_latest(const std::string& test_suite)
69*b0d29bc4SBrooks Davis {
70*b0d29bc4SBrooks Davis     const fs::path store_dir = layout::query_store_dir();
71*b0d29bc4SBrooks Davis     try {
72*b0d29bc4SBrooks Davis         const text::regex preg = text::regex::compile(
73*b0d29bc4SBrooks Davis             F("^results.%s.[0-9]{8}-[0-9]{6}-[0-9]{6}.db$") % test_suite, 0);
74*b0d29bc4SBrooks Davis 
75*b0d29bc4SBrooks Davis         std::string latest;
76*b0d29bc4SBrooks Davis 
77*b0d29bc4SBrooks Davis         const fs::directory dir(store_dir);
78*b0d29bc4SBrooks Davis         for (fs::directory::const_iterator iter = dir.begin();
79*b0d29bc4SBrooks Davis              iter != dir.end(); ++iter) {
80*b0d29bc4SBrooks Davis             const text::regex_matches matches = preg.match(iter->name);
81*b0d29bc4SBrooks Davis             if (matches) {
82*b0d29bc4SBrooks Davis                 if (latest.empty() || iter->name > latest) {
83*b0d29bc4SBrooks Davis                     latest = iter->name;
84*b0d29bc4SBrooks Davis                 }
85*b0d29bc4SBrooks Davis             } else {
86*b0d29bc4SBrooks Davis                 // Not a database file; skip.
87*b0d29bc4SBrooks Davis             }
88*b0d29bc4SBrooks Davis         }
89*b0d29bc4SBrooks Davis 
90*b0d29bc4SBrooks Davis         if (latest.empty())
91*b0d29bc4SBrooks Davis             throw store::error(
92*b0d29bc4SBrooks Davis                 F("No previous results file found for test suite %s")
93*b0d29bc4SBrooks Davis                 % test_suite);
94*b0d29bc4SBrooks Davis 
95*b0d29bc4SBrooks Davis         return store_dir / latest;
96*b0d29bc4SBrooks Davis     } catch (const fs::system_error& e) {
97*b0d29bc4SBrooks Davis         LW(F("Failed to open store dir %s: %s") % store_dir % e.what());
98*b0d29bc4SBrooks Davis         throw store::error(F("No previous results file found for test suite %s")
99*b0d29bc4SBrooks Davis                            % test_suite);
100*b0d29bc4SBrooks Davis     } catch (const text::regex_error& e) {
101*b0d29bc4SBrooks Davis         throw store::error(e.what());
102*b0d29bc4SBrooks Davis     }
103*b0d29bc4SBrooks Davis }
104*b0d29bc4SBrooks Davis 
105*b0d29bc4SBrooks Davis 
106*b0d29bc4SBrooks Davis /// Computes the identifier of a new tests results file.
107*b0d29bc4SBrooks Davis ///
108*b0d29bc4SBrooks Davis /// \param test_suite Identifier of the test suite.
109*b0d29bc4SBrooks Davis /// \param when Timestamp to attach to the identifier.
110*b0d29bc4SBrooks Davis ///
111*b0d29bc4SBrooks Davis /// \return Identifier of the file to be created.
112*b0d29bc4SBrooks Davis static std::string
new_id(const std::string & test_suite,const datetime::timestamp & when)113*b0d29bc4SBrooks Davis new_id(const std::string& test_suite, const datetime::timestamp& when)
114*b0d29bc4SBrooks Davis {
115*b0d29bc4SBrooks Davis     const std::string when_datetime = when.strftime("%Y%m%d-%H%M%S");
116*b0d29bc4SBrooks Davis     const int when_ms = static_cast<int>(when.to_microseconds() % 1000000);
117*b0d29bc4SBrooks Davis     return F("%s.%s-%06s") % test_suite % when_datetime % when_ms;
118*b0d29bc4SBrooks Davis }
119*b0d29bc4SBrooks Davis 
120*b0d29bc4SBrooks Davis 
121*b0d29bc4SBrooks Davis }  // anonymous namespace
122*b0d29bc4SBrooks Davis 
123*b0d29bc4SBrooks Davis 
124*b0d29bc4SBrooks Davis /// Value to request the creation of a new results file with an automatic name.
125*b0d29bc4SBrooks Davis ///
126*b0d29bc4SBrooks Davis /// Can be passed to new_db().
127*b0d29bc4SBrooks Davis const char* layout::results_auto_create_name = "NEW";
128*b0d29bc4SBrooks Davis 
129*b0d29bc4SBrooks Davis 
130*b0d29bc4SBrooks Davis /// Value to request the opening of the latest results file.
131*b0d29bc4SBrooks Davis ///
132*b0d29bc4SBrooks Davis /// Can be passed to find_results().
133*b0d29bc4SBrooks Davis const char* layout::results_auto_open_name = "LATEST";
134*b0d29bc4SBrooks Davis 
135*b0d29bc4SBrooks Davis 
136*b0d29bc4SBrooks Davis /// Resolves the results file for the given identifier.
137*b0d29bc4SBrooks Davis ///
138*b0d29bc4SBrooks Davis /// \param id Identifier of the test suite to open.
139*b0d29bc4SBrooks Davis ///
140*b0d29bc4SBrooks Davis /// \return Path to the requested file, if any.
141*b0d29bc4SBrooks Davis ///
142*b0d29bc4SBrooks Davis /// \throw store::error If there is no matching entry.
143*b0d29bc4SBrooks Davis fs::path
find_results(const std::string & id)144*b0d29bc4SBrooks Davis layout::find_results(const std::string& id)
145*b0d29bc4SBrooks Davis {
146*b0d29bc4SBrooks Davis     LI(F("Searching for a results file with id %s") % id);
147*b0d29bc4SBrooks Davis 
148*b0d29bc4SBrooks Davis     if (id == results_auto_open_name) {
149*b0d29bc4SBrooks Davis         const std::string test_suite = test_suite_for_path(fs::current_path());
150*b0d29bc4SBrooks Davis         return find_latest(test_suite);
151*b0d29bc4SBrooks Davis     } else {
152*b0d29bc4SBrooks Davis         const fs::path id_as_path(id);
153*b0d29bc4SBrooks Davis 
154*b0d29bc4SBrooks Davis         if (fs::exists(id_as_path) && !fs::is_directory(id_as_path)) {
155*b0d29bc4SBrooks Davis             if (id_as_path.is_absolute())
156*b0d29bc4SBrooks Davis                 return id_as_path;
157*b0d29bc4SBrooks Davis             else
158*b0d29bc4SBrooks Davis                 return id_as_path.to_absolute();
159*b0d29bc4SBrooks Davis         } else if (id.find('/') == std::string::npos) {
160*b0d29bc4SBrooks Davis             const fs::path candidate =
161*b0d29bc4SBrooks Davis                 query_store_dir() / (F("results.%s.db") % id);
162*b0d29bc4SBrooks Davis             if (fs::exists(candidate)) {
163*b0d29bc4SBrooks Davis                 return candidate;
164*b0d29bc4SBrooks Davis             } else {
165*b0d29bc4SBrooks Davis                 return find_latest(id);
166*b0d29bc4SBrooks Davis             }
167*b0d29bc4SBrooks Davis         } else {
168*b0d29bc4SBrooks Davis             INV(id.find('/') != std::string::npos);
169*b0d29bc4SBrooks Davis             return find_latest(test_suite_for_path(id_as_path));
170*b0d29bc4SBrooks Davis         }
171*b0d29bc4SBrooks Davis     }
172*b0d29bc4SBrooks Davis }
173*b0d29bc4SBrooks Davis 
174*b0d29bc4SBrooks Davis 
175*b0d29bc4SBrooks Davis /// Computes the path to a new database for the given test suite.
176*b0d29bc4SBrooks Davis ///
177*b0d29bc4SBrooks Davis /// \param id Identifier of the test suite to create.
178*b0d29bc4SBrooks Davis /// \param root Path to the root of the test suite being run, needed to properly
179*b0d29bc4SBrooks Davis ///     autogenerate the identifiers.
180*b0d29bc4SBrooks Davis ///
181*b0d29bc4SBrooks Davis /// \return Identifier of the created results file, if applicable, and the path
182*b0d29bc4SBrooks Davis /// to such file.
183*b0d29bc4SBrooks Davis layout::results_id_file_pair
new_db(const std::string & id,const fs::path & root)184*b0d29bc4SBrooks Davis layout::new_db(const std::string& id, const fs::path& root)
185*b0d29bc4SBrooks Davis {
186*b0d29bc4SBrooks Davis     std::string generated_id;
187*b0d29bc4SBrooks Davis     optional< fs::path > path;
188*b0d29bc4SBrooks Davis 
189*b0d29bc4SBrooks Davis     if (id == results_auto_create_name) {
190*b0d29bc4SBrooks Davis         generated_id = new_id(test_suite_for_path(root),
191*b0d29bc4SBrooks Davis                               datetime::timestamp::now());
192*b0d29bc4SBrooks Davis         path = query_store_dir() / (F("results.%s.db") % generated_id);
193*b0d29bc4SBrooks Davis         fs::mkdir_p(path.get().branch_path(), 0755);
194*b0d29bc4SBrooks Davis     } else {
195*b0d29bc4SBrooks Davis         path = fs::path(id);
196*b0d29bc4SBrooks Davis     }
197*b0d29bc4SBrooks Davis 
198*b0d29bc4SBrooks Davis     return std::make_pair(generated_id, path.get());
199*b0d29bc4SBrooks Davis }
200*b0d29bc4SBrooks Davis 
201*b0d29bc4SBrooks Davis 
202*b0d29bc4SBrooks Davis /// Computes the path to a new database for the given test suite.
203*b0d29bc4SBrooks Davis ///
204*b0d29bc4SBrooks Davis /// \param root Path to the root of the test suite being run; needed to properly
205*b0d29bc4SBrooks Davis ///     autogenerate the identifiers.
206*b0d29bc4SBrooks Davis /// \param when Timestamp for the test suite being run; needed to properly
207*b0d29bc4SBrooks Davis ///     autogenerate the identifiers.
208*b0d29bc4SBrooks Davis ///
209*b0d29bc4SBrooks Davis /// \return Identifier of the created results file, if applicable, and the path
210*b0d29bc4SBrooks Davis /// to such file.
211*b0d29bc4SBrooks Davis fs::path
new_db_for_migration(const fs::path & root,const datetime::timestamp & when)212*b0d29bc4SBrooks Davis layout::new_db_for_migration(const fs::path& root,
213*b0d29bc4SBrooks Davis                              const datetime::timestamp& when)
214*b0d29bc4SBrooks Davis {
215*b0d29bc4SBrooks Davis     const std::string generated_id = new_id(test_suite_for_path(root), when);
216*b0d29bc4SBrooks Davis     const fs::path path = query_store_dir() / (
217*b0d29bc4SBrooks Davis         F("results.%s.db") % generated_id);
218*b0d29bc4SBrooks Davis     fs::mkdir_p(path.branch_path(), 0755);
219*b0d29bc4SBrooks Davis     return path;
220*b0d29bc4SBrooks Davis }
221*b0d29bc4SBrooks Davis 
222*b0d29bc4SBrooks Davis 
223*b0d29bc4SBrooks Davis /// Gets the path to the store directory.
224*b0d29bc4SBrooks Davis ///
225*b0d29bc4SBrooks Davis /// Note that this function does not create the determined directory.  It is the
226*b0d29bc4SBrooks Davis /// responsibility of the caller to do so.
227*b0d29bc4SBrooks Davis ///
228*b0d29bc4SBrooks Davis /// \return Path to the directory holding all the database files.
229*b0d29bc4SBrooks Davis fs::path
query_store_dir(void)230*b0d29bc4SBrooks Davis layout::query_store_dir(void)
231*b0d29bc4SBrooks Davis {
232*b0d29bc4SBrooks Davis     const optional< fs::path > home = utils::get_home();
233*b0d29bc4SBrooks Davis     if (home) {
234*b0d29bc4SBrooks Davis         const fs::path& home_path = home.get();
235*b0d29bc4SBrooks Davis         if (home_path.is_absolute())
236*b0d29bc4SBrooks Davis             return home_path / ".kyua/store";
237*b0d29bc4SBrooks Davis         else
238*b0d29bc4SBrooks Davis             return home_path.to_absolute() / ".kyua/store";
239*b0d29bc4SBrooks Davis     } else {
240*b0d29bc4SBrooks Davis         LW("HOME not defined; creating store database in current "
241*b0d29bc4SBrooks Davis            "directory");
242*b0d29bc4SBrooks Davis         return fs::current_path();
243*b0d29bc4SBrooks Davis     }
244*b0d29bc4SBrooks Davis }
245*b0d29bc4SBrooks Davis 
246*b0d29bc4SBrooks Davis 
247*b0d29bc4SBrooks Davis /// Returns the test suite name for the current directory.
248*b0d29bc4SBrooks Davis ///
249*b0d29bc4SBrooks Davis /// \return The identifier of the current test suite.
250*b0d29bc4SBrooks Davis std::string
test_suite_for_path(const fs::path & path)251*b0d29bc4SBrooks Davis layout::test_suite_for_path(const fs::path& path)
252*b0d29bc4SBrooks Davis {
253*b0d29bc4SBrooks Davis     std::string test_suite;
254*b0d29bc4SBrooks Davis     if (path.is_absolute())
255*b0d29bc4SBrooks Davis         test_suite = path.str();
256*b0d29bc4SBrooks Davis     else
257*b0d29bc4SBrooks Davis         test_suite = path.to_absolute().str();
258*b0d29bc4SBrooks Davis     PRE(!test_suite.empty() && test_suite[0] == '/');
259*b0d29bc4SBrooks Davis 
260*b0d29bc4SBrooks Davis     std::replace(test_suite.begin(), test_suite.end(), '/', '_');
261*b0d29bc4SBrooks Davis     test_suite.erase(0, 1);
262*b0d29bc4SBrooks Davis 
263*b0d29bc4SBrooks Davis     return test_suite;
264*b0d29bc4SBrooks Davis }
265