mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-30 14:53:11 +00:00
wutil: Give narrow versions of a few functions
Note that this isn't technically *w*util, but the differences between the functions are basically just whether they do the wcs2string themselves or not.
This commit is contained in:
parent
4e03d3c264
commit
bc19647be2
2 changed files with 19 additions and 6 deletions
|
@ -189,20 +189,19 @@ bool set_cloexec(int fd) {
|
||||||
return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0;
|
return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int wopen_internal(const wcstring &pathname, int flags, mode_t mode, bool cloexec) {
|
int open_cloexec(const std::string &cstring, int flags, mode_t mode, bool cloexec) {
|
||||||
ASSERT_IS_NOT_FORKED_CHILD();
|
ASSERT_IS_NOT_FORKED_CHILD();
|
||||||
cstring tmp = wcs2string(pathname);
|
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
#ifdef O_CLOEXEC
|
#ifdef O_CLOEXEC
|
||||||
// Prefer to use O_CLOEXEC. It has to both be defined and nonzero.
|
// Prefer to use O_CLOEXEC. It has to both be defined and nonzero.
|
||||||
if (cloexec) {
|
if (cloexec) {
|
||||||
fd = open(tmp.c_str(), flags | O_CLOEXEC, mode);
|
fd = open(cstring.c_str(), flags | O_CLOEXEC, mode);
|
||||||
} else {
|
} else {
|
||||||
fd = open(tmp.c_str(), flags, mode);
|
fd = open(cstring.c_str(), flags, mode);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
fd = open(tmp.c_str(), flags, mode);
|
fd = open(cstring.c_str(), flags, mode);
|
||||||
if (fd >= 0 && !set_cloexec(fd)) {
|
if (fd >= 0 && !set_cloexec(fd)) {
|
||||||
close(fd);
|
close(fd);
|
||||||
fd = -1;
|
fd = -1;
|
||||||
|
@ -212,7 +211,8 @@ static int wopen_internal(const wcstring &pathname, int flags, mode_t mode, bool
|
||||||
}
|
}
|
||||||
|
|
||||||
int wopen_cloexec(const wcstring &pathname, int flags, mode_t mode) {
|
int wopen_cloexec(const wcstring &pathname, int flags, mode_t mode) {
|
||||||
return wopen_internal(pathname, flags, mode, true);
|
cstring tmp = wcs2string(pathname);
|
||||||
|
return open_cloexec(tmp, flags, mode, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
DIR *wopendir(const wcstring &name) {
|
DIR *wopendir(const wcstring &name) {
|
||||||
|
@ -815,6 +815,15 @@ file_id_t file_id_for_path(const wcstring &path) {
|
||||||
return result;
|
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);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
bool file_id_t::operator==(const file_id_t &rhs) const { return this->compare_file_id(rhs) == 0; }
|
bool file_id_t::operator==(const file_id_t &rhs) const { return this->compare_file_id(rhs) == 0; }
|
||||||
|
|
||||||
bool file_id_t::operator!=(const file_id_t &rhs) const { return !(*this == rhs); }
|
bool file_id_t::operator!=(const file_id_t &rhs) const { return !(*this == rhs); }
|
||||||
|
|
|
@ -28,6 +28,9 @@ bool set_cloexec(int fd);
|
||||||
/// possible).
|
/// possible).
|
||||||
int wopen_cloexec(const wcstring &pathname, int flags, mode_t mode = 0);
|
int wopen_cloexec(const wcstring &pathname, int flags, mode_t mode = 0);
|
||||||
|
|
||||||
|
/// Narrow version of wopen_cloexec.
|
||||||
|
int open_cloexec(const std::string &cstring, int flags, mode_t mode = 0, bool cloexec = true);
|
||||||
|
|
||||||
/// Mark an fd as nonblocking; returns errno or 0 on success.
|
/// Mark an fd as nonblocking; returns errno or 0 on success.
|
||||||
int make_fd_nonblocking(int fd);
|
int make_fd_nonblocking(int fd);
|
||||||
|
|
||||||
|
@ -195,6 +198,7 @@ struct hash<file_id_t> {
|
||||||
|
|
||||||
file_id_t file_id_for_fd(int fd);
|
file_id_t file_id_for_fd(int fd);
|
||||||
file_id_t file_id_for_path(const wcstring &path);
|
file_id_t file_id_for_path(const wcstring &path);
|
||||||
|
file_id_t file_id_for_path(const std::string &path);
|
||||||
|
|
||||||
extern const file_id_t kInvalidFileID;
|
extern const file_id_t kInvalidFileID;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue