2016-05-04 06:18:24 +08:00
|
|
|
// Prototypes for wide character equivalents of various standard unix functions.
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_WUTIL_H
|
|
|
|
#define FISH_WUTIL_H
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#include <dirent.h>
|
2016-11-15 13:31:51 +08:00
|
|
|
#include <stddef.h>
|
2016-05-04 06:18:24 +08:00
|
|
|
#include <stdio.h>
|
2005-10-06 06:37:08 +08:00
|
|
|
#include <sys/types.h>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <time.h>
|
2018-08-03 22:35:50 +08:00
|
|
|
#include <locale.h>
|
2011-12-27 11:18:46 +08:00
|
|
|
#include <string>
|
2016-04-21 14:00:54 +08:00
|
|
|
|
2012-02-18 07:55:54 +08:00
|
|
|
#include "common.h"
|
2017-10-11 15:08:26 +08:00
|
|
|
#include "maybe.h"
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of fopen(). This sets CLO_EXEC.
|
2012-02-19 01:11:22 +08:00
|
|
|
FILE *wfopen(const wcstring &path, const char *mode);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Sets CLO_EXEC on a given fd.
|
2012-03-02 16:27:40 +08:00
|
|
|
bool set_cloexec(int fd);
|
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of open() that also sets the close-on-exec flag (atomically when
|
|
|
|
/// possible).
|
2012-03-02 16:27:40 +08:00
|
|
|
int wopen_cloexec(const wcstring &pathname, int flags, mode_t mode = 0);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Mark an fd as nonblocking; returns errno or 0 on success.
|
2013-04-08 03:40:08 +08:00
|
|
|
int make_fd_nonblocking(int fd);
|
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Mark an fd as blocking; returns errno or 0 on success.
|
2013-04-08 03:40:08 +08:00
|
|
|
int make_fd_blocking(int fd);
|
|
|
|
|
2018-07-15 06:52:55 +08:00
|
|
|
/// Check if an fd is on a remote filesystem (NFS, SMB, CFS)
|
|
|
|
/// Return 1 if remote, 0 if local, -1 on error or if not implemented on this platform.
|
|
|
|
int fd_check_is_remote(int fd);
|
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of opendir(). Note that opendir() is guaranteed to set close-on-exec by
|
|
|
|
/// POSIX (hooray).
|
2012-02-19 01:11:22 +08:00
|
|
|
DIR *wopendir(const wcstring &name);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of stat().
|
2012-02-19 01:11:22 +08:00
|
|
|
int wstat(const wcstring &file_name, struct stat *buf);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of lstat().
|
2012-02-19 01:11:22 +08:00
|
|
|
int lwstat(const wcstring &file_name, struct stat *buf);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of access().
|
2012-02-19 01:11:22 +08:00
|
|
|
int waccess(const wcstring &pathname, int mode);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of unlink().
|
2012-02-19 01:11:22 +08:00
|
|
|
int wunlink(const wcstring &pathname);
|
2012-02-16 16:24:27 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of perror().
|
2014-04-28 04:34:51 +08:00
|
|
|
void wperror(const wchar_t *s);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Async-safe version of perror().
|
2013-01-10 09:06:20 +08:00
|
|
|
void safe_perror(const char *message);
|
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Async-safe version of strerror().
|
2013-01-10 09:06:20 +08:00
|
|
|
const char *safe_strerror(int err);
|
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of getcwd().
|
2016-03-11 10:17:39 +08:00
|
|
|
const wcstring wgetcwd();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of chdir().
|
2012-11-19 08:30:30 +08:00
|
|
|
int wchdir(const wcstring &dir);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-10-11 15:08:26 +08: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 23:23:56 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of readdir().
|
2017-05-10 12:02:05 +08:00
|
|
|
bool wreaddir(DIR *dir, wcstring &out_name);
|
|
|
|
bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, wcstring &out_name,
|
2016-05-04 06:18:24 +08:00
|
|
|
bool *out_is_dir);
|
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Like wreaddir, but skip items that are known to not be directories. If this requires a stat
|
|
|
|
/// (i.e. the file is a symlink), then return it. Note that this does not guarantee that everything
|
|
|
|
/// returned is a directory, it's just an optimization for cases where we would check for
|
|
|
|
/// directories anyways.
|
2015-08-09 05:52:04 +08:00
|
|
|
bool wreaddir_for_dirs(DIR *dir, wcstring *out_name);
|
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of dirname().
|
2012-11-19 08:30:30 +08:00
|
|
|
std::wstring wdirname(const std::wstring &path);
|
2006-06-14 21:22:40 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of basename().
|
2012-11-19 08:30:30 +08:00
|
|
|
std::wstring wbasename(const std::wstring &path);
|
2006-06-14 21:22:40 +08:00
|
|
|
|
2016-10-02 08:21:40 +08: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 13:03:27 +08:00
|
|
|
const wcstring &wgettext(const wchar_t *in);
|
2006-06-14 21:22:40 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of mkdir.
|
2012-11-19 08:30:30 +08:00
|
|
|
int wmkdir(const wcstring &dir, int mode);
|
2006-08-11 22:55:28 +08:00
|
|
|
|
2016-10-02 08:21:40 +08:00
|
|
|
/// Wide character version of rename.
|
2012-11-19 08:30:30 +08:00
|
|
|
int wrename(const wcstring &oldName, const wcstring &newName);
|
2006-10-21 06:33:47 +08:00
|
|
|
|
2016-09-28 12:07:10 +08:00
|
|
|
#define PUA1_START 0xE000
|
|
|
|
#define PUA1_END 0xF900
|
|
|
|
#define PUA2_START 0xF0000
|
|
|
|
#define PUA2_END 0xFFFFE
|
|
|
|
#define PUA3_START 0x100000
|
|
|
|
#define PUA3_END 0x10FFFE
|
|
|
|
|
|
|
|
// 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
|
|
|
|
#define iswalpha fish_iswalpha
|
2017-05-05 13:42:42 +08:00
|
|
|
#define iswalnum fish_iswalnum
|
2016-09-28 12:07:10 +08:00
|
|
|
#define iswgraph fish_iswgraph
|
|
|
|
#endif
|
|
|
|
int fish_iswalpha(wint_t wc);
|
2017-05-05 13:42:42 +08:00
|
|
|
int fish_iswalnum(wint_t wc);
|
2016-09-28 12:07:10 +08:00
|
|
|
int fish_iswgraph(wint_t wc);
|
|
|
|
|
|
|
|
int fish_wcswidth(const wchar_t *str);
|
|
|
|
int fish_wcswidth(const wcstring &str);
|
|
|
|
|
2018-07-29 08:56:42 +08:00
|
|
|
// returns an immortal locale_t corresponding to the C locale.
|
|
|
|
locale_t fish_c_locale();
|
|
|
|
|
2016-11-23 12:24:03 +08:00
|
|
|
int fish_wcstoi(const wchar_t *str, const wchar_t **endptr = NULL, int base = 10);
|
|
|
|
long fish_wcstol(const wchar_t *str, const wchar_t **endptr = NULL, int base = 10);
|
2016-11-25 10:43:50 +08:00
|
|
|
long long fish_wcstoll(const wchar_t *str, const wchar_t **endptr = NULL, int base = 10);
|
2016-12-10 16:36:26 +08:00
|
|
|
unsigned long long fish_wcstoull(const wchar_t *str, const wchar_t **endptr = NULL, int base = 10);
|
2018-07-29 15:41:03 +08:00
|
|
|
double fish_wcstod(const wchar_t *str, const wchar_t **endptr);
|
2016-10-02 08:21:40 +08:00
|
|
|
|
2016-05-04 06:18:24 +08: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 {
|
2014-04-29 06:14:33 +08:00
|
|
|
dev_t device;
|
|
|
|
ino_t inode;
|
|
|
|
uint64_t size;
|
|
|
|
time_t change_seconds;
|
|
|
|
long change_nanoseconds;
|
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 15:48:32 +08:00
|
|
|
time_t mod_seconds;
|
|
|
|
long mod_nanoseconds;
|
2016-05-04 06:18:24 +08:00
|
|
|
|
2014-04-29 06:14:33 +08:00
|
|
|
bool operator==(const file_id_t &rhs) const;
|
|
|
|
bool operator!=(const file_id_t &rhs) const;
|
2016-05-04 06:18:24 +08:00
|
|
|
|
|
|
|
// Used to permit these as keys in std::map.
|
2014-04-29 06:14:33 +08:00
|
|
|
bool operator<(const file_id_t &rhs) const;
|
2016-05-04 06:18:24 +08:00
|
|
|
|
2018-07-15 05:29:19 +08:00
|
|
|
static file_id_t from_stat(const struct stat &buf);
|
2016-05-04 06:18:24 +08: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 15:48:32 +08:00
|
|
|
int compare_file_id(const file_id_t &rhs) const;
|
2014-04-29 06:14:33 +08:00
|
|
|
};
|
2012-12-03 15:38:38 +08:00
|
|
|
|
2017-08-20 04:29:52 +08:00
|
|
|
#ifndef HASH_FILE_ID
|
|
|
|
#define HASH_FILE_ID 1
|
|
|
|
namespace std {
|
|
|
|
template<>
|
|
|
|
struct hash<file_id_t> {
|
2017-08-20 07:27:24 +08:00
|
|
|
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);
|
|
|
|
}
|
2017-08-20 04:29:52 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-04-28 04:34:51 +08:00
|
|
|
file_id_t file_id_for_fd(int fd);
|
|
|
|
file_id_t file_id_for_path(const wcstring &path);
|
|
|
|
|
|
|
|
extern const file_id_t kInvalidFileID;
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#endif
|