chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Getting disk space


From: Ozzi
Subject: Re: [Chicken-users] Getting disk space
Date: Tue, 18 Dec 2007 15:33:04 -0600
User-agent: Thunderbird 2.0.0.9 (Macintosh/20071031)

> Hi,
>
> Can anyone tell me how I can get available disk space from a chicken
> program.   I want the same results you get with df in linux.   If the
> answer is using process or open-input-pipe.etc. and actually running df
> please explain how this is done since I cannot get it to work no matter
> what I do.
> If there is a way of duplicating df in code - even in c - I would
> appreciate that as well.
>
> Bill

Below is the code I am using. Should work on at least Debian and OS X. It's my first adventure in FFI-land.

The df command basically wraps statfs. (statfs) will give you some of the same info df does in an alist. You'll have to do some math with the blocksize to get the size in useful units. Here are a couple functions I use:


(define (blocks-to-GiB blocksize blocks)
  (/ (round (* blocks (/ blocksize (expt 1024 3)) 10)) 10))

(define (blocks-to-GB blocksize blocks)
  (/ (round (* blocks (/ blocksize (expt 1000 3)) 10)) 10))


I'd love to see this wrapped up in an egg at some point, I just haven't the time to do it myself. If someone else wants to do it I'd be glad to help. I'm sure that it could be simplified/expanded/improved. I do know that there are some differences in the way BSD and Linux work on this, but I haven't dug into it very deeply. I got this working on OS X and Debian both, and it was good enough for me at the time.

Oz



(foreign-declare #<<END
#if defined(C_MACOSX)
#include <sys/param.h>
#include <sys/mount.h>
#else
#include <sys/statfs.h>
#endif
END
)

(define-foreign-record (statfs "struct statfs")
  (constructor: make-statfs)
  (destructor: free-statfs)
  (long f_bavail)
  (long f_blocks)
  (long f_bsize))

(define c-statfs
  (foreign-lambda int statfs c-string c-pointer))

(define (statfs)
  (let ((ptr (make-statfs)))
    (c-statfs "/" ptr)
    (let ((a `((f_bavail . ,(statfs-f_bavail ptr))
               (f_blocks . ,(statfs-f_blocks ptr))
               (f_bsize . ,(statfs-f_bsize ptr)))))
      (free-statfs ptr)
      a)))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]