Version française
Home     About     Download     Resources     Contact us    
Browse thread
How to find out free diskspace?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] How to find out free diskspace?
On Fri, Jul 01, 2005 at 06:28:21PM +0200, Bernd Kuhls wrote:
> Hi,
> 
> I am looking for some Ocaml code (or C bindings which work on 
> Linux/Solaris/Cygwin) to get the amount of free diskspace on a volume. 
> The function should receive a string and return an int64 value or 
> something similar.

The attached files have only been very lightly tested, but they appear
to work.  You can work out the amount of free disk space by
multiplying the f_bfree and f_bsize fields.

Note the functions as they stand assume that Unix.file_descr = int and
don't throw a useful Unix error if the underlying call fails.

Rich.

---------------------------------------------------------------- statfs.ml
type statfs = {
  f_type : int64;
  f_bsize : int64;
  f_blocks : int64;
  f_bfree : int64;
  f_bavail : int64;
  f_files : int64;
  f_ffree : int64;
  f_fsid : unit;  (* See note in statfs(2) *)
  f_fnamelen : int64;
}

external statfs : string -> statfs = "statfs_statfs"
external fstatfs : Unix.file_descr -> statfs = "statfs_fstatfs"

---------------------------------------------------------------- statfs_c.c
#include <errno.h>
#include <string.h>
#include <sys/vfs.h>

#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>

static value
copy_statfs (struct statfs *buf)
{
  CAMLparam0 ();
  CAMLlocal1 (bufv);
  bufv = caml_alloc (9, 0);
  caml_modify (&Field (bufv, 0), copy_int64 (buf->f_type));
  caml_modify (&Field (bufv, 1), copy_int64 (buf->f_bsize));
  caml_modify (&Field (bufv, 2), copy_int64 (buf->f_blocks));
  caml_modify (&Field (bufv, 3), copy_int64 (buf->f_bfree));
  caml_modify (&Field (bufv, 4), copy_int64 (buf->f_bavail));
  caml_modify (&Field (bufv, 5), copy_int64 (buf->f_files));
  caml_modify (&Field (bufv, 6), copy_int64 (buf->f_ffree));
  caml_modify (&Field (bufv, 7), Val_unit);
  caml_modify (&Field (bufv, 8), copy_int64 (buf->f_namelen));
  CAMLreturn (bufv);
}

CAMLprim value
statfs_statfs (value pathv)
{
  CAMLparam1 (pathv);
  CAMLlocal1 (bufv);
  const char *path = String_val (pathv);
  struct statfs buf;
  if (statfs (path, &buf) == -1)
    caml_failwith (strerror (errno));
  bufv = copy_statfs (&buf);
  CAMLreturn (bufv);
}

CAMLprim value
statfs_fstatfs (value fdv)
{
  CAMLparam1 (fdv);
  CAMLlocal1 (bufv);
  int fd = Int_val (fdv);
  struct statfs buf;
  if (fstatfs (fd, &buf) == -1)
    caml_failwith (strerror (errno));
  bufv = copy_statfs (&buf);
  CAMLreturn (bufv);
}


-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com