2017-03-08 21:51:07 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2017, Timothy Stack
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Timothy Stack nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
2022-03-16 22:38:08 +00:00
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
2017-03-08 21:51:07 +00:00
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2022-03-16 22:38:08 +00:00
|
|
|
#include <unistd.h>
|
2017-03-08 21:51:07 +00:00
|
|
|
|
2021-01-17 06:23:20 +00:00
|
|
|
#include "base/injector.bind.hh"
|
2021-03-06 00:17:28 +00:00
|
|
|
#include "base/lnav.gzip.hh"
|
2019-05-08 12:30:59 +00:00
|
|
|
#include "base/lnav_log.hh"
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "config.h"
|
2021-01-17 06:23:20 +00:00
|
|
|
#include "file_collection.hh"
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "file_vtab.cfg.hh"
|
|
|
|
#include "log_format.hh"
|
2020-11-20 05:36:51 +00:00
|
|
|
#include "logfile.hh"
|
2020-04-25 14:32:05 +00:00
|
|
|
#include "session_data.hh"
|
2017-03-14 13:05:46 +00:00
|
|
|
#include "vtab_module.hh"
|
2017-03-08 21:51:07 +00:00
|
|
|
|
2017-03-14 13:05:46 +00:00
|
|
|
struct lnav_file : public tvt_iterator_cursor<lnav_file> {
|
2022-03-31 15:59:19 +00:00
|
|
|
using iterator = std::vector<std::shared_ptr<logfile>>::iterator;
|
2017-03-14 13:05:46 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static constexpr const char* NAME = "lnav_file";
|
|
|
|
static constexpr const char* CREATE_STMT = R"(
|
2017-03-14 13:05:46 +00:00
|
|
|
-- Access lnav's open file list through this table.
|
|
|
|
CREATE TABLE lnav_file (
|
2019-03-13 05:41:39 +00:00
|
|
|
device integer, -- The device the file is stored on.
|
|
|
|
inode integer, -- The inode for the file on the device.
|
|
|
|
filepath text, -- The path to the file.
|
2021-03-06 00:17:28 +00:00
|
|
|
mimetype text, -- The MIME type for the file.
|
2022-07-21 22:44:30 +00:00
|
|
|
content_id text, -- The hash of some unique content in the file.
|
2019-03-13 05:41:39 +00:00
|
|
|
format text, -- The log file format for the file.
|
|
|
|
lines integer, -- The number of lines in the file.
|
2021-03-06 00:17:28 +00:00
|
|
|
time_offset integer, -- The millisecond offset for timestamps.
|
|
|
|
|
|
|
|
content BLOB HIDDEN -- The contents of the file.
|
2017-03-14 13:05:46 +00:00
|
|
|
);
|
|
|
|
)";
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
explicit lnav_file(file_collection& fc) : lf_collection(fc) {}
|
2017-03-14 13:05:46 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
iterator begin()
|
|
|
|
{
|
2020-11-10 06:17:17 +00:00
|
|
|
return this->lf_collection.fc_files.begin();
|
2017-03-08 21:51:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
iterator end()
|
|
|
|
{
|
2020-11-10 06:17:17 +00:00
|
|
|
return this->lf_collection.fc_files.end();
|
2017-03-08 21:51:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
int get_column(const cursor& vc, sqlite3_context* ctx, int col)
|
|
|
|
{
|
2018-04-03 14:36:09 +00:00
|
|
|
auto lf = *vc.iter;
|
2022-03-16 22:38:08 +00:00
|
|
|
const struct stat& st = lf->get_stat();
|
2022-03-31 15:59:19 +00:00
|
|
|
const auto& name = lf->get_filename();
|
2020-12-01 19:27:03 +00:00
|
|
|
auto format = lf->get_format();
|
2022-03-16 22:38:08 +00:00
|
|
|
const char* format_name = format != nullptr ? format->get_name().get()
|
|
|
|
: nullptr;
|
2017-03-14 13:05:46 +00:00
|
|
|
|
|
|
|
switch (col) {
|
|
|
|
case 0:
|
2019-01-14 03:14:26 +00:00
|
|
|
to_sqlite(ctx, (int64_t) st.st_dev);
|
2017-03-14 13:05:46 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2019-01-14 03:02:50 +00:00
|
|
|
to_sqlite(ctx, (int64_t) st.st_ino);
|
2017-03-14 13:05:46 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2019-01-14 03:02:50 +00:00
|
|
|
to_sqlite(ctx, name);
|
2017-03-14 13:05:46 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2022-03-29 05:00:49 +00:00
|
|
|
to_sqlite(ctx, fmt::to_string(lf->get_text_format()));
|
2017-03-14 13:05:46 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2022-07-21 22:44:30 +00:00
|
|
|
to_sqlite(
|
|
|
|
ctx,
|
|
|
|
fmt::format(FMT_STRING("v1:{}"), lf->get_content_id()));
|
2021-03-06 00:17:28 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2022-07-21 22:44:30 +00:00
|
|
|
to_sqlite(ctx, format_name);
|
|
|
|
break;
|
|
|
|
case 6:
|
2019-01-14 03:02:50 +00:00
|
|
|
to_sqlite(ctx, (int64_t) lf->size());
|
|
|
|
break;
|
2022-07-21 22:44:30 +00:00
|
|
|
case 7: {
|
2019-03-12 14:03:32 +00:00
|
|
|
auto tv = lf->get_time_offset();
|
|
|
|
int64_t ms = (tv.tv_sec * 1000LL) + tv.tv_usec / 1000LL;
|
|
|
|
|
|
|
|
to_sqlite(ctx, ms);
|
|
|
|
break;
|
|
|
|
}
|
2022-07-21 22:44:30 +00:00
|
|
|
case 8: {
|
2021-03-06 00:17:28 +00:00
|
|
|
auto& cfg = injector::get<const file_vtab::config&>();
|
|
|
|
auto lf_stat = lf->get_stat();
|
|
|
|
|
|
|
|
if (lf_stat.st_size > cfg.fvc_max_content_size) {
|
|
|
|
sqlite3_result_error(ctx, "file is too large", -1);
|
|
|
|
} else {
|
|
|
|
auto fd = lf->get_fd();
|
|
|
|
auto_mem<char> buf;
|
2022-03-16 22:38:08 +00:00
|
|
|
buf = (char*) malloc(lf_stat.st_size);
|
2021-03-06 00:17:28 +00:00
|
|
|
auto rc = pread(fd, buf, lf_stat.st_size, 0);
|
|
|
|
|
|
|
|
if (rc == -1) {
|
2022-03-29 05:00:49 +00:00
|
|
|
auto errmsg
|
|
|
|
= fmt::format(FMT_STRING("unable to read file: {}"),
|
|
|
|
strerror(errno));
|
2021-03-06 00:17:28 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
sqlite3_result_error(
|
|
|
|
ctx, errmsg.c_str(), errmsg.length());
|
2021-03-06 00:17:28 +00:00
|
|
|
} else if (rc != lf_stat.st_size) {
|
2022-03-16 22:38:08 +00:00
|
|
|
auto errmsg = fmt::format(
|
2022-03-29 05:00:49 +00:00
|
|
|
FMT_STRING("short read of file: {} < {}"),
|
|
|
|
rc,
|
|
|
|
lf_stat.st_size);
|
2021-03-06 00:17:28 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
sqlite3_result_error(
|
|
|
|
ctx, errmsg.c_str(), errmsg.length());
|
2021-03-06 00:17:28 +00:00
|
|
|
} else if (lnav::gzip::is_gzipped(buf, rc)) {
|
|
|
|
lnav::gzip::uncompress(lf->get_unique_path(), buf, rc)
|
|
|
|
.then([ctx](auto uncomp) {
|
|
|
|
auto pair = uncomp.release();
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
sqlite3_result_blob64(
|
|
|
|
ctx, pair.first, pair.second, free);
|
2021-03-06 00:17:28 +00:00
|
|
|
})
|
|
|
|
.otherwise([ctx](auto msg) {
|
2022-03-16 22:38:08 +00:00
|
|
|
sqlite3_result_error(
|
|
|
|
ctx, msg.c_str(), msg.size());
|
2021-03-06 00:17:28 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
sqlite3_result_blob64(ctx, buf.release(), rc, free);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-01-14 03:02:50 +00:00
|
|
|
default:
|
|
|
|
ensure(0);
|
2017-03-14 13:05:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SQLITE_OK;
|
2017-03-08 21:51:07 +00:00
|
|
|
}
|
2019-03-12 14:03:32 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
int delete_row(sqlite3_vtab* vt, sqlite3_int64 rowid)
|
|
|
|
{
|
|
|
|
vt->zErrMsg = sqlite3_mprintf("Rows cannot be deleted from this table");
|
2019-03-12 14:03:32 +00:00
|
|
|
return SQLITE_ERROR;
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
int insert_row(sqlite3_vtab* tab, sqlite3_int64& rowid_out)
|
|
|
|
{
|
|
|
|
tab->zErrMsg
|
|
|
|
= sqlite3_mprintf("Rows cannot be inserted into this table");
|
2019-03-12 14:03:32 +00:00
|
|
|
return SQLITE_ERROR;
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
int update_row(sqlite3_vtab* tab,
|
|
|
|
sqlite3_int64& rowid,
|
2019-03-13 05:27:41 +00:00
|
|
|
int64_t device,
|
|
|
|
int64_t inode,
|
2020-04-25 14:32:05 +00:00
|
|
|
std::string path,
|
2022-03-16 22:38:08 +00:00
|
|
|
const char* text_format,
|
2022-07-21 22:44:30 +00:00
|
|
|
const char* content_id,
|
2022-03-16 22:38:08 +00:00
|
|
|
const char* format,
|
2019-03-13 05:27:41 +00:00
|
|
|
int64_t lines,
|
2021-03-06 00:17:28 +00:00
|
|
|
int64_t time_offset,
|
2022-03-16 22:38:08 +00:00
|
|
|
const char* content)
|
|
|
|
{
|
2020-11-10 06:17:17 +00:00
|
|
|
auto lf = this->lf_collection.fc_files[rowid];
|
2019-03-12 14:03:32 +00:00
|
|
|
struct timeval tv = {
|
|
|
|
(int) (time_offset / 1000LL),
|
|
|
|
(int) (time_offset / (1000LL * 1000LL)),
|
|
|
|
};
|
|
|
|
|
|
|
|
lf->adjust_content_time(0, tv, true);
|
|
|
|
|
2020-04-25 14:32:05 +00:00
|
|
|
if (path != lf->get_filename()) {
|
|
|
|
if (lf->is_valid_filename()) {
|
|
|
|
throw sqlite_func_error(
|
|
|
|
"real file paths cannot be updated, only symbolic ones");
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
auto iter
|
|
|
|
= this->lf_collection.fc_file_names.find(lf->get_filename());
|
2020-04-25 14:32:05 +00:00
|
|
|
|
2020-11-10 06:17:17 +00:00
|
|
|
if (iter != this->lf_collection.fc_file_names.end()) {
|
|
|
|
auto loo = std::move(iter->second);
|
2020-04-25 14:32:05 +00:00
|
|
|
|
2020-11-10 06:17:17 +00:00
|
|
|
this->lf_collection.fc_file_names.erase(iter);
|
2020-04-25 14:32:05 +00:00
|
|
|
|
|
|
|
loo.loo_include_in_session = true;
|
2020-11-10 06:17:17 +00:00
|
|
|
this->lf_collection.fc_file_names[path] = std::move(loo);
|
2020-04-25 14:32:05 +00:00
|
|
|
lf->set_filename(path);
|
|
|
|
|
|
|
|
init_session();
|
|
|
|
load_session();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 14:03:32 +00:00
|
|
|
return SQLITE_OK;
|
|
|
|
};
|
2020-11-10 06:17:17 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
file_collection& lf_collection;
|
2017-03-14 13:05:46 +00:00
|
|
|
};
|
2017-03-08 21:51:07 +00:00
|
|
|
|
2021-01-17 06:23:20 +00:00
|
|
|
struct injectable_lnav_file : vtab_module<lnav_file> {
|
|
|
|
using vtab_module<lnav_file>::vtab_module;
|
|
|
|
using injectable = injectable_lnav_file(file_collection&);
|
|
|
|
};
|
2017-03-08 21:51:07 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static auto file_binder
|
|
|
|
= injector::bind_multiple<vtab_module_base>().add<injectable_lnav_file>();
|