The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Prompting for file deletion? NycUnxer Shell Programming and Scripting 2 11-01-2007 04:36 PM
file deletion problem shdwsclan UNIX for Dummies Questions & Answers 2 01-26-2006 10:59 PM
large file deletion run_time_error UNIX for Dummies Questions & Answers 2 08-25-2005 06:37 AM
Deletion of File in Unix rhomel101 UNIX for Dummies Questions & Answers 2 02-22-2005 01:47 AM
File deletion when server restarts k_oops9 Shell Programming and Scripting 6 11-22-2004 10:07 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-12-2007
siegfried siegfried is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 91
How to watch for file creation/deletion?

How do I write a C program that will watch a directory for file creation/deletion? Maybe it would receive a signal when someone creates a file?

thanks,
Siegfried
  #2 (permalink)  
Old 11-12-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by siegfried View Post
Maybe it would receive a signal when someone creates a file?
Not by default.

This, unfortunately requires polling, and the simplest way would be to monitor the modification time of the directory the files are supposed to appear in.

Not a good thing to do on an NFS mounted file system, unless you had the polling process on the same machine as the NFS share and then used some form of IPC to notify clients.
  #3 (permalink)  
Old 11-12-2007
siegfried siegfried is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 91
At http://os.cqu.edu.au/docs/kernel-doc...on/dnotify.txt I found an example. It seems to work for detecting file creation on fedora but not cygwin.

Is using F_NOTIFY as a parameter for fnctl the most standard way of doing this? Someone else said I should be using dbus but I don't know what that is.

Siegfried
  #4 (permalink)  
Old 11-12-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
The clue is in the name, "Linux Directory Notification".

Cygwin is not Linux.

I have checked NetBSD 2 and Solaris 7 and neither of them support this.
  #5 (permalink)  
Old 11-13-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Why not just readdir? Maintain a list of files you get each time and a new file is one that you get in readdir, but not in your list. Of course, if your directory is going to have thousands of files, it isn't a good idea.

@Porter,
Won't the directory modification time change when a file is deleted as well?

Perhaps a program that checks for dir modified time and checks against a maintained a list of files if the mtime has changed...
  #6 (permalink)  
Old 11-13-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by blowtorch View Post
@Porter,
Won't the directory modification time change when a file is deleted as well?
Yes so cheapest thing to do is monitor that single date, then if that changes delve deeper to find what actually changed.
Closed Thread

Bookmarks

Tags
linux, mtime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0