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 <assert.h>
|
|
|
|
#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>
|
2012-02-17 23:55:54 +00:00
|
|
|
#include <pthread.h>
|
2016-05-03 22:18:24 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2016-11-15 05:31:51 +00:00
|
|
|
#include <wctype.h>
|
2012-02-24 20:13:35 +00:00
|
|
|
#include <map>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <memory>
|
2016-05-03 22:18:24 +00:00
|
|
|
#include <string>
|
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
|
2016-05-03 22:18:24 +00:00
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
typedef std::string cstring;
|
|
|
|
|
2016-04-03 05:19:44 +00:00
|
|
|
const file_id_t kInvalidFileID = {(dev_t)-1LL, (ino_t)-1LL, (uint64_t)-1LL, -1, -1, -1, -1};
|
2014-04-27 20:34:51 +00:00
|
|
|
|
2006-02-02 15:23:56 +00:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#ifdef MAXPATHLEN
|
|
|
|
#define PATH_MAX MAXPATHLEN
|
|
|
|
#else
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Fallback length of MAXPATHLEN. Hopefully a sane value.
|
|
|
|
#define PATH_MAX 4096
|
2006-02-02 15:23:56 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2016-09-10 21:38:28 +00:00
|
|
|
/// Lock to protect wgettext.
|
|
|
|
static pthread_mutex_t wgettext_lock;
|
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
/// Map used as cache by wgettext.
|
|
|
|
typedef std::map<wcstring, wcstring> wgettext_map_t;
|
|
|
|
static wgettext_map_t wgettext_map;
|
2016-09-10 21:38:28 +00:00
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, std::wstring &out_name,
|
|
|
|
bool *out_is_dir) {
|
|
|
|
struct dirent *d = readdir(dir);
|
|
|
|
if (!d) return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-20 10:13:31 +00:00
|
|
|
out_name = str2wcstring(d->d_name);
|
2016-10-30 19:38:56 +00:00
|
|
|
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
|
2016-10-30 19:38:56 +00:00
|
|
|
if (d->d_type == DT_DIR) {
|
|
|
|
// Known directory.
|
|
|
|
is_dir = true;
|
|
|
|
check_with_stat = false;
|
|
|
|
} else if (d->d_type == DT_LNK || d->d_type == DT_UNKNOWN) {
|
|
|
|
// 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('/');
|
|
|
|
fullpath.append(d->d_name);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
bool wreaddir(DIR *dir, std::wstring &out_name) {
|
2012-11-19 00:30:30 +00:00
|
|
|
struct dirent *d = readdir(dir);
|
|
|
|
if (!d) return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-20 10:13:31 +00:00
|
|
|
out_name = str2wcstring(d->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) {
|
2015-08-08 21:52:04 +00:00
|
|
|
struct dirent *result = NULL;
|
2016-05-03 22:18:24 +00:00
|
|
|
while (result == NULL) {
|
2015-08-08 21:52:04 +00:00
|
|
|
struct dirent *d = readdir(dir);
|
|
|
|
if (!d) break;
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2015-08-08 21:52:04 +00:00
|
|
|
#if HAVE_STRUCT_DIRENT_D_TYPE
|
2016-05-03 22:18:24 +00:00
|
|
|
switch (d->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: {
|
|
|
|
// These may be directories.
|
2015-08-08 21:52:04 +00:00
|
|
|
result = d;
|
|
|
|
break;
|
2016-05-03 22:18:24 +00:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Nothing else can.
|
2015-08-08 21:52:04 +00:00
|
|
|
break;
|
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.
|
2015-08-08 21:52:04 +00:00
|
|
|
result = d;
|
|
|
|
#endif
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
return result != NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
const wcstring wgetcwd() {
|
2016-03-11 02:17:39 +00:00
|
|
|
wcstring retval;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-03-11 02:17:39 +00:00
|
|
|
char *res = getcwd(NULL, 0);
|
2016-05-03 22:18:24 +00:00
|
|
|
if (res) {
|
2016-03-11 02:17:39 +00:00
|
|
|
retval = str2wcstring(res);
|
|
|
|
free(res);
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
2016-03-11 02:17:39 +00:00
|
|
|
debug(0, _(L"getcwd() failed with errno %d/%s"), errno, strerror(errno));
|
|
|
|
retval = wcstring();
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-03-11 02:17:39 +00:00
|
|
|
return retval;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
int wchdir(const wcstring &dir) {
|
2016-10-02 00:21:40 +00:00
|
|
|
cstring tmp = wcs2string(dir);
|
2012-11-19 00:30:30 +00:00
|
|
|
return chdir(tmp.c_str());
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 00:21:40 +00:00
|
|
|
FILE *wfopen(const wcstring &path, const char *mode) {
|
|
|
|
int permissions = 0, options = 0;
|
|
|
|
size_t idx = 0;
|
|
|
|
switch (mode[idx++]) {
|
|
|
|
case 'r': {
|
|
|
|
permissions = O_RDONLY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'w': {
|
|
|
|
permissions = O_WRONLY;
|
|
|
|
options = O_CREAT | O_TRUNC;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'a': {
|
|
|
|
permissions = O_WRONLY;
|
|
|
|
options = O_CREAT | O_APPEND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
errno = EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Skip binary.
|
|
|
|
if (mode[idx] == 'b') idx++;
|
|
|
|
|
|
|
|
// Consider append option.
|
|
|
|
if (mode[idx] == '+') permissions = O_RDWR;
|
|
|
|
|
|
|
|
int fd = wopen_cloexec(path, permissions | options, 0666);
|
|
|
|
if (fd < 0) return NULL;
|
|
|
|
FILE *result = fdopen(fd, mode);
|
|
|
|
if (result == NULL) close(fd);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set_cloexec(int fd) {
|
|
|
|
int flags = fcntl(fd, F_GETFD, 0);
|
|
|
|
if (flags < 0) {
|
|
|
|
return false;
|
2016-11-02 21:42:33 +00:00
|
|
|
}
|
|
|
|
if (flags & FD_CLOEXEC) {
|
2016-10-02 00:21:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-02 21:42:33 +00:00
|
|
|
return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0;
|
2016-10-02 00:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int wopen_internal(const wcstring &pathname, int flags, mode_t mode, bool cloexec) {
|
|
|
|
ASSERT_IS_NOT_FORKED_CHILD();
|
|
|
|
cstring tmp = wcs2string(pathname);
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
#ifdef O_CLOEXEC
|
|
|
|
// Prefer to use O_CLOEXEC. It has to both be defined and nonzero.
|
|
|
|
if (cloexec) {
|
|
|
|
fd = open(tmp.c_str(), flags | O_CLOEXEC, mode);
|
|
|
|
} else {
|
|
|
|
fd = open(tmp.c_str(), flags, mode);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
fd = open(tmp.c_str(), flags, mode);
|
|
|
|
if (fd >= 0 && !set_cloexec(fd)) {
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wopen_cloexec(const wcstring &pathname, int flags, mode_t mode) {
|
|
|
|
return wopen_internal(pathname, flags, mode, true);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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') {
|
2014-04-27 20:34:51 +00:00
|
|
|
fwprintf(stderr, L"%ls: ", s);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
fwprintf(stderr, L"%s\n", 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;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
static inline void safe_append(char *buffer, const char *s, size_t buffsize) {
|
2013-01-10 01:06:20 +00:00
|
|
|
strncat(buffer, s, buffsize - strlen(buffer) - 1);
|
|
|
|
}
|
|
|
|
|
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.
|
2013-05-25 20:42:16 +00:00
|
|
|
return strerror(err);
|
2014-12-08 00:43:38 +00:00
|
|
|
#elif defined(HAVE__SYS__ERRS) || defined(HAVE_SYS_ERRLIST)
|
|
|
|
#ifdef HAVE_SYS_ERRLIST
|
2016-05-03 22:18:24 +00:00
|
|
|
if (err >= 0 && err < sys_nerr && sys_errlist[err] != NULL) {
|
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
|
|
|
|
2014-04-28 01:27:34 +00:00
|
|
|
write_ignore(STDERR_FILENO, buff, strlen(buff));
|
2013-01-10 01:06:20 +00:00
|
|
|
errno = err;
|
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path) {
|
2016-10-04 00:51:27 +00:00
|
|
|
if (pathname.size() == 0) return NULL;
|
|
|
|
|
|
|
|
cstring real_path("");
|
2016-10-02 00:21:40 +00:00
|
|
|
cstring narrow_path = wcs2string(pathname);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-10-04 00:51:27 +00:00
|
|
|
// Strip trailing slashes. This is needed to be bug-for-bug compatible with GNU realpath which
|
|
|
|
// treats "/a//" as equivalent to "/a" whether or not /a exists.
|
|
|
|
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
|
|
|
|
2016-10-04 00:51:27 +00:00
|
|
|
char *narrow_res = realpath(narrow_path.c_str(), NULL);
|
|
|
|
if (narrow_res) {
|
|
|
|
real_path.append(narrow_res);
|
2016-05-03 22:18:24 +00:00
|
|
|
} else {
|
2016-10-09 21:36:08 +00:00
|
|
|
size_t pathsep_idx = narrow_path.rfind('/');
|
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 {
|
|
|
|
if (pathsep_idx == cstring::npos) {
|
|
|
|
// No pathsep means a single path component relative to pwd.
|
|
|
|
narrow_res = realpath(".", NULL);
|
|
|
|
if (!narrow_res) DIE("unexpected realpath(\".\") failure");
|
|
|
|
pathsep_idx = 0;
|
|
|
|
} else {
|
|
|
|
// Only call realpath() on the portion up to the last component.
|
|
|
|
narrow_res = realpath(narrow_path.substr(0, pathsep_idx).c_str(), NULL);
|
|
|
|
if (!narrow_res) return NULL;
|
|
|
|
pathsep_idx++;
|
|
|
|
}
|
|
|
|
real_path.append(narrow_res);
|
|
|
|
// This test is to deal with pathological cases such as /../../x => //x.
|
|
|
|
if (real_path.size() > 1) real_path.append("/");
|
|
|
|
real_path.append(narrow_path.substr(pathsep_idx, cstring::npos));
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-19 00:46:13 +00:00
|
|
|
#if __APPLE__ && __DARWIN_C_LEVEL < 200809L
|
2016-05-27 21:41:16 +00:00
|
|
|
// OS X Snow Leopard is broken with respect to the dynamically allocated buffer returned by
|
|
|
|
// realpath(). It's not dynamically allocated so attempting to free that buffer triggers a
|
|
|
|
// malloc/free error. Thus we don't attempt the free in this case.
|
2016-05-19 00:46:13 +00:00
|
|
|
#else
|
2012-11-19 00:30:30 +00:00
|
|
|
free(narrow_res);
|
2016-05-19 00:46:13 +00:00
|
|
|
#endif
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-10-04 00:51:27 +00:00
|
|
|
wcstring wreal_path = str2wcstring(real_path);
|
|
|
|
if (resolved_path) {
|
|
|
|
wcslcpy(resolved_path, wreal_path.c_str(), PATH_MAX);
|
|
|
|
return resolved_path;
|
|
|
|
}
|
|
|
|
return wcsdup(wreal_path.c_str());
|
2006-02-02 15:23:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:18:24 +00:00
|
|
|
wcstring wdirname(const wcstring &path) {
|
2011-12-27 03:18:46 +00:00
|
|
|
char *tmp = wcs2str(path.c_str());
|
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) {
|
2011-12-27 03:18:46 +00:00
|
|
|
char *tmp = wcs2str(path.c_str());
|
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() {
|
2012-02-24 20:13:35 +00:00
|
|
|
pthread_mutex_init(&wgettext_lock, NULL);
|
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() {
|
2012-02-17 23:55:54 +00:00
|
|
|
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
|
|
|
pthread_once(&once, wgettext_really_init);
|
|
|
|
}
|
|
|
|
|
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();
|
2016-07-21 05:30:58 +00:00
|
|
|
scoped_lock locker(wgettext_lock);
|
2016-03-18 22:14:16 +00:00
|
|
|
wcstring &val = wgettext_map[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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test if the given string is a valid variable name.
|
|
|
|
///
|
|
|
|
/// \return null if this is a valid name, and a pointer to the first invalid character otherwise.
|
|
|
|
const wchar_t *wcsvarname(const wchar_t *str) {
|
2016-11-22 02:10:15 +00:00
|
|
|
if (str[0] == L'\0') return str;
|
2016-09-28 04:07:10 +00:00
|
|
|
while (*str) {
|
|
|
|
if ((!fish_iswalnum(*str)) && (*str != L'_')) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test if the given string is a valid variable name.
|
|
|
|
///
|
|
|
|
/// \return null if this is a valid name, and a pointer to the first invalid character otherwise.
|
|
|
|
const wchar_t *wcsvarname(const wcstring &str) { return wcsvarname(str.c_str()); }
|
|
|
|
|
2016-11-17 06:00:33 +00:00
|
|
|
/// Test if the string is a valid function name.
|
2016-09-28 04:07:10 +00:00
|
|
|
///
|
2016-11-17 06:00:33 +00:00
|
|
|
/// \return true if it is valid else false.
|
|
|
|
bool wcsfuncname(const wcstring &str) {
|
|
|
|
if (str.size() == 0) return false;
|
|
|
|
if (str.at(0) == L'-') return false;
|
|
|
|
if (str.find_first_of(L'/') != wcstring::npos) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-28 04:07:10 +00:00
|
|
|
|
|
|
|
/// Test if the given string is valid in a variable name.
|
|
|
|
///
|
|
|
|
/// \return true if this is a valid name, false otherwise.
|
|
|
|
bool wcsvarchr(wchar_t chr) { return fish_iswalnum(chr) || chr == L'_'; }
|
|
|
|
|
|
|
|
/// Convenience variants on fish_wcwswidth().
|
|
|
|
///
|
|
|
|
/// See fallback.h for the normal definitions.
|
|
|
|
int fish_wcswidth(const wchar_t *str) { return fish_wcswidth(str, wcslen(str)); }
|
|
|
|
|
|
|
|
/// 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()); }
|
|
|
|
|
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;
|
|
|
|
long result = wcstol(str, &_endptr, base);
|
|
|
|
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;
|
|
|
|
return (int)result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
long result = wcstol(str, &_endptr, base);
|
|
|
|
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;
|
|
|
|
long long result = wcstoll(str, &_endptr, base);
|
|
|
|
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;
|
|
|
|
unsigned long long result = wcstoull(str, &_endptr, base);
|
|
|
|
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-05-03 22:18:24 +00:00
|
|
|
file_id_t file_id_t::file_id_from_stat(const struct stat *buf) {
|
2014-04-28 22:14:33 +00:00
|
|
|
assert(buf != NULL);
|
2016-05-03 22:18:24 +00:00
|
|
|
|
2014-04-28 22:14:33 +00:00
|
|
|
file_id_t result = {};
|
|
|
|
result.device = buf->st_dev;
|
|
|
|
result.inode = buf->st_ino;
|
|
|
|
result.size = buf->st_size;
|
|
|
|
result.change_seconds = buf->st_ctime;
|
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_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
|
2014-04-28 22:14:33 +00:00
|
|
|
result.change_nanoseconds = buf->st_ctime_nsec;
|
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 = buf->st_mtime_nsec;
|
2014-04-28 22:14:33 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
result.change_nanoseconds = buf->st_ctimespec.tv_nsec;
|
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 = buf->st_mtimespec.tv_nsec;
|
2014-04-28 22:14:33 +00:00
|
|
|
#elif defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_XOPEN_SOURCE)
|
|
|
|
result.change_nanoseconds = buf->st_ctim.tv_nsec;
|
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 = 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 = {};
|
2016-05-03 22:18:24 +00:00
|
|
|
if (0 == fstat(fd, &buf)) {
|
2014-04-28 22:14:33 +00:00
|
|
|
result = file_id_t::file_id_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)) {
|
2014-04-28 22:14:33 +00:00
|
|
|
result = file_id_t::file_id_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; }
|