options/ansi: Implemented utimes and remove

options/posix: Implemented futimens, utimensat and utime
sysdeps/managarm: Implemented utimensat

Signed-off-by: Dennisbonke <admin@dennisbonke.com>
This commit is contained in:
Dennisbonke 2020-06-04 12:20:03 +02:00
parent 3f9007b3bc
commit 2e8865ffb1
9 changed files with 162 additions and 15 deletions

View file

@ -7,6 +7,8 @@
#include <stdlib.h>
#include <wchar.h>
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include <bits/ensure.h>
@ -185,9 +187,15 @@ struct ResizePrinter {
};
int remove(const char *filename) {
__ensure(!"Not implemented");
__builtin_unreachable();
struct stat statbuf;
if(stat(filename, &statbuf) != 0)
return -1;
if(S_ISDIR(statbuf.st_mode))
return rmdir(filename);
else
return unlink(filename);
}
int rename(const char *path, const char *new_path) {
if(!mlibc::sys_rename) {
MLIBC_MISSING_SYSDEP();
@ -200,6 +208,7 @@ int rename(const char *path, const char *new_path) {
}
return 0;
}
int renameat(int olddirfd, const char *old_path, int newdirfd, const char *new_path) {
if(!mlibc::sys_renameat) {
MLIBC_MISSING_SYSDEP();

View file

@ -334,9 +334,31 @@ int clock_settime(clockid_t, const struct timespec *) {
__builtin_unreachable();
}
int utimes(const char *, const struct timeval[2]) {
__ensure(!"Not implemented");
__builtin_unreachable();
int utimes(const char *filename, const struct timeval times[2]) {
if (!mlibc::sys_utimensat) {
MLIBC_MISSING_SYSDEP();
errno = ENOSYS;
return -1;
}
struct timespec time[2];
if(times == nullptr) {
time[0].tv_sec = UTIME_NOW;
time[0].tv_nsec = UTIME_NOW;
time[1].tv_sec = UTIME_NOW;
time[1].tv_nsec = UTIME_NOW;
} else {
time[0].tv_sec = times[0].tv_sec;
time[0].tv_nsec = times[0].tv_usec * 1000;
time[1].tv_sec = times[1].tv_sec;
time[1].tv_nsec = times[1].tv_usec * 1000;
}
if (int e = mlibc::sys_utimensat(AT_FDCWD, filename, time, 0); e) {
errno = e;
return -1;
}
return 0;
}
time_t time(time_t *out) {

View file

@ -15,7 +15,7 @@ extern "C" {
int ioctl(int fd, unsigned long request, ...);
#define FIONREAD 0x541B
#define FIONBIO 0x5421
#define FIONBIO 0x5421
#ifdef __cplusplus
}

View file

@ -133,6 +133,7 @@ int sys_close(int fd);
[[gnu::weak]] int sys_chmod(const char *pathname, mode_t mode);
[[gnu::weak]] int sys_fchmod(int fd, mode_t mode);
[[gnu::weak]] int sys_fchmodat(int fd, const char *pathname, mode_t mode, int flags);
[[gnu::weak]] int sys_utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags);
#endif // !defined(MLIBC_BUILDING_RTDL)
// mlibc assumes that anonymous memory returned by sys_vm_map() is zeroed by the kernel / whatever is behind the sysdeps

View file

@ -59,10 +59,20 @@ int fstatat(int dirfd, const char *path, struct stat *result, int flags) {
}
int futimens(int fd, const struct timespec times[2]) {
mlibc::infoLogger() << "\e[31mmlibc: futimens() is not implemented correctly\e[39m"
<< frg::endlog;
if (!mlibc::sys_utimensat) {
MLIBC_MISSING_SYSDEP();
errno = ENOSYS;
return -1;
}
if (int e = mlibc::sys_utimensat(fd, nullptr, times, 0); e) {
errno = e;
return -1;
}
return 0;
}
int mkdir(const char *path, mode_t) {
mlibc::infoLogger() << "\e[31mmlibc: mkdir() ignores its mode\e[39m" << frg::endlog;
if(!mlibc::sys_mkdir) {
@ -76,6 +86,7 @@ int mkdir(const char *path, mode_t) {
}
return 0;
}
int mkdirat(int dirfd, const char *path, mode_t mode) {
mlibc::infoLogger() << "\e[31mmlibc: mkdirat() ignores its mode\e[39m" << frg::endlog;
if(!mlibc::sys_mkdirat) {
@ -122,13 +133,26 @@ mode_t umask(mode_t) {
<< frg::endlog;
return 0;
}
int utimensat(int, const char *, const struct timespec times[2], int) {
mlibc::infoLogger() << "\e[31mmlibc: utimensat() is not implemented correctly\e[39m"
<< frg::endlog;
int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) {
if(pathname == nullptr) {
errno = EINVAL;
return -1;
}
if (!mlibc::sys_utimensat) {
MLIBC_MISSING_SYSDEP();
errno = ENOSYS;
return -1;
}
if (int e = mlibc::sys_utimensat(dirfd, pathname, times, flags); e) {
errno = e;
return -1;
}
return 0;
}
int stat(const char *path, struct stat *result) {
if(!mlibc::sys_stat) {
MLIBC_MISSING_SYSDEP();

View file

@ -1,10 +1,35 @@
#include <utime.h>
#include <fcntl.h>
#include <errno.h>
#include <bits/ensure.h>
#include <mlibc/sysdeps.hpp>
int utime(const char *, const struct utimbuf *times) {
__ensure(!"Not implemented");
__builtin_unreachable();
int utime(const char *filename, const struct utimbuf *times) {
if (!mlibc::sys_utimensat) {
MLIBC_MISSING_SYSDEP();
errno = ENOSYS;
return -1;
}
struct timespec time[2];
if(times) {
time[0].tv_sec = times->actime;
time[0].tv_nsec = 0;
time[1].tv_sec = times->modtime;
time[1].tv_nsec = 0;
} else {
time[0].tv_sec = UTIME_NOW;
time[0].tv_nsec = UTIME_NOW;
time[1].tv_sec = UTIME_NOW;
time[1].tv_nsec = UTIME_NOW;
}
if (int e = mlibc::sys_utimensat(AT_FDCWD, filename, time, 0); e) {
errno = e;
return -1;
}
return 0;
}

View file

@ -3,6 +3,10 @@
#include <abi-bits/stat.h>
// Used by utimensat and friends
#define UTIME_NOW ((1l << 30) - 1l)
#define UTIME_OMIT ((1l << 30) - 2l)
#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)

View file

@ -11,6 +11,11 @@
extern "C" {
#endif
uint32_t htonl(uint32_t);
uint16_t htons(uint16_t);
uint32_t ntohl(uint32_t);
uint16_t ntohs(uint16_t);
#define IN6_IS_ADDR_UNSPECIFIED(a) ({ \
uint32_t *_a = (uint32_t *)((a)->s6_addr); \
!_a[0] && \

View file

@ -4261,5 +4261,62 @@ int sys_fchmodat(int fd, const char *pathname, mode_t mode, int flags) {
}
}
int sys_utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) {
SignalGuard sguard;
HelAction actions[3];
globalQueue.trim();
managarm::posix::CntRequest<MemoryAllocator> req(getSysdepsAllocator());
req.set_request_type(managarm::posix::CntReqType::UTIMENSAT);
req.set_fd(dirfd);
if(pathname != nullptr)
req.set_path(frg::string<MemoryAllocator>(getSysdepsAllocator(), pathname));
if(times) {
req.set_tv_sec(times->tv_sec);
req.set_tv_nsec(times->tv_nsec);
} else {
req.set_tv_sec(UTIME_NOW);
req.set_tv_nsec(UTIME_NOW);
}
req.set_flags(flags);
frg::string<MemoryAllocator> ser(getSysdepsAllocator());
req.SerializeToString(&ser);
actions[0].type = kHelActionOffer;
actions[0].flags = kHelItemAncillary;
actions[1].type = kHelActionSendFromBuffer;
actions[1].flags = kHelItemChain;
actions[1].buffer = ser.data();
actions[1].length = ser.size();
actions[2].type = kHelActionRecvInline;
actions[2].flags = 0;
HEL_CHECK(helSubmitAsync(getPosixLane(), actions, 3,
globalQueue.getQueue(), 0, 0));
auto element = globalQueue.dequeueSingle();
auto offer = parseSimple(element);
auto send_req = parseSimple(element);
auto recv_resp = parseInline(element);
HEL_CHECK(offer->error);
HEL_CHECK(send_req->error);
HEL_CHECK(recv_resp->error);
managarm::posix::SvrResponse<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp->data, recv_resp->length);
if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) {
return ENOENT;
}else if(resp.error() == managarm::posix::Errors::NO_SUCH_FD) {
return EBADF;
}else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) {
return EINVAL;
}else if(resp.error() == managarm::posix::Errors::NOT_SUPPORTED) {
return ENOTSUP;
}else{
__ensure(resp.error() == managarm::posix::Errors::SUCCESS);
return 0;
}
}
} //namespace mlibc