caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: Bernd Kuhls <bernd.kuhls@informatik.uni-oldenburg.de>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] How to find out free diskspace?
Date: Fri, 1 Jul 2005 19:19:50 +0100	[thread overview]
Message-ID: <20050701181950.GA2557@furbychan.cocan.org> (raw)
In-Reply-To: <da3qe7$23i$1@sea.gmane.org>

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


  parent reply	other threads:[~2005-07-01 18:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-01 16:28 Bernd Kuhls
2005-07-01 18:06 ` [Caml-list] " Sylvain LE GALL
2005-07-01 18:19 ` Richard Jones [this message]
2005-07-03 11:54   ` Damien Doligez
2005-07-03 12:35     ` Richard Jones
2005-07-21 15:52   ` Bernd Kuhls
2005-07-21 16:18     ` [Caml-list] " Stephane Glondu
2005-07-22 12:46       ` Bernd Kuhls
2005-07-22 14:10         ` [Caml-list] " Eric Cooper
2005-07-22 17:40       ` Bernd Kuhls

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050701181950.GA2557@furbychan.cocan.org \
    --to=rich@annexia.org \
    --cc=bernd.kuhls@informatik.uni-oldenburg.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).