1 /**
2  * Copyright (c) 2020, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * @file file_collection.hh
30  */
31 
32 #ifndef lnav_file_collection_hh
33 #define lnav_file_collection_hh
34 
35 #include <map>
36 #include <set>
37 #include <list>
38 #include <string>
39 #include <utility>
40 
41 #include "safe/safe.h"
42 
43 #include "base/future_util.hh"
44 #include "logfile_fwd.hh"
45 #include "archive_manager.hh"
46 #include "file_format.hh"
47 #include "tailer/tailer.looper.hh"
48 
49 struct tailer_progress {
50     std::string tp_message;
51 };
52 
53 struct scan_progress {
54     std::list<archive_manager::extract_progress> sp_extractions;
55     std::map<std::string, tailer_progress> sp_tailers;
56 };
57 
58 using safe_scan_progress = safe::Safe<scan_progress>;
59 
60 struct other_file_descriptor {
61     file_format_t ofd_format;
62     std::string ofd_description;
63 
other_file_descriptorother_file_descriptor64     other_file_descriptor(file_format_t format = file_format_t::FF_UNKNOWN,
65                           std::string description = "")
66         : ofd_format(format), ofd_description(std::move(description)) {}
67 };
68 
69 struct file_error_info {
70     const time_t fei_mtime;
71     const std::string fei_description;
72 };
73 
74 struct file_collection {
75     bool fc_invalidate_merge{false};
76 
77     bool fc_recursive{false};
78     bool fc_rotated{false};
79 
80     std::map<std::string, file_error_info> fc_name_to_errors;
81     std::map<std::string, logfile_open_options> fc_file_names;
82     std::vector<std::shared_ptr<logfile>> fc_files;
83     int fc_files_generation{0};
84     std::vector<std::pair<std::shared_ptr<logfile>, std::string>>
85         fc_renamed_files;
86     std::set<std::string> fc_closed_files;
87     std::map<std::string, other_file_descriptor> fc_other_files;
88     std::set<std::string> fc_synced_files;
89     std::shared_ptr<safe_scan_progress> fc_progress;
90     std::vector<struct stat> fc_new_stats;
91     size_t fc_largest_path_length{0};
92 
file_collectionfile_collection93     file_collection()
94         : fc_progress(std::make_shared<safe::Safe<scan_progress>>())
95     {}
96 
clearfile_collection97     void clear()
98     {
99         this->fc_name_to_errors.clear();
100         this->fc_file_names.clear();
101         this->fc_files.clear();
102         this->fc_closed_files.clear();
103         this->fc_other_files.clear();
104         this->fc_new_stats.clear();
105     }
106 
107     file_collection rescan_files(bool required = false);
108 
109     void
110     expand_filename(lnav::futures::future_queue<file_collection> &fq,
111                     const std::string &path,
112                     logfile_open_options &loo,
113                     bool required);
114 
115     std::future<file_collection>
116     watch_logfile(const std::string &filename, logfile_open_options &loo,
117                   bool required);
118 
119     void merge(const file_collection &other);
120 
121     void close_files(const std::vector<std::shared_ptr<logfile>> &files);
122 
123     void regenerate_unique_file_names();
124 };
125 
126 
127 #endif
128