2016-05-03 22:18:24 +00:00
|
|
|
// Prototypes for wide character equivalents of various standard unix functions.
|
2005-10-04 15:11:39 +00:00
|
|
|
#ifndef FISH_WUTIL_H
|
|
|
|
#define FISH_WUTIL_H
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2022-08-21 21:51:33 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
2022-08-21 06:14:48 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <dirent.h>
|
2016-11-15 05:31:51 +00:00
|
|
|
#include <stddef.h>
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <stdint.h>
|
2022-09-25 19:04:33 +00:00
|
|
|
#include <sys/stat.h>
|
2005-10-05 22:37:08 +00:00
|
|
|
#include <sys/types.h>
|
2022-09-22 19:00:58 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
// This include is required on macOS 10.10 for locale_t
|
2022-10-27 00:24:03 +00:00
|
|
|
#include <xlocale.h> // IWYU pragma: keep
|
2022-09-22 19:00:58 +00:00
|
|
|
#endif
|
2019-10-13 22:50:48 +00:00
|
|
|
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
|
|
|
#include <locale>
|
|
|
|
#include <memory>
|
2019-05-05 10:09:25 +00:00
|
|
|
#include <string>
|
2016-04-21 06:00:54 +00:00
|
|
|
|
2012-02-17 23:55:54 +00:00
|
|
|
#include "common.h"
|
2017-10-11 07:08:26 +00:00
|
|
|
#include "maybe.h"
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2021-06-14 00:19:52 +00:00
|
|
|
class autoclose_fd_t;
|
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of opendir(). Note that opendir() is guaranteed to set close-on-exec by
|
|
|
|
/// POSIX (hooray).
|
2012-02-18 17:11:22 +00:00
|
|
|
DIR *wopendir(const wcstring &name);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of stat().
|
2012-02-18 17:11:22 +00:00
|
|
|
int wstat(const wcstring &file_name, struct stat *buf);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of lstat().
|
2012-02-18 17:11:22 +00:00
|
|
|
int lwstat(const wcstring &file_name, struct stat *buf);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of access().
|
2019-11-19 00:54:36 +00:00
|
|
|
int waccess(const wcstring &file_name, int mode);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of unlink().
|
2019-11-19 00:54:36 +00:00
|
|
|
int wunlink(const wcstring &file_name);
|
2012-02-16 08:24:27 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of perror().
|
2014-04-27 20:34:51 +00:00
|
|
|
void wperror(const wchar_t *s);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of getcwd().
|
2019-12-21 20:45:44 +00:00
|
|
|
wcstring wgetcwd();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2021-09-05 00:11:21 +00:00
|
|
|
/// Wide character version of readlink().
|
|
|
|
maybe_t<wcstring> wreadlink(const wcstring &file_name);
|
|
|
|
|
2017-10-11 07:08:26 +00:00
|
|
|
/// Wide character version of realpath function.
|
|
|
|
/// \returns the canonicalized path, or none if the path is invalid.
|
|
|
|
maybe_t<wcstring> wrealpath(const wcstring &pathname);
|
2006-02-02 15:23:56 +00:00
|
|
|
|
2018-09-17 00:55:15 +00:00
|
|
|
/// Given an input path, "normalize" it:
|
|
|
|
/// 1. Collapse multiple /s into a single /, except maybe at the beginning.
|
|
|
|
/// 2. .. goes up a level.
|
|
|
|
/// 3. Remove /./ in the middle.
|
2020-09-12 17:26:04 +00:00
|
|
|
wcstring normalize_path(const wcstring &path, bool allow_leading_double_slashes = true);
|
2018-09-17 00:55:15 +00:00
|
|
|
|
2018-11-18 02:02:28 +00:00
|
|
|
/// Given an input path \p path and a working directory \p wd, do a "normalizing join" in a way
|
|
|
|
/// appropriate for cd. That is, return effectively wd + path while resolving leading ../s from
|
|
|
|
/// path. The intent here is to allow 'cd' out of a directory which may no longer exist, without
|
|
|
|
/// allowing 'cd' into a directory that may not exist; see #5341.
|
|
|
|
wcstring path_normalize_for_cd(const wcstring &wd, const wcstring &path);
|
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of dirname().
|
2021-03-21 23:00:29 +00:00
|
|
|
std::wstring wdirname(std::wstring path);
|
2006-06-14 13:22:40 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of basename().
|
2021-03-21 23:00:29 +00:00
|
|
|
std::wstring wbasename(std::wstring path);
|
2006-06-14 13:22:40 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character wrapper around the gettext function. For historic reasons, unlike the real
|
|
|
|
/// gettext function, wgettext takes care of setting the correct domain, etc. using the textdomain
|
|
|
|
/// and bindtextdomain functions. This should probably be moved out of wgettext, so that wgettext
|
|
|
|
/// will be nothing more than a wrapper around gettext, like all other functions in this file.
|
2016-06-02 05:03:27 +00:00
|
|
|
const wcstring &wgettext(const wchar_t *in);
|
2006-06-14 13:22:40 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of mkdir.
|
2019-11-19 00:54:36 +00:00
|
|
|
int wmkdir(const wcstring &name, int mode);
|
2006-08-11 14:55:28 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Wide character version of rename.
|
2019-11-19 00:54:36 +00:00
|
|
|
int wrename(const wcstring &oldName, const wcstring &newv);
|
2006-10-20 22:33:47 +00:00
|
|
|
|
2020-07-30 02:00:04 +00:00
|
|
|
/// Write a wide string to a file descriptor. This avoids doing any additional allocation.
|
|
|
|
/// This does NOT retry on EINTR or EAGAIN, it simply returns.
|
|
|
|
/// \return -1 on error in which case errno will have been set. In this event, the number of bytes
|
|
|
|
/// actually written cannot be obtained.
|
2020-09-28 00:36:23 +00:00
|
|
|
ssize_t wwrite_to_fd(const wchar_t *input, size_t len, int fd);
|
2020-07-30 02:00:04 +00:00
|
|
|
|
|
|
|
/// Variant of above that accepts a wcstring.
|
|
|
|
inline ssize_t wwrite_to_fd(const wcstring &s, int fd) {
|
|
|
|
return wwrite_to_fd(s.c_str(), s.size(), fd);
|
|
|
|
}
|
|
|
|
|
2016-09-28 04:07:10 +00:00
|
|
|
// We need this because there are too many implementations that don't return the proper answer for
|
|
|
|
// some code points. See issue #3050.
|
|
|
|
#ifndef FISH_NO_ISW_WRAPPERS
|
2017-05-05 05:42:42 +00:00
|
|
|
#define iswalnum fish_iswalnum
|
2016-09-28 04:07:10 +00:00
|
|
|
#define iswgraph fish_iswgraph
|
|
|
|
#endif
|
2017-05-05 05:42:42 +00:00
|
|
|
int fish_iswalnum(wint_t wc);
|
2016-09-28 04:07:10 +00:00
|
|
|
int fish_iswgraph(wint_t wc);
|
|
|
|
|
|
|
|
int fish_wcswidth(const wchar_t *str);
|
|
|
|
int fish_wcswidth(const wcstring &str);
|
|
|
|
|
2018-07-29 00:56:42 +00:00
|
|
|
// returns an immortal locale_t corresponding to the C locale.
|
|
|
|
locale_t fish_c_locale();
|
|
|
|
|
2021-10-13 19:09:40 +00:00
|
|
|
void fish_invalidate_numeric_locale();
|
|
|
|
locale_t fish_numeric_locale();
|
|
|
|
|
2019-11-19 02:34:50 +00:00
|
|
|
int fish_wcstoi(const wchar_t *str, const wchar_t **endptr = nullptr, int base = 10);
|
|
|
|
long fish_wcstol(const wchar_t *str, const wchar_t **endptr = nullptr, int base = 10);
|
|
|
|
long long fish_wcstoll(const wchar_t *str, const wchar_t **endptr = nullptr, int base = 10);
|
|
|
|
unsigned long long fish_wcstoull(const wchar_t *str, const wchar_t **endptr = nullptr,
|
|
|
|
int base = 10);
|
2022-03-25 14:56:31 +00:00
|
|
|
double fish_wcstod(const wchar_t *str, wchar_t **endptr, size_t len);
|
2018-11-04 23:53:31 +00:00
|
|
|
double fish_wcstod(const wchar_t *str, wchar_t **endptr);
|
2022-03-25 14:56:31 +00:00
|
|
|
double fish_wcstod(const wcstring &str, wchar_t **endptr);
|
2022-03-13 10:23:35 +00:00
|
|
|
double fish_wcstod_underscores(const wchar_t *str, wchar_t **endptr);
|
2016-10-02 00:21:40 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
/// Class for representing a file's inode. We use this to detect and avoid symlink loops, among
|
|
|
|
/// other things. While an inode / dev pair is sufficient to distinguish co-existing files, Linux
|
|
|
|
/// seems to aggressively re-use inodes, so it cannot determine if a file has been deleted (ABA
|
|
|
|
/// problem). Therefore we include richer information.
|
|
|
|
struct file_id_t {
|
2021-05-02 20:55:41 +00:00
|
|
|
dev_t device{static_cast<dev_t>(-1LL)};
|
|
|
|
ino_t inode{static_cast<ino_t>(-1LL)};
|
|
|
|
uint64_t size{static_cast<uint64_t>(-1LL)};
|
2022-07-08 00:07:32 +00:00
|
|
|
time_t change_seconds{std::numeric_limits<time_t>::min()};
|
2021-05-02 20:55:41 +00:00
|
|
|
long change_nanoseconds{-1};
|
2022-07-08 00:07:32 +00:00
|
|
|
time_t mod_seconds{std::numeric_limits<time_t>::min()};
|
2021-05-02 20:55:41 +00:00
|
|
|
long mod_nanoseconds{-1};
|
|
|
|
|
|
|
|
constexpr file_id_t() = default;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2014-04-28 22:14:33 +00:00
|
|
|
bool operator==(const file_id_t &rhs) const;
|
|
|
|
bool operator!=(const file_id_t &rhs) const;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
|
|
|
// Used to permit these as keys in std::map.
|
2014-04-28 22:14:33 +00:00
|
|
|
bool operator<(const file_id_t &rhs) const;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2018-07-14 21:29:19 +00:00
|
|
|
static file_id_t from_stat(const struct stat &buf);
|
2022-07-08 00:07:32 +00:00
|
|
|
bool older_than(const file_id_t &rhs) const;
|
2021-06-14 00:19:52 +00:00
|
|
|
wcstring dump() const;
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
private:
|
Attempt to fix the sporadic uvar test failures on Linux
We identify when the universal variable file has changed out from under us by
comparing a bunch of fields from its stat: inode, device, size, high-precision
timestamp, generation. Linux aggressively reuses inodes, and the size may be
the same by coincidence (which is the case in the tests). Also, Linux
officially has nanosecond precision, but in practice it seems to only uses
millisecond precision for storing mtimes. Thus if there are three or more
updates within a millisecond, every field we check may be the same, and we are
vulnerable to the ABA problem. I believe this explains the occasional test
failures.
The solution is to manually set the nanosecond field of the mtime timestamp to
something unlikely to be duplicated, like a random number, or better yet, the
current time (with nanosecond precision). This is more in the spirit of the
timestamp, and it means we're around a million times less likely to collide.
This seems to fix the tests.
2015-11-09 07:48:32 +00:00
|
|
|
int compare_file_id(const file_id_t &rhs) const;
|
2014-04-28 22:14:33 +00:00
|
|
|
};
|
2012-12-03 07:38:38 +00:00
|
|
|
|
2022-09-25 19:04:33 +00:00
|
|
|
/// Types of files that may be in a directory.
|
|
|
|
enum class dir_entry_type_t : uint8_t {
|
|
|
|
fifo = 1, // FIFO file
|
|
|
|
chr, // character device
|
|
|
|
dir, // directory
|
|
|
|
blk, // block device
|
|
|
|
reg, // regular file
|
|
|
|
lnk, // symlink
|
|
|
|
sock, // socket
|
|
|
|
whiteout, // whiteout (from BSD)
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Class for iterating over a directory, wrapping readdir().
|
|
|
|
/// This allows enumerating the contents of a directory, exposing the file type if the filesystem
|
|
|
|
/// itself exposes that from readdir(). stat() is incurred only if necessary: if the entry is a
|
|
|
|
/// symlink, or if the caller asks for the stat buffer.
|
|
|
|
/// Symlinks are followed.
|
|
|
|
class dir_iter_t : noncopyable_t {
|
|
|
|
public:
|
|
|
|
struct entry_t;
|
|
|
|
|
|
|
|
/// Open a directory at a given path. On failure, \p error() will return the error code.
|
|
|
|
/// Note opendir is guaranteed to set close-on-exec by POSIX (hooray).
|
|
|
|
explicit dir_iter_t(const wcstring &path);
|
|
|
|
|
|
|
|
/// Advance this iterator.
|
|
|
|
/// \return a pointer to the entry, or nullptr if the entry is finished, or an error occurred.
|
|
|
|
/// The returned pointer is only valid until the next call to next().
|
|
|
|
const entry_t *next();
|
|
|
|
|
|
|
|
/// \return the errno value for the last error, or 0 if none.
|
|
|
|
int error() const { return error_; }
|
|
|
|
|
|
|
|
/// \return if we are valid: successfully opened a directory.
|
|
|
|
bool valid() const { return dir_ != nullptr; }
|
|
|
|
|
|
|
|
/// \return the underlying file descriptor, or -1 if invalid.
|
2022-10-08 00:28:55 +00:00
|
|
|
int fd() const { return dir_ ? dirfd(&*dir_) : -1; }
|
2022-09-25 19:04:33 +00:00
|
|
|
|
2022-09-26 02:31:04 +00:00
|
|
|
/// Rewind the directory to the beginning.
|
|
|
|
void rewind();
|
|
|
|
|
2022-09-25 19:04:33 +00:00
|
|
|
~dir_iter_t();
|
|
|
|
dir_iter_t(dir_iter_t &&);
|
|
|
|
dir_iter_t &operator=(dir_iter_t &&);
|
|
|
|
|
|
|
|
/// An entry returned by dir_iter_t.
|
|
|
|
struct entry_t : noncopyable_t {
|
|
|
|
/// File name of this entry.
|
|
|
|
wcstring name{};
|
|
|
|
|
|
|
|
/// inode of this entry.
|
|
|
|
ino_t inode{};
|
|
|
|
|
|
|
|
/// \return the type of this entry if it is already available, otherwise none().
|
|
|
|
maybe_t<dir_entry_type_t> fast_type() const { return type_; }
|
|
|
|
|
|
|
|
/// \return the type of this entry, falling back to stat() if necessary.
|
|
|
|
/// If stat() fails because the file has disappeared, this will return none().
|
|
|
|
/// If stat() fails because of a broken symlink, this will return type lnk.
|
|
|
|
maybe_t<dir_entry_type_t> check_type() const;
|
|
|
|
|
|
|
|
/// \return whether this is a directory. This may call stat().
|
|
|
|
bool is_dir() const { return check_type() == dir_entry_type_t::dir; }
|
|
|
|
|
|
|
|
/// \return the stat buff for this entry, invoking stat() if necessary.
|
|
|
|
const maybe_t<struct stat> &stat() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Reset our fields.
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
// Populate our stat buffer, and type. Errors are silently ignored.
|
|
|
|
void do_stat() const;
|
|
|
|
|
|
|
|
// Stat buff for this entry, or none if not yet computed.
|
|
|
|
mutable maybe_t<struct stat> stat_{};
|
|
|
|
|
|
|
|
// The type of the entry. This is initially none; it may be populated eagerly via readdir()
|
|
|
|
// on some filesystems, or later via stat(). If stat() fails, the error is silently ignored
|
|
|
|
// and the type is left as none(). Note this is an unavoidable race.
|
|
|
|
mutable maybe_t<dir_entry_type_t> type_{};
|
|
|
|
|
|
|
|
// fd of the DIR*, used for fstatat().
|
|
|
|
int dirfd_{-1};
|
|
|
|
|
|
|
|
entry_t();
|
|
|
|
~entry_t();
|
|
|
|
entry_t(entry_t &&) = default;
|
|
|
|
entry_t &operator=(entry_t &&) = default;
|
|
|
|
friend class dir_iter_t;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2022-10-08 00:28:55 +00:00
|
|
|
struct dir_closer_t {
|
|
|
|
void operator()(DIR *dir) const { (void)closedir(dir); }
|
|
|
|
};
|
|
|
|
std::unique_ptr<DIR, dir_closer_t> dir_{nullptr};
|
2022-09-25 19:04:33 +00:00
|
|
|
int error_{0};
|
|
|
|
entry_t entry_;
|
|
|
|
};
|
|
|
|
|
2017-08-19 20:29:52 +00:00
|
|
|
#ifndef HASH_FILE_ID
|
2022-09-20 18:58:37 +00:00
|
|
|
#define HASH_FILE_ID 1
|
2017-08-19 20:29:52 +00:00
|
|
|
namespace std {
|
2019-05-05 10:09:25 +00:00
|
|
|
template <>
|
|
|
|
struct hash<file_id_t> {
|
|
|
|
size_t operator()(const file_id_t &f) const {
|
|
|
|
std::hash<decltype(f.device)> hasher1;
|
|
|
|
std::hash<decltype(f.inode)> hasher2;
|
|
|
|
|
|
|
|
return hasher1(f.device) ^ hasher2(f.inode);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace std
|
2017-08-19 20:29:52 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-27 20:34:51 +00:00
|
|
|
file_id_t file_id_for_fd(int fd);
|
2021-06-14 00:19:52 +00:00
|
|
|
file_id_t file_id_for_fd(const autoclose_fd_t &fd);
|
2014-04-27 20:34:51 +00:00
|
|
|
file_id_t file_id_for_path(const wcstring &path);
|
2019-05-31 07:33:50 +00:00
|
|
|
file_id_t file_id_for_path(const std::string &path);
|
2014-04-27 20:34:51 +00:00
|
|
|
|
|
|
|
extern const file_id_t kInvalidFileID;
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#endif
|