From owner-freebsd-hackers Tue Aug 28 0:42:59 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from robin.mail.pas.earthlink.net (robin.mail.pas.earthlink.net [207.217.120.65]) by hub.freebsd.org (Postfix) with ESMTP id B97B237B408 for ; Tue, 28 Aug 2001 00:42:55 -0700 (PDT) (envelope-from tlambert2@mindspring.com) Received: from mindspring.com (dialup-209.245.143.233.Dial1.SanJose1.Level3.net [209.245.143.233]) by robin.mail.pas.earthlink.net (8.11.5/8.9.3) with ESMTP id f7S7gro00820; Tue, 28 Aug 2001 00:42:53 -0700 (PDT) Message-ID: <3B8B4BA7.EEDE7A55@mindspring.com> Date: Tue, 28 Aug 2001 00:43:35 -0700 From: Terry Lambert Reply-To: tlambert2@mindspring.com X-Mailer: Mozilla 4.7 [en]C-CCK-MCD {Sony} (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: Matthew Hagerty Cc: freebsd-hackers@freebsd.org Subject: Re: To determine if a file has grown? References: <5.1.0.14.2.20010827185351.01ba3aa8@pop.voyager.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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