Use ruby script to determine if there is enough space left on a device

Hi.

I want to use FileUtils to copy via ruby.

This works, but sometimes my second hdd could be almost full.

In this case, my script will fail because it can not copy 100%.

So now I need a safeguard, and I want to find out how much space is left
on a device.

Is this possible with ruby alone? To tell me “Ok, hdd1 has 1 gigabyte
empty space left”. I could solve this on linux with df / free but my
ruby script should also work for windows (so that I can backup things on
it too).

So a pure ruby solution is preferred.

Thanks.

Marc H. [email protected] wrote:

So now I need a safeguard, and I want to find out how much space is left
on a device.

Is this possible with ruby alone? To tell me “Ok, hdd1 has 1 gigabyte
empty space left”. I could solve this on linux with df / free but my
ruby script should also work for windows (so that I can backup things on
it too).

So a pure ruby solution is preferred.

Nothing pure Ruby.

statvfs/fstatvfs() are the POSIX functions, at least.

There’s a C extension or two out there which exposes statvfs(),
but I don’t know non-*nix at all.

What is:

syscall(137,fn,b)

doing? Or more precisely, how can I find out?

The number 137 tells me not much.

Subject: Use ruby script to determine if there is enough space left on a
device
Date: mer 21 ago 13 02:28:39 +0200

Quoting Marc H. ([email protected]):

So a pure ruby solution is preferred.

Some years ago I had a similar need. I wrote the following, which
manages to be pure Ruby, but is also ABSOLUTELY NON-PORTABLE (I work
only on Linux):

def partition_occupation(fn)
b=’ '*128
syscall(137,fn,b)
a=b.unpack(‘QQQQQ’)
(a[2]-a[4]).to_f/a[2]
end

Returns a floating value from 0 to 1, which represents the current
occupation of the partition that includes the file whose path is
passed as input.

The proper way would be to include an appropriate method in the Ruby
source code (dir.c, possibly). But to propose a patch, you’d have to
know how to obtain this value in windows, too (on posix systems you’d
use statvfs, as Eric mentions). And you’d have to be comfortable with
the entanglement of ifdefs that render that code multi-platform.

Carlo

On Wed, Aug 21, 2013 at 8:28 AM, Marc H. [email protected]
wrote:

So a pure ruby solution is preferred.

try the tools here djberg96 (Daniel Berger) · GitHub

kind regards -botp