A very basic tail -f implementation

From: James F.Hranicky [mailto:jfh@cise.ufl.edu]
Sent: Wednesday, July 31, 2002 3:50 PM
To: ruby-talk@ruby-lang.org
Subject: Re: A very basic tail -f implementation

I could try to document a little more in the best english
I’m capable
of. That is, it will probably be quite awful. :wink:

A couple of suggestions:

  • Check for rotation by checking for changes in the inode number

This is proving difficult in practice because, as the Pickaxe says, the
File::Stat info is recorded at the moment the File::Stat object is created;
changes made to the file after that point will not be reflected. This
applies to filehandles as well, as experimentation has shown. Simply
calling the class method won’t work either because that requires the
filename which, by that point, has already changed.

If I’m doing something wrong, I’d love to know how to make this work!

  • Allow tailing from arbitrary points in the file by lines, e.g

Heh - funny you should mention it. I already submitted a very similar
approach to Florian. :slight_smile:

Regards,

Dan

···

-----Original Message-----
On Wed, 31 Jul 2002 21:02:04 +0900 > Florian Frank flori@eavesdrop.ping.de wrote:

That’s true, you just have to stat the actual filename everytime and
check against the current filehandle.

I wrote my own very simple tail -f thingy in Ruby a few months back,
and was recently working on improving it when I noticed that Florian
released his version. Here’s what I have so far. Anyone can use any
of it however they want.

t.txt (2.65 KB)

···

On Thu, 1 Aug 2002 21:23:09 +0900 “Berger, Daniel” djberge@qwest.com wrote:

- Check for rotation by checking for changes in the inode number

This is proving difficult in practice because, as the Pickaxe says, the
File::Stat info is recorded at the moment the File::Stat object is created;
changes made to the file after that point will not be reflected. This
applies to filehandles as well, as experimentation has shown. Simply
calling the class method won’t work either because that requires the
filename which, by that point, has already changed.

If I’m doing something wrong, I’d love to know how to make this work!


Jim Hranicky, Senior SysAdmin UF/CISE Department |
E314D CSE Building Phone (352) 392-1499 |
jfh@cise.ufl.edu http://www.cise.ufl.edu/~jfh |


“Given a choice between a complex, difficult-to-understand, disconcerting
explanation and a simplistic, comforting one, many prefer simplistic
comfort if it’s remotely plausible, especially if it involves blaming
someone else for their problems.”
– Bob Lewis, Infoworld

Right. You also ought to check the device number, in case the file
is somewhow moved to another device and ends up with the same inode
number. At least, that’s what I did when I added the ‘-F’ option
to tail(1) in NetBSD. Maybe I’m just anal-retentive. :slight_smile:

Also, you might want to check to see if the file has been shortened,
in case the writer truncates it and starts writing again at the
beginning.

cjs

···

On Thu, 1 Aug 2002, James F.Hranicky wrote:

On Thu, 1 Aug 2002 21:23:09 +0900 > “Berger, Daniel” djberge@qwest.com wrote:

- Check for rotation by checking for changes in the inode number


That’s true, you just have to stat the actual filename everytime and
check against the current filehandle.


Curt Sampson cjs@cynic.net +81 90 7737 2974 http://www.netbsd.org
Don’t you know, in this new Dark Age, we’re all light. --XTC