lnav/src/file_vtab.cc

238 lines
8.4 KiB
C++
Raw Normal View History

/**
* 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
* (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>
#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"
#include "file_collection.hh"
2022-03-16 22:38:08 +00:00
#include "file_vtab.cfg.hh"
#include "log_format.hh"
#include "logfile.hh"
2020-04-25 14:32:05 +00:00
#include "session_data.hh"
#include "vtab_module.hh"
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;
2022-03-16 22:38:08 +00:00
static constexpr const char* NAME = "lnav_file";
static constexpr const char* CREATE_STMT = R"(
-- 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.
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.
);
)";
2022-03-16 22:38:08 +00:00
explicit lnav_file(file_collection& fc) : lf_collection(fc) {}
2022-03-16 22:38:08 +00:00
iterator begin()
{
return this->lf_collection.fc_files.begin();
}
2022-03-16 22:38:08 +00:00
iterator end()
{
return this->lf_collection.fc_files.end();
}
2022-03-16 22:38:08 +00:00
int get_column(const cursor& vc, sqlite3_context* ctx, int col)
{
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();
auto format = lf->get_format();
2022-03-16 22:38:08 +00:00
const char* format_name = format != nullptr ? format->get_name().get()
: nullptr;
switch (col) {
case 0:
2019-01-14 03:14:26 +00:00
to_sqlite(ctx, (int64_t) st.st_dev);
break;
case 1:
to_sqlite(ctx, (int64_t) st.st_ino);
break;
case 2:
to_sqlite(ctx, name);
break;
case 3:
2022-03-29 05:00:49 +00:00
to_sqlite(ctx, fmt::to_string(lf->get_text_format()));
break;
case 4:
to_sqlite(
ctx,
fmt::format(FMT_STRING("v1:{}"), lf->get_content_id()));
2021-03-06 00:17:28 +00:00
break;
case 5:
to_sqlite(ctx, format_name);
break;
case 6:
to_sqlite(ctx, (int64_t) lf->size());
break;
case 7: {
auto tv = lf->get_time_offset();
int64_t ms = (tv.tv_sec * 1000LL) + tv.tv_usec / 1000LL;
to_sqlite(ctx, ms);
break;
}
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;
}
default:
ensure(0);
break;
}
return SQLITE_OK;
}
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");
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");
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,
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)
{
auto lf = this->lf_collection.fc_files[rowid];
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
if (iter != this->lf_collection.fc_file_names.end()) {
auto loo = std::move(iter->second);
2020-04-25 14:32:05 +00:00
this->lf_collection.fc_file_names.erase(iter);
2020-04-25 14:32:05 +00:00
loo.loo_include_in_session = true;
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();
}
}
return SQLITE_OK;
};
2022-03-16 22:38:08 +00:00
file_collection& lf_collection;
};
struct injectable_lnav_file : vtab_module<lnav_file> {
using vtab_module<lnav_file>::vtab_module;
using injectable = injectable_lnav_file(file_collection&);
};
2022-03-16 22:38:08 +00:00
static auto file_binder
= injector::bind_multiple<vtab_module_base>().add<injectable_lnav_file>();