1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/signals.h>
#include <caml/io.h>
#include "caml/unixsupport.h"
#include "cst2constr.h"
#ifndef S_IFLNK
#define S_IFLNK 0
#endif
#ifndef S_IFIFO
#define S_IFIFO 0
#endif
#ifndef S_IFSOCK
#define S_IFSOCK 0
#endif
#ifndef S_IFBLK
#define S_IFBLK 0
#endif
#ifndef EOVERFLOW
#define EOVERFLOW ERANGE
#endif
static const int file_kind_table[] = {
S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFLNK, S_IFIFO, S_IFSOCK
};
/* Transform a (seconds, nanoseconds) time stamp (in the style of
struct timespec) to a number of seconds in floating-point.
Make sure the integer part of the result is always equal to [seconds]
(issue #9490). */
static double stat_timestamp(time_t sec, long nsec)
{
/* The conversion of sec to FP is exact for the foreseeable future.
(It starts rounding when sec > 2^53, i.e. in 285 million years.) */
double s = (double) sec;
/* The conversion of nsec to fraction of seconds can round.
Still, we have 0 <= n < 1.0. */
double n = (double) nsec / 1e9;
/* The sum s + n can round up, hence s <= t + <= s + 1.0 */
double t = s + n;
/* Detect the "round up to s + 1" case and decrease t so that
its integer part is s. */
if (t == s + 1.0) t = nextafter(t, s);
return t;
}
static value stat_aux(int use_64, struct stat *buf)
{
CAMLparam0();
CAMLlocal5(atime, mtime, ctime, offset, v);
#if defined(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
# define NSEC(buf, field) buf->st_##field##tim.tv_nsec
#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
# define NSEC(buf, field) buf->st_##field##timespec.tv_nsec
#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
# define NSEC(buf, field) buf->st_##field##timensec
#else
# define NSEC(buf, field) 0
#endif
atime = caml_copy_double(stat_timestamp(buf->st_atime, NSEC(buf, a)));
mtime = caml_copy_double(stat_timestamp(buf->st_mtime, NSEC(buf, m)));
ctime = caml_copy_double(stat_timestamp(buf->st_ctime, NSEC(buf, c)));
#undef NSEC
offset = use_64 ? Val_file_offset(buf->st_size) : Val_int (buf->st_size);
v = caml_alloc_small(12, 0);
Field (v, 0) = Val_int (buf->st_dev);
Field (v, 1) = Val_int (buf->st_ino);
Field (v, 2) = caml_unix_cst_to_constr(buf->st_mode & S_IFMT, file_kind_table,
sizeof(file_kind_table) / sizeof(int), 0);
Field (v, 3) = Val_int (buf->st_mode & 07777);
Field (v, 4) = Val_int (buf->st_nlink);
Field (v, 5) = Val_int (buf->st_uid);
Field (v, 6) = Val_int (buf->st_gid);
Field (v, 7) = Val_int (buf->st_rdev);
Field (v, 8) = offset;
Field (v, 9) = atime;
Field (v, 10) = mtime;
Field (v, 11) = ctime;
CAMLreturn(v);
}
CAMLprim value caml_unix_stat(value path)
{
CAMLparam1(path);
int ret;
struct stat buf;
char * p;
caml_unix_check_path(path, "stat");
p = caml_stat_strdup(String_val(path));
caml_enter_blocking_section();
ret = stat(p, &buf);
caml_leave_blocking_section();
caml_stat_free(p);
if (ret == -1) caml_uerror("stat", path);
if (buf.st_size > Max_long && (buf.st_mode & S_IFMT) == S_IFREG)
caml_unix_error(EOVERFLOW, "stat", path);
CAMLreturn(stat_aux(0, &buf));
}
CAMLprim value caml_unix_lstat(value path)
{
CAMLparam1(path);
int ret;
struct stat buf;
char * p;
caml_unix_check_path(path, "lstat");
p = caml_stat_strdup(String_val(path));
caml_enter_blocking_section();
#ifdef HAS_SYMLINK
ret = lstat(p, &buf);
#else
ret = stat(p, &buf);
#endif
caml_leave_blocking_section();
caml_stat_free(p);
if (ret == -1) caml_uerror("lstat", path);
if (buf.st_size > Max_long && (buf.st_mode & S_IFMT) == S_IFREG)
caml_unix_error(EOVERFLOW, "lstat", path);
CAMLreturn(stat_aux(0, &buf));
}
CAMLprim value caml_unix_fstat(value fd)
{
int ret;
struct stat buf;
caml_enter_blocking_section();
ret = fstat(Int_val(fd), &buf);
caml_leave_blocking_section();
if (ret == -1) caml_uerror("fstat", Nothing);
if (buf.st_size > Max_long && (buf.st_mode & S_IFMT) == S_IFREG)
caml_unix_error(EOVERFLOW, "fstat", Nothing);
return stat_aux(0, &buf);
}
CAMLprim value caml_unix_stat_64(value path)
{
CAMLparam1(path);
int ret;
struct stat buf;
char * p;
caml_unix_check_path(path, "stat");
p = caml_stat_strdup(String_val(path));
caml_enter_blocking_section();
ret = stat(p, &buf);
caml_leave_blocking_section();
caml_stat_free(p);
if (ret == -1) caml_uerror("stat", path);
CAMLreturn(stat_aux(1, &buf));
}
CAMLprim value caml_unix_lstat_64(value path)
{
CAMLparam1(path);
int ret;
struct stat buf;
char * p;
caml_unix_check_path(path, "lstat");
p = caml_stat_strdup(String_val(path));
caml_enter_blocking_section();
#ifdef HAS_SYMLINK
ret = lstat(p, &buf);
#else
ret = stat(p, &buf);
#endif
caml_leave_blocking_section();
caml_stat_free(p);
if (ret == -1) caml_uerror("lstat", path);
CAMLreturn(stat_aux(1, &buf));
}
CAMLprim value caml_unix_fstat_64(value fd)
{
int ret;
struct stat buf;
caml_enter_blocking_section();
ret = fstat(Int_val(fd), &buf);
caml_leave_blocking_section();
if (ret == -1) caml_uerror("fstat", Nothing);
return stat_aux(1, &buf);
}
|