2013-06-06 14:01:32 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2013, 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;
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @file lnav_config.cc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2013-06-06 14:10:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-03-02 00:35:30 +00:00
|
|
|
#include <glob.h>
|
2013-06-06 14:01:32 +00:00
|
|
|
#include <sys/stat.h>
|
2015-08-10 04:03:23 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "pcrecpp.h"
|
2013-06-06 14:01:32 +00:00
|
|
|
|
2015-09-15 07:24:56 +00:00
|
|
|
#include "auto_fd.hh"
|
2014-03-02 00:35:30 +00:00
|
|
|
#include "lnav_log.hh"
|
|
|
|
#include "auto_mem.hh"
|
2015-08-10 04:03:23 +00:00
|
|
|
#include "auto_pid.hh"
|
2013-06-06 14:01:32 +00:00
|
|
|
#include "lnav_config.hh"
|
2015-08-10 04:03:23 +00:00
|
|
|
#include "yajlpp.hh"
|
2013-06-06 14:01:32 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-03-02 00:35:30 +00:00
|
|
|
static const int MAX_CRASH_LOG_COUNT = 16;
|
|
|
|
|
2013-06-06 14:01:32 +00:00
|
|
|
string dotlnav_path(const char *sub)
|
|
|
|
{
|
|
|
|
string retval;
|
|
|
|
char * home;
|
|
|
|
|
|
|
|
home = getenv("HOME");
|
|
|
|
if (home) {
|
2013-06-06 14:13:00 +00:00
|
|
|
char hpath[2048];
|
2013-06-06 14:01:32 +00:00
|
|
|
|
|
|
|
snprintf(hpath, sizeof(hpath), "%s/.lnav/%s", home, sub);
|
|
|
|
retval = hpath;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
retval = sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool check_experimental(const char *feature_name)
|
|
|
|
{
|
|
|
|
const char *env_value = getenv("LNAV_EXP");
|
|
|
|
|
2014-03-06 14:58:49 +00:00
|
|
|
require(feature_name != NULL);
|
2013-06-06 14:01:32 +00:00
|
|
|
|
|
|
|
if (env_value && strcasestr(env_value, feature_name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ensure_dotlnav(void)
|
|
|
|
{
|
|
|
|
string path = dotlnav_path("");
|
|
|
|
|
|
|
|
if (!path.empty()) {
|
2015-09-18 03:55:31 +00:00
|
|
|
log_perror(mkdir(path.c_str(), 0755));
|
2013-06-06 14:01:32 +00:00
|
|
|
}
|
2013-07-23 12:55:08 +00:00
|
|
|
|
|
|
|
path = dotlnav_path("formats");
|
|
|
|
if (!path.empty()) {
|
2015-09-18 03:55:31 +00:00
|
|
|
log_perror(mkdir(path.c_str(), 0755));
|
2013-07-23 12:55:08 +00:00
|
|
|
}
|
2014-11-05 17:01:09 +00:00
|
|
|
|
|
|
|
path = dotlnav_path("formats/installed");
|
|
|
|
if (!path.empty()) {
|
2015-09-18 03:55:31 +00:00
|
|
|
log_perror(mkdir(path.c_str(), 0755));
|
2014-11-05 17:01:09 +00:00
|
|
|
}
|
|
|
|
|
2014-03-02 00:35:30 +00:00
|
|
|
path = dotlnav_path("crash");
|
|
|
|
if (!path.empty()) {
|
2015-09-18 03:55:31 +00:00
|
|
|
log_perror(mkdir(path.c_str(), 0755));
|
2014-03-02 00:35:30 +00:00
|
|
|
}
|
|
|
|
lnav_log_crash_dir = strdup(path.c_str());
|
|
|
|
|
|
|
|
{
|
|
|
|
static_root_mem<glob_t, globfree> gl;
|
|
|
|
|
|
|
|
path += "/*";
|
|
|
|
if (glob(path.c_str(), GLOB_NOCHECK, NULL, gl.inout()) == 0) {
|
|
|
|
for (int lpc = 0;
|
2014-03-02 07:40:12 +00:00
|
|
|
lpc < ((int)gl->gl_pathc - MAX_CRASH_LOG_COUNT);
|
2014-03-02 00:35:30 +00:00
|
|
|
lpc++) {
|
2015-09-18 03:55:31 +00:00
|
|
|
log_perror(remove(gl->gl_pathv[lpc]));
|
2014-03-02 00:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 04:22:04 +00:00
|
|
|
path = dotlnav_path("formats/default");
|
|
|
|
if (!path.empty()) {
|
2015-09-18 03:55:31 +00:00
|
|
|
log_perror(mkdir(path.c_str(), 0755));
|
2013-08-29 04:22:04 +00:00
|
|
|
}
|
2013-06-06 14:01:32 +00:00
|
|
|
}
|
2015-08-10 04:03:23 +00:00
|
|
|
|
|
|
|
void install_git_format(const char *repo)
|
|
|
|
{
|
|
|
|
static pcrecpp::RE repo_name_converter("[^\\w]");
|
|
|
|
|
|
|
|
auto_pid git_cmd(fork());
|
|
|
|
|
|
|
|
if (git_cmd.in_child()) {
|
|
|
|
string formats_path = dotlnav_path("formats/");
|
|
|
|
string local_name = repo;
|
|
|
|
string local_path;
|
|
|
|
|
|
|
|
repo_name_converter.GlobalReplace("_", &local_name);
|
|
|
|
local_path = formats_path + local_name;
|
|
|
|
if (access(local_path.c_str(), R_OK) == 0) {
|
|
|
|
printf("Updating format repo: %s\n", repo);
|
|
|
|
chdir(local_path.c_str());
|
|
|
|
execlp("git", "git", "pull", NULL);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
execlp("git", "git", "clone", repo, local_path.c_str(), NULL);
|
|
|
|
}
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
git_cmd.wait_for_child();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_repo_path(yajlpp_parse_context *ypc, const unsigned char *str, size_t len)
|
|
|
|
{
|
|
|
|
string path = string((const char *)str, len);
|
|
|
|
|
|
|
|
install_git_format(path.c_str());
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct json_path_handler format_handlers[] = {
|
|
|
|
json_path_handler("^/format-repos#$", read_repo_path),
|
|
|
|
|
|
|
|
json_path_handler()
|
|
|
|
};
|
|
|
|
|
|
|
|
void install_extra_formats()
|
|
|
|
{
|
|
|
|
string config_root = dotlnav_path("remote-config");
|
2015-09-15 07:24:56 +00:00
|
|
|
auto_fd fd;
|
2015-08-10 04:03:23 +00:00
|
|
|
|
|
|
|
if (access(config_root.c_str(), R_OK) == 0) {
|
|
|
|
char pull_cmd[1024];
|
|
|
|
|
|
|
|
printf("Updating lnav remote config repo...\n");
|
|
|
|
snprintf(pull_cmd, sizeof(pull_cmd),
|
|
|
|
"cd '%s' && git pull",
|
|
|
|
config_root.c_str());
|
|
|
|
system(pull_cmd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char clone_cmd[1024];
|
|
|
|
|
|
|
|
printf("Cloning lnav remote config repo...\n");
|
|
|
|
snprintf(clone_cmd, sizeof(clone_cmd),
|
|
|
|
"git clone https://github.com/tstack/lnav-config.git %s",
|
|
|
|
config_root.c_str());
|
|
|
|
system(clone_cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
string config_json = config_root + "/remote-config.json";
|
|
|
|
if ((fd = open(config_json.c_str(), O_RDONLY)) == -1) {
|
|
|
|
perror("Unable to open remote-config.json");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
yajlpp_parse_context ypc_config(config_root, format_handlers);
|
|
|
|
auto_mem<yajl_handle_t> jhandle(yajl_free);
|
|
|
|
unsigned char buffer[4096];
|
|
|
|
ssize_t rc;
|
|
|
|
|
|
|
|
jhandle = yajl_alloc(&ypc_config.ypc_callbacks, NULL, &ypc_config);
|
|
|
|
yajl_config(jhandle, yajl_allow_comments, 1);
|
|
|
|
while ((rc = read(fd, buffer, sizeof(buffer))) > 0) {
|
|
|
|
if (yajl_parse(jhandle,
|
|
|
|
buffer,
|
|
|
|
rc) != yajl_status_ok) {
|
|
|
|
fprintf(stderr, "Unable to parse remote-config.json -- %s",
|
|
|
|
yajl_get_error(jhandle, 1, buffer, rc));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yajl_complete_parse(jhandle);
|
|
|
|
}
|
|
|
|
}
|