2016-05-03 22:18:24 +00:00
|
|
|
// Wide character equivalents of various standard unix functions.
|
2016-09-28 04:07:10 +00:00
|
|
|
#define FISH_NO_ISW_WRAPPERS
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
#include <dirent.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2006-06-14 13:22:40 +00:00
|
|
|
#include <libgen.h>
|
2016-05-03 22:18:24 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
2019-10-13 22:50:48 +00:00
|
|
|
|
2019-05-05 10:09:25 +00:00
|
|
|
#include <cstring>
|
2018-07-14 22:52:55 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/statvfs.h>
|
2016-05-03 22:18:24 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2016-11-15 05:31:51 +00:00
|
|
|
#include <wctype.h>
|
2017-02-11 02:47:02 +00:00
|
|
|
|
2019-04-29 01:13:55 +00:00
|
|
|
#include <atomic>
|
2019-10-13 22:50:48 +00:00
|
|
|
#include <cwchar>
|
2016-05-03 22:18:24 +00:00
|
|
|
#include <string>
|
2017-08-19 16:55:06 +00:00
|
|
|
#include <unordered_map>
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "common.h"
|
2016-04-21 06:00:54 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2019-05-27 22:56:53 +00:00
|
|
|
#include "flog.h"
|
2019-05-28 02:47:13 +00:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-11-26 00:56:39 +00:00
|
|
|
using cstring = std::string;
|
2016-10-02 00:21:40 +00:00
|
|
|
|
2019-11-19 01:08:16 +00:00
|
|
|
const file_id_t kInvalidFileID = {static_cast<dev_t>(-1LL),
|
|
|
|
static_cast<ino_t>(-1LL),
|
|
|
|
static_cast<uint64_t>(-1LL),
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
-1,
|
|
|
|
-1};
|
2014-04-27 20:34:51 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Map used as cache by wgettext.
|
2017-08-19 23:27:24 +00:00
|
|
|
static owning_lock<std::unordered_map<wcstring, wcstring>> wgettext_map;
|
2016-09-10 21:38:28 +00:00
|
|
|
|
2017-05-10 04:02:05 +00:00
|
|
|
bool wreaddir_resolving(DIR *dir, const wcstring &dir_path, wcstring &out_name, bool *out_is_dir) {
|
2019-01-02 13:16:39 +00:00
|
|
|
struct dirent *result = readdir(dir);
|
|
|
|
if (!result) {
|
2019-03-14 22:12:14 +00:00
|
|
|
out_name.clear();
|
2017-05-10 04:02:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-01-02 13:16:39 +00:00
|
|
|
out_name = str2wcstring(result->d_name);
|
|
|
|
if (!out_is_dir) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-10-30 19:38:56 +00:00
|
|
|
// The caller cares if this is a directory, so check.
|
|
|
|
bool is_dir = false;
|
|
|
|
// We may be able to skip stat, if the readdir can tell us the file type directly.
|
|
|
|
bool check_with_stat = true;
|
2014-12-11 07:24:42 +00:00
|
|
|
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
|
2019-01-02 13:16:39 +00:00
|
|
|
if (result->d_type == DT_DIR) {
|
2016-10-30 19:38:56 +00:00
|
|
|
// Known directory.
|
|
|
|
is_dir = true;
|
|
|
|
check_with_stat = false;
|
2019-01-02 13:16:39 +00:00
|
|
|
} else if (result->d_type == DT_LNK || result->d_type == DT_UNKNOWN) {
|
2016-10-30 19:38:56 +00:00
|
|
|
// We want to treat symlinks to directories as directories. Use stat to resolve it.
|
|
|
|
check_with_stat = true;
|
|
|
|
} else {
|
|
|
|
// Regular file.
|
|
|
|
is_dir = false;
|
|
|
|
check_with_stat = false;
|
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
#endif // HAVE_STRUCT_DIRENT_D_TYPE
|
2016-10-30 19:38:56 +00:00
|
|
|
if (check_with_stat) {
|
|
|
|
// We couldn't determine the file type from the dirent; check by stat'ing it.
|
|
|
|
cstring fullpath = wcs2string(dir_path);
|
|
|
|
fullpath.push_back('/');
|
2019-01-02 13:16:39 +00:00
|
|
|
fullpath.append(result->d_name);
|
2016-10-30 19:38:56 +00:00
|
|
|
struct stat buf;
|
|
|
|
if (stat(fullpath.c_str(), &buf) != 0) {
|
|
|
|
is_dir = false;
|
|
|
|
} else {
|
|
|
|
is_dir = static_cast<bool>(S_ISDIR(buf.st_mode));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-02-20 10:13:31 +00:00
|
|
|
}
|
2016-10-30 19:38:56 +00:00
|
|
|
*out_is_dir = is_dir;
|
2012-02-20 10:13:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-10 04:02:05 +00:00
|
|
|
bool wreaddir(DIR *dir, wcstring &out_name) {
|
2019-01-02 13:16:39 +00:00
|
|
|
struct dirent *result = readdir(dir);
|
|
|
|
if (!result) {
|
2019-03-14 22:12:14 +00:00
|
|
|
out_name.clear();
|
2017-05-10 04:02:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-05-18 05:35:28 +00:00
|
|
|
|
2019-01-02 13:16:39 +00:00
|
|
|
out_name = str2wcstring(result->d_name);
|
2012-02-18 02:08:08 +00:00
|
|
|
return true;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
bool wreaddir_for_dirs(DIR *dir, wcstring *out_name) {
|
2019-11-19 02:34:50 +00:00
|
|
|
struct dirent *result = nullptr;
|
2017-05-10 04:02:05 +00:00
|
|
|
while (!result) {
|
2019-01-02 13:16:39 +00:00
|
|
|
result = readdir(dir);
|
|
|
|
if (!result) break;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2015-08-08 21:52:04 +00:00
|
|
|
#if HAVE_STRUCT_DIRENT_D_TYPE
|
2019-01-02 13:16:39 +00:00
|
|
|
switch (result->d_type) {
|
2015-08-08 21:52:04 +00:00
|
|
|
case DT_DIR:
|
|
|
|
case DT_LNK:
|
2016-05-03 22:18:24 +00:00
|
|
|
case DT_UNKNOWN: {
|
2017-05-10 04:02:05 +00:00
|
|
|
break; // these may be directories
|
2016-05-03 22:18:24 +00:00
|
|
|
}
|
|
|
|
default: {
|
2017-05-10 04:02:05 +00:00
|
|
|
break; // nothing else can
|
2016-05-03 22:18:24 +00:00
|
|
|
}
|
2015-08-08 21:52:04 +00:00
|
|
|
}
|
|
|
|
#else
|
2016-05-03 22:18:24 +00:00
|
|
|
// We can't determine if it's a directory or not, so just return it.
|
2017-05-10 04:02:05 +00:00
|
|
|
break;
|
2015-08-08 21:52:04 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-05-10 04:02:05 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
if (result && out_name) {
|
2015-08-08 21:52:04 +00:00
|
|
|
*out_name = str2wcstring(result->d_name);
|
|
|
|
}
|
2019-11-26 00:51:28 +00:00
|
|
|
return result != nullptr;
|
2015-08-08 21:52:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:45:44 +00:00
|
|
|
wcstring wgetcwd() {
|
2017-09-26 15:00:23 +00:00
|
|
|
char cwd[PATH_MAX];
|
2017-09-26 14:49:54 +00:00
|
|
|
char *res = getcwd(cwd, sizeof(cwd));
|
2016-05-03 22:18:24 +00:00
|
|
|
if (res) {
|
2017-09-26 14:49:54 +00:00
|
|
|
return str2wcstring(res);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-05-30 09:54:09 +00:00
|
|
|
FLOGF(error, _(L"getcwd() failed with errno %d/%s"), errno, std::strerror(errno));
|
2017-09-26 14:49:54 +00:00
|
|
|
return wcstring();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 00:51:49 +00:00
|
|
|
int set_cloexec(int fd, bool should_set) {
|
2019-04-13 19:22:46 +00:00
|
|
|
// Note we don't want to overwrite existing flags like O_NONBLOCK which may be set. So fetch the
|
2019-12-14 00:51:49 +00:00
|
|
|
// existing flags and modify them.
|
2019-04-13 19:22:46 +00:00
|
|
|
int flags = fcntl(fd, F_GETFD, 0);
|
|
|
|
if (flags < 0) {
|
2019-12-14 00:51:49 +00:00
|
|
|
return -1;
|
2019-04-13 19:22:46 +00:00
|
|
|
}
|
2019-12-14 00:51:49 +00:00
|
|
|
int new_flags = flags;
|
|
|
|
if (should_set) {
|
|
|
|
new_flags |= FD_CLOEXEC;
|
|
|
|
} else {
|
|
|
|
new_flags &= ~FD_CLOEXEC;
|
|
|
|
}
|
|
|
|
if (flags == new_flags) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return fcntl(fd, F_SETFD, new_flags);
|
2019-04-13 19:22:46 +00:00
|
|
|
}
|
2016-10-02 00:21:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 18:25:49 +00:00
|
|
|
int open_cloexec(const std::string &path, int flags, mode_t mode) {
|
|
|
|
return open_cloexec(path.c_str(), flags, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int open_cloexec(const char *path, int flags, mode_t mode) {
|
2016-10-02 00:21:40 +00:00
|
|
|
int fd;
|
|
|
|
|
2020-06-08 02:58:52 +00:00
|
|
|
// Prefer to use O_CLOEXEC.
|
2016-10-02 00:21:40 +00:00
|
|
|
#ifdef O_CLOEXEC
|
2020-01-28 18:25:49 +00:00
|
|
|
fd = open(path, flags | O_CLOEXEC, mode);
|
2016-10-02 00:21:40 +00:00
|
|
|
#else
|
2020-01-28 18:25:49 +00:00
|
|
|
fd = open(path, flags, mode);
|
2016-10-02 00:21:40 +00:00
|
|
|
if (fd >= 0 && !set_cloexec(fd)) {
|
2020-01-29 22:01:55 +00:00
|
|
|
exec_close(fd);
|
2016-10-02 00:21:40 +00:00
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return fd;
|
|
|
|
}
|
2019-06-10 00:43:25 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
int wopen_cloexec(const wcstring &pathname, int flags, mode_t mode) {
|
2019-05-31 07:33:50 +00:00
|
|
|
cstring tmp = wcs2string(pathname);
|
2020-01-28 18:25:49 +00:00
|
|
|
return open_cloexec(tmp, flags, mode);
|
2016-10-02 00:21:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
DIR *wopendir(const wcstring &name) {
|
2016-10-02 00:21:40 +00:00
|
|
|
const cstring tmp = wcs2string(name);
|
2011-12-27 03:18:46 +00:00
|
|
|
return opendir(tmp.c_str());
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 11:28:19 +00:00
|
|
|
dir_t::dir_t(const wcstring &path) {
|
|
|
|
const cstring tmp = wcs2string(path);
|
|
|
|
this->dir = opendir(tmp.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_t::~dir_t() {
|
|
|
|
if (this->dir != nullptr) {
|
|
|
|
closedir(this->dir);
|
|
|
|
this->dir = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-05 10:09:25 +00:00
|
|
|
bool dir_t::valid() const { return this->dir != nullptr; }
|
2018-09-22 11:28:19 +00:00
|
|
|
|
2020-04-03 02:39:29 +00:00
|
|
|
bool dir_t::read(wcstring &name) const { return wreaddir(this->dir, name); }
|
2018-09-22 11:28:19 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int wstat(const wcstring &file_name, struct stat *buf) {
|
2016-10-02 00:21:40 +00:00
|
|
|
const cstring tmp = wcs2string(file_name);
|
2011-12-27 03:18:46 +00:00
|
|
|
return stat(tmp.c_str(), buf);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int lwstat(const wcstring &file_name, struct stat *buf) {
|
2016-10-02 00:21:40 +00:00
|
|
|
const cstring tmp = wcs2string(file_name);
|
2011-12-27 03:18:46 +00:00
|
|
|
return lstat(tmp.c_str(), buf);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int waccess(const wcstring &file_name, int mode) {
|
2016-10-02 00:21:40 +00:00
|
|
|
const cstring tmp = wcs2string(file_name);
|
2011-12-27 03:18:46 +00:00
|
|
|
return access(tmp.c_str(), mode);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int wunlink(const wcstring &file_name) {
|
2016-10-02 00:21:40 +00:00
|
|
|
const cstring tmp = wcs2string(file_name);
|
2012-02-16 08:24:27 +00:00
|
|
|
return unlink(tmp.c_str());
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
void wperror(const wchar_t *s) {
|
2012-11-19 00:30:30 +00:00
|
|
|
int e = errno;
|
2016-05-03 22:18:24 +00:00
|
|
|
if (s[0] != L'\0') {
|
2019-03-12 21:06:01 +00:00
|
|
|
std::fwprintf(stderr, L"%ls: ", s);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2019-03-12 22:07:07 +00:00
|
|
|
std::fwprintf(stderr, L"%s\n", std::strerror(e));
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int make_fd_nonblocking(int fd) {
|
2013-04-07 19:40:08 +00:00
|
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
int err = 0;
|
2016-06-02 05:03:27 +00:00
|
|
|
bool nonblocking = flags & O_NONBLOCK;
|
|
|
|
if (!nonblocking) {
|
2013-04-07 19:40:08 +00:00
|
|
|
err = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
return err == -1 ? errno : 0;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int make_fd_blocking(int fd) {
|
2013-04-07 19:40:08 +00:00
|
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
int err = 0;
|
2016-06-02 05:03:27 +00:00
|
|
|
bool nonblocking = flags & O_NONBLOCK;
|
|
|
|
if (nonblocking) {
|
2013-04-07 19:40:08 +00:00
|
|
|
err = fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
|
|
|
|
}
|
|
|
|
return err == -1 ? errno : 0;
|
|
|
|
}
|
|
|
|
|
2018-07-14 22:52:55 +00:00
|
|
|
int fd_check_is_remote(int fd) {
|
|
|
|
#if defined(__linux__)
|
2019-05-29 18:46:30 +00:00
|
|
|
struct statfs buf {};
|
2018-07-14 22:52:55 +00:00
|
|
|
if (fstatfs(fd, &buf) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Linux has constants for these like NFS_SUPER_MAGIC, SMB_SUPER_MAGIC, CIFS_MAGIC_NUMBER but
|
|
|
|
// these are in varying headers. Simply hard code them.
|
2020-02-15 09:10:59 +00:00
|
|
|
// NOTE: The cast is necessary for 32-bit systems because of the 4-byte CIFS_MAGIC_NUMBER
|
2020-04-08 23:56:59 +00:00
|
|
|
switch (static_cast<unsigned int>(buf.f_type)) {
|
2020-02-17 13:12:27 +00:00
|
|
|
case 0x6969: // NFS_SUPER_MAGIC
|
|
|
|
case 0x517B: // SMB_SUPER_MAGIC
|
2020-04-29 15:25:31 +00:00
|
|
|
case 0xFE534D42U: // SMB2_MAGIC_NUMBER - not in the manpage
|
2020-04-03 02:35:37 +00:00
|
|
|
case 0xFF534D42U: // CIFS_MAGIC_NUMBER
|
2018-07-14 22:52:55 +00:00
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
// Other FSes are assumed local.
|
|
|
|
return 0;
|
|
|
|
}
|
2018-12-13 09:26:15 +00:00
|
|
|
#elif defined(ST_LOCAL)
|
|
|
|
// ST_LOCAL is a flag to statvfs, which is itself standardized.
|
|
|
|
// In practice the only system to use this path is NetBSD.
|
|
|
|
struct statvfs buf {};
|
|
|
|
if (fstatvfs(fd, &buf) < 0) return -1;
|
|
|
|
return (buf.f_flag & ST_LOCAL) ? 0 : 1;
|
|
|
|
#elif defined(MNT_LOCAL)
|
2018-07-14 22:52:55 +00:00
|
|
|
struct statfs buf {};
|
|
|
|
if (fstatfs(fd, &buf) < 0) return -1;
|
|
|
|
return (buf.f_flags & MNT_LOCAL) ? 0 : 1;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
static inline void safe_append(char *buffer, const char *s, size_t buffsize) {
|
2019-03-12 22:07:07 +00:00
|
|
|
std::strncat(buffer, s, buffsize - std::strlen(buffer) - 1);
|
2013-01-10 01:06:20 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
// In general, strerror is not async-safe, and therefore we cannot use it directly. So instead we
|
|
|
|
// have to grub through sys_nerr and sys_errlist directly On GNU toolchain, this will produce a
|
|
|
|
// deprecation warning from the linker (!!), which appears impossible to suppress!
|
2016-05-03 22:18:24 +00:00
|
|
|
const char *safe_strerror(int err) {
|
2013-05-25 20:42:16 +00:00
|
|
|
#if defined(__UCLIBC__)
|
2016-05-03 22:18:24 +00:00
|
|
|
// uClibc does not have sys_errlist, however, its strerror is believed to be async-safe.
|
|
|
|
// See issue #808.
|
2019-03-12 22:07:07 +00:00
|
|
|
return std::strerror(err);
|
2014-12-08 00:43:38 +00:00
|
|
|
#elif defined(HAVE__SYS__ERRS) || defined(HAVE_SYS_ERRLIST)
|
|
|
|
#ifdef HAVE_SYS_ERRLIST
|
2019-11-19 02:34:50 +00:00
|
|
|
if (err >= 0 && err < sys_nerr && sys_errlist[err] != nullptr) {
|
2013-01-10 01:06:20 +00:00
|
|
|
return sys_errlist[err];
|
|
|
|
}
|
2014-12-08 00:43:38 +00:00
|
|
|
#elif defined(HAVE__SYS__ERRS)
|
|
|
|
extern const char _sys_errs[];
|
|
|
|
extern const int _sys_index[];
|
|
|
|
extern int _sys_num_err;
|
|
|
|
|
|
|
|
if (err >= 0 && err < _sys_num_err) {
|
2016-05-03 22:18:24 +00:00
|
|
|
return &_sys_errs[_sys_index[err]];
|
2014-12-08 00:43:38 +00:00
|
|
|
}
|
2016-05-03 22:18:24 +00:00
|
|
|
#endif // either HAVE__SYS__ERRS or HAVE_SYS_ERRLIST
|
|
|
|
#endif // defined(HAVE__SYS__ERRS) || defined(HAVE_SYS_ERRLIST)
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2016-05-04 22:19:47 +00:00
|
|
|
int saved_err = errno;
|
|
|
|
static char buff[384]; // use a shared buffer for this case
|
|
|
|
char errnum_buff[64];
|
|
|
|
format_long_safe(errnum_buff, err);
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2016-05-04 22:19:47 +00:00
|
|
|
buff[0] = '\0';
|
|
|
|
safe_append(buff, "unknown error (errno was ", sizeof buff);
|
|
|
|
safe_append(buff, errnum_buff, sizeof buff);
|
|
|
|
safe_append(buff, ")", sizeof buff);
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2016-05-04 22:19:47 +00:00
|
|
|
errno = saved_err;
|
|
|
|
return buff;
|
2013-01-10 01:06:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
void safe_perror(const char *message) {
|
|
|
|
// Note we cannot use strerror, because on Linux it uses gettext, which is not safe.
|
2013-01-10 01:06:20 +00:00
|
|
|
int err = errno;
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2013-01-10 01:06:20 +00:00
|
|
|
char buff[384];
|
|
|
|
buff[0] = '\0';
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
if (message) {
|
2013-01-10 01:06:20 +00:00
|
|
|
safe_append(buff, message, sizeof buff);
|
|
|
|
safe_append(buff, ": ", sizeof buff);
|
|
|
|
}
|
|
|
|
safe_append(buff, safe_strerror(err), sizeof buff);
|
|
|
|
safe_append(buff, "\n", sizeof buff);
|
2013-01-12 20:55:23 +00:00
|
|
|
|
2019-03-12 22:07:07 +00:00
|
|
|
ignore_result(write(STDERR_FILENO, buff, std::strlen(buff)));
|
2013-01-10 01:06:20 +00:00
|
|
|
errno = err;
|
|
|
|
}
|
|
|
|
|
2018-11-18 16:56:24 +00:00
|
|
|
/// Wide character realpath. The last path component does not need to be valid. If an error occurs,
|
|
|
|
/// wrealpath() returns none() and errno is likely set.
|
2017-10-11 07:08:26 +00:00
|
|
|
maybe_t<wcstring> wrealpath(const wcstring &pathname) {
|
|
|
|
if (pathname.empty()) return none();
|
2016-10-04 00:51:27 +00:00
|
|
|
|
2017-10-11 07:08:26 +00:00
|
|
|
cstring real_path;
|
2016-10-02 00:21:40 +00:00
|
|
|
cstring narrow_path = wcs2string(pathname);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2018-11-18 20:23:36 +00:00
|
|
|
// Strip trailing slashes. This is treats "/a//" as equivalent to "/a" if /a is a non-directory.
|
2016-10-04 00:51:27 +00:00
|
|
|
while (narrow_path.size() > 1 && narrow_path.at(narrow_path.size() - 1) == '/') {
|
|
|
|
narrow_path.erase(narrow_path.size() - 1, 1);
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2017-10-11 06:31:27 +00:00
|
|
|
char tmpbuf[PATH_MAX];
|
|
|
|
char *narrow_res = realpath(narrow_path.c_str(), tmpbuf);
|
2018-11-18 16:56:24 +00:00
|
|
|
|
2016-10-04 00:51:27 +00:00
|
|
|
if (narrow_res) {
|
|
|
|
real_path.append(narrow_res);
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
2018-11-18 19:25:19 +00:00
|
|
|
// Check if everything up to the last path component is valid.
|
2016-10-09 21:36:08 +00:00
|
|
|
size_t pathsep_idx = narrow_path.rfind('/');
|
2018-11-18 16:56:24 +00:00
|
|
|
|
2016-10-04 00:51:27 +00:00
|
|
|
if (pathsep_idx == 0) {
|
|
|
|
// If the only pathsep is the first character then it's an absolute path with a
|
|
|
|
// single path component and thus doesn't need conversion.
|
|
|
|
real_path = narrow_path;
|
|
|
|
} else {
|
2018-11-18 19:25:19 +00:00
|
|
|
// Only call realpath() on the portion up to the last component.
|
|
|
|
errno = 0;
|
2018-11-18 20:23:36 +00:00
|
|
|
|
2016-10-04 00:51:27 +00:00
|
|
|
if (pathsep_idx == cstring::npos) {
|
2018-11-18 19:25:19 +00:00
|
|
|
// If there is no "/", this is a file in $PWD, so give the realpath to that.
|
|
|
|
narrow_res = realpath(".", tmpbuf);
|
2016-10-04 00:51:27 +00:00
|
|
|
} else {
|
2018-11-18 21:54:34 +00:00
|
|
|
errno = 0;
|
|
|
|
// Only call realpath() on the portion up to the last component.
|
|
|
|
narrow_res = realpath(narrow_path.substr(0, pathsep_idx).c_str(), tmpbuf);
|
2018-11-18 19:25:19 +00:00
|
|
|
}
|
2018-11-18 16:56:24 +00:00
|
|
|
|
2018-11-18 19:25:19 +00:00
|
|
|
if (!narrow_res) return none();
|
|
|
|
|
|
|
|
pathsep_idx++;
|
|
|
|
real_path.append(narrow_res);
|
2018-11-18 16:56:24 +00:00
|
|
|
|
2018-11-18 20:23:36 +00:00
|
|
|
// This test is to deal with cases such as /../../x => //x.
|
2016-10-04 00:51:27 +00:00
|
|
|
if (real_path.size() > 1) real_path.append("/");
|
2018-11-18 20:23:36 +00:00
|
|
|
|
2016-10-04 00:51:27 +00:00
|
|
|
real_path.append(narrow_path.substr(pathsep_idx, cstring::npos));
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2017-10-11 07:08:26 +00:00
|
|
|
return str2wcstring(real_path);
|
2006-02-02 15:23:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 00:55:15 +00:00
|
|
|
wcstring normalize_path(const wcstring &path) {
|
|
|
|
// Count the leading slashes.
|
|
|
|
const wchar_t sep = L'/';
|
|
|
|
size_t leading_slashes = 0;
|
|
|
|
for (wchar_t c : path) {
|
|
|
|
if (c != sep) break;
|
|
|
|
leading_slashes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
wcstring_list_t comps = split_string(path, sep);
|
|
|
|
wcstring_list_t new_comps;
|
|
|
|
for (wcstring &comp : comps) {
|
|
|
|
if (comp.empty() || comp == L".") {
|
|
|
|
continue;
|
2018-10-13 05:15:16 +00:00
|
|
|
} else if (comp != L"..") {
|
2018-09-17 00:55:15 +00:00
|
|
|
new_comps.push_back(std::move(comp));
|
2018-10-13 05:15:16 +00:00
|
|
|
} else if (!new_comps.empty() && new_comps.back() != L"..") {
|
|
|
|
// '..' with a real path component, drop that path component.
|
|
|
|
new_comps.pop_back();
|
|
|
|
} else if (leading_slashes == 0) {
|
|
|
|
// We underflowed the .. and are a relative (not absolute) path.
|
|
|
|
new_comps.push_back(L"..");
|
2018-09-17 00:55:15 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-13 05:15:16 +00:00
|
|
|
wcstring result = join_strings(new_comps, sep);
|
|
|
|
// Prepend one or two leading slashes.
|
|
|
|
// Two slashes are preserved. Three+ slashes are collapsed to one. (!)
|
|
|
|
result.insert(0, leading_slashes > 2 ? 1 : leading_slashes, sep);
|
|
|
|
// Ensure ./ normalizes to . and not empty.
|
|
|
|
if (result.empty()) result.push_back(L'.');
|
|
|
|
return result;
|
2018-09-17 00:55:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 02:02:28 +00:00
|
|
|
wcstring path_normalize_for_cd(const wcstring &wd, const wcstring &path) {
|
|
|
|
// Fast paths.
|
|
|
|
const wchar_t sep = L'/';
|
|
|
|
assert(!wd.empty() && wd.front() == sep && wd.back() == sep &&
|
|
|
|
"Invalid working directory, it must start and end with /");
|
|
|
|
if (path.empty()) {
|
|
|
|
return wd;
|
|
|
|
} else if (path.front() == sep) {
|
|
|
|
return path;
|
|
|
|
} else if (path.front() != L'.') {
|
|
|
|
return wd + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split our strings by the sep.
|
|
|
|
wcstring_list_t wd_comps = split_string(wd, sep);
|
|
|
|
wcstring_list_t path_comps = split_string(path, sep);
|
|
|
|
|
|
|
|
// Remove empty segments from wd_comps.
|
|
|
|
// In particular this removes the leading and trailing empties.
|
|
|
|
wd_comps.erase(std::remove(wd_comps.begin(), wd_comps.end(), L""), wd_comps.end());
|
|
|
|
|
|
|
|
// Erase leading . and .. components from path_comps, popping from wd_comps as we go.
|
|
|
|
size_t erase_count = 0;
|
|
|
|
for (const wcstring &comp : path_comps) {
|
|
|
|
bool erase_it = false;
|
|
|
|
if (comp.empty() || comp == L".") {
|
|
|
|
erase_it = true;
|
|
|
|
} else if (comp == L".." && !wd_comps.empty()) {
|
|
|
|
erase_it = true;
|
|
|
|
wd_comps.pop_back();
|
|
|
|
}
|
|
|
|
if (erase_it) {
|
|
|
|
erase_count++;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Append un-erased elements to wd_comps and join them, then prepend the leading /.
|
|
|
|
std::move(path_comps.begin() + erase_count, path_comps.end(), std::back_inserter(wd_comps));
|
|
|
|
wcstring result = join_strings(wd_comps, sep);
|
|
|
|
result.insert(0, 1, L'/');
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
wcstring wdirname(const wcstring &path) {
|
2017-08-05 22:08:39 +00:00
|
|
|
char *tmp = wcs2str(path);
|
2012-11-19 00:30:30 +00:00
|
|
|
char *narrow_res = dirname(tmp);
|
2011-12-27 03:18:46 +00:00
|
|
|
wcstring result = format_string(L"%s", narrow_res);
|
|
|
|
free(tmp);
|
|
|
|
return result;
|
2006-06-14 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
wcstring wbasename(const wcstring &path) {
|
2017-08-05 22:08:39 +00:00
|
|
|
char *tmp = wcs2str(path);
|
2012-11-19 00:30:30 +00:00
|
|
|
char *narrow_res = basename(tmp);
|
2011-12-27 03:18:46 +00:00
|
|
|
wcstring result = format_string(L"%s", narrow_res);
|
|
|
|
free(tmp);
|
|
|
|
return result;
|
2006-06-14 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
// Really init wgettext.
|
|
|
|
static void wgettext_really_init() {
|
2013-04-08 17:20:56 +00:00
|
|
|
fish_bindtextdomain(PACKAGE_NAME, LOCALEDIR);
|
|
|
|
fish_textdomain(PACKAGE_NAME);
|
2006-07-19 22:55:49 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
/// For wgettext: Internal init function. Automatically called when a translation is first
|
|
|
|
/// requested.
|
|
|
|
static void wgettext_init_if_necessary() {
|
2019-04-29 01:13:55 +00:00
|
|
|
static std::once_flag s_wgettext_init{};
|
|
|
|
std::call_once(s_wgettext_init, wgettext_really_init);
|
2012-02-17 23:55:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 05:03:27 +00:00
|
|
|
const wcstring &wgettext(const wchar_t *in) {
|
2016-05-03 22:18:24 +00:00
|
|
|
// Preserve errno across this since this is often used in printing error messages.
|
2012-11-19 00:30:30 +00:00
|
|
|
int err = errno;
|
2016-06-02 05:03:27 +00:00
|
|
|
wcstring key = in;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-17 23:55:54 +00:00
|
|
|
wgettext_init_if_necessary();
|
2018-09-01 20:11:42 +00:00
|
|
|
auto wmap = wgettext_map.acquire();
|
|
|
|
wcstring &val = (*wmap)[key];
|
2016-05-03 22:18:24 +00:00
|
|
|
if (val.empty()) {
|
2016-10-02 00:21:40 +00:00
|
|
|
cstring mbs_in = wcs2string(key);
|
2013-04-08 17:20:56 +00:00
|
|
|
char *out = fish_gettext(mbs_in.c_str());
|
2016-03-28 01:01:19 +00:00
|
|
|
val = format_string(L"%s", out);
|
2012-02-24 20:13:35 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
errno = err;
|
2016-03-18 22:14:16 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
// The returned string is stored in the map.
|
|
|
|
// TODO: If we want to shrink the map, this would be a problem.
|
2016-06-02 05:03:27 +00:00
|
|
|
return val;
|
2006-07-19 22:55:49 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int wmkdir(const wcstring &name, int mode) {
|
2016-10-02 00:21:40 +00:00
|
|
|
cstring name_narrow = wcs2string(name);
|
2012-11-19 00:30:30 +00:00
|
|
|
return mkdir(name_narrow.c_str(), mode);
|
2006-09-08 14:11:28 +00:00
|
|
|
}
|
2006-10-20 22:33:47 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int wrename(const wcstring &old, const wcstring &newv) {
|
2016-10-02 00:21:40 +00:00
|
|
|
cstring old_narrow = wcs2string(old);
|
|
|
|
cstring new_narrow = wcs2string(newv);
|
2012-11-19 00:30:30 +00:00
|
|
|
return rename(old_narrow.c_str(), new_narrow.c_str());
|
2006-10-20 22:33:47 +00:00
|
|
|
}
|
2012-08-04 18:07:42 +00:00
|
|
|
|
2016-09-28 04:07:10 +00:00
|
|
|
/// Return one if the code point is in a Unicode private use area.
|
|
|
|
int fish_is_pua(wint_t wc) {
|
|
|
|
if (PUA1_START <= wc && wc < PUA1_END) return 1;
|
|
|
|
if (PUA2_START <= wc && wc < PUA2_END) return 1;
|
|
|
|
if (PUA3_START <= wc && wc < PUA3_END) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We need this because there are too many implementations that don't return the proper answer for
|
|
|
|
/// some code points. See issue #3050.
|
|
|
|
int fish_iswalnum(wint_t wc) {
|
2016-10-17 23:23:29 +00:00
|
|
|
if (fish_reserved_codepoint(wc)) return 0;
|
2016-09-28 04:07:10 +00:00
|
|
|
if (fish_is_pua(wc)) return 0;
|
|
|
|
return iswalnum(wc);
|
|
|
|
}
|
|
|
|
|
2017-05-05 05:42:42 +00:00
|
|
|
#if 0
|
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.
|
|
|
|
int fish_iswalpha(wint_t wc) {
|
2016-10-17 23:23:29 +00:00
|
|
|
if (fish_reserved_codepoint(wc)) return 0;
|
2016-09-28 04:07:10 +00:00
|
|
|
if (fish_is_pua(wc)) return 0;
|
|
|
|
return iswalpha(wc);
|
|
|
|
}
|
2017-05-05 05:42:42 +00:00
|
|
|
#endif
|
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.
|
|
|
|
int fish_iswgraph(wint_t wc) {
|
2016-10-17 23:23:29 +00:00
|
|
|
if (fish_reserved_codepoint(wc)) return 0;
|
2016-09-28 04:07:10 +00:00
|
|
|
if (fish_is_pua(wc)) return 1;
|
|
|
|
return iswgraph(wc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience variants on fish_wcwswidth().
|
|
|
|
///
|
|
|
|
/// See fallback.h for the normal definitions.
|
2019-03-12 21:06:01 +00:00
|
|
|
int fish_wcswidth(const wchar_t *str) { return fish_wcswidth(str, std::wcslen(str)); }
|
2016-09-28 04:07:10 +00:00
|
|
|
|
|
|
|
/// Convenience variants on fish_wcwswidth().
|
|
|
|
///
|
|
|
|
/// See fallback.h for the normal definitions.
|
|
|
|
int fish_wcswidth(const wcstring &str) { return fish_wcswidth(str.c_str(), str.size()); }
|
|
|
|
|
2018-07-29 00:56:42 +00:00
|
|
|
locale_t fish_c_locale() {
|
2019-11-19 02:34:50 +00:00
|
|
|
static const locale_t loc = newlocale(LC_ALL_MASK, "C", nullptr);
|
2018-07-29 00:56:42 +00:00
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
|
2016-11-23 04:24:03 +00:00
|
|
|
/// Like fish_wcstol(), but fails on a value outside the range of an int.
|
|
|
|
///
|
|
|
|
/// This is needed because BSD and GNU implementations differ in several ways that make it really
|
|
|
|
/// annoying to use them in a portable fashion.
|
|
|
|
///
|
|
|
|
/// The caller doesn't have to zero errno. Sets errno to -1 if the int ends with something other
|
|
|
|
/// than a digit. Leading whitespace is ignored (per the base wcstol implementation). Trailing
|
|
|
|
/// whitespace is also ignored. We also treat empty strings and strings containing only whitespace
|
|
|
|
/// as invalid.
|
|
|
|
int fish_wcstoi(const wchar_t *str, const wchar_t **endptr, int base) {
|
|
|
|
while (iswspace(*str)) ++str; // skip leading whitespace
|
|
|
|
if (!*str) { // this is because some implementations don't handle this sensibly
|
|
|
|
errno = EINVAL;
|
|
|
|
if (endptr) *endptr = str;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
wchar_t *_endptr;
|
2019-03-12 21:06:01 +00:00
|
|
|
long result = std::wcstol(str, &_endptr, base);
|
2016-11-23 04:24:03 +00:00
|
|
|
if (result > INT_MAX) {
|
|
|
|
result = INT_MAX;
|
2016-10-02 00:21:40 +00:00
|
|
|
errno = ERANGE;
|
2016-11-23 04:24:03 +00:00
|
|
|
} else if (result < INT_MIN) {
|
|
|
|
result = INT_MIN;
|
2016-10-02 00:21:40 +00:00
|
|
|
errno = ERANGE;
|
|
|
|
}
|
2016-11-23 04:24:03 +00:00
|
|
|
while (iswspace(*_endptr)) ++_endptr; // skip trailing whitespace
|
|
|
|
if (!errno && *_endptr) {
|
|
|
|
if (_endptr == str) {
|
|
|
|
errno = EINVAL;
|
|
|
|
} else {
|
|
|
|
errno = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (endptr) *endptr = _endptr;
|
2019-11-19 01:08:16 +00:00
|
|
|
return static_cast<int>(result);
|
2016-11-23 04:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An enhanced version of wcstol().
|
|
|
|
///
|
|
|
|
/// This is needed because BSD and GNU implementations differ in several ways that make it really
|
|
|
|
/// annoying to use them in a portable fashion.
|
|
|
|
///
|
|
|
|
/// The caller doesn't have to zero errno. Sets errno to -1 if the int ends with something other
|
|
|
|
/// than a digit. Leading whitespace is ignored (per the base wcstol implementation). Trailing
|
|
|
|
/// whitespace is also ignored.
|
|
|
|
long fish_wcstol(const wchar_t *str, const wchar_t **endptr, int base) {
|
|
|
|
while (iswspace(*str)) ++str; // skip leading whitespace
|
|
|
|
if (!*str) { // this is because some implementations don't handle this sensibly
|
|
|
|
errno = EINVAL;
|
|
|
|
if (endptr) *endptr = str;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
wchar_t *_endptr;
|
2019-03-12 21:06:01 +00:00
|
|
|
long result = std::wcstol(str, &_endptr, base);
|
2016-11-23 04:24:03 +00:00
|
|
|
while (iswspace(*_endptr)) ++_endptr; // skip trailing whitespace
|
|
|
|
if (!errno && *_endptr) {
|
|
|
|
if (_endptr == str) {
|
|
|
|
errno = EINVAL;
|
|
|
|
} else {
|
|
|
|
errno = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (endptr) *endptr = _endptr;
|
|
|
|
return result;
|
2016-10-02 00:21:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 02:43:50 +00:00
|
|
|
/// An enhanced version of wcstoll().
|
|
|
|
///
|
|
|
|
/// This is needed because BSD and GNU implementations differ in several ways that make it really
|
|
|
|
/// annoying to use them in a portable fashion.
|
|
|
|
///
|
|
|
|
/// The caller doesn't have to zero errno. Sets errno to -1 if the int ends with something other
|
|
|
|
/// than a digit. Leading whitespace is ignored (per the base wcstoll implementation). Trailing
|
|
|
|
/// whitespace is also ignored.
|
|
|
|
long long fish_wcstoll(const wchar_t *str, const wchar_t **endptr, int base) {
|
|
|
|
while (iswspace(*str)) ++str; // skip leading whitespace
|
|
|
|
if (!*str) { // this is because some implementations don't handle this sensibly
|
|
|
|
errno = EINVAL;
|
|
|
|
if (endptr) *endptr = str;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
wchar_t *_endptr;
|
2019-03-12 21:06:01 +00:00
|
|
|
long long result = std::wcstoll(str, &_endptr, base);
|
2016-11-25 02:43:50 +00:00
|
|
|
while (iswspace(*_endptr)) ++_endptr; // skip trailing whitespace
|
|
|
|
if (!errno && *_endptr) {
|
|
|
|
if (_endptr == str) {
|
|
|
|
errno = EINVAL;
|
|
|
|
} else {
|
|
|
|
errno = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (endptr) *endptr = _endptr;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-10 08:36:26 +00:00
|
|
|
/// An enhanced version of wcstoull().
|
|
|
|
///
|
|
|
|
/// This is needed because BSD and GNU implementations differ in several ways that make it really
|
|
|
|
/// annoying to use them in a portable fashion.
|
|
|
|
///
|
|
|
|
/// The caller doesn't have to zero errno. Sets errno to -1 if the int ends with something other
|
2016-11-28 20:57:58 +00:00
|
|
|
/// than a digit. Leading minus is considered invalid. Leading whitespace is ignored (per the base
|
|
|
|
/// wcstoull implementation). Trailing whitespace is also ignored.
|
2016-12-10 08:36:26 +00:00
|
|
|
unsigned long long fish_wcstoull(const wchar_t *str, const wchar_t **endptr, int base) {
|
|
|
|
while (iswspace(*str)) ++str; // skip leading whitespace
|
2016-11-28 20:57:58 +00:00
|
|
|
if (!*str || // this is because some implementations don't handle this sensibly
|
|
|
|
*str == '-') // disallow minus as the first character to avoid questionable wrap-around
|
|
|
|
{
|
2016-12-10 08:36:26 +00:00
|
|
|
errno = EINVAL;
|
|
|
|
if (endptr) *endptr = str;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
wchar_t *_endptr;
|
2019-03-12 21:06:01 +00:00
|
|
|
unsigned long long result = std::wcstoull(str, &_endptr, base);
|
2016-12-10 08:36:26 +00:00
|
|
|
while (iswspace(*_endptr)) ++_endptr; // skip trailing whitespace
|
|
|
|
if (!errno && *_endptr) {
|
|
|
|
if (_endptr == str) {
|
|
|
|
errno = EINVAL;
|
|
|
|
} else {
|
|
|
|
errno = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (endptr) *endptr = _endptr;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-04 23:53:31 +00:00
|
|
|
/// Like wcstod(), but wcstod() is enormously expensive on some platforms so this tries to have a
|
|
|
|
/// fast path.
|
|
|
|
double fish_wcstod(const wchar_t *str, wchar_t **endptr) {
|
|
|
|
// The "fast path." If we're all ASCII and we fit inline, use strtod().
|
|
|
|
char narrow[128];
|
2019-03-12 21:06:01 +00:00
|
|
|
size_t len = std::wcslen(str);
|
2018-11-07 06:59:11 +00:00
|
|
|
size_t len_plus_0 = 1 + len;
|
|
|
|
auto is_digit = [](wchar_t c) { return '0' <= c && c <= '9'; };
|
|
|
|
if (len_plus_0 <= sizeof narrow && std::all_of(str, str + len, is_digit)) {
|
2018-11-04 23:53:31 +00:00
|
|
|
// Fast path. Copy the string into a local buffer and run strtod() on it.
|
2018-11-07 06:59:11 +00:00
|
|
|
// We can ignore the locale-taking version because we are limited to ASCII digits.
|
2018-11-04 23:53:31 +00:00
|
|
|
std::copy(str, str + len_plus_0, narrow);
|
|
|
|
char *narrow_endptr = nullptr;
|
|
|
|
double ret = strtod(narrow, endptr ? &narrow_endptr : nullptr);
|
|
|
|
if (endptr) {
|
|
|
|
assert(narrow_endptr && "narrow_endptr should not be null");
|
|
|
|
*endptr = const_cast<wchar_t *>(str + (narrow_endptr - narrow));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return wcstod_l(str, endptr, fish_c_locale());
|
|
|
|
}
|
|
|
|
|
2018-07-14 21:29:19 +00:00
|
|
|
file_id_t file_id_t::from_stat(const struct stat &buf) {
|
2014-04-28 22:14:33 +00:00
|
|
|
file_id_t result = {};
|
2018-07-14 21:29:19 +00:00
|
|
|
result.device = buf.st_dev;
|
|
|
|
result.inode = buf.st_ino;
|
|
|
|
result.size = buf.st_size;
|
|
|
|
result.change_seconds = buf.st_ctime;
|
|
|
|
result.mod_seconds = buf.st_mtime;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2016-10-10 22:40:33 +00:00
|
|
|
#ifdef HAVE_STRUCT_STAT_ST_CTIME_NSEC
|
2018-07-14 21:29:19 +00:00
|
|
|
result.change_nanoseconds = buf.st_ctime_nsec;
|
|
|
|
result.mod_nanoseconds = buf.st_mtime_nsec;
|
2014-04-28 22:14:33 +00:00
|
|
|
#elif defined(__APPLE__)
|
2018-07-14 21:29:19 +00:00
|
|
|
result.change_nanoseconds = buf.st_ctimespec.tv_nsec;
|
|
|
|
result.mod_nanoseconds = buf.st_mtimespec.tv_nsec;
|
2014-04-28 22:14:33 +00:00
|
|
|
#elif defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_XOPEN_SOURCE)
|
2018-07-14 21:29:19 +00:00
|
|
|
result.change_nanoseconds = buf.st_ctim.tv_nsec;
|
|
|
|
result.mod_nanoseconds = buf.st_mtim.tv_nsec;
|
2014-04-28 22:14:33 +00:00
|
|
|
#else
|
|
|
|
result.change_nanoseconds = 0;
|
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
|
|
|
result.mod_nanoseconds = 0;
|
2014-04-28 22:14:33 +00:00
|
|
|
#endif
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2014-04-28 22:14:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
file_id_t file_id_for_fd(int fd) {
|
2014-04-27 20:34:51 +00:00
|
|
|
file_id_t result = kInvalidFileID;
|
|
|
|
struct stat buf = {};
|
2017-02-06 18:01:33 +00:00
|
|
|
if (fd >= 0 && 0 == fstat(fd, &buf)) {
|
2018-07-14 21:29:19 +00:00
|
|
|
result = file_id_t::from_stat(buf);
|
2014-04-27 20:34:51 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
file_id_t file_id_for_path(const wcstring &path) {
|
2014-04-27 20:34:51 +00:00
|
|
|
file_id_t result = kInvalidFileID;
|
|
|
|
struct stat buf = {};
|
2016-05-03 22:18:24 +00:00
|
|
|
if (0 == wstat(path, &buf)) {
|
2018-07-14 21:29:19 +00:00
|
|
|
result = file_id_t::from_stat(buf);
|
2014-04-27 20:34:51 +00:00
|
|
|
}
|
2019-05-31 07:33:50 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_id_t file_id_for_path(const std::string &path) {
|
|
|
|
file_id_t result = kInvalidFileID;
|
|
|
|
struct stat buf = {};
|
|
|
|
if (0 == stat(path.c_str(), &buf)) {
|
|
|
|
result = file_id_t::from_stat(buf);
|
|
|
|
}
|
2014-04-27 20:34:51 +00:00
|
|
|
return result;
|
|
|
|
}
|
2014-04-28 22:14:33 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
bool file_id_t::operator==(const file_id_t &rhs) const { return this->compare_file_id(rhs) == 0; }
|
2014-04-28 22:14:33 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
bool file_id_t::operator!=(const file_id_t &rhs) const { return !(*this == rhs); }
|
2014-04-28 22:14:33 +00:00
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
template <typename T>
|
|
|
|
int compare(T a, T b) {
|
|
|
|
if (a < b) {
|
2014-04-28 22:14:33 +00:00
|
|
|
return -1;
|
2016-05-03 22:18:24 +00:00
|
|
|
} else if (a > b) {
|
2014-04-28 22:14:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int file_id_t::compare_file_id(const file_id_t &rhs) const {
|
|
|
|
// Compare each field, stopping when we get to a non-equal field.
|
2014-04-28 22:14:33 +00:00
|
|
|
int ret = 0;
|
2016-05-03 22:18:24 +00:00
|
|
|
if (!ret) ret = compare(device, rhs.device);
|
|
|
|
if (!ret) ret = compare(inode, rhs.inode);
|
|
|
|
if (!ret) ret = compare(size, rhs.size);
|
|
|
|
if (!ret) ret = compare(change_seconds, rhs.change_seconds);
|
|
|
|
if (!ret) ret = compare(change_nanoseconds, rhs.change_nanoseconds);
|
|
|
|
if (!ret) ret = compare(mod_seconds, rhs.mod_seconds);
|
|
|
|
if (!ret) ret = compare(mod_nanoseconds, rhs.mod_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 07:48:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
bool file_id_t::operator<(const file_id_t &rhs) const { return this->compare_file_id(rhs) < 0; }
|