Fix futimes() regression

Fixes #647
This commit is contained in:
Justine Tunney 2022-10-05 19:25:07 -07:00
parent f155205eb0
commit 81ee11a16e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
22 changed files with 200 additions and 138 deletions

View file

@ -16,57 +16,34 @@
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/timespec.h"
#include "libc/calls/struct/timespec.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 timestamps on file.
*
* @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
* @note truncates to second precision on rhel5
* @see utimensat() for modern version
* @asyncsignalsafe
* @see stat()
* @threadsafe
*/
int utimes(const char *path, const struct timeval tv[2]) {
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);
struct timespec ts[2];
if (tv) {
ts[0].tv_sec = tv[0].tv_sec;
ts[0].tv_nsec = tv[0].tv_usec * 1000;
ts[1].tv_sec = tv[1].tv_sec;
ts[1].tv_nsec = tv[1].tv_usec * 1000;
rc = __utimens(AT_FDCWD, path, ts, 0);
} else {
rc = sys_utimes_nt(path, tv);
rc = __utimens(AT_FDCWD, path, 0, 0);
}
STRACE("utimes(%#s, {%s, %s}) → %d% m", path, DescribeTimeval(0, tv),
DescribeTimeval(0, tv ? tv + 1 : 0), rc);