From: Mark Hahn Date: 2002-01-01T09:19:51+09:00 Subject: [ruby-talk:29916] Re: How to check free diskspace? (fwd) No one has answered the main question, which is: Why is there no support for a platform-independent df in Ruby (or Java for that matter)? Any good program that writes to a disk should be able to take preventive action before a disk fills up. Waiting for a "disk full" exception is a recipe for disaster. -----Original Message----- From: Mathieu Bouchard [mailto:matju@sympatico.ca] Sent: Monday, December 31, 2001 4:10 PM To: ruby-talk ML Subject: [ruby-talk:29915] Re: How to check free diskspace? (fwd) On Mon, 31 Dec 2001, Mathieu Bouchard wrote: > On Tue, 1 Jan 2002, Le Wang wrote: > > Hi all, > > I've checked "Programming Ruby", and searched the ML to no avail. So > > how do you check free disk space, other than to parse 'df' output? > Mount = Struct.new( > :device, > :path, > :fs_type, > :options, > :freq, > :passno) > mounts = File.open("/etc/mtab") {|f| > f.map {|x| > Mount.new(*x.chomp.split) }} Er... I forgot to finish the code. This only tells what's mounted and where. I realized that, unfortunately, the functionality you really want is system-dependent. It doesn't seem to be covered by POSIX. GNU's "df" command (in fileutils package) can use any of several implementations, including BSD, SysV, AIX, ... Here's a new version which includes df support. Mount = Struct.new( :device, :path, :fs_type, :options, :freq, :passno, :total, :used, :free) mounts = File.open("/etc/mtab") {|f| f.map {|x| m = Mount.new(*x.chomp.split) m.freq=m.freq.to_i m.passno=m.passno.to_i m }} File.popen("sh -c 'LC_ALL=en df -P'") {|f| 2.times { f.readline } f.map{|r| dev,*stuff = r.chomp.split /\s+/ m = mounts.find {|d| d.device == dev } m.total,m.used,m.free = stuff[0..2].map{|x| x.to_i }}} ________________________________________________________________ Mathieu Bouchard http://hostname.2y.net/~matju