Minor improvements to file_id_t

This commit is contained in:
ridiculousfish 2021-06-13 17:19:52 -07:00
parent ba2e7db7e8
commit 1c20bdcbf9
2 changed files with 24 additions and 0 deletions

View File

@ -26,6 +26,7 @@
#include "common.h"
#include "fallback.h" // IWYU pragma: keep
#include "fds.h"
#include "flog.h"
#include "wcstringutil.h"
@ -764,6 +765,10 @@ file_id_t file_id_for_fd(int fd) {
return result;
}
file_id_t file_id_for_fd(const autoclose_fd_t &fd) {
return file_id_for_fd(fd.fd());
}
file_id_t file_id_for_path(const wcstring &path) {
file_id_t result = kInvalidFileID;
struct stat buf = {};
@ -786,6 +791,20 @@ bool file_id_t::operator==(const file_id_t &rhs) const { return this->compare_fi
bool file_id_t::operator!=(const file_id_t &rhs) const { return !(*this == rhs); }
wcstring file_id_t::dump() const {
using llong = long long;
wcstring result;
append_format(result, L" device: %lld\n", llong(device));
append_format(result, L" inode: %lld\n", llong(inode));
append_format(result, L" size: %lld\n", llong(size));
append_format(result, L" change: %lld\n", llong(change_seconds));
append_format(result, L"change_nano: %lld\n", llong(change_nanoseconds));
append_format(result, L" mod: %lld\n", llong(mod_seconds));
append_format(result, L" mod_nano: %lld", llong(mod_nanoseconds));
return result;
}
template <typename T>
int compare(T a, T b) {
if (a < b) {

View File

@ -19,6 +19,8 @@
#include "common.h"
#include "maybe.h"
class autoclose_fd_t;
/// Wide character version of opendir(). Note that opendir() is guaranteed to set close-on-exec by
/// POSIX (hooray).
DIR *wopendir(const wcstring &name);
@ -155,6 +157,8 @@ struct file_id_t {
static file_id_t from_stat(const struct stat &buf);
wcstring dump() const;
private:
int compare_file_id(const file_id_t &rhs) const;
};
@ -184,6 +188,7 @@ struct hash<file_id_t> {
#endif
file_id_t file_id_for_fd(int fd);
file_id_t file_id_for_fd(const autoclose_fd_t &fd);
file_id_t file_id_for_path(const wcstring &path);
file_id_t file_id_for_path(const std::string &path);