caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* How to find out free diskspace?
@ 2005-07-01 16:28 Bernd Kuhls
  2005-07-01 18:06 ` [Caml-list] " Sylvain LE GALL
  2005-07-01 18:19 ` Richard Jones
  0 siblings, 2 replies; 10+ messages in thread
From: Bernd Kuhls @ 2005-07-01 16:28 UTC (permalink / raw)
  To: caml-list

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.

Regards, Bernd


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] How to find out free diskspace?
  2005-07-01 16:28 How to find out free diskspace? Bernd Kuhls
@ 2005-07-01 18:06 ` Sylvain LE GALL
  2005-07-01 18:19 ` Richard Jones
  1 sibling, 0 replies; 10+ messages in thread
From: Sylvain LE GALL @ 2005-07-01 18:06 UTC (permalink / raw)
  To: Bernd Kuhls; +Cc: caml-list

Hello,

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.
> 
> Regards, Bernd
> 

Well, i think you can get this information using the C call "statfs"
combined with the reading of /etc/mtab, on linux.

For windows i remember that you can open some special files at the root
of each volume, which should give you information about this...

If you find a solution, could you drop me an email, i would like to
integrate it in one of my library.

Thank you
Regard
Sylvain Le Gall


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] How to find out free diskspace?
  2005-07-01 16:28 How to find out free diskspace? Bernd Kuhls
  2005-07-01 18:06 ` [Caml-list] " Sylvain LE GALL
@ 2005-07-01 18:19 ` Richard Jones
  2005-07-03 11:54   ` Damien Doligez
  2005-07-21 15:52   ` Bernd Kuhls
  1 sibling, 2 replies; 10+ messages in thread
From: Richard Jones @ 2005-07-01 18:19 UTC (permalink / raw)
  To: Bernd Kuhls; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] How to find out free diskspace?
  2005-07-01 18:19 ` Richard Jones
@ 2005-07-03 11:54   ` Damien Doligez
  2005-07-03 12:35     ` Richard Jones
  2005-07-21 15:52   ` Bernd Kuhls
  1 sibling, 1 reply; 10+ messages in thread
From: Damien Doligez @ 2005-07-03 11:54 UTC (permalink / raw)
  To: caml users

On Jul 1, 2005, at 20:19, Richard Jones wrote:

> 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));
>   [...]
>

There's a nasty bug lurking in this code.  Depending on your
C compiler, you might be computing &Field (bufv, 0) before
the call to copy_int64, which can trigger a GC and change the
value of bufv, hence invalidating the address you've just
computed.

You should do it this way:

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

-- Damien


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] How to find out free diskspace?
  2005-07-03 11:54   ` Damien Doligez
@ 2005-07-03 12:35     ` Richard Jones
  0 siblings, 0 replies; 10+ messages in thread
From: Richard Jones @ 2005-07-03 12:35 UTC (permalink / raw)
  To: Damien Doligez; +Cc: caml users

