LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-30-2009, 12:36 AM   #1
henriquetft
LQ Newbie
 
Registered: Oct 2009
Distribution: Debian
Posts: 6

Rep: Reputation: 0
Monitoring file system with inotify


Hi everyone,

I need to keep track of certain files on the disk. I need to know when a file is deleted, or moved for example. I'm using linux and I thought inotify could to do this job.

But the problem is: Using inotify, when I move a file, I need to have the destination folder also being watched in order to know the new location of the moved file. But these files can be moved to anywhere. So of course, it is not workable to have all directories being watched. Even when a file is renamed, I am not able to find out which is the new name of the file.

Any suggestions on how can I solve that?

Thanks in advance.
 
Old 10-30-2009, 12:55 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,462
Blog Entries: 54

Rep: Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899
Quote:
Originally Posted by henriquetft View Post
when I move a file, I need to have the destination folder also being watched in order to know the new location of the moved file.
That doesn't indicate a problem with Inotify but rather a fundamental misunderstanding of How Things Work.


Quote:
Originally Posted by henriquetft View Post
these files can be moved to anywhere. (..) Even when a file is renamed, I am not able to find out which is the new name of the file.
It could be beneficial (for you) if you post 0) the exact reasons why you need this, 1) the name of the process(es) causing this, 2) and a clear example of this. That would make it way easier to post specific solutions. For instance if this is only about unprivileged users then it would never be necessary to watch a whole filesystem as they are not able to move files to say /sbin anyway.
 
Old 10-30-2009, 11:09 AM   #3
henriquetft
LQ Newbie
 
Registered: Oct 2009
Distribution: Debian
Posts: 6

Original Poster
Rep: Reputation: 0
OK, I'm not saying this is a problem with inotify, I'm saying that apparently i cannot do what i want to do with it, and i'm asking for suggestions on alternative ways.

OK, it is not necessary to watch some dirs, like /sbin, but, anyway, the number of directories I have to watch is too big, so I want a better solution.

As I said, I need to know where a certain file is, at any point of time...
 
Old 10-30-2009, 01:13 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,462
Blog Entries: 54

Rep: Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899
I think you fail to understand me properly. Allow me to explain as tersely as possible. Notice that at the time of posting I had approximately 16K+ posts here on LQ. At least two thirds of those I raked up by helping LQ members troubleshoot their problems (or sometimes: their questions). When members post specific, detailed, technical information then troubleshooting often becomes (more) easy. When they do not then asking questions will often provide the required information to either nudge them on, do research or propose a workaround or solution. Persistence, patience, process of elimination. Right now what you are doing is basically repeating yourself without providing any of the requested information. That leads to frustration on both sides. Frustration leads to anger and we all know what anger leads to...
So. To recap: it would be efficient and beneficial (for you) if you post
0) the exact reasons why you need this (what are you really trying to accomplish, prevent or anticipate),
1) the name of the process(es) causing this (or user(s) details),
2) and an example of this.
 
Old 10-30-2009, 11:46 PM   #5
henriquetft
LQ Newbie
 
Registered: Oct 2009
Distribution: Debian
Posts: 6

Original Poster
Rep: Reputation: 0
Hello unSpawn,

thank you for your good will on help me.

My goal is to maintain certain properties for some files. These properties are kept in another file, that references the file and its properties. (for this reason I need to know where a certain file really is, at any time).

I was searching on the internet and I'm almost sure that there is no way to do it without adding watches for all directories. I was looking at the source code of beagle (desktop indexer), and I saw it was doing this manner.
 
Old 10-31-2009, 12:14 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If a file is copied then it's a new file. If a file is moved to another file system, it must be copied; therefore, you must either assume that a file will stay on a single file system, or you must find another way to deal with moved files. Too much automation is a bad thing because it almost always involves hiding in the dark and making assumptions so you don't have to bother the user. Just out of curiosity, why should a moved file be considered the same file if it can't be tracked by your program already? What if a file is moved or deleted when your program isn't running?
Kevin Barry

Last edited by ta0kira; 10-31-2009 at 12:16 AM.
 
Old 10-31-2009, 12:04 PM   #7
henriquetft
LQ Newbie
 
Registered: Oct 2009
Distribution: Debian
Posts: 6

Original Poster
Rep: Reputation: 0
Yes. Now (since I had these issues), among other assumptions, I'm assuming my program is always running and it is limited to a single file system.

And answering your question, the moved file, is still the same file, and for that reason should keep its properties. But I only can know that, if my program is running when the move take place.

Citing the example of beagle, it apparently adds watches for your home dir (and all subdirs). When it detects a "moved from" event, without a "moved to" event (ie. the file was moved to a unwatched dir), then it removes metadata for that file (in the same way when the file itself is removed).
 
Old 10-31-2009, 03:38 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If your system supports procfs, you might just open the files you're tracking and if they seem to no longer be there, check the symlink for the corresponding file in /proc/self/fd. That's assuming procfs will update the file location if it moves while it's open, which I assume it does because /proc is generated on the fly.
Kevin Barry
 
Old 10-31-2009, 05:25 PM   #9
henriquetft
LQ Newbie
 
Registered: Oct 2009
Distribution: Debian
Posts: 6

Original Poster
Rep: Reputation: 0
hmm, very clever. Thanks for this.

Yes, the symlink is updated. But I think it is only workable if the number of files is small, because I need to open all files and I think I need to add inotify watches too, at least for the directory of the file to know when to look for the procfs informations about the new path.
 
  


Reply

Tags
inotify, linux


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
network monitoring:unable to launch nagios network monitoring system oladapo1980 Linux - Newbie 0 07-21-2009 01:45 PM
inotify for recursive directory monitoring venkat_k Programming 4 12-24-2008 03:45 AM
remove inotify system call linuxdoniv Programming 0 08-06-2008 06:07 AM
folder size monitoring NFS file system haiders Linux - General 6 01-15-2007 02:10 PM
monitoring file system Tinkster Linux - Security 14 06-09-2002 04:25 AM


All times are GMT -5. The time now is 08:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration