Skip to content

Commit

Permalink
Merge 13858c2 into 7ad5408
Browse files Browse the repository at this point in the history
  • Loading branch information
xenu committed May 25, 2021
2 parents 7ad5408 + 13858c2 commit cd70bed
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/Time-HiRes/HiRes.pm
Expand Up @@ -50,7 +50,7 @@ our @EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval
stat lstat utime
);

our $VERSION = '1.9767';
our $VERSION = '1.9768';
our $XS_VERSION = $VERSION;
$VERSION = eval $VERSION;

Expand Down
74 changes: 74 additions & 0 deletions dist/Time-HiRes/HiRes.xs
Expand Up @@ -186,11 +186,21 @@ START_MY_CXT
# undef clock_getres
# define clock_getres(clock_id, tp) _clock_getres(clock_id, tp)

# undef futimens
# define futimens(fd, times) _futimens(aTHX_ fd, times)

# undef utimensat
# define utimensat(dirfd, path, times, flags) _utimensat(aTHX_ dirfd, path, times, flags)

# ifndef CLOCK_REALTIME
# define CLOCK_REALTIME 1
# define CLOCK_MONOTONIC 2
# endif

# ifndef AT_FDCWD
# define AT_FDCWD -100
# endif

/* If the performance counter delta drifts more than 0.5 seconds from the
* system time then we recalibrate to the system time. This means we may
* move *backwards* in time! */
Expand Down Expand Up @@ -320,6 +330,70 @@ _clock_getres(clockid_t clock_id, struct timespec *tp)
return 0;
}

static int
_futimens(pTHX_ int fd, const struct timespec times[2])
{
size_t i;
HANDLE h;
FT_t ft_times[2];

h = (HANDLE)_get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
errno = EBADF;
return -1;
}

for (i = 0; i < 2; ++i) {
if (!times)
GetSystemTimePreciseAsFileTime(&ft_times[i].ft_val);
else {
if (times[i].tv_sec < 0 || times[i].tv_nsec < 0 ||
times[i].tv_nsec >= IV_1E9)
{
errno = EINVAL;
return -1;
}

ft_times[i].ft_i64 = EPOCH_BIAS +
((__int64)times[i].tv_sec * IV_1E7) + (times[i].tv_nsec / 100);
}
}

if(!SetFileTime(h, NULL, &ft_times[0].ft_val, &ft_times[1].ft_val)) {
if (GetLastError() == ERROR_ACCESS_DENIED)
errno = EACCES;
else
errno = EINVAL;

return -1;
}

return 0;
}

static int
_utimensat(pTHX_ int dirfd, const char *path, const struct timespec times[2],
int flags)
{
int fd, ret;

/* Time::HiRes doesn't need those things, so we didn't implement them */
if (dirfd != AT_FDCWD || flags) {
errno = EINVAL;
return -1;
}

fd = _open(path, _O_WRONLY);
if (fd == -1)
return -1;

ret = futimens(fd, times);

_close(fd);

return ret;
}

#endif /* #if defined(WIN32) || defined(CYGWIN_WITH_W32API) */

/* Do not use H A S _ N A N O S L E E P
Expand Down
3 changes: 3 additions & 0 deletions dist/Time-HiRes/Makefile.PL
Expand Up @@ -1023,6 +1023,9 @@ sub main {
# we provide our own implementations of those functions on win32
DEFINE('TIME_HIRES_CLOCK_GETTIME');
DEFINE('TIME_HIRES_CLOCK_GETRES');
DEFINE('TIME_HIRES_UTIME');
DEFINE('HAS_FUTIMENS');
DEFINE('HAS_UTIMENSAT');
$LIBS = [];
print "System is $^O, skipping full configure...\n";
open(XDEFINE, '>', 'xdefine') or die "$0: Cannot create xdefine: $!\n";
Expand Down

0 comments on commit cd70bed

Please sign in to comment.