On Sun, Jul 03, 2005 at 01:54:15PM +0200, Damien Doligez wrote:
> You should do it this way:
> 
> static value
> copy_statfs (struct statfs *buf)
> {
>   CAMLparam0 ();
>   CAMLlocal1 (bufv, v);
>   bufv = caml_alloc (9, 0);
>   v = copy_int64 (buf->f_type); caml_modify (&Field (bufv, 0), v);

Thanks for this tip.  I'm going to go away and fix OCamlODE now, which
does a lot of this.

Rich.

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to find out free diskspace?
  2005-07-01 18:19 ` Richard Jones
  2005-07-03 11:54   ` Damien Doligez
@ 2005-07-21 15:52   ` Bernd Kuhls
  2005-07-21 16:18     ` [Caml-list] " Stephane Glondu
  1 sibling, 1 reply; 10+ messages in thread
From: Bernd Kuhls @ 2005-07-21 15:52 UTC (permalink / raw)
  To: caml-list

Richard Jones wrote:
> ---------------------------------------------------------------- statfs_c.c

Hi Richard,

thanks for you code. It is working nicely here. I made some adjustments
to it so it works now on Linux, Solaris/Sparc, FreeBSD and OpenBSD.

The only thing missing is how to return "-1" values to Ocaml when the 
stats functions are not available on that machine?

#ifdef HAVE_SYS_PARAM_H
#  include <sys/param.h>
#  if (defined(__FreeBSD__) && __FreeBSD_version >= 503001) || 
defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#    include <sys/mount.h>
#    define HAVE_STATS 1
#  endif
#endif
#ifdef HAVE_SYS_VFS_H
#  include <sys/vfs.h>
#  define HAVE_STATS 1
#endif

#ifdef HAVE_STATS
static value
#if ((defined (sun) || defined (__sun__)))
copy_statfs (struct statvfs *buf)
#else
copy_statfs (struct statfs *buf)
#endif

{
+  CAMLparam0 ();
+  CAMLlocal2 (bufv, v);
+  bufv = caml_alloc (11, 0);
... (lots of your code)
#endif

The code for "ifndef HAVE_STATS" is still lacking, how should it look like?

When this is finished and working I will post the complete code here.

Greetings, Bernd


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: How to find out free diskspace?
  2005-07-21 15:52   ` Bernd Kuhls
@ 2005-07-21 16:18     ` Stephane Glondu
  2005-07-22 12:46       ` Bernd Kuhls
  2005-07-22 17:40       ` Bernd Kuhls
  0 siblings, 2 replies; 10+ messages in thread
From: Stephane Glondu @ 2005-07-21 16:18 UTC (permalink / raw)
  To: caml-list

On Thursday 21 July 2005 08:52, Bernd Kuhls wrote:
> The code for "ifndef HAVE_STATS" is still lacking, how should it look
> like?

I would say raise an exception.

Stephane Glondu.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to find out free diskspace?
  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
  1 sibling, 1 reply; 10+ messages in thread
From: Bernd Kuhls @ 2005-07-22 12:46 UTC (permalink / raw)
  To: caml-list

Stephane Glondu wrote:
> On Thursday 21 July 2005 08:52, Bernd Kuhls wrote:
> 
>>The code for "ifndef HAVE_STATS" is still lacking, how should it look
>>like?
> 
> 
> I would say raise an exception.

Hi,

this would be necessary for runtime, but my question was about what to 
code for compile time when the headers files necessary can't be found?

Greetings, Bernd


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: How to find out free diskspace?
  2005-07-22 12:46       ` Bernd Kuhls
@ 2005-07-22 14:10         ` Eric Cooper
  0 siblings, 0 replies; 10+ messages in thread
From: Eric Cooper @ 2005-07-22 14:10 UTC (permalink / raw)
  To: caml-list

On Fri, Jul 22, 2005 at 02:46:23PM +0200, Bernd Kuhls wrote:
> Stephane Glondu wrote:
> >On Thursday 21 July 2005 08:52, Bernd Kuhls wrote:
> >
> >>The code for "ifndef HAVE_STATS" is still lacking, how should it look
> >>like?
> >
> >
> >I would say raise an exception.
> 
> Hi,
> 
> this would be necessary for runtime, but my question was about what to 
> code for compile time when the headers files necessary can't be found?

Do you want it to fail at compile time or run time?  For the former,
use the #error "..." directive.  For the latter, define a function
that always throws an exception.

-- 
Eric Cooper             e c c @ c m u . e d u


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to find out free diskspace?
  2005-07-21 16:18     ` [Caml-list] " Stephane Glondu
  2005-07-22 12:46       ` Bernd Kuhls
@ 2005-07-22 17:40       ` Bernd Kuhls
  1 sibling, 0 replies; 10+ messages in thread
From: Bernd Kuhls @ 2005-07-22 17:40 UTC (permalink / raw)
  To: caml-list

Stephane Glondu wrote:
> On Thursday 21 July 2005 08:52, Bernd Kuhls wrote:
> 
>>The code for "ifndef HAVE_STATS" is still lacking, how should it look
>>like?
> 
> 
> I would say raise an exception.

Hi,

OK, got it working here. Needs some further testing but looks good already.

Greetings, Bernd


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-07-22 17:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-01 16:28 How to find out free diskspace? Bernd Kuhls
2005-07-01 18:06 ` [Caml-list] " Sylvain LE GALL
2005-07-01 18:19 ` Richard Jones
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

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).