/* Get a sort of unique machine identifier. Prefer the MAC address; if that fails, fall back to the hostname; if that fails, pick something. */ static std::string get_machine_identifier(void) { std::string result; unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {}; if (get_mac_address(mac_addr)) { result.reserve(2 * MAC_ADDRESS_MAX_LEN); for (size_t i=0; i < MAC_ADDRESS_MAX_LEN; i++) { char buff[3]; snprintf(buff, sizeof buff, "%02x", mac_addr[i]); result.append(buff); } } else if (get_hostname_identifier(&result)) { /* Hooray */ } else { /* Fallback */ result.assign("nohost"); } return result; }
static bool load_or_save_variables(bool save) { const wcstring wdir = fishd_get_config(); const std::string dir = wcs2string(wdir); if (dir.empty()) return false; const std::string machine_id = get_machine_identifier(); const std::string machine_id_path = get_variables_file_path(dir, machine_id); bool success = load_or_save_variables_at_path(save, machine_id_path); if (! success && ! save && errno == ENOENT) { /* We failed to load, because the file was not found. Older fish used the hostname only. Try *moving* the filename based on the hostname into place; if that succeeds try again. Silently "upgraded." */ std::string hostname_id; if (get_hostname_identifier(&hostname_id) && hostname_id != machine_id) { std::string hostname_path = get_variables_file_path(dir, hostname_id); if (0 == rename(hostname_path.c_str(), machine_id_path.c_str())) { /* We renamed - try again */ success = load_or_save_variables_at_path(save, machine_id_path); } } } return success; }
bool env_universal_t::load() { scoped_lock locker(lock); callback_data_list_t callbacks; const wcstring vars_path = explicit_vars_path.empty() ? default_vars_path() : explicit_vars_path; bool success = load_from_path(vars_path, &callbacks); if (! success && ! tried_renaming && errno == ENOENT) { /* We failed to load, because the file was not found. Older fish used the hostname only. Try *moving* the filename based on the hostname into place; if that succeeds try again. Silently "upgraded." */ tried_renaming = true; std::string hostname_id; if (get_hostname_identifier(&hostname_id)) { const wcstring hostname_path = wdirname(vars_path) + L'/' + str2wcstring(hostname_id); if (0 == wrename(hostname_path, vars_path)) { /* We renamed - try again */ success = this->load(); } } } return success; }