mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-01-21 00:51:43 +08:00
Reorganize env_universal_t so that the public bits are at the top
No functional change here.
This commit is contained in:
parent
0d8cb0125a
commit
fa7402c415
|
@ -253,7 +253,7 @@ static wcstring encode_serialized(const wcstring_list_t &vals) {
|
|||
}
|
||||
|
||||
env_universal_t::env_universal_t(wcstring path, bool load_legacy)
|
||||
: vars_path(std::move(path)), load_legacy(load_legacy) {}
|
||||
: vars_path_(std::move(path)), load_legacy_(load_legacy) {}
|
||||
|
||||
env_universal_t::env_universal_t() : env_universal_t(default_vars_path(), true /* load_legacy */) {}
|
||||
|
||||
|
@ -491,15 +491,15 @@ bool env_universal_t::move_new_vars_file_into_place(const wcstring &src, const w
|
|||
}
|
||||
|
||||
void env_universal_t::initialize(callback_data_list_t &callbacks) {
|
||||
if (vars_path.empty()) return;
|
||||
if (vars_path_.empty()) return;
|
||||
scoped_lock locker(lock);
|
||||
|
||||
if (load_from_path(vars_path, callbacks)) {
|
||||
if (load_from_path(vars_path_, callbacks)) {
|
||||
// Successfully loaded from our normal path.
|
||||
return;
|
||||
}
|
||||
|
||||
if (errno == ENOENT && load_legacy) {
|
||||
if (errno == ENOENT && load_legacy_) {
|
||||
// We failed to load, because the file was not found. Attempt to load from our legacy paths.
|
||||
if (auto dir = default_vars_path_directory()) {
|
||||
for (const wcstring &path : get_legacy_paths(*dir)) {
|
||||
|
@ -631,7 +631,7 @@ bool env_universal_t::open_and_acquire_lock(const wcstring &path, autoclose_fd_t
|
|||
// Returns true if modified variables were written, false if not. (There may still be variable
|
||||
// changes due to other processes on a false return).
|
||||
bool env_universal_t::sync(callback_data_list_t &callbacks) {
|
||||
if (vars_path.empty()) return false;
|
||||
if (vars_path_.empty()) return false;
|
||||
|
||||
FLOGF(uvar_file, L"universal log sync");
|
||||
scoped_lock locker(lock);
|
||||
|
@ -666,18 +666,18 @@ bool env_universal_t::sync(callback_data_list_t &callbacks) {
|
|||
// with fire anyways.
|
||||
// If we have no changes, just load.
|
||||
if (modified.empty()) {
|
||||
this->load_from_path(vars_path, callbacks);
|
||||
this->load_from_path(vars_path_, callbacks);
|
||||
FLOGF(uvar_file, L"universal log no modifications");
|
||||
return false;
|
||||
}
|
||||
|
||||
const wcstring directory = wdirname(vars_path);
|
||||
const wcstring directory = wdirname(vars_path_);
|
||||
autoclose_fd_t vars_fd{};
|
||||
|
||||
FLOGF(uvar_file, L"universal log performing full sync");
|
||||
|
||||
// Open the file.
|
||||
if (!this->open_and_acquire_lock(vars_path, &vars_fd)) {
|
||||
if (!this->open_and_acquire_lock(vars_path_, &vars_fd)) {
|
||||
FLOGF(uvar_file, L"universal log open_and_acquire_lock() failed");
|
||||
return false;
|
||||
}
|
||||
|
@ -687,7 +687,7 @@ bool env_universal_t::sync(callback_data_list_t &callbacks) {
|
|||
this->load_from_fd(vars_fd.fd(), callbacks);
|
||||
|
||||
if (ok_to_save) {
|
||||
return this->save(directory, vars_path);
|
||||
return this->save(directory, vars_path_);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -39,62 +39,6 @@ bool get_hostname_identifier(wcstring &result);
|
|||
|
||||
/// Class representing universal variables.
|
||||
class env_universal_t {
|
||||
// Path that we save to.
|
||||
const wcstring vars_path;
|
||||
|
||||
// Whether to load from legacy paths.
|
||||
const bool load_legacy;
|
||||
|
||||
// The table of variables. Note this is sorted; this ensures that the output file is in sorted
|
||||
// order.
|
||||
var_table_t vars;
|
||||
|
||||
// Keys that have been modified, and need to be written. A value here that is not present in
|
||||
// vars indicates a deleted value.
|
||||
std::unordered_set<wcstring> modified;
|
||||
|
||||
// A generation count which is incremented every time an exported variable is modified.
|
||||
uint64_t export_generation{1};
|
||||
|
||||
// Whether it's OK to save. This may be set to false if we discover that a future version of
|
||||
// fish wrote the uvars contents.
|
||||
bool ok_to_save{true};
|
||||
|
||||
mutable std::mutex lock;
|
||||
bool load_from_path(const wcstring &path, callback_data_list_t &callbacks);
|
||||
void load_from_fd(int fd, callback_data_list_t &callbacks);
|
||||
|
||||
void set_internal(const wcstring &key, const env_var_t &var);
|
||||
bool remove_internal(const wcstring &key);
|
||||
|
||||
// Functions concerned with saving.
|
||||
bool open_and_acquire_lock(const wcstring &path, autoclose_fd_t *out_fd);
|
||||
autoclose_fd_t open_temporary_file(const wcstring &directory, wcstring *out_path);
|
||||
bool write_to_fd(int fd, const wcstring &path);
|
||||
bool move_new_vars_file_into_place(const wcstring &src, const wcstring &dst);
|
||||
|
||||
// File id from which we last read.
|
||||
file_id_t last_read_file = kInvalidFileID;
|
||||
|
||||
// Given a variable table, generate callbacks representing the difference between our vars and
|
||||
// the new vars. Also update our exports generation count as necessary.
|
||||
void generate_callbacks_and_update_exports(const var_table_t &new_vars,
|
||||
callback_data_list_t &callbacks);
|
||||
|
||||
// Given a variable table, copy unmodified values into self.
|
||||
void acquire_variables(var_table_t &&vars_to_acquire);
|
||||
|
||||
static bool populate_1_variable(const wchar_t *input, env_var_t::env_var_flags_t flags,
|
||||
var_table_t *vars, wcstring *storage);
|
||||
|
||||
static void parse_message_2x_internal(const wcstring &msg, var_table_t *vars,
|
||||
wcstring *storage);
|
||||
static void parse_message_30_internal(const wcstring &msg, var_table_t *vars,
|
||||
wcstring *storage);
|
||||
static uvar_format_t read_message_internal(int fd, var_table_t *vars);
|
||||
|
||||
bool save(const wcstring &directory, const wcstring &vars_path);
|
||||
|
||||
public:
|
||||
// Construct referencing a path \p path.
|
||||
// If \p load_legacy is true, then attempt to load from legacy paths as well.
|
||||
|
@ -144,6 +88,62 @@ class env_universal_t {
|
|||
|
||||
/// Access the export generation.
|
||||
uint64_t get_export_generation() const;
|
||||
|
||||
private:
|
||||
// Path that we save to.
|
||||
const wcstring vars_path_;
|
||||
|
||||
// Whether to load from legacy paths.
|
||||
const bool load_legacy_;
|
||||
|
||||
// The table of variables.
|
||||
var_table_t vars;
|
||||
|
||||
// Keys that have been modified, and need to be written. A value here that is not present in
|
||||
// vars indicates a deleted value.
|
||||
std::unordered_set<wcstring> modified;
|
||||
|
||||
// A generation count which is incremented every time an exported variable is modified.
|
||||
uint64_t export_generation{1};
|
||||
|
||||
// Whether it's OK to save. This may be set to false if we discover that a future version of
|
||||
// fish wrote the uvars contents.
|
||||
bool ok_to_save{true};
|
||||
|
||||
mutable std::mutex lock;
|
||||
bool load_from_path(const wcstring &path, callback_data_list_t &callbacks);
|
||||
void load_from_fd(int fd, callback_data_list_t &callbacks);
|
||||
|
||||
void set_internal(const wcstring &key, const env_var_t &var);
|
||||
bool remove_internal(const wcstring &key);
|
||||
|
||||
// Functions concerned with saving.
|
||||
bool open_and_acquire_lock(const wcstring &path, autoclose_fd_t *out_fd);
|
||||
autoclose_fd_t open_temporary_file(const wcstring &directory, wcstring *out_path);
|
||||
bool write_to_fd(int fd, const wcstring &path);
|
||||
bool move_new_vars_file_into_place(const wcstring &src, const wcstring &dst);
|
||||
|
||||
// File id from which we last read.
|
||||
file_id_t last_read_file = kInvalidFileID;
|
||||
|
||||
// Given a variable table, generate callbacks representing the difference between our vars and
|
||||
// the new vars. Also update our exports generation count as necessary.
|
||||
void generate_callbacks_and_update_exports(const var_table_t &new_vars,
|
||||
callback_data_list_t &callbacks);
|
||||
|
||||
// Given a variable table, copy unmodified values into self.
|
||||
void acquire_variables(var_table_t &&vars_to_acquire);
|
||||
|
||||
static bool populate_1_variable(const wchar_t *input, env_var_t::env_var_flags_t flags,
|
||||
var_table_t *vars, wcstring *storage);
|
||||
|
||||
static void parse_message_2x_internal(const wcstring &msg, var_table_t *vars,
|
||||
wcstring *storage);
|
||||
static void parse_message_30_internal(const wcstring &msg, var_table_t *vars,
|
||||
wcstring *storage);
|
||||
static uvar_format_t read_message_internal(int fd, var_table_t *vars);
|
||||
|
||||
bool save(const wcstring &directory, const wcstring &vars_path);
|
||||
};
|
||||
|
||||
/// The "universal notifier" is an object responsible for broadcasting and receiving universal
|
||||
|
|
Loading…
Reference in New Issue
Block a user