Improve docs of more system calls

This change also found a few POSIX compliance bugs with errnos. Another
bug was discovered where, on Windows, pread() and pwrite() could modify
the file position in cases where ReadFile() returned an error e.g. when
seeking past the end of file. We also have more tests!
This commit is contained in:
Justine Tunney 2022-10-02 22:14:33 -07:00
parent af24c19db3
commit ccbae7799e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
39 changed files with 589 additions and 175 deletions

View file

@ -16,34 +16,59 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/asan.internal.h"
#include "libc/calls/struct/itimerval.internal.h"
#include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/timeval.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/errfuns.h"
#include "libc/zipos/zipos.internal.h"
/**
* Changes last accessed/modified times on file.
* Changes last accessed/modified timestamps on file.
*
* @param times is access/modified and NULL means now
* @return 0 on success or -1 w/ errno
* @param tv is access/modified timestamps, or NULL which means now
* @return 0 on success, or -1 w/ errno
* @raise ENOTSUP if `path` is a zip filesystem path
* @raise EROFS if `path` is on a read-only filesystem
* @raise EFAULT if `path` or `tv` points to invalid memory
* @raise EPERM if pledge() is in play without fattr promise
* @raise ENOENT if `path` doesn't exist or is an empty string
* @raise EACCES if unveil() is in play and `path` isn't unveiled
* @raise ELOOP if a loop was detected resolving components of `path`
* @raise ENAMETOOLONG if symlink-resolved `path` length exceeds `PATH_MAX`
* @raise ENAMETOOLONG if component in `path` exists longer than `NAME_MAX`
* @raise EINVAL if `tv` specifies a microseconds value that's out of range
* @raise EACCES if we don't have permission to search a component of `path`
* @raise ENOTDIR if a directory component in `path` exists as non-directory
* @raise ENOSYS on bare metal
* @asyncsignalsafe
* @see stat()
*/
int utimes(const char *path, const struct timeval tv[2]) {
if (IsAsan()) {
if (tv && !__asan_is_valid(tv, sizeof(*tv) * 2)) {
return efault();
}
}
if (!IsWindows()) {
/*
* we don't modernize utimes() into utimensat() because the
* latter is poorly supported and utimes() works everywhere
*/
return sys_utimes(path, tv);
int rc;
struct ZiposUri zipname;
if (IsMetal()) {
rc = enosys();
} else if (IsAsan() && tv &&
(!__asan_is_valid_timeval(tv + 0) ||
!__asan_is_valid_timeval(tv + 1))) {
rc = efault();
} else if (_weaken(__zipos_parseuri) &&
_weaken(__zipos_parseuri)(path, &zipname) != -1) {
rc = enotsup();
} else if (!IsWindows()) {
// we don't modernize utimes() into utimensat() because the
// latter is poorly supported and utimes() works everywhere
rc = sys_utimes(path, tv);
} else {
return sys_utimes_nt(path, tv);
rc = sys_utimes_nt(path, tv);
}
STRACE("utimes(%#s, {%s, %s}) → %d% m", path, DescribeTimeval(0, tv),
DescribeTimeval(0, tv ? tv + 1 : 0), rc);
return rc;
}