|
Home > Archive > Unix Programming > July 2006 > File Handling
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| munish.nr@gmail.com 2006-07-14, 7:25 am |
| Hi,
I am developing an application on Linux environment. My application is
opening a file(say X) and read some configurations parameters from it
and closing the file X and doing some functional processing. In the
meantime if user modified/change the file X , then my application
should reopen the file X and agan reading the parameters and working
furhter.
now the problem how my application will come to know that the file has
been modified or chaged.
Does nybody knows how i can make my application know that teh
particular file has beem changed.
I will really appreciate an email with comments form you!
Thanks,
Munish Nayyar
| |
| iwinux 2006-07-14, 7:25 am |
| munish.nr@gmail.com ??:
> now the problem how my application will come to know that the file has
> been modified or chaged.
You may get the answer from the book called
Advanced programming in the UNIX Environment.
By the way, before you program on unix, you should read this book first.
| |
| Rainer Temme 2006-07-14, 7:25 am |
| munish.nr@gmail.com wrote:
> now the problem how my application will come to know that the file has
> been modified or chaged.
You could call stat() on the filename after you read the file,
and keep the resulting "struct stat" somewhere.
Later, you could call stat() again (cyclically) and compare the
members of the old struct stat and the new struct stat.
If they are unchanged, the file is likely to be unmodified.
As well if the file is small, you could compute a checksum (for
instance crc32 or adler from zlib) or a hash (md5 sha1) from it, and
compare the original checksum/hash with the newer.
Rainer
| |
| Barry Margolin 2006-07-14, 1:19 pm |
| In article <e981vq$up0$1@daniel-new.mch.sbs.de>,
Rainer Temme <Rainer.Temme@NoSpam.Siemens.Com> wrote:
> munish.nr@gmail.com wrote:
>
> You could call stat() on the filename after you read the file,
> and keep the resulting "struct stat" somewhere.
Don't some flavors of Unix and Linux now have a file system notification
facility, where a process can register to get a signal if a file has
changed?
>
> Later, you could call stat() again (cyclically) and compare the
> members of the old struct stat and the new struct stat.
> If they are unchanged, the file is likely to be unmodified.
The only member you generally care about is mtime. If someone modifies
the file and then sets mtime back, they presumably don't want
applications like this to notice and reload the configuration.
> As well if the file is small, you could compute a checksum (for
> instance crc32 or adler from zlib) or a hash (md5 sha1) from it, and
> compare the original checksum/hash with the newer.
Unless reloading the configuration is very expensive, this seems like
overkill in most cases.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| munish.nr@gmail.com 2006-07-14, 1:19 pm |
|
Rainer Temme wrote:
> munish.nr@gmail.com wrote:
>
> You could call stat() on the filename after you read the file,
> and keep the resulting "struct stat" somewhere.
>
> Later, you could call stat() again (cyclically) and compare the
> members of the old struct stat and the new struct stat.
> If they are unchanged, the file is likely to be unmodified.
>
Thats fine but at what time i should called stat again. this is the
main problem, is there any way by which my application at run time get
some signal on file modification!
> As well if the file is small, you could compute a checksum (for
> instance crc32 or adler from zlib) or a hash (md5 sha1) from it, and
> compare the original checksum/hash with the newer.
>
> Rainer
| |
| munish.nr@gmail.com 2006-07-14, 1:19 pm |
| Thats fine.
Barry Margolin wrote:
> In article <e981vq$up0$1@daniel-new.mch.sbs.de>,
> Rainer Temme <Rainer.Temme@NoSpam.Siemens.Com> wrote:
>
>
> Don't some flavors of Unix and Linux now have a file system notification
> facility, where a process can register to get a signal if a file has
> changed?
>
yes, i also thought same. but i dont know to whom to register to
receive such a signal.
>
> The only member you generally care about is mtime. If someone modifies
> the file and then sets mtime back, they presumably don't want
> applications like this to notice and reload the configuration.
>
But my requirement is like that.
>
> Unless reloading the configuration is very expensive, this seems like
> overkill in most cases.
>
My application functions like that only!
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Rainer Temme 2006-07-14, 1:19 pm |
| Barry Margolin wrote:
> Don't some flavors of Unix and Linux now have a file system notification
> facility, where a process can register to get a signal if a file has
> changed?
For linux with kernel 2.4 and newer ...
man fcntl ... look for F_NOTIFY
But it's not a portable thing.
Rainer
| |
| Chuck Dillon 2006-07-14, 1:19 pm |
| munish.nr@gmail.com wrote:
> Rainer Temme wrote:
>
>
>
> Thats fine but at what time i should called stat again. this is the
> main problem, is there any way by which my application at run time get
> some signal on file modification!
One clean way to implement the poll is to fork() a child job whose only
purpose is to stat() the file at some frequency and send the main app a
signal if a change is seen.
Look for FAM on your system. Try 'man 3X fam'. Note, that you need to
consider whether you can count on FAM always being available where your
program runs. If not, then polling with stat/fstat is what you'll want.
-- ced
>
>
>
>
>
>
>
>
--
Chuck Dillon
Manager of Software Development, Bioinformatics
NimbleGen Systems Inc.
| |
| Pascal Bourguignon 2006-07-14, 1:19 pm |
| munish.nr@gmail.com writes:
> Hi,
>
> I am developing an application on Linux environment. My application is
> opening a file(say X) and read some configurations parameters from it
> and closing the file X and doing some functional processing. In the
> meantime if user modified/change the file X , then my application
> should reopen the file X and agan reading the parameters and working
> furhter.
>
> now the problem how my application will come to know that the file has
> been modified or chaged.
>
> Does nybody knows how i can make my application know that teh
> particular file has beem changed.
Depends on the kind of program.
http://catb.org/~esr/writings/taoup/
If it was a daemon, the user would send a HUP signal to the process to
let it know it should reload the configuration file.
Since you use the name "application" we'll assume you have a user
interface, so you could just add a menu item or a command to this user
interface to let the user inform the application that it should reload
the configuration file. But really, an "application" should rather
let the user change the configuration from within the "application",
using some dialog or configration commands.
Perhaps it's not an "application"?
A program can detect when a file has been modified, but it is not
advisable to let it reload the configuration file as soon as it's
modified, because the user could leave the configiration file _half_
modified. Some editor save automatically the file during the edit.
So the file could change before the user has completed the
modifications.
--
__Pascal Bourguignon__ http://www.informatimago.com/
Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.
| |
| Robert Harris 2006-07-14, 1:19 pm |
| munish.nr@gmail.com wrote:
> Hi,
>
> I am developing an application on Linux environment. My application is
> opening a file(say X) and read some configurations parameters from it
> and closing the file X and doing some functional processing. In the
> meantime if user modified/change the file X , then my application
> should reopen the file X and agan reading the parameters and working
> furhter.
>
> now the problem how my application will come to know that the file has
> been modified or chaged.
>
> Does nybody knows how i can make my application know that teh
> particular file has beem changed.
>
> I will really appreciate an email with comments form you!
>
> Thanks,
> Munish Nayyar
"man inotify" will tell you how to get notification with recent Linux
kernels.
Robert
|
|
|
|
|