[session] cleanup the session directory

pull/69/head
Timothy Stack 11 years ago
parent fe1e82eeaf
commit 84dad8e2e9

@ -1025,7 +1025,7 @@ static void handle_paging_key(int ch)
tc->horiz_shift(tc->get_top(),
tc->get_bottom(),
tc->get_left(),
"(search",
"$search",
range);
if (range.second != INT_MAX) {
tc->set_left(range.second);
@ -1046,7 +1046,7 @@ static void handle_paging_key(int ch)
tc->horiz_shift(tc->get_top(),
tc->get_bottom(),
tc->get_left(),
"(search",
"$search",
range);
if (range.first != -1) {
tc->set_left(range.first);
@ -1717,18 +1717,16 @@ int sql_callback(sqlite3_stmt *stmt)
static void rl_search(void *dummy, readline_curses *rc)
{
static string last_search[LNV__MAX];
string name;
switch (lnav_data.ld_mode) {
case LNM_SEARCH:
name = "(search";
name = "$search";
break;
case LNM_CAPTURE:
assert(0);
name = "(capture";
name = "$capture";
break;
case LNM_COMMAND:
@ -1770,7 +1768,8 @@ static void rl_search(void *dummy, readline_curses *rc)
int index = (tc - lnav_data.ld_views);
auto_ptr<grep_highlighter> &gc = lnav_data.ld_search_child[index];
if ((gc.get() == NULL) || (rc->get_value() != last_search[index])) {
if ((gc.get() == NULL) ||
(rc->get_value() != lnav_data.ld_last_search[index])) {
const char *errptr;
pcre * code;
int eoff;
@ -1822,7 +1821,7 @@ static void rl_search(void *dummy, readline_curses *rc)
auto_ptr<grep_highlighter> gh(new grep_highlighter(gp, name, hm));
gc = gh;
last_search[index] = rc->get_value();
lnav_data.ld_last_search[index] = rc->get_value();
}
}
}

@ -170,6 +170,7 @@ struct _lnav_data {
fd_set ld_read_fds;
std::auto_ptr<grep_highlighter> ld_grep_child[LG__MAX];
std::string ld_last_search[LNV__MAX];
log_vtab_manager * ld_vtab_manager;
auto_mem<sqlite3, sqlite_close_wrapper> ld_db;

@ -50,7 +50,10 @@
using namespace std;
static const size_t MAX_SESSIONS = 16;
static const size_t MAX_SESSIONS = 8;
static const size_t MAX_SESSION_FILE_COUNT = 256;
typedef std::vector<std::pair<int, string> > timestamped_list_t;
static string bookmark_file_name(const string &name)
{
@ -69,7 +72,7 @@ static string bookmark_file_name(const string &name)
static string latest_bookmark_file(const string &name)
{
std::vector<std::pair<int, string> > file_names;
timestamped_list_t file_names;
static_root_mem<glob_t, globfree> file_list;
string mark_file_pattern;
char mark_base_name[256];
@ -104,6 +107,101 @@ static string latest_bookmark_file(const string &name)
return file_names.back().second;
}
struct session_file_info {
session_file_info(int timestamp,
const string &id,
const string &path)
: sfi_timestamp(timestamp), sfi_id(id), sfi_path(path) {
};
bool operator<(const session_file_info &other) const {
if (this->sfi_timestamp < other.sfi_timestamp)
return true;
if (this->sfi_path < other.sfi_path)
return true;
return false;
};
int sfi_timestamp;
string sfi_id;
string sfi_path;
};
static void cleanup_session_data(void)
{
static_root_mem<glob_t, globfree> session_file_list;
std::list<struct session_file_info> session_info_list;
map<string, int> session_count;
string session_file_pattern;
session_file_pattern = dotlnav_path("*-*.ts*.json");
if (glob(session_file_pattern.c_str(),
0,
NULL,
session_file_list.inout()) == 0) {
for (size_t lpc = 0; lpc < session_file_list->gl_pathc; lpc++) {
const char *path = session_file_list->gl_pathv[lpc];
char hash_id[64];
int timestamp;
const char *base;
base = strrchr(path, '/') + 1;
if (sscanf(base, "file-%63[^.].ts%d.json",
hash_id, &timestamp) == 2) {
session_count[hash_id] += 1;
session_info_list.push_back(session_file_info(
timestamp, hash_id, path));
}
if (sscanf(base,
"view-info-%63[^.].ts%d.ppid%*d.json",
hash_id,
&timestamp) == 2) {
session_count[hash_id] += 1;
session_info_list.push_back(session_file_info(
timestamp, hash_id, path));
}
}
}
session_info_list.sort();
while (session_info_list.size() > MAX_SESSION_FILE_COUNT) {
const session_file_info &front = session_info_list.front();
if (session_count[front.sfi_id] == 1) {
session_info_list.splice(session_info_list.end(),
session_info_list,
session_info_list.begin());
}
else {
if (remove(front.sfi_path.c_str()) != 0) {
fprintf(stderr,
"error: Unable to remove session file: %s -- %s\n",
front.sfi_path.c_str(),
strerror(errno));
}
session_count[front.sfi_id] -= 1;
session_info_list.pop_front();
}
}
session_info_list.sort();
while (session_info_list.size() > MAX_SESSION_FILE_COUNT) {
const session_file_info &front = session_info_list.front();
if (remove(front.sfi_path.c_str()) != 0) {
fprintf(stderr,
"error: Unable to remove session file: %s -- %s\n",
front.sfi_path.c_str(),
strerror(errno));
}
session_count[front.sfi_id] -= 1;
session_info_list.pop_front();
}
}
void init_session(void)
{
byte_array<SHA_DIGEST_LENGTH> hash;
@ -131,6 +229,8 @@ void scan_sessions(void)
string old_session_name;
int index;
cleanup_session_data();
if (lnav_data.ld_session_file_index >= 0 &&
lnav_data.ld_session_file_index < (int)session_file_names.size()) {
iter = session_file_names.begin();
@ -168,7 +268,14 @@ void scan_sessions(void)
session_file_names.sort();
while (session_file_names.size() > MAX_SESSIONS) {
unlink(session_file_names.front().second.c_str());
const std::string &name = session_file_names.front().second;
if (remove(name.c_str()) != 0) {
fprintf(stderr,
"error: Unable to remove session: %s -- %s\n",
name.c_str(),
strerror(errno));
}
session_file_names.pop_front();
}
@ -496,6 +603,9 @@ void save_session(void)
view_map.gen(-1);
else
view_map.gen((long long)tc.get_top());
view_map.gen("search");
view_map.gen(lnav_data.ld_last_search[lpc]);
}
}

@ -57,7 +57,6 @@ int yajlpp_parse_context::map_key(void *ctx,
void yajlpp_parse_context::update_callbacks(void)
{
pcre_input pi(this->ypc_path);
bool found = false;
this->ypc_callbacks = DEFAULT_CALLBACKS;
@ -70,7 +69,6 @@ void yajlpp_parse_context::update_callbacks(void)
this->ypc_callbacks.yajl_integer = jph.jph_callbacks.yajl_integer;
this->ypc_callbacks.yajl_double = jph.jph_callbacks.yajl_double;
this->ypc_callbacks.yajl_string = jph.jph_callbacks.yajl_string;
found = true;
break;
}
}

Loading…
Cancel
Save