Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 28 Aug 2001 00:43:35 -0700
From:      Terry Lambert <tlambert2@mindspring.com>
To:        Matthew Hagerty <mhagerty@voyager.net>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: To determine if a file has grown?
Message-ID:  <3B8B4BA7.EEDE7A55@mindspring.com>
References:  <5.1.0.14.2.20010827185351.01ba3aa8@pop.voyager.net>

next in thread | previous in thread | raw e-mail | index | archive | help
Matthew Hagerty wrote:
> Is there a fast and/or efficient way to determine if a file size has
> changed without reopening the file every time?  I'm writing a program that
> needs to open a file and watch it to see when data gets written to the file
> (from an external source or another part of the same program), then read
> the data to process it.  I was looking at stat() but I've read that it is a
> high overhead function.  Any insight would be greatly appreciated.

For FFS, you can register for a kevent that will tell you that
the file has changed (man kqueue).  If you are running another
FS, like NFS, generally this is not supported, and you will not
get anything back telling you about the change.

FS research in this area has created a thing called "notifications";
the kevent you will get is a [poor] cousing of notifications, when
it comes to file system events which you want to watch, but it is
sufficient to your task for FFS.

For some other local FS, you can either add the relevent kevent
(via "KNOTE()" macros -- see FFS for how to implement) or, for
NFS, you are out of luck.

If you are "out of luck" (NFS, or unwilling/unable to make the FS
changes necessary), you can simply open the file -- and keep it
open forever.  Then each time you wish to check, you should do an
fstat() of the open descriptor.  This will be much less expensive
than a stat, but more expensive than a kevent, since you will end
up checking periodically, even if nothing has changed.

If you are doing this to pass messages between programs, you will
probably want to use a pipe, socket, or System V message queue
(man msgget) instead.

-- Terry

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3B8B4BA7.EEDE7A55>