2016-04-18 13:47:37 +08:00
|
|
|
// History functions, part of the user interface.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
#include <ctype.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <errno.h>
|
2006-10-21 06:36:49 +08:00
|
|
|
#include <fcntl.h>
|
2016-05-04 07:23:30 +08:00
|
|
|
#include <pthread.h>
|
2017-02-13 12:24:22 +08:00
|
|
|
#include <stddef.h>
|
2016-04-18 13:47:37 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2006-10-21 06:36:49 +08:00
|
|
|
#include <string.h>
|
2016-12-16 13:50:27 +08:00
|
|
|
// We need the sys/file.h for the flock() declaration on Linux but not OS X.
|
|
|
|
#include <sys/file.h> // IWYU pragma: keep
|
2016-04-18 13:47:37 +08:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
2006-10-21 06:36:49 +08:00
|
|
|
#include <time.h>
|
2016-04-18 13:47:37 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <wctype.h>
|
2017-02-11 10:47:02 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
#include <algorithm>
|
2017-02-07 02:51:27 +08:00
|
|
|
#include <atomic>
|
2016-12-16 13:50:27 +08:00
|
|
|
#include <cwchar>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <functional>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <iterator>
|
2016-04-18 13:47:37 +08:00
|
|
|
#include <map>
|
2017-02-07 01:55:46 +08:00
|
|
|
#include <numeric>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <type_traits>
|
2017-08-20 04:29:52 +08:00
|
|
|
#include <unordered_set>
|
2016-04-21 14:00:54 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "common.h"
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "env.h"
|
2016-04-18 13:47:37 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "history.h"
|
2016-07-14 13:33:50 +08:00
|
|
|
#include "io.h"
|
2016-04-18 13:47:37 +08:00
|
|
|
#include "iothread.h"
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "lru.h"
|
|
|
|
#include "parse_constants.h"
|
2016-04-18 13:47:37 +08:00
|
|
|
#include "parse_tree.h"
|
2017-05-21 12:03:31 +08:00
|
|
|
#include "parse_util.h"
|
2016-04-18 13:47:37 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "reader.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2012-02-16 16:24:27 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Our history format is intended to be valid YAML. Here it is:
|
|
|
|
//
|
|
|
|
// - cmd: ssh blah blah blah
|
|
|
|
// when: 2348237
|
|
|
|
// paths:
|
|
|
|
// - /path/to/something
|
|
|
|
// - /path/to/something_else
|
|
|
|
//
|
|
|
|
// Newlines are replaced by \n. Backslashes are replaced by \\.
|
|
|
|
|
2017-07-01 11:24:55 +08:00
|
|
|
// This is the history session ID we use by default if the user has not set env var FISH_HISTORY.
|
|
|
|
#define DFLT_FISH_HISTORY_SESSION_ID L"fish"
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// When we rewrite the history, the number of items we keep.
|
2012-02-06 12:54:41 +08:00
|
|
|
#define HISTORY_SAVE_MAX (1024 * 256)
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Default buffer size for flushing to the history file.
|
2017-02-07 02:36:46 +08:00
|
|
|
#define HISTORY_OUTPUT_BUFFER_SIZE (64 * 1024)
|
|
|
|
|
|
|
|
// The file access mode we use for creating history files
|
|
|
|
static constexpr int history_file_mode = 0644;
|
|
|
|
|
|
|
|
// How many times we retry to save
|
|
|
|
// Saving may fail if the file is modified in between our opening
|
|
|
|
// the file and taking the lock
|
|
|
|
static constexpr int max_save_tries = 1024;
|
2013-04-28 06:21:14 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
namespace {
|
2016-03-19 06:14:16 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Helper class for certain output. This is basically a string that allows us to ensure we only
|
|
|
|
/// flush at record boundaries, and avoids the copying of ostringstream. Have you ever tried to
|
|
|
|
/// implement your own streambuf? Total insanity.
|
2016-05-06 06:09:31 +08:00
|
|
|
static size_t safe_strlen(const char *s) { return s ? strlen(s) : 0; }
|
2016-04-18 13:47:37 +08:00
|
|
|
class history_output_buffer_t {
|
2013-04-28 06:21:14 +08:00
|
|
|
std::vector<char> buffer;
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
public:
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Add a bit more to HISTORY_OUTPUT_BUFFER_SIZE because we flush once we've exceeded that size.
|
2017-02-07 01:55:46 +08:00
|
|
|
explicit history_output_buffer_t(size_t reserve_amt = HISTORY_OUTPUT_BUFFER_SIZE + 128) {
|
|
|
|
buffer.reserve(reserve_amt);
|
|
|
|
}
|
2013-04-28 06:21:14 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Append one or more strings.
|
2016-04-18 13:47:37 +08:00
|
|
|
void append(const char *s1, const char *s2 = NULL, const char *s3 = NULL) {
|
2017-02-07 01:55:46 +08:00
|
|
|
constexpr size_t ptr_count = 3;
|
|
|
|
const char *ptrs[ptr_count] = {s1, s2, s3};
|
|
|
|
size_t lengths[ptr_count] = {safe_strlen(s1), safe_strlen(s2), safe_strlen(s3)};
|
2013-04-28 06:21:14 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Determine the additional size we'll need.
|
2017-02-07 01:55:46 +08:00
|
|
|
size_t additional_length = std::accumulate(std::begin(lengths), std::end(lengths), 0);
|
|
|
|
buffer.reserve(buffer.size() + additional_length);
|
|
|
|
|
|
|
|
// Append
|
2017-02-08 13:52:35 +08:00
|
|
|
for (size_t i = 0; i < ptr_count; i++) {
|
2017-02-07 01:55:46 +08:00
|
|
|
if (lengths[i] > 0) {
|
|
|
|
buffer.insert(buffer.end(), ptrs[i], ptrs[i] + lengths[i]);
|
|
|
|
}
|
2013-04-28 06:21:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Output to a given fd, resetting our buffer. Returns true on success, false on error.
|
2016-04-18 13:47:37 +08:00
|
|
|
bool flush_to_fd(int fd) {
|
2017-02-07 01:55:46 +08:00
|
|
|
if (buffer.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool result = write_loop(fd, &buffer.at(0), buffer.size()) >= 0;
|
|
|
|
buffer.clear();
|
2013-04-28 06:21:14 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Return how much data we've accumulated.
|
2017-02-07 01:55:46 +08:00
|
|
|
size_t output_size() const { return buffer.size(); }
|
2013-04-28 06:21:14 +08:00
|
|
|
};
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
class time_profiler_t {
|
2012-03-02 06:56:34 +08:00
|
|
|
const char *what;
|
|
|
|
double start;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
public:
|
|
|
|
explicit time_profiler_t(const char *w) {
|
2016-05-06 06:09:31 +08:00
|
|
|
what = w;
|
|
|
|
start = timef();
|
2012-03-02 06:56:34 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
~time_profiler_t() {
|
2016-05-06 06:09:31 +08:00
|
|
|
double end = timef();
|
|
|
|
debug(2, "%s: %.0f ms", what, (end - start) * 1000);
|
2012-03-02 06:56:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-16 13:50:27 +08:00
|
|
|
/// Lock the history file.
|
|
|
|
/// Returns true on success, false on failure.
|
|
|
|
static bool history_file_lock(int fd, int lock_type) {
|
2017-02-07 02:00:34 +08:00
|
|
|
static std::atomic<bool> do_locking(true);
|
2016-12-16 13:50:27 +08:00
|
|
|
if (!do_locking) return false;
|
|
|
|
|
|
|
|
double start_time = timef();
|
|
|
|
int retval = flock(fd, lock_type);
|
|
|
|
double duration = timef() - start_time;
|
|
|
|
if (duration > 0.25) {
|
|
|
|
debug(1, _(L"Locking the history file took too long (%.3f seconds)."), duration);
|
2017-02-07 02:00:34 +08:00
|
|
|
// we've decided to stop doing any locking behavior
|
|
|
|
// but make sure we don't leave the file locked!
|
|
|
|
if (retval == 0 && lock_type != LOCK_UN) {
|
|
|
|
flock(fd, LOCK_UN);
|
|
|
|
}
|
2016-12-16 13:50:27 +08:00
|
|
|
do_locking = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return retval != -1;
|
2012-12-03 08:39:35 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Our LRU cache is used for restricting the amount of history we have, and limiting how long we
|
|
|
|
/// order it.
|
2017-01-28 04:18:16 +08:00
|
|
|
class history_lru_item_t {
|
2016-04-18 13:47:37 +08:00
|
|
|
public:
|
2017-01-28 04:18:16 +08:00
|
|
|
wcstring text;
|
2012-02-06 12:54:41 +08:00
|
|
|
time_t timestamp;
|
2012-02-16 16:24:27 +08:00
|
|
|
path_list_t required_paths;
|
2017-01-28 04:18:16 +08:00
|
|
|
explicit history_lru_item_t(const history_item_t &item)
|
|
|
|
: text(item.str()),
|
2016-04-18 13:47:37 +08:00
|
|
|
timestamp(item.timestamp()),
|
|
|
|
required_paths(item.get_required_paths()) {}
|
2012-02-06 12:54:41 +08:00
|
|
|
};
|
|
|
|
|
2017-01-28 04:18:16 +08:00
|
|
|
class history_lru_cache_t : public lru_cache_t<history_lru_cache_t, history_lru_item_t> {
|
|
|
|
typedef lru_cache_t<history_lru_cache_t, history_lru_item_t> super;
|
|
|
|
|
2017-01-30 09:56:03 +08:00
|
|
|
public:
|
2017-01-28 04:18:16 +08:00
|
|
|
using super::super;
|
2012-02-06 12:54:41 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Function to add a history item.
|
2016-04-18 13:47:37 +08:00
|
|
|
void add_item(const history_item_t &item) {
|
|
|
|
// Skip empty items.
|
|
|
|
if (item.empty()) return;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// See if it's in the cache. If it is, update the timestamp. If not, we create a new node
|
|
|
|
// and add it. Note that calling get_node promotes the node to the front.
|
2017-01-28 04:18:16 +08:00
|
|
|
wcstring key = item.str();
|
|
|
|
history_lru_item_t *node = this->get(key);
|
2016-05-06 06:09:31 +08:00
|
|
|
if (node == NULL) {
|
2017-01-28 04:18:16 +08:00
|
|
|
this->insert(std::move(key), history_lru_item_t(item));
|
2016-05-06 06:09:31 +08:00
|
|
|
} else {
|
|
|
|
node->timestamp = std::max(node->timestamp, item.timestamp());
|
|
|
|
// What to do about paths here? Let's just ignore them.
|
2012-02-06 12:54:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-01-30 05:21:50 +08:00
|
|
|
// The set of histories
|
|
|
|
// Note that histories are currently immortal
|
2016-04-18 13:47:37 +08:00
|
|
|
class history_collection_t {
|
2017-01-30 05:21:50 +08:00
|
|
|
owning_lock<std::map<wcstring, std::unique_ptr<history_t>>> histories;
|
2016-03-19 06:14:16 +08:00
|
|
|
|
2017-02-08 13:52:35 +08:00
|
|
|
public:
|
2017-01-30 05:21:50 +08:00
|
|
|
history_t &get_creating(const wcstring &name);
|
2016-03-19 06:14:16 +08:00
|
|
|
void save();
|
|
|
|
};
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
} // anonymous namespace
|
2016-03-19 06:14:16 +08:00
|
|
|
|
|
|
|
static history_collection_t histories;
|
2012-02-06 08:42:24 +08:00
|
|
|
|
|
|
|
static wcstring history_filename(const wcstring &name, const wcstring &suffix);
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Replaces newlines with a literal backslash followed by an n, and replaces backslashes with two
|
|
|
|
/// backslashes.
|
2014-07-30 05:41:21 +08:00
|
|
|
static void escape_yaml(std::string *str);
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Inverse of escape_yaml.
|
2014-07-30 05:41:21 +08:00
|
|
|
static void unescape_yaml(std::string *str);
|
2012-02-06 12:54:41 +08:00
|
|
|
|
2016-05-06 06:09:31 +08:00
|
|
|
/// Read one line, stripping off any newline, and updating cursor. Note that our input string is NOT
|
|
|
|
/// null terminated; it's just a memory mapped file.
|
|
|
|
static size_t read_line(const char *base, size_t cursor, size_t len, std::string &result) {
|
|
|
|
// Locate the newline.
|
|
|
|
assert(cursor <= len);
|
|
|
|
const char *start = base + cursor;
|
2016-07-01 17:48:50 +08:00
|
|
|
const char *a_newline = (char *)memchr(start, '\n', len - cursor);
|
|
|
|
if (a_newline != NULL) { // we found a newline
|
|
|
|
result.assign(start, a_newline - start);
|
2016-05-06 06:09:31 +08:00
|
|
|
// Return the amount to advance the cursor; skip over the newline.
|
2016-07-01 17:48:50 +08:00
|
|
|
return a_newline - start + 1;
|
2016-05-06 06:09:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We ran off the end.
|
|
|
|
result.clear();
|
|
|
|
return len - cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trims leading spaces in the given string, returning how many there were.
|
|
|
|
static size_t trim_leading_spaces(std::string &str) {
|
|
|
|
size_t i = 0, max = str.size();
|
|
|
|
while (i < max && str[i] == ' ') i++;
|
|
|
|
str.erase(0, i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool extract_prefix_and_unescape_yaml(std::string *key, std::string *value,
|
|
|
|
const std::string &line) {
|
|
|
|
size_t where = line.find(":");
|
|
|
|
if (where != std::string::npos) {
|
|
|
|
key->assign(line, 0, where);
|
|
|
|
|
|
|
|
// Skip a space after the : if necessary.
|
|
|
|
size_t val_start = where + 1;
|
|
|
|
if (val_start < line.size() && line.at(val_start) == ' ') val_start++;
|
|
|
|
value->assign(line, val_start, line.size() - val_start);
|
|
|
|
|
|
|
|
unescape_yaml(key);
|
|
|
|
unescape_yaml(value);
|
|
|
|
}
|
|
|
|
return where != std::string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove backslashes from all newlines. This makes a string from the history file better formated
|
|
|
|
/// for on screen display.
|
|
|
|
static wcstring history_unescape_newlines_fish_1_x(const wcstring &in_str) {
|
|
|
|
wcstring out;
|
|
|
|
for (const wchar_t *in = in_str.c_str(); *in; in++) {
|
|
|
|
if (*in == L'\\') {
|
|
|
|
if (*(in + 1) != L'\n') {
|
|
|
|
out.push_back(*in);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out.push_back(*in);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to infer the history file type based on inspecting the data.
|
|
|
|
static history_file_type_t infer_file_type(const char *data, size_t len) {
|
|
|
|
history_file_type_t result = history_type_unknown;
|
|
|
|
if (len > 0) { // old fish started with a #
|
|
|
|
if (data[0] == '#') {
|
|
|
|
result = history_type_fish_1_x;
|
|
|
|
} else { // assume new fish
|
|
|
|
result = history_type_fish_2_0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decode an item via the fish 1.x format. Adapted from fish 1.x's item_get().
|
|
|
|
static history_item_t decode_item_fish_1_x(const char *begin, size_t length) {
|
|
|
|
const char *end = begin + length;
|
|
|
|
const char *pos = begin;
|
|
|
|
wcstring out;
|
|
|
|
bool was_backslash = false;
|
|
|
|
bool first_char = true;
|
|
|
|
bool timestamp_mode = false;
|
|
|
|
time_t timestamp = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
wchar_t c;
|
|
|
|
size_t res;
|
|
|
|
mbstate_t state = {};
|
|
|
|
|
|
|
|
if (MB_CUR_MAX == 1) { // single-byte locale
|
|
|
|
c = (unsigned char)*pos;
|
|
|
|
res = 1;
|
|
|
|
} else {
|
|
|
|
res = mbrtowc(&c, pos, end - pos, &state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res == (size_t)-1) {
|
|
|
|
pos++;
|
|
|
|
continue;
|
|
|
|
} else if (res == (size_t)-2) {
|
|
|
|
break;
|
|
|
|
} else if (res == (size_t)0) {
|
|
|
|
pos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pos += res;
|
|
|
|
|
|
|
|
if (c == L'\n') {
|
|
|
|
if (timestamp_mode) {
|
|
|
|
const wchar_t *time_string = out.c_str();
|
|
|
|
while (*time_string && !iswdigit(*time_string)) time_string++;
|
|
|
|
|
|
|
|
if (*time_string) {
|
2016-11-23 12:24:03 +08:00
|
|
|
time_t tm = (time_t)fish_wcstol(time_string);
|
|
|
|
if (!errno && tm >= 0) {
|
2016-05-06 06:09:31 +08:00
|
|
|
timestamp = tm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out.clear();
|
|
|
|
timestamp_mode = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!was_backslash) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_char) {
|
|
|
|
first_char = false;
|
|
|
|
if (c == L'#') timestamp_mode = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.push_back(c);
|
|
|
|
was_backslash = (c == L'\\') && !was_backslash;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = history_unescape_newlines_fish_1_x(out);
|
|
|
|
return history_item_t(out, timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decode an item via the fish 2.0 format.
|
|
|
|
static history_item_t decode_item_fish_2_0(const char *base, size_t len) {
|
|
|
|
wcstring cmd;
|
|
|
|
time_t when = 0;
|
|
|
|
path_list_t paths;
|
|
|
|
|
|
|
|
size_t indent = 0, cursor = 0;
|
|
|
|
std::string key, value, line;
|
|
|
|
|
|
|
|
// Read the "- cmd:" line.
|
|
|
|
size_t advance = read_line(base, cursor, len, line);
|
|
|
|
trim_leading_spaces(line);
|
|
|
|
if (!extract_prefix_and_unescape_yaml(&key, &value, line) || key != "- cmd") {
|
|
|
|
goto done; //!OCLINT(goto is the cleanest way to handle bad input)
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor += advance;
|
|
|
|
cmd = str2wcstring(value);
|
|
|
|
|
|
|
|
// Read the remaining lines.
|
|
|
|
for (;;) {
|
|
|
|
size_t advance = read_line(base, cursor, len, line);
|
|
|
|
|
|
|
|
size_t this_indent = trim_leading_spaces(line);
|
|
|
|
if (indent == 0) indent = this_indent;
|
|
|
|
|
|
|
|
if (this_indent == 0 || indent != this_indent) break;
|
|
|
|
|
|
|
|
if (!extract_prefix_and_unescape_yaml(&key, &value, line)) break;
|
|
|
|
|
|
|
|
// We are definitely going to consume this line.
|
|
|
|
cursor += advance;
|
|
|
|
|
|
|
|
if (key == "when") {
|
|
|
|
// Parse an int from the timestamp. Should this fail, strtol returns 0; that's
|
|
|
|
// acceptable.
|
|
|
|
char *end = NULL;
|
|
|
|
long tmp = strtol(value.c_str(), &end, 0);
|
|
|
|
when = tmp;
|
|
|
|
} else if (key == "paths") {
|
|
|
|
// Read lines starting with " - " until we can't read any more.
|
|
|
|
for (;;) {
|
|
|
|
size_t advance = read_line(base, cursor, len, line);
|
|
|
|
if (trim_leading_spaces(line) <= indent) break;
|
|
|
|
|
|
|
|
if (strncmp(line.c_str(), "- ", 2)) break;
|
|
|
|
|
|
|
|
// We're going to consume this line.
|
|
|
|
cursor += advance;
|
|
|
|
|
|
|
|
// Skip the leading dash-space and then store this path it.
|
|
|
|
line.erase(0, 2);
|
|
|
|
unescape_yaml(&line);
|
|
|
|
paths.push_back(str2wcstring(line));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
history_item_t result(cmd, when);
|
|
|
|
result.set_required_paths(paths);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static history_item_t decode_item(const char *base, size_t len, history_file_type_t type) {
|
|
|
|
if (type == history_type_fish_2_0) return decode_item_fish_2_0(base, len);
|
|
|
|
if (type == history_type_fish_1_x) return decode_item_fish_1_x(base, len);
|
|
|
|
return history_item_t(L"");
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// We can merge two items if they are the same command. We use the more recent timestamp, more
|
|
|
|
/// recent identifier, and the longer list of required paths.
|
2016-04-18 13:47:37 +08:00
|
|
|
bool history_item_t::merge(const history_item_t &item) {
|
2012-03-02 06:56:34 +08:00
|
|
|
bool result = false;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (this->contents == item.contents) {
|
2012-03-02 06:56:34 +08:00
|
|
|
this->creation_timestamp = std::max(this->creation_timestamp, item.creation_timestamp);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (this->required_paths.size() < item.required_paths.size()) {
|
2012-03-02 06:56:34 +08:00
|
|
|
this->required_paths = item.required_paths;
|
|
|
|
}
|
2016-04-18 13:47:37 +08:00
|
|
|
if (this->identifier < item.identifier) {
|
2014-03-29 14:22:03 +08:00
|
|
|
this->identifier = item.identifier;
|
|
|
|
}
|
2012-03-02 06:56:34 +08:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-09-24 11:12:15 +08:00
|
|
|
#if 0
|
2016-04-18 13:47:37 +08:00
|
|
|
history_item_t::history_item_t(const wcstring &str)
|
2016-09-24 11:12:15 +08:00
|
|
|
: contents(str), contents_lower(L""), creation_timestamp(time(NULL)), identifier(0) {
|
|
|
|
for (wcstring::const_iterator it = str.begin(); it != str.end(); ++it) {
|
|
|
|
contents_lower.push_back(towlower(*it));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
history_item_t::history_item_t(const wcstring &str, time_t when, history_identifier_t ident)
|
2016-09-24 11:12:15 +08:00
|
|
|
: contents(str), contents_lower(L""), creation_timestamp(when), identifier(ident) {
|
|
|
|
for (wcstring::const_iterator it = str.begin(); it != str.end(); ++it) {
|
|
|
|
contents_lower.push_back(towlower(*it));
|
|
|
|
}
|
|
|
|
}
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-09-24 11:12:15 +08:00
|
|
|
bool history_item_t::matches_search(const wcstring &term, enum history_search_type_t type,
|
|
|
|
bool case_sensitive) const {
|
|
|
|
// We don't use a switch below because there are only three cases and if the strings are the
|
|
|
|
// same length we can use the faster HISTORY_SEARCH_TYPE_EXACT for the other two cases.
|
|
|
|
//
|
|
|
|
// Too, we consider equal strings to match a prefix search, so that autosuggest will allow
|
|
|
|
// suggesting what you've typed.
|
|
|
|
if (case_sensitive) {
|
|
|
|
if (type == HISTORY_SEARCH_TYPE_EXACT || term.size() == contents.size()) {
|
|
|
|
return term == contents;
|
|
|
|
} else if (type == HISTORY_SEARCH_TYPE_CONTAINS) {
|
2016-07-30 12:24:26 +08:00
|
|
|
return contents.find(term) != wcstring::npos;
|
2016-09-24 11:12:15 +08:00
|
|
|
} else if (type == HISTORY_SEARCH_TYPE_PREFIX) {
|
2012-11-19 16:31:03 +08:00
|
|
|
return string_prefixes_string(term, contents);
|
2016-04-18 13:47:37 +08:00
|
|
|
}
|
2016-09-24 11:12:15 +08:00
|
|
|
} else {
|
|
|
|
wcstring lterm(L"");
|
|
|
|
for (wcstring::const_iterator it = term.begin(); it != term.end(); ++it) {
|
|
|
|
lterm.push_back(towlower(*it));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == HISTORY_SEARCH_TYPE_EXACT || lterm.size() == contents.size()) {
|
|
|
|
return lterm == contents_lower;
|
|
|
|
} else if (type == HISTORY_SEARCH_TYPE_CONTAINS) {
|
|
|
|
return contents_lower.find(lterm) != wcstring::npos;
|
|
|
|
} else if (type == HISTORY_SEARCH_TYPE_PREFIX) {
|
|
|
|
return string_prefixes_string(lterm, contents_lower);
|
2016-04-18 13:47:37 +08:00
|
|
|
}
|
2012-02-07 02:52:13 +08:00
|
|
|
}
|
2016-09-24 11:12:15 +08:00
|
|
|
DIE("unexpected history_search_type_t value");
|
2012-02-07 02:52:13 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Append our YAML history format to the provided vector at the given offset, updating the offset.
|
2016-04-18 13:47:37 +08:00
|
|
|
static void append_yaml_to_buffer(const wcstring &wcmd, time_t timestamp,
|
|
|
|
const path_list_t &required_paths,
|
|
|
|
history_output_buffer_t *buffer) {
|
2012-12-03 08:39:35 +08:00
|
|
|
std::string cmd = wcs2string(wcmd);
|
2014-07-30 05:41:21 +08:00
|
|
|
escape_yaml(&cmd);
|
2013-04-28 06:21:14 +08:00
|
|
|
buffer->append("- cmd: ", cmd.c_str(), "\n");
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2013-04-28 06:21:14 +08:00
|
|
|
char timestamp_str[96];
|
2016-04-18 13:47:37 +08:00
|
|
|
snprintf(timestamp_str, sizeof timestamp_str, "%ld", (long)timestamp);
|
2014-09-28 21:03:55 +08:00
|
|
|
buffer->append(" when: ", timestamp_str, "\n");
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!required_paths.empty()) {
|
2014-09-28 21:03:55 +08:00
|
|
|
buffer->append(" paths:\n");
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
for (path_list_t::const_iterator iter = required_paths.begin();
|
|
|
|
iter != required_paths.end(); ++iter) {
|
2012-02-16 16:24:27 +08:00
|
|
|
std::string path = wcs2string(*iter);
|
2014-07-30 05:41:21 +08:00
|
|
|
escape_yaml(&path);
|
2013-04-28 06:21:14 +08:00
|
|
|
buffer->append(" - ", path.c_str(), "\n");
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-06 12:54:41 +08:00
|
|
|
}
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Parse a timestamp line that looks like this: spaces, "when:", spaces, timestamp, newline
|
|
|
|
/// The string is NOT null terminated; however we do know it contains a newline, so stop when we
|
|
|
|
/// reach it.
|
2016-04-18 13:47:37 +08:00
|
|
|
static bool parse_timestamp(const char *str, time_t *out_when) {
|
2012-04-16 13:49:27 +08:00
|
|
|
const char *cursor = str;
|
2016-04-18 13:47:37 +08:00
|
|
|
// Advance past spaces.
|
|
|
|
while (*cursor == ' ') cursor++;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Look for "when:".
|
2012-04-16 13:49:27 +08:00
|
|
|
size_t when_len = 5;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (strncmp(cursor, "when:", when_len) != 0) return false;
|
2012-04-16 13:49:27 +08:00
|
|
|
cursor += when_len;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Advance past spaces.
|
|
|
|
while (*cursor == ' ') cursor++;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Try to parse a timestamp.
|
2012-04-16 13:49:27 +08:00
|
|
|
long timestamp = 0;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (isdigit(*cursor) && (timestamp = strtol(cursor, NULL, 0)) > 0) {
|
2012-04-16 13:49:27 +08:00
|
|
|
*out_when = (time_t)timestamp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Returns a pointer to the start of the next line, or NULL. The next line must itself end with a
|
|
|
|
/// newline. Note that the string is not null terminated.
|
2016-04-18 13:47:37 +08:00
|
|
|
static const char *next_line(const char *start, size_t length) {
|
|
|
|
// Handle the hopeless case.
|
|
|
|
if (length < 1) return NULL;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Get a pointer to the end, that we must not pass.
|
|
|
|
const char *const end = start + length;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Skip past the next newline.
|
2012-04-17 11:26:50 +08:00
|
|
|
const char *nextline = (const char *)memchr(start, '\n', length);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!nextline || nextline >= end) {
|
2012-04-17 11:26:50 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-04-18 13:47:37 +08:00
|
|
|
// Skip past the newline character itself.
|
|
|
|
if (++nextline >= end) {
|
2012-04-17 11:26:50 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Make sure this new line is itself "newline terminated". If it's not, return NULL.
|
2012-04-17 11:26:50 +08:00
|
|
|
const char *next_newline = (const char *)memchr(nextline, '\n', end - nextline);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!next_newline) {
|
2012-04-17 11:26:50 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-04-17 11:26:50 +08:00
|
|
|
return nextline;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Support for iteratively locating the offsets of history items.
|
|
|
|
/// Pass the address and length of a mapped region.
|
|
|
|
/// Pass a pointer to a cursor size_t, initially 0.
|
|
|
|
/// If custoff_timestamp is nonzero, skip items created at or after that timestamp.
|
2016-05-06 06:09:31 +08:00
|
|
|
/// Returns (size_t)-1 when done.
|
2016-04-18 13:47:37 +08:00
|
|
|
static size_t offset_of_next_item_fish_2_0(const char *begin, size_t mmap_length,
|
|
|
|
size_t *inout_cursor, time_t cutoff_timestamp) {
|
2012-04-16 13:49:27 +08:00
|
|
|
size_t cursor = *inout_cursor;
|
2016-05-06 06:09:31 +08:00
|
|
|
size_t result = (size_t)-1;
|
2016-04-18 13:47:37 +08:00
|
|
|
while (cursor < mmap_length) {
|
2014-07-30 05:41:21 +08:00
|
|
|
const char *line_start = begin + cursor;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Advance the cursor to the next line.
|
2016-07-01 17:48:50 +08:00
|
|
|
const char *a_newline = (const char *)memchr(line_start, '\n', mmap_length - cursor);
|
|
|
|
if (a_newline == NULL) break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Advance the cursor past this line. +1 is for the newline.
|
2016-07-01 17:48:50 +08:00
|
|
|
cursor = a_newline - begin + 1;
|
2012-04-16 13:49:27 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Skip lines with a leading space, since these are in the interior of one of our items.
|
|
|
|
if (line_start[0] == ' ') continue;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Skip very short lines to make one of the checks below easier.
|
2016-07-01 17:48:50 +08:00
|
|
|
if (a_newline - line_start < 3) continue;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Try to be a little YAML compatible. Skip lines with leading %, ---, or ...
|
|
|
|
if (!memcmp(line_start, "%", 1) || !memcmp(line_start, "---", 3) ||
|
|
|
|
!memcmp(line_start, "...", 3))
|
2012-04-16 13:49:27 +08:00
|
|
|
continue;
|
2015-09-17 12:31:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Hackish: fish 1.x rewriting a fish 2.0 history file can produce lines with lots of
|
|
|
|
// leading "- cmd: - cmd: - cmd:". Trim all but one leading "- cmd:".
|
2014-07-30 05:41:21 +08:00
|
|
|
const char *double_cmd = "- cmd: - cmd: ";
|
|
|
|
const size_t double_cmd_len = strlen(double_cmd);
|
2016-10-10 05:36:08 +08:00
|
|
|
while ((size_t)(a_newline - line_start) > double_cmd_len &&
|
2016-04-18 13:47:37 +08:00
|
|
|
!memcmp(line_start, double_cmd, double_cmd_len)) {
|
|
|
|
// Skip over just one of the - cmd. In the end there will be just one left.
|
2014-07-30 05:41:21 +08:00
|
|
|
line_start += strlen("- cmd: ");
|
|
|
|
}
|
2015-09-17 12:31:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Hackish: fish 1.x rewriting a fish 2.0 history file can produce commands like "when:
|
|
|
|
// 123456". Ignore those.
|
2014-07-30 05:41:21 +08:00
|
|
|
const char *cmd_when = "- cmd: when:";
|
|
|
|
const size_t cmd_when_len = strlen(cmd_when);
|
2016-10-10 05:36:08 +08:00
|
|
|
if ((size_t)(a_newline - line_start) >= cmd_when_len &&
|
2016-10-11 10:52:22 +08:00
|
|
|
!memcmp(line_start, cmd_when, cmd_when_len)) {
|
2014-07-30 05:41:21 +08:00
|
|
|
continue;
|
2016-10-10 05:36:08 +08:00
|
|
|
}
|
2015-09-17 12:31:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// At this point, we know line_start is at the beginning of an item. But maybe we want to
|
|
|
|
// skip this item because of timestamps. A 0 cutoff means we don't care; if we do care, then
|
|
|
|
// try parsing out a timestamp.
|
|
|
|
if (cutoff_timestamp != 0) {
|
|
|
|
// Hackish fast way to skip items created after our timestamp. This is the mechanism by
|
|
|
|
// which we avoid "seeing" commands from other sessions that started after we started.
|
|
|
|
// We try hard to ensure that our items are sorted by their timestamps, so in theory we
|
|
|
|
// could just break, but I don't think that works well if (for example) the clock
|
|
|
|
// changes. So we'll read all subsequent items.
|
|
|
|
const char *const end = begin + mmap_length;
|
|
|
|
|
|
|
|
// Walk over lines that we think are interior. These lines are not null terminated, but
|
|
|
|
// are guaranteed to contain a newline.
|
2012-04-17 11:26:50 +08:00
|
|
|
bool has_timestamp = false;
|
2014-07-30 05:41:21 +08:00
|
|
|
time_t timestamp = 0;
|
2012-04-17 11:26:50 +08:00
|
|
|
const char *interior_line;
|
2013-11-25 23:07:17 +08:00
|
|
|
|
2012-04-17 11:26:50 +08:00
|
|
|
for (interior_line = next_line(line_start, end - line_start);
|
2016-04-18 13:47:37 +08:00
|
|
|
interior_line != NULL && !has_timestamp;
|
|
|
|
interior_line = next_line(interior_line, end - interior_line)) {
|
|
|
|
// If the first character is not a space, it's not an interior line, so we're done.
|
|
|
|
if (interior_line[0] != ' ') break;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Hackish optimization: since we just stepped over some interior line, update the
|
|
|
|
// cursor so we don't have to look at these lines next time.
|
2012-04-17 11:26:50 +08:00
|
|
|
cursor = interior_line - begin;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Try parsing a timestamp from this line. If we succeed, the loop will break.
|
2012-04-17 11:26:50 +08:00
|
|
|
has_timestamp = parse_timestamp(interior_line, ×tamp);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Skip this item if the timestamp is past our cutoff.
|
|
|
|
if (has_timestamp && timestamp > cutoff_timestamp) {
|
2012-04-16 13:49:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-17 11:26:50 +08:00
|
|
|
}
|
2012-04-16 13:49:27 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// We made it through the gauntlet.
|
2012-04-16 13:49:27 +08:00
|
|
|
result = line_start - begin;
|
2016-05-06 06:09:31 +08:00
|
|
|
break; //!OCLINT(avoid branching statement as last in loop)
|
2012-04-16 13:49:27 +08:00
|
|
|
}
|
2016-05-06 06:09:31 +08:00
|
|
|
|
2012-04-16 13:49:27 +08:00
|
|
|
*inout_cursor = cursor;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Same as offset_of_next_item_fish_2_0, but for fish 1.x (pre fishfish).
|
|
|
|
/// Adapted from history_populate_from_mmap in history.c
|
2016-04-18 13:47:37 +08:00
|
|
|
static size_t offset_of_next_item_fish_1_x(const char *begin, size_t mmap_length,
|
2016-05-06 06:09:31 +08:00
|
|
|
size_t *inout_cursor) {
|
|
|
|
if (mmap_length == 0 || *inout_cursor >= mmap_length) return (size_t)-1;
|
2012-06-16 07:22:37 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *end = begin + mmap_length;
|
|
|
|
const char *pos;
|
|
|
|
bool ignore_newline = false;
|
|
|
|
bool do_push = true;
|
2012-06-16 07:22:37 +08:00
|
|
|
bool all_done = false;
|
|
|
|
size_t result = *inout_cursor;
|
2016-05-06 06:09:31 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
for (pos = begin + *inout_cursor; pos < end && !all_done; pos++) {
|
|
|
|
if (do_push) {
|
2012-11-19 08:30:30 +08:00
|
|
|
ignore_newline = (*pos == '#');
|
|
|
|
do_push = false;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-06 06:09:31 +08:00
|
|
|
if (*pos == '\\') {
|
|
|
|
pos++;
|
|
|
|
} else if (*pos == '\n') {
|
|
|
|
if (!ignore_newline) {
|
|
|
|
// pos will be left pointing just after this newline, because of the ++ in the loop.
|
|
|
|
all_done = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-06 06:09:31 +08:00
|
|
|
ignore_newline = false;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
|
2012-06-16 07:22:37 +08:00
|
|
|
*inout_cursor = (pos - begin);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Returns the offset of the next item based on the given history type, or -1.
|
2016-04-18 13:47:37 +08:00
|
|
|
static size_t offset_of_next_item(const char *begin, size_t mmap_length,
|
|
|
|
history_file_type_t mmap_type, size_t *inout_cursor,
|
|
|
|
time_t cutoff_timestamp) {
|
2016-05-29 13:28:26 +08:00
|
|
|
size_t result = (size_t)-1;
|
|
|
|
if (mmap_type == history_type_fish_2_0) {
|
|
|
|
result = offset_of_next_item_fish_2_0(begin, mmap_length, inout_cursor, cutoff_timestamp);
|
|
|
|
} else if (mmap_type == history_type_fish_1_x) {
|
|
|
|
result = offset_of_next_item_fish_1_x(begin, mmap_length, inout_cursor);
|
2012-06-16 07:22:37 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-30 05:21:50 +08:00
|
|
|
history_t &history_collection_t::get_creating(const wcstring &name) {
|
|
|
|
// Return a history for the given name, creating it if necessary
|
2016-04-18 13:47:37 +08:00
|
|
|
// Note that histories are currently never deleted, so we can return a reference to them without
|
2017-01-30 05:21:50 +08:00
|
|
|
// using something like shared_ptr
|
2017-08-19 03:26:35 +08:00
|
|
|
auto &&hs = histories.acquire();
|
2017-01-30 05:21:50 +08:00
|
|
|
std::unique_ptr<history_t> &hist = hs.value[name];
|
2017-02-08 13:52:35 +08:00
|
|
|
if (!hist) {
|
2017-01-30 05:21:50 +08:00
|
|
|
hist = make_unique<history_t>(name);
|
|
|
|
}
|
|
|
|
return *hist;
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2017-01-30 05:21:50 +08:00
|
|
|
history_t &history_t::history_with_name(const wcstring &name) {
|
|
|
|
return histories.get_creating(name);
|
|
|
|
}
|
2016-04-18 13:47:37 +08:00
|
|
|
|
|
|
|
history_t::history_t(const wcstring &pname)
|
|
|
|
: name(pname),
|
|
|
|
first_unwritten_new_item_index(0),
|
|
|
|
has_pending_item(false),
|
|
|
|
disable_automatic_save_counter(0),
|
|
|
|
mmap_start(NULL),
|
|
|
|
mmap_length(0),
|
|
|
|
mmap_type(history_file_type_t(-1)),
|
|
|
|
mmap_file_id(kInvalidFileID),
|
|
|
|
boundary_timestamp(time(NULL)),
|
|
|
|
countdown_to_vacuum(-1),
|
|
|
|
loaded_old(false),
|
2017-08-19 03:26:35 +08:00
|
|
|
chaos_mode(false) {}
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::add(const history_item_t &item, bool pending) {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Try merging with the last item.
|
|
|
|
if (!new_items.empty() && new_items.back().merge(item)) {
|
|
|
|
// We merged, so we don't have to add anything. Maybe this item was pending, but it just got
|
|
|
|
// merged with an item that is not pending, so pending just becomes false.
|
2015-04-20 17:04:17 +08:00
|
|
|
this->has_pending_item = false;
|
2016-04-18 13:47:37 +08:00
|
|
|
} else {
|
|
|
|
// We have to add a new item.
|
2012-03-02 06:56:34 +08:00
|
|
|
new_items.push_back(item);
|
2015-04-20 17:04:17 +08:00
|
|
|
this->has_pending_item = pending;
|
2014-03-29 14:22:03 +08:00
|
|
|
save_internal_unless_disabled();
|
2012-03-02 06:56:34 +08:00
|
|
|
}
|
2014-03-29 14:22:03 +08:00
|
|
|
}
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::save_internal_unless_disabled() {
|
|
|
|
// This must be called while locked.
|
2014-03-29 14:22:03 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Respect disable_automatic_save_counter.
|
|
|
|
if (disable_automatic_save_counter > 0) {
|
2014-03-29 14:22:03 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// We may or may not vacuum. We try to vacuum every kVacuumFrequency items, but start the
|
|
|
|
// countdown at a random number so that even if the user never runs more than 25 commands, we'll
|
|
|
|
// eventually vacuum. If countdown_to_vacuum is -1, it means we haven't yet picked a value for
|
|
|
|
// the counter.
|
2012-12-03 16:14:09 +08:00
|
|
|
const int kVacuumFrequency = 25;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (countdown_to_vacuum < 0) {
|
2012-12-03 16:14:09 +08:00
|
|
|
static unsigned int seed = (unsigned int)time(NULL);
|
2016-04-18 13:47:37 +08:00
|
|
|
// Generate a number in the range [0, kVacuumFrequency).
|
2012-12-03 16:14:09 +08:00
|
|
|
countdown_to_vacuum = rand_r(&seed) / (RAND_MAX / kVacuumFrequency + 1);
|
2012-03-02 06:56:34 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Determine if we're going to vacuum.
|
2012-12-03 16:14:09 +08:00
|
|
|
bool vacuum = false;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (countdown_to_vacuum == 0) {
|
2012-12-03 16:14:09 +08:00
|
|
|
countdown_to_vacuum = kVacuumFrequency;
|
|
|
|
vacuum = true;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// This might be a good candidate for moving to a background thread.
|
2016-11-02 10:12:14 +08:00
|
|
|
time_profiler_t profiler(vacuum ? "save_internal vacuum" //!OCLINT(unused var)
|
2016-05-28 05:41:16 +08:00
|
|
|
: "save_internal no vacuum"); //!OCLINT(side-effect)
|
2012-12-03 16:14:09 +08:00
|
|
|
this->save_internal(vacuum);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Update our countdown.
|
2012-12-03 16:14:09 +08:00
|
|
|
assert(countdown_to_vacuum > 0);
|
|
|
|
countdown_to_vacuum--;
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::add(const wcstring &str, history_identifier_t ident, bool pending) {
|
2012-12-03 17:53:52 +08:00
|
|
|
time_t when = time(NULL);
|
2016-04-18 13:47:37 +08:00
|
|
|
// Big hack: do not allow timestamps equal to our boundary date. This is because we include
|
|
|
|
// items whose timestamps are equal to our boundary when reading old history, so we can catch
|
|
|
|
// "just closed" items. But this means that we may interpret our own items, that we just wrote,
|
|
|
|
// as old items, if we wrote them in the same second as our birthdate.
|
|
|
|
if (when == this->boundary_timestamp) {
|
2012-12-03 17:53:52 +08:00
|
|
|
when++;
|
2014-07-26 01:08:21 +08:00
|
|
|
}
|
2012-12-03 17:53:52 +08:00
|
|
|
|
2015-04-20 17:04:17 +08:00
|
|
|
this->add(history_item_t(str, when, ident), pending);
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2016-09-24 11:12:15 +08:00
|
|
|
// Remove matching history entries from our list of new items. This only supports literal,
|
|
|
|
// case-sensitive, matches.
|
|
|
|
void history_t::remove(const wcstring &str_to_remove) {
|
2016-04-18 13:47:37 +08:00
|
|
|
// Add to our list of deleted items.
|
2016-09-24 11:12:15 +08:00
|
|
|
deleted_items.insert(str_to_remove);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-12-03 08:39:35 +08:00
|
|
|
size_t idx = new_items.size();
|
2016-04-18 13:47:37 +08:00
|
|
|
while (idx--) {
|
2016-09-24 11:12:15 +08:00
|
|
|
bool matched = new_items.at(idx).str() == str_to_remove;
|
|
|
|
if (matched) {
|
2012-12-03 08:39:35 +08:00
|
|
|
new_items.erase(new_items.begin() + idx);
|
2016-04-18 13:47:37 +08:00
|
|
|
// If this index is before our first_unwritten_new_item_index, then subtract one from
|
|
|
|
// that index so it stays pointing at the same item. If it is equal to or larger, then
|
|
|
|
// we have not yet writen this item, so we don't have to adjust the index.
|
|
|
|
if (idx < first_unwritten_new_item_index) {
|
2012-12-03 08:39:35 +08:00
|
|
|
first_unwritten_new_item_index--;
|
|
|
|
}
|
2012-06-05 12:24:42 +08:00
|
|
|
}
|
|
|
|
}
|
2012-12-03 08:39:35 +08:00
|
|
|
assert(first_unwritten_new_item_index <= new_items.size());
|
2012-06-05 12:24:42 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::set_valid_file_paths(const wcstring_list_t &valid_file_paths,
|
|
|
|
history_identifier_t ident) {
|
|
|
|
// 0 identifier is used to mean "not necessary".
|
|
|
|
if (ident == 0) {
|
2014-03-29 14:22:03 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Look for an item with the given identifier. It is likely to be at the end of new_items.
|
|
|
|
for (history_item_list_t::reverse_iterator iter = new_items.rbegin(); iter != new_items.rend();
|
|
|
|
++iter) {
|
|
|
|
if (iter->identifier == ident) { // found it
|
2014-03-29 14:22:03 +08:00
|
|
|
iter->required_paths = valid_file_paths;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 09:59:50 +08:00
|
|
|
void history_t::get_history(wcstring_list_t &result) {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// If we have a pending item, we skip the first encountered (i.e. last) new item.
|
2015-04-20 17:04:17 +08:00
|
|
|
bool next_is_pending = this->has_pending_item;
|
2017-08-25 09:59:50 +08:00
|
|
|
std::unordered_set<wcstring> seen;
|
2015-04-20 17:04:17 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Append new items. Note that in principle we could use const_reverse_iterator, but we do not
|
|
|
|
// because reverse_iterator is not convertible to const_reverse_iterator. See
|
2017-02-17 21:30:43 +08:00
|
|
|
// https://github.com/fish-shell/fish-shell/issues/431.
|
2016-04-18 13:47:37 +08:00
|
|
|
for (history_item_list_t::reverse_iterator iter = new_items.rbegin(); iter < new_items.rend();
|
|
|
|
++iter) {
|
|
|
|
// Skip a pending item if we have one.
|
|
|
|
if (next_is_pending) {
|
2015-04-20 17:04:17 +08:00
|
|
|
next_is_pending = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-08-25 09:59:50 +08:00
|
|
|
if (seen.insert(iter->str()).second) result.push_back(iter->str());
|
2012-03-20 02:52:18 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Append old items.
|
2012-03-20 02:52:18 +08:00
|
|
|
load_old_if_needed();
|
2016-04-18 13:47:37 +08:00
|
|
|
for (std::deque<size_t>::reverse_iterator iter = old_item_offsets.rbegin();
|
|
|
|
iter != old_item_offsets.rend(); ++iter) {
|
2012-03-20 02:52:18 +08:00
|
|
|
size_t offset = *iter;
|
2016-04-18 13:47:37 +08:00
|
|
|
const history_item_t item =
|
2016-05-06 06:09:31 +08:00
|
|
|
decode_item(mmap_start + offset, mmap_length - offset, mmap_type);
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2017-08-25 09:59:50 +08:00
|
|
|
if (seen.insert(item.str()).second) result.push_back(item.str());
|
2012-03-20 02:52:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
history_item_t history_t::item_at_index(size_t idx) {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// 0 is considered an invalid index.
|
2012-02-06 08:42:24 +08:00
|
|
|
assert(idx > 0);
|
|
|
|
idx--;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Determine how many "resolved" (non-pending) items we have. We can have at most one pending
|
|
|
|
// item, and it's always the last one.
|
2015-04-20 17:04:17 +08:00
|
|
|
size_t resolved_new_item_count = new_items.size();
|
2016-04-18 13:47:37 +08:00
|
|
|
if (this->has_pending_item && resolved_new_item_count > 0) {
|
2015-04-20 17:04:17 +08:00
|
|
|
resolved_new_item_count -= 1;
|
|
|
|
}
|
2015-09-17 12:31:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// idx == 0 corresponds to the last resolved item.
|
|
|
|
if (idx < resolved_new_item_count) {
|
2015-04-20 17:04:17 +08:00
|
|
|
return new_items.at(resolved_new_item_count - idx - 1);
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Now look in our old items.
|
2015-04-20 17:04:17 +08:00
|
|
|
idx -= resolved_new_item_count;
|
2012-02-06 08:42:24 +08:00
|
|
|
load_old_if_needed();
|
|
|
|
size_t old_item_count = old_item_offsets.size();
|
2016-04-18 13:47:37 +08:00
|
|
|
if (idx < old_item_count) {
|
|
|
|
// idx == 0 corresponds to last item in old_item_offsets.
|
2012-02-06 08:42:24 +08:00
|
|
|
size_t offset = old_item_offsets.at(old_item_count - idx - 1);
|
2016-05-06 06:09:31 +08:00
|
|
|
return decode_item(mmap_start + offset, mmap_length - offset, mmap_type);
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Index past the valid range, so return an empty history item.
|
2012-02-06 08:42:24 +08:00
|
|
|
return history_item_t(wcstring(), 0);
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::populate_from_mmap(void) {
|
2012-06-16 07:22:37 +08:00
|
|
|
mmap_type = infer_file_type(mmap_start, mmap_length);
|
2012-02-16 16:24:27 +08:00
|
|
|
size_t cursor = 0;
|
2016-04-18 13:47:37 +08:00
|
|
|
for (;;) {
|
|
|
|
size_t offset =
|
|
|
|
offset_of_next_item(mmap_start, mmap_length, mmap_type, &cursor, boundary_timestamp);
|
|
|
|
// If we get back -1, we're done.
|
2016-05-06 06:09:31 +08:00
|
|
|
if (offset == (size_t)-1) break;
|
2012-03-02 06:56:34 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Remember this item.
|
2012-11-18 18:23:22 +08:00
|
|
|
old_item_offsets.push_back(offset);
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2017-02-07 02:09:31 +08:00
|
|
|
bool history_t::map_fd(int fd, const char **out_map_start, size_t *out_map_len) const {
|
|
|
|
if (fd < 0) {
|
2016-10-31 05:52:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take a read lock to guard against someone else appending. This is released when the file
|
|
|
|
// is closed (below). We will read the file after releasing the lock, but that's not a
|
|
|
|
// problem, because we never modify already written data. In short, the purpose of this lock
|
|
|
|
// is to ensure we don't see the file size change mid-update.
|
|
|
|
//
|
|
|
|
// We may fail to lock (e.g. on lockless NFS - see issue #685. In that case, we proceed as
|
|
|
|
// if it did not fail. The risk is that we may get an incomplete history item; this is
|
|
|
|
// unlikely because we only treat an item as valid if it has a terminating newline.
|
|
|
|
//
|
|
|
|
// Simulate a failing lock in chaos_mode.
|
2017-02-07 02:09:31 +08:00
|
|
|
bool result = false;
|
2016-12-16 13:50:27 +08:00
|
|
|
if (!chaos_mode) history_file_lock(fd, LOCK_SH);
|
2016-10-31 05:52:36 +08:00
|
|
|
off_t len = lseek(fd, 0, SEEK_END);
|
|
|
|
if (len != (off_t)-1) {
|
|
|
|
size_t mmap_length = (size_t)len;
|
|
|
|
if (lseek(fd, 0, SEEK_SET) == 0) {
|
|
|
|
char *mmap_start;
|
2016-11-23 12:24:03 +08:00
|
|
|
if ((mmap_start = (char *)mmap(0, mmap_length, PROT_READ, MAP_PRIVATE, fd, 0)) !=
|
|
|
|
MAP_FAILED) {
|
2016-10-31 05:52:36 +08:00
|
|
|
result = true;
|
|
|
|
*out_map_start = mmap_start;
|
|
|
|
*out_map_len = mmap_length;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-04-16 13:49:27 +08:00
|
|
|
}
|
2016-12-16 13:50:27 +08:00
|
|
|
if (!chaos_mode) history_file_lock(fd, LOCK_UN);
|
2017-02-07 02:09:31 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Do a private, read-only map of the entirety of a history file with the given name. Returns true
|
|
|
|
/// if successful. Returns the mapped memory region by reference.
|
|
|
|
bool history_t::map_file(const wcstring &name, const char **out_map_start, size_t *out_map_len,
|
|
|
|
file_id_t *file_id) const {
|
|
|
|
wcstring filename = history_filename(name, L"");
|
|
|
|
if (filename.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = wopen_cloexec(filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the file ID if requested.
|
|
|
|
if (file_id != NULL) *file_id = file_id_for_fd(fd);
|
|
|
|
bool result = this->map_fd(fd, out_map_start, out_map_len);
|
2016-10-31 05:52:36 +08:00
|
|
|
close(fd);
|
2012-04-16 13:49:27 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
bool history_t::load_old_if_needed(void) {
|
2012-04-16 13:49:27 +08:00
|
|
|
if (loaded_old) return true;
|
|
|
|
loaded_old = true;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
bool ok = false;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (map_file(name, &mmap_start, &mmap_length, &mmap_file_id)) {
|
|
|
|
// Here we've mapped the file.
|
2012-04-16 13:49:27 +08:00
|
|
|
ok = true;
|
2016-05-06 06:09:31 +08:00
|
|
|
time_profiler_t profiler("populate_from_mmap"); //!OCLINT(side-effect)
|
2012-04-16 13:49:27 +08:00
|
|
|
this->populate_from_mmap();
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-03-26 16:21:10 +08:00
|
|
|
return ok;
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_search_t::skip_matches(const wcstring_list_t &skips) {
|
2012-02-07 03:52:24 +08:00
|
|
|
external_skips = skips;
|
|
|
|
std::sort(external_skips.begin(), external_skips.end());
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
bool history_search_t::should_skip_match(const wcstring &str) const {
|
2012-02-07 03:52:24 +08:00
|
|
|
return std::binary_search(external_skips.begin(), external_skips.end(), str);
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
bool history_search_t::go_forwards() {
|
|
|
|
// Pop the top index (if more than one) and return if we have any left.
|
|
|
|
if (prev_matches.size() > 1) {
|
2012-02-06 14:30:42 +08:00
|
|
|
prev_matches.pop_back();
|
2012-02-06 14:48:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
bool history_search_t::go_backwards() {
|
|
|
|
// Backwards means increasing our index.
|
2016-05-06 06:09:31 +08:00
|
|
|
const size_t max_idx = (size_t)-1;
|
2012-02-06 14:30:42 +08:00
|
|
|
|
|
|
|
size_t idx = 0;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!prev_matches.empty()) idx = prev_matches.back().first;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
if (idx == max_idx) return false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2013-04-06 15:28:55 +08:00
|
|
|
const bool main_thread = is_main_thread();
|
2013-05-05 17:33:17 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
while (++idx < max_idx) {
|
|
|
|
if (main_thread ? reader_interrupted() : reader_thread_job_is_stale()) {
|
2013-04-06 15:28:55 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-05 17:33:17 +08:00
|
|
|
|
2012-02-06 14:30:42 +08:00
|
|
|
const history_item_t item = history->item_at_index(idx);
|
2016-04-18 13:47:37 +08:00
|
|
|
// We're done if it's empty or we cancelled.
|
|
|
|
if (item.empty()) {
|
2012-02-06 08:42:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Look for a term that matches and that we haven't seen before.
|
2012-02-07 03:52:24 +08:00
|
|
|
const wcstring &str = item.str();
|
2016-09-24 11:12:15 +08:00
|
|
|
if (item.matches_search(term, search_type, case_sensitive) && !match_already_made(str) &&
|
2016-04-18 13:47:37 +08:00
|
|
|
!should_skip_match(str)) {
|
2012-02-16 16:24:27 +08:00
|
|
|
prev_matches.push_back(prev_match_t(idx, item));
|
2012-02-06 08:42:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Goes to the end (forwards).
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_search_t::go_to_end(void) { prev_matches.clear(); }
|
2012-02-06 15:22:18 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Returns if we are at the end, which is where we start.
|
2016-04-18 13:47:37 +08:00
|
|
|
bool history_search_t::is_at_end(void) const { return prev_matches.empty(); }
|
2012-02-06 15:22:18 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Goes to the beginning (backwards).
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_search_t::go_to_beginning(void) {
|
|
|
|
// Go backwards as far as we can.
|
2016-05-06 06:09:31 +08:00
|
|
|
while (go_backwards()) { //!OCLINT(empty while statement)
|
|
|
|
// Do nothing.
|
|
|
|
}
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
history_item_t history_search_t::current_item() const {
|
2016-05-06 06:09:31 +08:00
|
|
|
assert(!prev_matches.empty()); //!OCLINT(double negative)
|
2012-02-06 14:30:42 +08:00
|
|
|
return prev_matches.back().second;
|
2006-10-22 09:21:02 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
wcstring history_search_t::current_string() const {
|
2012-02-16 16:24:27 +08:00
|
|
|
history_item_t item = this->current_item();
|
|
|
|
return item.str();
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
bool history_search_t::match_already_made(const wcstring &match) const {
|
|
|
|
for (std::vector<prev_match_t>::const_iterator iter = prev_matches.begin();
|
|
|
|
iter != prev_matches.end(); ++iter) {
|
|
|
|
if (iter->second.str() == match) return true;
|
2012-02-06 14:30:42 +08:00
|
|
|
}
|
|
|
|
return false;
|
2006-10-22 09:21:02 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
static void replace_all(std::string *str, const char *needle, const char *replacement) {
|
2012-02-16 16:24:27 +08:00
|
|
|
size_t needle_len = strlen(needle), replacement_len = strlen(replacement);
|
2012-02-06 08:42:24 +08:00
|
|
|
size_t offset = 0;
|
2016-04-18 13:47:37 +08:00
|
|
|
while ((offset = str->find(needle, offset)) != std::string::npos) {
|
2014-07-30 05:41:21 +08:00
|
|
|
str->replace(offset, needle_len, replacement);
|
2012-02-16 16:24:27 +08:00
|
|
|
offset += replacement_len;
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
static void escape_yaml(std::string *str) {
|
|
|
|
replace_all(str, "\\", "\\\\"); // replace one backslash with two
|
|
|
|
replace_all(str, "\n", "\\n"); // replace newline with backslash + literal n
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// This function is called frequently, so it ought to be fast.
|
2016-04-18 13:47:37 +08:00
|
|
|
static void unescape_yaml(std::string *str) {
|
2014-07-30 05:41:21 +08:00
|
|
|
size_t cursor = 0, size = str->size();
|
2016-04-18 13:47:37 +08:00
|
|
|
while (cursor < size) {
|
2013-04-08 05:59:48 +08:00
|
|
|
// Operate on a const version of str, to avoid needless COWs that at() does.
|
2014-07-30 05:41:21 +08:00
|
|
|
const std::string &const_str = *str;
|
2013-05-05 17:33:17 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Look for a backslash.
|
2013-04-08 05:59:48 +08:00
|
|
|
size_t backslash = const_str.find('\\', cursor);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (backslash == std::string::npos || backslash + 1 >= size) {
|
|
|
|
// Either not found, or found as the last character.
|
2013-04-08 05:59:48 +08:00
|
|
|
break;
|
2016-04-18 13:47:37 +08:00
|
|
|
} else {
|
|
|
|
// Backslash found. Maybe we'll do something about it. Be sure to invoke the const
|
|
|
|
// version of at().
|
2013-04-08 05:59:48 +08:00
|
|
|
char escaped_char = const_str.at(backslash + 1);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (escaped_char == '\\') {
|
2013-04-08 05:59:48 +08:00
|
|
|
// Two backslashes in a row. Delete the second one.
|
2014-07-30 05:41:21 +08:00
|
|
|
str->erase(backslash + 1, 1);
|
2013-04-08 05:59:48 +08:00
|
|
|
size--;
|
2016-04-18 13:47:37 +08:00
|
|
|
} else if (escaped_char == 'n') {
|
2013-04-08 05:59:48 +08:00
|
|
|
// Backslash + n. Replace with a newline.
|
2014-07-30 05:41:21 +08:00
|
|
|
str->replace(backslash, 2, "\n");
|
2013-04-08 05:59:48 +08:00
|
|
|
size--;
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
2016-04-18 13:47:37 +08:00
|
|
|
// The character at index backslash has now been made whole; start at the next
|
|
|
|
// character.
|
2013-04-08 05:59:48 +08:00
|
|
|
cursor = backslash + 1;
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-06 12:54:41 +08:00
|
|
|
}
|
|
|
|
|
2017-07-01 08:13:02 +08:00
|
|
|
static wcstring history_filename(const wcstring &session_id, const wcstring &suffix) {
|
2017-07-01 12:03:05 +08:00
|
|
|
if (session_id.empty()) return L"";
|
|
|
|
|
|
|
|
wcstring result;
|
|
|
|
if (!path_get_data(result)) return L"";
|
|
|
|
|
|
|
|
result.append(L"/");
|
|
|
|
result.append(session_id);
|
|
|
|
result.append(L"_history");
|
|
|
|
result.append(suffix);
|
|
|
|
return result;
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::clear_file_state() {
|
2014-07-26 01:08:21 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
2016-04-18 13:47:37 +08:00
|
|
|
// Erase everything we know about our file.
|
|
|
|
if (mmap_start != NULL && mmap_start != MAP_FAILED) {
|
2012-02-16 16:24:27 +08:00
|
|
|
munmap((void *)mmap_start, mmap_length);
|
|
|
|
}
|
2012-03-02 06:56:34 +08:00
|
|
|
mmap_start = NULL;
|
2012-02-16 16:24:27 +08:00
|
|
|
mmap_length = 0;
|
|
|
|
loaded_old = false;
|
|
|
|
old_item_offsets.clear();
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::compact_new_items() {
|
|
|
|
// Keep only the most recent items with the given contents. This algorithm could be made more
|
|
|
|
// efficient, but likely would consume more memory too.
|
2017-08-20 04:29:52 +08:00
|
|
|
std::unordered_set<wcstring> seen;
|
2012-03-02 06:56:34 +08:00
|
|
|
size_t idx = new_items.size();
|
2016-04-18 13:47:37 +08:00
|
|
|
while (idx--) {
|
2012-03-02 06:56:34 +08:00
|
|
|
const history_item_t &item = new_items[idx];
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!seen.insert(item.contents).second) {
|
|
|
|
// This item was not inserted because it was already in the set, so delete the item at
|
|
|
|
// this index.
|
2012-03-02 06:56:34 +08:00
|
|
|
new_items.erase(new_items.begin() + idx);
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
if (idx < first_unwritten_new_item_index) {
|
|
|
|
// Decrement first_unwritten_new_item_index if we are deleting a previously written
|
|
|
|
// item.
|
2012-12-03 08:39:35 +08:00
|
|
|
first_unwritten_new_item_index--;
|
|
|
|
}
|
2012-03-02 06:56:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// Given the fd of an existing history file, or -1 if none, write
|
|
|
|
// a new history file to temp_fd. Returns true on success, false
|
|
|
|
// on error
|
|
|
|
bool history_t::rewrite_to_temporary_file(int existing_fd, int dst_fd) const {
|
2016-04-18 13:47:37 +08:00
|
|
|
// This must be called while locked.
|
2012-03-02 06:56:34 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// We are reading FROM existing_fd and writing TO dst_fd
|
|
|
|
// dst_fd must be valid; existing_fd does not need to be
|
|
|
|
assert(dst_fd >= 0);
|
|
|
|
|
|
|
|
// Make an LRU cache to save only the last N elements.
|
|
|
|
history_lru_cache_t lru(HISTORY_SAVE_MAX);
|
|
|
|
|
|
|
|
// Map in existing items (which may have changed out from underneath us, so don't trust our
|
|
|
|
// old mmap'd data).
|
|
|
|
const char *local_mmap_start = NULL;
|
|
|
|
size_t local_mmap_size = 0;
|
|
|
|
if (existing_fd >= 0 && map_fd(existing_fd, &local_mmap_start, &local_mmap_size)) {
|
|
|
|
const history_file_type_t local_mmap_type =
|
|
|
|
infer_file_type(local_mmap_start, local_mmap_size);
|
|
|
|
size_t cursor = 0;
|
|
|
|
for (;;) {
|
2017-02-08 13:52:35 +08:00
|
|
|
size_t offset =
|
|
|
|
offset_of_next_item(local_mmap_start, local_mmap_size, local_mmap_type, &cursor, 0);
|
2017-02-07 03:04:07 +08:00
|
|
|
// If we get back -1, we're done.
|
|
|
|
if (offset == (size_t)-1) break;
|
|
|
|
|
|
|
|
// Try decoding an old item.
|
2017-02-08 13:52:35 +08:00
|
|
|
const history_item_t old_item =
|
|
|
|
decode_item(local_mmap_start + offset, local_mmap_size - offset, local_mmap_type);
|
2017-02-07 03:04:07 +08:00
|
|
|
|
|
|
|
if (old_item.empty() || deleted_items.count(old_item.str()) > 0) {
|
|
|
|
// debug(0, L"Item is deleted : %s\n", old_item.str().c_str());
|
|
|
|
continue;
|
2012-03-02 06:56:34 +08:00
|
|
|
}
|
2017-02-07 03:04:07 +08:00
|
|
|
// Add this old item.
|
|
|
|
lru.add_item(old_item);
|
2012-02-24 02:49:30 +08:00
|
|
|
}
|
2017-02-07 03:04:07 +08:00
|
|
|
munmap((void *)local_mmap_start, local_mmap_size);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// Insert any unwritten new items
|
|
|
|
for (auto iter = new_items.cbegin() + this->first_unwritten_new_item_index;
|
2017-02-08 13:52:35 +08:00
|
|
|
iter != new_items.cend(); ++iter) {
|
2017-02-07 03:04:07 +08:00
|
|
|
lru.add_item(*iter);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// Stable-sort our items by timestamp
|
|
|
|
// This is because we may have read "old" items with a later timestamp than our "new" items
|
|
|
|
// This is the essential step that roughly orders items by history
|
2017-02-08 13:52:35 +08:00
|
|
|
lru.stable_sort([](const history_lru_item_t &item1, const history_lru_item_t &item2) {
|
2017-02-07 03:04:07 +08:00
|
|
|
return item1.timestamp < item2.timestamp;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Write them out.
|
|
|
|
bool ok = true;
|
|
|
|
history_output_buffer_t buffer(HISTORY_OUTPUT_BUFFER_SIZE);
|
|
|
|
for (const auto &key_item : lru) {
|
|
|
|
const history_lru_item_t &item = key_item.second;
|
|
|
|
append_yaml_to_buffer(item.text, item.timestamp, item.required_paths, &buffer);
|
|
|
|
if (buffer.output_size() >= HISTORY_OUTPUT_BUFFER_SIZE) {
|
|
|
|
ok = buffer.flush_to_fd(dst_fd);
|
2017-02-08 13:52:35 +08:00
|
|
|
if (!ok) {
|
2017-02-07 03:04:07 +08:00
|
|
|
debug(2, L"Error %d when writing to temporary history file", errno);
|
|
|
|
break;
|
2014-05-04 06:27:58 +08:00
|
|
|
}
|
2012-12-03 17:53:52 +08:00
|
|
|
}
|
2017-02-07 03:04:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
ok = buffer.flush_to_fd(dst_fd);
|
2017-02-08 13:52:35 +08:00
|
|
|
if (!ok) {
|
2017-02-07 03:04:07 +08:00
|
|
|
debug(2, L"Error %d when writing to temporary history file", errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// Returns the fd of an opened temporary file, or -1 on failure
|
|
|
|
static int create_temporary_file(const wcstring &name_template, wcstring *out_path) {
|
|
|
|
int out_fd = -1;
|
|
|
|
for (size_t attempt = 0; attempt < 10 && out_fd == -1; attempt++) {
|
2017-08-06 06:08:39 +08:00
|
|
|
char *narrow_str = wcs2str(name_template);
|
2017-02-07 03:04:07 +08:00
|
|
|
out_fd = fish_mkstemp_cloexec(narrow_str);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (out_fd >= 0) {
|
2017-02-07 03:04:07 +08:00
|
|
|
*out_path = str2wcstring(narrow_str);
|
|
|
|
}
|
|
|
|
free(narrow_str);
|
|
|
|
}
|
|
|
|
return out_fd;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
bool history_t::save_internal_via_rewrite() {
|
|
|
|
// This must be called while locked.
|
|
|
|
ASSERT_IS_LOCKED(lock);
|
|
|
|
bool ok = false;
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// We want to rewrite the file, while holding the lock for as briefly as possible
|
|
|
|
// To do this, we speculatively write a file, and then lock and see if our original file changed
|
|
|
|
// Repeat until we succeed or give up
|
|
|
|
const wcstring target_name = history_filename(name, wcstring());
|
|
|
|
const wcstring tmp_name_template = history_filename(name, L".XXXXXX");
|
|
|
|
if (target_name.empty() || tmp_name_template.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-26 17:19:51 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// Make our temporary file
|
|
|
|
// Remember that we have to close this fd!
|
|
|
|
wcstring tmp_name;
|
|
|
|
int tmp_fd = create_temporary_file(tmp_name_template, &tmp_name);
|
|
|
|
if (tmp_fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool done = false;
|
2017-02-08 13:52:35 +08:00
|
|
|
for (int i = 0; i < max_save_tries && !done; i++) {
|
2017-02-07 03:04:07 +08:00
|
|
|
// Open any target file, but do not lock it right away
|
|
|
|
int target_fd_before = wopen_cloexec(target_name, O_RDONLY | O_CREAT, history_file_mode);
|
2017-02-08 13:52:35 +08:00
|
|
|
file_id_t orig_file_id = file_id_for_fd(target_fd_before); // possibly invalid
|
2017-02-07 03:04:07 +08:00
|
|
|
bool wrote = this->rewrite_to_temporary_file(target_fd_before, tmp_fd);
|
|
|
|
if (target_fd_before >= 0) {
|
|
|
|
close(target_fd_before);
|
|
|
|
}
|
2017-02-08 13:52:35 +08:00
|
|
|
if (!wrote) {
|
2017-02-07 03:04:07 +08:00
|
|
|
// Failed to write, no good
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The crux! We rewrote the history file; see if the history file changed while we
|
|
|
|
// were rewriting it. Make an effort to take the lock before checking, to avoid racing.
|
|
|
|
// If the open fails, then proceed; this may be because there is no current history
|
|
|
|
file_id_t new_file_id = kInvalidFileID;
|
|
|
|
int target_fd_after = wopen_cloexec(target_name, O_RDONLY);
|
|
|
|
if (target_fd_after >= 0) {
|
|
|
|
// critical to take the lock before checking file IDs,
|
|
|
|
// and hold it until after we are done replacing
|
|
|
|
// Also critical to check the file at the path, NOT based on our fd
|
|
|
|
// It's only OK to replace the file while holding the lock
|
|
|
|
history_file_lock(target_fd_after, LOCK_EX);
|
|
|
|
new_file_id = file_id_for_path(target_name);
|
|
|
|
}
|
|
|
|
bool can_replace_file = (new_file_id == orig_file_id || new_file_id == kInvalidFileID);
|
2017-02-08 13:52:35 +08:00
|
|
|
if (!can_replace_file) {
|
2017-02-07 03:04:07 +08:00
|
|
|
// The file has changed, so we're going to re-read it
|
|
|
|
// Truncate our tmp_fd so we can reuse it
|
|
|
|
if (ftruncate(tmp_fd, 0) == -1 || lseek(tmp_fd, 0, SEEK_SET) == -1) {
|
|
|
|
debug(2, L"Error %d when truncating temporary history file", errno);
|
2013-04-28 06:21:14 +08:00
|
|
|
}
|
2017-02-08 13:52:35 +08:00
|
|
|
} else {
|
2017-02-07 03:04:07 +08:00
|
|
|
// The file is unchanged, or the new file doesn't exist or we can't read it
|
|
|
|
// We also attempted to take the lock, so we feel confident in replacing it
|
|
|
|
|
|
|
|
// Ensure we maintain the ownership and permissions of the original (#2355). If the
|
|
|
|
// stat fails, we assume (hope) our default permissions are correct. This
|
|
|
|
// corresponds to e.g. someone running sudo -E as the very first command. If they
|
|
|
|
// did, it would be tricky to set the permissions correctly. (bash doesn't get this
|
|
|
|
// case right either).
|
|
|
|
struct stat sbuf;
|
|
|
|
if (target_fd_after >= 0 && fstat(target_fd_after, &sbuf) >= 0) {
|
|
|
|
if (fchown(tmp_fd, sbuf.st_uid, sbuf.st_gid) == -1) {
|
|
|
|
debug(2, L"Error %d when changing ownership of history file", errno);
|
|
|
|
}
|
|
|
|
if (fchmod(tmp_fd, sbuf.st_mode) == -1) {
|
|
|
|
debug(2, L"Error %d when changing mode of history file", errno);
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// Slide it into place
|
|
|
|
if (wrename(tmp_name, target_name) == -1) {
|
|
|
|
debug(2, L"Error %d when renaming history file", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We did it
|
|
|
|
done = true;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
if (target_fd_after >= 0) {
|
|
|
|
close(target_fd_after);
|
|
|
|
}
|
2012-12-03 08:39:35 +08:00
|
|
|
}
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2017-02-07 03:04:07 +08:00
|
|
|
// Ensure we never leave the old file around
|
|
|
|
wunlink(tmp_name);
|
|
|
|
close(tmp_fd);
|
|
|
|
|
|
|
|
if (done) {
|
2016-04-18 13:47:37 +08:00
|
|
|
// We've saved everything, so we have no more unsaved items.
|
2012-12-03 15:38:38 +08:00
|
|
|
this->first_unwritten_new_item_index = new_items.size();
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// We deleted our deleted items.
|
2012-12-03 15:38:38 +08:00
|
|
|
this->deleted_items.clear();
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Our history has been written to the file, so clear our state so we can re-reference the
|
|
|
|
// file.
|
2012-12-03 15:38:38 +08:00
|
|
|
this->clear_file_state();
|
|
|
|
}
|
|
|
|
|
2012-12-03 08:39:35 +08:00
|
|
|
return ok;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-07 02:36:46 +08:00
|
|
|
// Function called to save our unwritten history file by appending to the existing history file
|
|
|
|
// Returns true on success, false on failure.
|
2016-04-18 13:47:37 +08:00
|
|
|
bool history_t::save_internal_via_appending() {
|
|
|
|
// This must be called while locked.
|
2012-12-03 08:39:35 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// No deleting allowed.
|
2012-12-03 15:38:38 +08:00
|
|
|
assert(deleted_items.empty());
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2012-12-03 08:39:35 +08:00
|
|
|
bool ok = false;
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// If the file is different (someone vacuumed it) then we need to update our mmap.
|
2012-12-03 15:38:38 +08:00
|
|
|
bool file_changed = false;
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Get the path to the real history file.
|
2012-12-03 08:39:35 +08:00
|
|
|
wcstring history_path = history_filename(name, wcstring());
|
2017-07-01 08:13:02 +08:00
|
|
|
if (history_path.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2017-02-07 02:36:46 +08:00
|
|
|
// We are going to open the file, lock it, append to it, and then close it
|
2017-02-08 13:52:35 +08:00
|
|
|
// After locking it, we need to stat the file at the path; if there is a new file there, it
|
|
|
|
// means
|
2017-02-07 02:36:46 +08:00
|
|
|
// the file was replaced and we have to try again
|
|
|
|
// Limit our max tries so we don't do this forever
|
|
|
|
int history_fd = -1;
|
2017-02-08 13:52:35 +08:00
|
|
|
for (int i = 0; i < max_save_tries; i++) {
|
2017-02-07 02:36:46 +08:00
|
|
|
int fd = wopen_cloexec(history_path, O_WRONLY | O_APPEND);
|
|
|
|
if (fd < 0) {
|
|
|
|
// can't open, we're hosed
|
|
|
|
break;
|
|
|
|
}
|
2016-04-18 13:47:37 +08:00
|
|
|
// Exclusive lock on the entire file. This is released when we close the file (below). This
|
|
|
|
// may fail on (e.g.) lockless NFS. If so, proceed as if it did not fail; the risk is that
|
|
|
|
// we may get interleaved history items, which is considered better than no history, or
|
|
|
|
// forcing everything through the slow copy-move mode. We try to minimize this possibility
|
|
|
|
// by writing with O_APPEND.
|
|
|
|
//
|
|
|
|
// Simulate a failing lock in chaos_mode
|
2017-02-07 02:36:46 +08:00
|
|
|
if (!chaos_mode) history_file_lock(fd, LOCK_EX);
|
|
|
|
const file_id_t file_id = file_id_for_fd(fd);
|
|
|
|
if (file_id_for_path(history_path) != file_id) {
|
|
|
|
// The file has changed, we're going to retry
|
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
// File IDs match, so the file we opened is still at that path
|
|
|
|
// We're going to use this fd
|
|
|
|
if (file_id != this->mmap_file_id) {
|
|
|
|
file_changed = true;
|
|
|
|
}
|
|
|
|
history_fd = fd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 13:47:37 +08:00
|
|
|
|
2017-02-07 02:36:46 +08:00
|
|
|
if (history_fd >= 0) {
|
2016-04-18 13:47:37 +08:00
|
|
|
// We (hopefully successfully) took the exclusive lock. Append to the file.
|
|
|
|
// Note that this is sketchy for a few reasons:
|
|
|
|
// - Another shell may have appended its own items with a later timestamp, so our file may
|
|
|
|
// no longer be sorted by timestamp.
|
|
|
|
// - Another shell may have appended the same items, so our file may now contain
|
|
|
|
// duplicates.
|
|
|
|
//
|
|
|
|
// We cannot modify any previous parts of our file, because other instances may be reading
|
|
|
|
// those portions. We can only append.
|
|
|
|
//
|
|
|
|
// Originally we always rewrote the file on saving, which avoided both of these problems.
|
|
|
|
// However, appending allows us to save history after every command, which is nice!
|
|
|
|
//
|
|
|
|
// Periodically we "clean up" the file by rewriting it, so that most of the time it doesn't
|
|
|
|
// have duplicates, although we don't yet sort by timestamp (the timestamp isn't really used
|
|
|
|
// for much anyways).
|
|
|
|
|
|
|
|
// So far so good. Write all items at or after first_unwritten_new_item_index. Note that we
|
|
|
|
// write even a pending item - pending items are ignored by history within the command
|
|
|
|
// itself, but should still be written to the file.
|
2017-02-07 02:36:46 +08:00
|
|
|
// TODO: consider filling the buffer ahead of time, so we can just lock, splat, and unlock?
|
2013-04-28 06:21:14 +08:00
|
|
|
bool errored = false;
|
2017-02-07 02:36:46 +08:00
|
|
|
// Use a small buffer size for appending, we usually only have 1 item
|
|
|
|
history_output_buffer_t buffer(64);
|
2016-04-18 13:47:37 +08:00
|
|
|
while (first_unwritten_new_item_index < new_items.size()) {
|
2013-04-28 06:21:14 +08:00
|
|
|
const history_item_t &item = new_items.at(first_unwritten_new_item_index);
|
|
|
|
append_yaml_to_buffer(item.str(), item.timestamp(), item.get_required_paths(), &buffer);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (buffer.output_size() >= HISTORY_OUTPUT_BUFFER_SIZE) {
|
2017-02-07 02:36:46 +08:00
|
|
|
errored = !buffer.flush_to_fd(history_fd);
|
2013-04-28 06:21:14 +08:00
|
|
|
if (errored) break;
|
|
|
|
}
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// We wrote this item, hooray.
|
2013-04-28 06:21:14 +08:00
|
|
|
first_unwritten_new_item_index++;
|
|
|
|
}
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2017-02-07 02:36:46 +08:00
|
|
|
if (!errored && buffer.flush_to_fd(history_fd)) {
|
2013-04-28 06:21:14 +08:00
|
|
|
ok = true;
|
2012-12-03 08:39:35 +08:00
|
|
|
}
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2017-02-07 02:36:46 +08:00
|
|
|
// Since we just modified the file, update our mmap_file_id to match its current state
|
2017-02-08 13:52:35 +08:00
|
|
|
// Otherwise we'll think the file has been changed by someone else the next time we go to
|
|
|
|
// write
|
|
|
|
// We don't update the mapping since we only appended to the file, and everything we
|
|
|
|
// appended
|
2017-02-07 02:36:46 +08:00
|
|
|
// remains in our new_items
|
|
|
|
this->mmap_file_id = file_id_for_fd(history_fd);
|
|
|
|
|
|
|
|
close(history_fd);
|
2013-04-28 06:21:14 +08:00
|
|
|
}
|
2012-02-06 08:42:24 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// If someone has replaced the file, forget our file state.
|
|
|
|
if (file_changed) {
|
2012-12-03 15:38:38 +08:00
|
|
|
this->clear_file_state();
|
|
|
|
}
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2012-12-03 08:39:35 +08:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Save the specified mode to file; optionally also vacuums.
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::save_internal(bool vacuum) {
|
2012-12-03 08:39:35 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Nothing to do if there's no new items.
|
|
|
|
if (first_unwritten_new_item_index >= new_items.size() && deleted_items.empty()) return;
|
2012-12-03 08:39:35 +08:00
|
|
|
|
2017-07-01 08:13:02 +08:00
|
|
|
if (history_filename(name, L"").empty()) {
|
|
|
|
// We're in the "incognito" mode. Pretend we've saved the history.
|
|
|
|
this->first_unwritten_new_item_index = new_items.size();
|
|
|
|
this->deleted_items.clear();
|
|
|
|
this->clear_file_state();
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Compact our new items so we don't have duplicates.
|
2012-12-03 08:39:35 +08:00
|
|
|
this->compact_new_items();
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Try saving. If we have items to delete, we have to rewrite the file. If we do not, we can
|
|
|
|
// append to it.
|
2012-12-03 08:39:35 +08:00
|
|
|
bool ok = false;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!vacuum && deleted_items.empty()) {
|
|
|
|
// Try doing a fast append.
|
2012-12-03 15:38:38 +08:00
|
|
|
ok = save_internal_via_appending();
|
|
|
|
}
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!ok) {
|
|
|
|
// We did not or could not append; rewrite the file ("vacuum" it).
|
2016-06-15 10:21:50 +08:00
|
|
|
this->save_internal_via_rewrite();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
2012-02-06 12:54:41 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::save(void) {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2012-12-03 15:38:38 +08:00
|
|
|
this->save_internal(false);
|
|
|
|
}
|
|
|
|
|
2016-07-14 13:33:50 +08:00
|
|
|
// Formats a single history record, including a trailing newline. Returns true
|
|
|
|
// if bytes were written to the output stream and false otherwise.
|
2016-09-19 11:21:27 +08:00
|
|
|
static bool format_history_record(const history_item_t &item, const wchar_t *show_time_format,
|
2016-10-17 11:33:33 +08:00
|
|
|
bool null_terminate, io_streams_t &streams) {
|
2016-09-19 11:21:27 +08:00
|
|
|
if (show_time_format) {
|
2016-07-14 13:33:50 +08:00
|
|
|
const time_t seconds = item.timestamp();
|
|
|
|
struct tm timestamp;
|
|
|
|
if (!localtime_r(&seconds, ×tamp)) return false;
|
2016-09-19 11:21:27 +08:00
|
|
|
const int max_tstamp_length = 100;
|
|
|
|
wchar_t timestamp_string[max_tstamp_length + 1];
|
|
|
|
if (std::wcsftime(timestamp_string, max_tstamp_length, show_time_format, ×tamp) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-25 12:22:32 +08:00
|
|
|
streams.out.append(timestamp_string);
|
2016-07-14 13:33:50 +08:00
|
|
|
}
|
|
|
|
streams.out.append(item.str());
|
2016-11-15 13:31:51 +08:00
|
|
|
if (null_terminate) {
|
|
|
|
streams.out.append(L'\0');
|
|
|
|
} else {
|
|
|
|
streams.out.append(L'\n');
|
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-22 10:36:32 +08:00
|
|
|
bool history_t::search(history_search_type_t search_type, wcstring_list_t search_args,
|
2016-09-24 11:12:15 +08:00
|
|
|
const wchar_t *show_time_format, long max_items, bool case_sensitive,
|
2016-10-17 11:33:33 +08:00
|
|
|
bool null_terminate, io_streams_t &streams) {
|
2016-07-21 13:30:58 +08:00
|
|
|
// scoped_lock locker(lock);
|
2016-07-14 13:33:50 +08:00
|
|
|
if (search_args.empty()) {
|
|
|
|
// Start at one because zero is the current command.
|
2016-09-21 13:32:38 +08:00
|
|
|
for (int i = 1; !this->item_at_index(i).empty() && max_items; ++i, --max_items) {
|
2016-10-17 11:33:33 +08:00
|
|
|
if (!format_history_record(this->item_at_index(i), show_time_format, null_terminate,
|
|
|
|
streams)) {
|
2016-09-19 11:21:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-14 13:33:50 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (wcstring_list_t::const_iterator iter = search_args.begin(); iter != search_args.end();
|
|
|
|
++iter) {
|
|
|
|
const wcstring &search_string = *iter;
|
|
|
|
if (search_string.empty()) {
|
|
|
|
streams.err.append_format(L"Searching for the empty string isn't allowed");
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-24 11:12:15 +08:00
|
|
|
history_search_t searcher =
|
|
|
|
history_search_t(*this, search_string, search_type, case_sensitive);
|
2016-07-14 13:33:50 +08:00
|
|
|
while (searcher.go_backwards()) {
|
2016-10-17 11:33:33 +08:00
|
|
|
if (!format_history_record(searcher.current_item(), show_time_format, null_terminate,
|
|
|
|
streams)) {
|
2016-07-14 13:33:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-21 13:32:38 +08:00
|
|
|
if (--max_items == 0) return true;
|
2016-07-14 13:33:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::disable_automatic_saving() {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2014-03-29 14:22:03 +08:00
|
|
|
disable_automatic_save_counter++;
|
2016-04-18 13:47:37 +08:00
|
|
|
assert(disable_automatic_save_counter != 0); // overflow!
|
2014-03-29 14:22:03 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::enable_automatic_saving() {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2016-04-18 13:47:37 +08:00
|
|
|
assert(disable_automatic_save_counter > 0); // underflow
|
2014-03-29 14:22:03 +08:00
|
|
|
disable_automatic_save_counter--;
|
|
|
|
save_internal_unless_disabled();
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::clear(void) {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2012-02-16 16:24:27 +08:00
|
|
|
new_items.clear();
|
2012-06-05 12:24:42 +08:00
|
|
|
deleted_items.clear();
|
2012-12-03 08:39:35 +08:00
|
|
|
first_unwritten_new_item_index = 0;
|
2012-02-16 16:24:27 +08:00
|
|
|
old_item_offsets.clear();
|
|
|
|
wcstring filename = history_filename(name, L"");
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!filename.empty()) wunlink(filename);
|
2012-02-16 16:24:27 +08:00
|
|
|
this->clear_file_state();
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
bool history_t::is_empty(void) {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2012-12-06 05:33:07 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// If we have new items, we're not empty.
|
|
|
|
if (!new_items.empty()) return false;
|
2012-12-05 08:00:35 +08:00
|
|
|
|
|
|
|
bool empty = false;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (loaded_old) {
|
|
|
|
// If we've loaded old items, see if we have any offsets.
|
2012-12-05 08:00:35 +08:00
|
|
|
empty = old_item_offsets.empty();
|
2016-04-18 13:47:37 +08:00
|
|
|
} else {
|
|
|
|
// If we have not loaded old items, don't actually load them (which may be expensive); just
|
|
|
|
// stat the file and see if it exists and is nonempty.
|
2012-12-05 08:00:35 +08:00
|
|
|
const wcstring where = history_filename(name, L"");
|
2017-07-01 08:13:02 +08:00
|
|
|
if (where.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-05 08:00:35 +08:00
|
|
|
struct stat buf = {};
|
2016-04-18 13:47:37 +08:00
|
|
|
if (wstat(where, &buf) != 0) {
|
|
|
|
// Access failed, assume missing.
|
2012-12-05 08:00:35 +08:00
|
|
|
empty = true;
|
2016-04-18 13:47:37 +08:00
|
|
|
} else {
|
|
|
|
// We're empty if the file is empty.
|
2012-12-05 08:00:35 +08:00
|
|
|
empty = (buf.st_size == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return empty;
|
2012-07-10 13:54:08 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Populates from older location (in config path, rather than data path) This is accomplished by
|
|
|
|
/// clearing ourselves, and copying the contents of the old history file to the new history file.
|
|
|
|
/// The new contents will automatically be re-mapped later.
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::populate_from_config_path() {
|
2017-07-01 08:13:02 +08:00
|
|
|
wcstring new_file = history_filename(name, wcstring());
|
|
|
|
if (new_file.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-19 11:47:38 +08:00
|
|
|
wcstring old_file;
|
|
|
|
if (path_get_config(old_file)) {
|
|
|
|
old_file.append(L"/");
|
|
|
|
old_file.append(name);
|
|
|
|
old_file.append(L"_history");
|
|
|
|
int src_fd = wopen_cloexec(old_file, O_RDONLY, 0);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (src_fd != -1) {
|
|
|
|
// Clear must come after we've retrieved the new_file name, and before we open
|
|
|
|
// destination file descriptor, since it destroys the name and the file.
|
2015-09-19 11:47:38 +08:00
|
|
|
this->clear();
|
|
|
|
|
2017-02-07 02:36:46 +08:00
|
|
|
int dst_fd = wopen_cloexec(new_file, O_WRONLY | O_CREAT, history_file_mode);
|
2015-09-19 11:47:38 +08:00
|
|
|
char buf[BUFSIZ];
|
2016-03-04 10:49:12 +08:00
|
|
|
ssize_t size;
|
2015-09-19 11:47:38 +08:00
|
|
|
while ((size = read(src_fd, buf, BUFSIZ)) > 0) {
|
2016-03-04 10:49:12 +08:00
|
|
|
ssize_t written = write(dst_fd, buf, static_cast<size_t>(size));
|
|
|
|
if (written < 0) {
|
2016-04-18 13:47:37 +08:00
|
|
|
// This message does not have high enough priority to be shown by default.
|
2015-09-19 11:47:38 +08:00
|
|
|
debug(2, L"Error when writing history file");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(src_fd);
|
|
|
|
close(dst_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 12:50:57 +08:00
|
|
|
/// Decide whether we ought to import a bash history line into fish. This is a very crude heuristic.
|
2016-04-18 13:47:37 +08:00
|
|
|
static bool should_import_bash_history_line(const std::string &line) {
|
|
|
|
if (line.empty()) return false;
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2017-05-21 12:03:31 +08:00
|
|
|
parse_node_tree_t parse_tree;
|
|
|
|
wcstring wide_line = str2wcstring(line);
|
|
|
|
if (!parse_tree_from_string(wide_line, parse_flag_none, &parse_tree, NULL)) return false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-05-21 12:03:31 +08:00
|
|
|
// In doing this test do not allow incomplete strings. Hence the "false" argument.
|
|
|
|
parse_error_list_t errors;
|
|
|
|
parse_util_detect_errors(wide_line, &errors, false);
|
|
|
|
if (!errors.empty()) return false;
|
|
|
|
|
|
|
|
// The following are Very naive tests!
|
|
|
|
|
|
|
|
// Skip comments.
|
|
|
|
if (line[0] == '#') return false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Skip lines with backticks.
|
|
|
|
if (line.find('`') != std::string::npos) return false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-05-21 12:03:31 +08:00
|
|
|
// Skip lines with [[...]] and ((...)) since we don't handle those constructs.
|
|
|
|
if (line.find("[[") != std::string::npos) return false;
|
|
|
|
if (line.find("]]") != std::string::npos) return false;
|
|
|
|
if (line.find("((") != std::string::npos) return false;
|
|
|
|
if (line.find("))") != std::string::npos) return false;
|
|
|
|
|
|
|
|
// Skip lines that end with a backslash. We do not handle multiline commands from bash history.
|
2017-02-09 12:50:57 +08:00
|
|
|
if (line.back() == '\\') return false;
|
|
|
|
|
2012-06-16 07:22:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-09 12:50:57 +08:00
|
|
|
/// Import a bash command history file. Bash's history format is very simple: just lines with #s for
|
|
|
|
/// comments. Ignore a few commands that are bash-specific. It makes no attempt to handle multiline
|
|
|
|
/// commands. We can't actually parse bash syntax and the bash history file does not unambiguously
|
|
|
|
/// encode multiline commands.
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::populate_from_bash(FILE *stream) {
|
2017-07-01 11:24:55 +08:00
|
|
|
// We do not import bash history if an alternative fish history file is being used.
|
|
|
|
if (history_session_id() != DFLT_FISH_HISTORY_SESSION_ID) return;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-09 12:50:57 +08:00
|
|
|
// Process the entire history file until EOF is observed.
|
2017-07-01 11:24:55 +08:00
|
|
|
bool eof = false;
|
2017-02-09 12:50:57 +08:00
|
|
|
while (!eof) {
|
|
|
|
auto line = std::string();
|
|
|
|
|
|
|
|
// Loop until we've read a line or EOF is observed.
|
|
|
|
while (true) {
|
2012-06-16 07:22:37 +08:00
|
|
|
char buff[128];
|
2017-02-09 12:50:57 +08:00
|
|
|
if (!fgets(buff, sizeof buff, stream)) {
|
|
|
|
eof = true;
|
|
|
|
break;
|
2012-06-16 07:22:37 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-09 12:50:57 +08:00
|
|
|
// Deal with the newline if present.
|
|
|
|
char *a_newline = strchr(buff, '\n');
|
|
|
|
if (a_newline) *a_newline = '\0';
|
|
|
|
line.append(buff);
|
|
|
|
if (a_newline) break;
|
2012-06-16 07:22:37 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2017-02-09 12:50:57 +08:00
|
|
|
// Add this line if it doesn't contain anything we know we can't handle.
|
|
|
|
if (should_import_bash_history_line(line)) this->add(str2wcstring(line));
|
2012-06-16 07:22:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::incorporate_external_changes() {
|
|
|
|
// To incorporate new items, we simply update our timestamp to now, so that items from previous
|
|
|
|
// instances get added. We then clear the file state so that we remap the file. Note that this
|
|
|
|
// is somehwhat expensive because we will be going back over old items. An optimization would be
|
|
|
|
// to preserve old_item_offsets so that they don't have to be recomputed. (However, then items
|
|
|
|
// *deleted* in other instances would not show up here).
|
2014-07-26 01:08:21 +08:00
|
|
|
time_t new_timestamp = time(NULL);
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2014-07-26 01:08:21 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// If for some reason the clock went backwards, we don't want to start dropping items; therefore
|
|
|
|
// we only do work if time has progressed. This also makes multiple calls cheap.
|
|
|
|
if (new_timestamp > this->boundary_timestamp) {
|
2014-07-26 01:08:21 +08:00
|
|
|
this->boundary_timestamp = new_timestamp;
|
|
|
|
this->clear_file_state();
|
2016-06-24 13:02:39 +08:00
|
|
|
|
|
|
|
// We also need to erase new_items, since we go through those first, and that means we
|
|
|
|
// will not properly interleave them with items from other instances.
|
|
|
|
// We'll pick them up from the file (#2312)
|
|
|
|
this->save_internal(false);
|
|
|
|
this->new_items.clear();
|
2016-11-20 14:39:58 +08:00
|
|
|
this->first_unwritten_new_item_index = 0;
|
2014-07-26 01:08:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_init() {}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_collection_t::save() {
|
2017-01-30 05:21:50 +08:00
|
|
|
// Save all histories
|
2017-08-19 03:26:35 +08:00
|
|
|
auto &&h = histories.acquire();
|
2017-01-30 05:21:50 +08:00
|
|
|
for (auto &p : h.value) {
|
|
|
|
p.second->save();
|
2012-02-06 12:54:41 +08:00
|
|
|
}
|
2006-10-21 06:36:49 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_destroy() { histories.save(); }
|
2006-10-21 06:36:49 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_sanity_check() {
|
|
|
|
// No sanity checking implemented yet...
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2017-07-01 12:03:05 +08:00
|
|
|
/// Return the prefix for the files to be used for command and read history.
|
2017-07-01 08:13:02 +08:00
|
|
|
wcstring history_session_id() {
|
2017-07-01 11:24:55 +08:00
|
|
|
wcstring result = DFLT_FISH_HISTORY_SESSION_ID;
|
2017-07-01 08:13:02 +08:00
|
|
|
|
2017-08-06 09:22:49 +08:00
|
|
|
const env_var_t var = env_get(L"FISH_HISTORY");
|
|
|
|
if (!var.missing()) {
|
|
|
|
wcstring session_id = var.as_string();
|
2017-07-01 08:13:02 +08:00
|
|
|
if (session_id.empty()) {
|
|
|
|
result = L"";
|
|
|
|
} else if (session_id == L"default") {
|
|
|
|
; // using the default value
|
|
|
|
} else if (valid_var_name(session_id)) {
|
|
|
|
result = session_id;
|
|
|
|
} else {
|
2017-08-19 03:26:35 +08:00
|
|
|
debug(0, _(L"History session ID '%ls' is not a valid variable name. "
|
|
|
|
L"Falling back to `%ls`."),
|
2017-07-01 08:13:02 +08:00
|
|
|
session_id.c_str(), result.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-25 09:52:28 +08:00
|
|
|
path_list_t valid_paths(const path_list_t &paths, const wcstring &working_directory) {
|
2012-02-16 16:24:27 +08:00
|
|
|
ASSERT_IS_BACKGROUND_THREAD();
|
2017-01-25 09:52:28 +08:00
|
|
|
wcstring_list_t result;
|
|
|
|
for (const wcstring &path : paths) {
|
|
|
|
if (path_is_valid(path, working_directory)) {
|
|
|
|
result.push_back(path);
|
2012-02-16 03:33:41 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-16 16:24:27 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-25 09:52:28 +08:00
|
|
|
bool all_paths_are_valid(const path_list_t &paths, const wcstring &working_directory) {
|
2012-02-16 03:33:41 +08:00
|
|
|
ASSERT_IS_BACKGROUND_THREAD();
|
2017-01-25 09:52:28 +08:00
|
|
|
for (const wcstring &path : paths) {
|
2017-01-27 12:00:43 +08:00
|
|
|
if (!path_is_valid(path, working_directory)) {
|
2017-01-25 09:52:28 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2012-02-16 03:33:41 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
static bool string_could_be_path(const wcstring &potential_path) {
|
|
|
|
// Assume that things with leading dashes aren't paths.
|
|
|
|
if (potential_path.empty() || potential_path.at(0) == L'-') {
|
2012-02-16 03:33:41 +08:00
|
|
|
return false;
|
2014-03-29 14:22:03 +08:00
|
|
|
}
|
2012-02-16 03:33:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::add_pending_with_file_detection(const wcstring &str) {
|
2012-02-16 03:33:41 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Find all arguments that look like they could be file paths.
|
2013-02-03 06:50:22 +08:00
|
|
|
bool impending_exit = false;
|
2014-03-29 14:22:03 +08:00
|
|
|
parse_node_tree_t tree;
|
|
|
|
parse_tree_from_string(str, parse_flag_none, &tree, NULL);
|
|
|
|
size_t count = tree.size();
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2017-01-25 09:52:28 +08:00
|
|
|
path_list_t potential_paths;
|
2016-04-18 13:47:37 +08:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2014-03-29 14:22:03 +08:00
|
|
|
const parse_node_t &node = tree.at(i);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!node.has_source()) {
|
2014-03-29 14:22:03 +08:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
if (node.type == symbol_argument) {
|
2014-03-29 14:22:03 +08:00
|
|
|
wcstring potential_path = node.get_source(str);
|
|
|
|
bool unescaped = unescape_string_in_place(&potential_path, UNESCAPE_DEFAULT);
|
2016-04-18 13:47:37 +08:00
|
|
|
if (unescaped && string_could_be_path(potential_path)) {
|
2014-03-29 14:22:03 +08:00
|
|
|
potential_paths.push_back(potential_path);
|
|
|
|
}
|
2016-04-18 13:47:37 +08:00
|
|
|
} else if (node.type == symbol_plain_statement) {
|
|
|
|
// Hack hack hack - if the command is likely to trigger an exit, then don't do
|
|
|
|
// background file detection, because we won't be able to write it to our history file
|
|
|
|
// before we exit.
|
|
|
|
if (tree.decoration_for_plain_statement(node) == parse_statement_decoration_exec) {
|
2014-03-29 14:22:03 +08:00
|
|
|
impending_exit = true;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-03-29 14:22:03 +08:00
|
|
|
wcstring command;
|
|
|
|
tree.command_for_plain_statement(node, str, &command);
|
|
|
|
unescape_string_in_place(&command, UNESCAPE_DEFAULT);
|
2017-04-05 12:28:57 +08:00
|
|
|
if (command == L"exit" || command == L"reboot") {
|
2014-03-29 14:22:03 +08:00
|
|
|
impending_exit = true;
|
2012-02-16 03:33:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// If we got a path, we'll perform file detection for autosuggestion hinting.
|
2014-03-29 14:22:03 +08:00
|
|
|
history_identifier_t identifier = 0;
|
2016-04-18 13:47:37 +08:00
|
|
|
if (!potential_paths.empty() && !impending_exit) {
|
|
|
|
// Grab the next identifier.
|
2014-03-29 14:22:03 +08:00
|
|
|
static history_identifier_t sLastIdentifier = 0;
|
|
|
|
identifier = ++sLastIdentifier;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Prevent saving until we're done, so we have time to get the paths.
|
2014-03-29 14:22:03 +08:00
|
|
|
this->disable_automatic_saving();
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2017-01-25 09:52:28 +08:00
|
|
|
// Check for which paths are valid on a background thread,
|
|
|
|
// then on the main thread update our history item
|
|
|
|
const wcstring wd = env_get_pwd_slash();
|
2017-01-27 12:00:43 +08:00
|
|
|
iothread_perform([=]() { return valid_paths(potential_paths, wd); },
|
|
|
|
[=](path_list_t validated_paths) {
|
|
|
|
this->set_valid_file_paths(validated_paths, identifier);
|
|
|
|
this->enable_automatic_saving();
|
|
|
|
});
|
2012-02-16 03:33:41 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// Actually add the item to the history.
|
2015-04-20 17:04:17 +08:00
|
|
|
this->add(str, identifier, true /* pending */);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-04-18 13:47:37 +08:00
|
|
|
// If we think we're about to exit, save immediately, regardless of any disabling. This may
|
|
|
|
// cause us to lose file hinting for some commands, but it beats losing history items.
|
|
|
|
if (impending_exit) {
|
2014-03-29 14:22:03 +08:00
|
|
|
this->save();
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 17:04:17 +08:00
|
|
|
|
2016-04-20 09:17:39 +08:00
|
|
|
/// Very simple, just mark that we have no more pending items.
|
2016-04-18 13:47:37 +08:00
|
|
|
void history_t::resolve_pending() {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2015-04-20 17:04:17 +08:00
|
|
|
this->has_pending_item = false;
|
|
|
|
}
|