1 /**
2 * Copyright (c) 2007-2012, 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 lnav_util.hh
30 *
31 * Dumping ground for useful functions with no other home.
32 */
33
34 #ifndef lnav_util_hh
35 #define lnav_util_hh
36
37 #include <time.h>
38 #include <sys/time.h>
39 #include <poll.h>
40 #include <sys/types.h>
41 #include <fcntl.h>
42 #include <sys/resource.h>
43
44 #include "spookyhash/SpookyV2.h"
45
46 #include <future>
47 #include <string>
48 #include <vector>
49 #include <numeric>
50 #include <type_traits>
51
52 #include "ptimec.hh"
53 #include "byte_array.hh"
54 #include "optional.hpp"
55 #include "base/result.h"
56 #include "base/intern_string.hh"
57 #include "fmt/format.h"
58 #include "ghc/filesystem.hpp"
59
60 #if SIZEOF_OFF_T == 8
61 #define FORMAT_OFF_T "%lld"
62 #elif SIZEOF_OFF_T == 4
63 #define FORMAT_OFF_T "%ld"
64 #else
65 #error "off_t has unhandled size..."
66 #endif
67
68 class hasher {
69 public:
hasher()70 hasher() {
71 this->h_context.Init(0, 0);
72 }
73
update(const std::string & str)74 hasher &update(const std::string &str) {
75 this->h_context.Update(str.data(), str.length());
76
77 return *this;
78 }
79
update(const string_fragment & str)80 hasher &update(const string_fragment &str) {
81 this->h_context.Update(str.data(), str.length());
82
83 return *this;
84 }
85
update(const char * bits,size_t len)86 hasher &update(const char *bits, size_t len) {
87 this->h_context.Update(bits, len);
88
89 return *this;
90 }
91
92 template<typename T,
93 typename = std::enable_if<std::is_arithmetic<T>::value>>
update(T value)94 hasher &update(T value) {
95 this->h_context.Update(&value, sizeof(value));
96
97 return *this;
98 }
99
to_string()100 std::string to_string() {
101 byte_array<2, uint64> bits;
102
103 this->h_context.Final(bits.out(0), bits.out(1));
104 return bits.to_string();
105 }
106 private:
107 SpookyHash h_context;
108 };
109
110 bool change_to_parent_dir();
111
112 bool next_format(const char * const fmt[], int &index, int &locked_index);
113
114 namespace std {
to_string(const string & s)115 inline string to_string(const string &s) { return s; }
to_string(const char * s)116 inline string to_string(const char *s) { return s; }
117 }
118
is_glob(const char * fn)119 inline bool is_glob(const char *fn)
120 {
121 return (strchr(fn, '*') != nullptr ||
122 strchr(fn, '?') != nullptr ||
123 strchr(fn, '[') != nullptr);
124 };
125
126 std::string build_path(const std::vector<ghc::filesystem::path> &paths);
127
128 Result<std::string, std::string> read_file(const ghc::filesystem::path &path);
129
pollfd_revents(const std::vector<struct pollfd> & pollfds,int fd)130 inline short pollfd_revents(const std::vector<struct pollfd> &pollfds, int fd) {
131 auto iter = std::find_if(pollfds.begin(), pollfds.end(), [fd](const auto& entry) {
132 return entry.fd == fd;
133 });
134
135 if (iter == pollfds.end()) {
136 return 0;
137 }
138
139 return iter->revents;
140 }
141
pollfd_ready(const std::vector<struct pollfd> & pollfds,int fd,short events=POLLIN|POLLHUP)142 inline bool pollfd_ready(const std::vector<struct pollfd> &pollfds, int fd,
143 short events = POLLIN | POLLHUP)
144 {
145 return std::any_of(pollfds.begin(), pollfds.end(),
146 [fd, events](const auto &entry) {
147 return entry.fd == fd && entry.revents & events;
148 });
149 };
150
rusagesub(const struct rusage & left,const struct rusage & right,struct rusage & diff_out)151 inline void rusagesub(const struct rusage &left, const struct rusage &right, struct rusage &diff_out)
152 {
153 timersub(&left.ru_utime, &right.ru_utime, &diff_out.ru_utime);
154 timersub(&left.ru_stime, &right.ru_stime, &diff_out.ru_stime);
155 diff_out.ru_maxrss = left.ru_maxrss - right.ru_maxrss;
156 diff_out.ru_ixrss = left.ru_ixrss - right.ru_ixrss;
157 diff_out.ru_idrss = left.ru_idrss - right.ru_idrss;
158 diff_out.ru_isrss = left.ru_isrss - right.ru_isrss;
159 diff_out.ru_minflt = left.ru_minflt - right.ru_minflt;
160 diff_out.ru_majflt = left.ru_majflt - right.ru_majflt;
161 diff_out.ru_nswap = left.ru_nswap - right.ru_nswap;
162 diff_out.ru_inblock = left.ru_inblock - right.ru_inblock;
163 diff_out.ru_oublock = left.ru_oublock - right.ru_oublock;
164 diff_out.ru_msgsnd = left.ru_msgsnd - right.ru_msgsnd;
165 diff_out.ru_msgrcv = left.ru_msgrcv - right.ru_msgrcv;
166 diff_out.ru_nvcsw = left.ru_nvcsw - right.ru_nvcsw;
167 diff_out.ru_nivcsw = left.ru_nivcsw - right.ru_nivcsw;
168 }
169
rusageadd(const struct rusage & left,const struct rusage & right,struct rusage & diff_out)170 inline void rusageadd(const struct rusage &left, const struct rusage &right, struct rusage &diff_out)
171 {
172 timeradd(&left.ru_utime, &right.ru_utime, &diff_out.ru_utime);
173 timeradd(&left.ru_stime, &right.ru_stime, &diff_out.ru_stime);
174 diff_out.ru_maxrss = left.ru_maxrss + right.ru_maxrss;
175 diff_out.ru_ixrss = left.ru_ixrss + right.ru_ixrss;
176 diff_out.ru_idrss = left.ru_idrss + right.ru_idrss;
177 diff_out.ru_isrss = left.ru_isrss + right.ru_isrss;
178 diff_out.ru_minflt = left.ru_minflt + right.ru_minflt;
179 diff_out.ru_majflt = left.ru_majflt + right.ru_majflt;
180 diff_out.ru_nswap = left.ru_nswap + right.ru_nswap;
181 diff_out.ru_inblock = left.ru_inblock + right.ru_inblock;
182 diff_out.ru_oublock = left.ru_oublock + right.ru_oublock;
183 diff_out.ru_msgsnd = left.ru_msgsnd + right.ru_msgsnd;
184 diff_out.ru_msgrcv = left.ru_msgrcv + right.ru_msgrcv;
185 diff_out.ru_nvcsw = left.ru_nvcsw + right.ru_nvcsw;
186 diff_out.ru_nivcsw = left.ru_nivcsw + right.ru_nivcsw;
187 }
188
statp(const ghc::filesystem::path & path,struct stat * buf)189 inline int statp(const ghc::filesystem::path &path, struct stat *buf) {
190 return stat(path.c_str(), buf);
191 }
192
openp(const ghc::filesystem::path & path,int flags)193 inline int openp(const ghc::filesystem::path &path, int flags) {
194 return open(path.c_str(), flags);
195 }
196
openp(const ghc::filesystem::path & path,int flags,mode_t mode)197 inline int openp(const ghc::filesystem::path &path, int flags, mode_t mode) {
198 return open(path.c_str(), flags, mode);
199 }
200
201 Result<std::pair<ghc::filesystem::path, int>, std::string>
202 open_temp_file(const ghc::filesystem::path &pattern);
203
204 bool is_dev_null(const struct stat &st);
205 bool is_dev_null(int fd);
206
207 template<typename A>
208 struct final_action { // slightly simplified
209 A act;
final_actionfinal_action210 final_action(A a) :act{a} {}
~final_actionfinal_action211 ~final_action() { act(); }
212 };
213
214 template<typename A>
finally(A act)215 final_action<A> finally(A act) // deduce action type
216 {
217 return final_action<A>{act};
218 }
219
220 std::string ok_prefix(std::string msg);
221 std::string err_prefix(std::string msg);
222 Result<std::string, std::string> err_to_ok(std::string msg);
223
224 #endif
225