From fe75a3a6504e763dd74e48ce0a143603ad61fc6c Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Fri, 19 Apr 2019 18:47:07 -0700 Subject: [PATCH] Migrate autoload file checks to file_id_t --- src/autoload.cpp | 22 ++++++---------------- src/autoload.h | 16 +++++++++------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/autoload.cpp b/src/autoload.cpp index 02932183f..e551b0ee1 100644 --- a/src/autoload.cpp +++ b/src/autoload.cpp @@ -27,20 +27,10 @@ static const int kAutoloadStalenessInterval = 15; file_access_attempt_t access_file(const wcstring &path, int mode) { file_access_attempt_t result = {}; - struct stat statbuf; - if (wstat(path, &statbuf)) { - result.error = errno; - } else { - result.mod_time = statbuf.st_mtime; - if (waccess(path, mode)) { - result.error = errno; - } else { - result.accessible = true; - } + file_id_t file_id = file_id_for_path(path); + if (file_id != kInvalidFileID && 0 == waccess(path, mode)) { + result.file_id = file_id; } - - // Note that we record the last checked time after the call, on the assumption that in a slow - // filesystem, the lag comes before the kernel check, not after. result.last_checked = time(NULL); return result; } @@ -178,7 +168,7 @@ bool autoload_t::locate_file_and_maybe_load_it(const wcstring &cmd, bool really_ // If we can use this function, return whether we were able to access it. if (use_cached(func, really_load, allow_stale_functions)) { - return func->access.accessible; + return func->access.accessible(); } } @@ -194,7 +184,7 @@ bool autoload_t::locate_file_and_maybe_load_it(const wcstring &cmd, bool really_ wcstring path = next + L"/" + cmd + L".fish"; const file_access_attempt_t access = access_file(path, R_OK); - if (!access.accessible) { + if (!access.accessible()) { continue; } @@ -205,7 +195,7 @@ bool autoload_t::locate_file_and_maybe_load_it(const wcstring &cmd, bool really_ // Generate the source if we need to load it. bool need_to_load_function = really_load && - (func == NULL || func->access.mod_time != access.mod_time || !func->is_loaded); + (func == NULL || func->access.file_id != access.file_id || !func->is_loaded); if (need_to_load_function) { // Generate the script source. script_source = L"source " + escape_string(path, ESCAPE_ALL); diff --git a/src/autoload.h b/src/autoload.h index 0ff887404..9421ee306 100644 --- a/src/autoload.h +++ b/src/autoload.h @@ -15,14 +15,16 @@ /// Record of an attempt to access a file. struct file_access_attempt_t { - /// Modification time of the file - time_t mod_time; - /// When we last checked the file + /// If filled, the file ID of the checked file. + /// Otherwise the file did not exist or was otherwise inaccessible. + /// Note that this will never contain kInvalidFileID. + maybe_t file_id; + + /// When we last checked the file. time_t last_checked; - /// Whether or not we believe we can access this file - bool accessible; - /// If we cannot access the file, the error code encountered. - int error; + + /// Whether or not we believe we can access this file. + bool accessible() const { return file_id.has_value(); } }; file_access_attempt_t access_file(const wcstring &path, int mode);