PR#6285: add support for nanosecond precision in Unix.stat()

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@15877 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
master
Jérémie Dimino 2015-03-05 11:02:53 +00:00
parent 3d89f3ef7e
commit 3a903acafb
4 changed files with 67 additions and 3 deletions

View File

@ -0,0 +1,28 @@
/***********************************************************************/
/* */
/* OCaml */
/* */
/* Jeremie Dimino, Jane Street Capital */
/* */
/* Copyright 2015 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../../LICENSE. */
/* */
/***********************************************************************/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "../../otherlibs/unix/nanosecond_stat.h"
int main() {
struct stat *buf;
double a, m, c;
a = (double)NSEC(buf, a);
m = (double)NSEC(buf, m);
c = (double)NSEC(buf, c);
return 0;
}

9
configure vendored
View File

@ -1291,6 +1291,15 @@ if sh ./hasgot pwrite; then
echo "#define HAS_PWRITE" >> s.h
fi
nanosecond_stat=none
for i in 1 2 3; do
if sh ./trycompile -DHAS_NANOSECOND_STAT=$i nanosecond_stat.c; then nanosecond_stat=$i; break; fi
done
if test $nanosecond_stat != "none"; then
inf "stat() supports nanosecond precision."
echo "#define HAS_NANOSECOND_STAT $nanosecond_stat" >> s.h
fi
nargs=none
for i in 5 6; do
if sh ./trycompile -DNUM_ARGS=${i} gethostbyname.c; then nargs=$i; break; fi

View File

@ -0,0 +1,25 @@
/***********************************************************************/
/* */
/* OCaml */
/* */
/* Jeremie Dimino, Jane Street Capital */
/* */
/* Copyright 2015 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../../LICENSE. */
/* */
/***********************************************************************/
/* This file is used by the configure test program nanosecond_stat.c
and stat.c in this directory */
#if HAS_NANOSECOND_STAT == 1
# define NSEC(buf, field) buf->st_##field##tim.tv_nsec
#elif HAS_NANOSECOND_STAT == 2
# define NSEC(buf, field) buf->st_##field##timespec.tv_nsec
#elif HAS_NANOSECOND_STAT == 3
# define NSEC(buf, field) buf->st_##field##timensec
#else
# define NSEC(buf, field) 0
#endif

View File

@ -48,9 +48,11 @@ static value stat_aux(int use_64, struct stat *buf)
CAMLparam0();
CAMLlocal5(atime, mtime, ctime, offset, v);
atime = copy_double((double) buf->st_atime);
mtime = copy_double((double) buf->st_mtime);
ctime = copy_double((double) buf->st_ctime);
#include "nanosecond_stat.h"
atime = caml_copy_double((double) buf->st_atime + (NSEC(buf, a) / 1000000000.0));
mtime = caml_copy_double((double) buf->st_mtime + (NSEC(buf, m) / 1000000000.0));
ctime = caml_copy_double((double) buf->st_ctime + (NSEC(buf, c) / 1000000000.0));
#undef NSEC
offset = use_64 ? Val_file_offset(buf->st_size) : Val_int (buf->st_size);
v = alloc_small(12, 0);
Field (v, 0) = Val_int (buf->st_dev);