This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: File::Stat annoyances
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
I need to stop looking at the Ruby source code, I really do. Because, you see, today I discovered what a mess the File::Stat class is. Oh, it works (though I discovered an issue today), but it goes through a lot of contortions for what ought to be a dead simple class.
When you call File::Stat.new, the attributes of that object are unaffected by further modifications to the file after the call. Ok, no problem. So, why is the stat buf being stored as an opaque data structure? Why not just call stat() within the File::Stat#initialize method, and set all the instance variables there? As it stands now, each File::Stat attribute is, in fact, a separate method call that has to grab the opaque data structure first, get the proper field, and convert it to a VALUE. Dumb.
The issue that I discovered today is that File.stat returns bogus values for attributes like 'size' and 'blksize' for character or block devices. Actually, scratch that. The proper behavior is muddled because the documentation doesn't agree with the implementation as far as what the stat() function should return for character and block devices, depending on your platform. Rich Teer's Solaris System's Programming (p. 357, if you must know) states that members like st_size and st_blksize are undefined on Solaris for character and block devices, but this doesn't appear to be the case in practice. The st_size member appears to get set to INT_MAX, while the st_blksize member value looks legit. Linux, on the other hand, actually does set the st_size member to NULL, but it still sets the st_blksize member.
So, where does this leave us? I suppose, for st_size at least, we'll have to add an explicit check for block and character devices, and set the size to 0 manually. I'm not sure about the rest of the fields. I'm going to tread into comp.unix.solaris and see what they have to say on the subject.