LinuxQuestions.org
Visit Jeremy's Blog.
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 08-21-2008, 03:23 AM   #1
MaRock
LQ Newbie
 
Registered: Aug 2008
Posts: 13

Rep: Reputation: 0
how do i find if somefile is being written to a folder at any point in time?


hi all,

how do i find if somefile is being written to a folder at any point in time. i.e in other words , i want to know if a file being written to a folder is done or is still being done.
eg: i copy file 'x' to folder 'Y' now i want to copy x into another folder 'Z' but i want to make sure that 'x' is completely copied to the folder 'Y' before the file is copied from 'Y' to 'Z'. is there a command or code logic which will tell me if the write is done or not?

--Maneesh
 
Old 08-21-2008, 03:36 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
So, you need to know whether there are open files in a certain directory? "lsof" may help.
 
Old 08-21-2008, 03:38 AM   #3
MaRock
LQ Newbie
 
Registered: Aug 2008
Posts: 13

Original Poster
Rep: Reputation: 0
thanks burschik

Quote:
Originally Posted by burschik View Post
So, you need to know whether there are open files in a certain directory? "lsof" may help.
thanks burschik

i'll try this one:
 
Old 08-21-2008, 01:48 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Looks like burschik has the solution to your problem, but to answer your initial question and the topic of your thread, you'd need to use the F_NOTIFY fcntl in a C program to receive a signal when a certain file or directory has some sort of access event such as read, write, etc.

To apply that concept to your problem (strictly academic at this point,) you'd register an alarm with alarm with a few seconds' delay and each write operation would cause a signal because of the fcntl. The handler for this signal would reset the alarm and when the write operations stopped the alarm would time out, and that would be your notification of the end of file modification.
ta0kira

edit:
inotify might also be of help.

Last edited by ta0kira; 08-21-2008 at 01:59 PM.
 
Old 08-22-2008, 02:41 AM   #5
MaRock
LQ Newbie
 
Registered: Aug 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks ta0kira,

what burschik suggested was optimal for my requirement,
But i was just about to write a daemonprogram, which would be monitoring a certain folder. I believe your logic would best suite my requirement.
That said, i have a few doubts(might be silly though) , i dont have a strong programming background ,and so i'll need all possible help..

1.what kind of signal would the alarm be ? (sound, bit pattern, flag?)

2. Is it possible to mutate the alarm signal to a required type?
i.e. if it by default gives a sound signal, can it be made to jsut raise a flag instead?

3. Can u pls give a sample code bit , to showcase this imolementation?

4. And how do i integrate this C program with my shell Script?

-Maneesh
 
Old 08-22-2008, 02:48 AM   #6
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Before you start writing your own daemon, have a look at incron.
 
Old 08-22-2008, 02:57 AM   #7
MaRock
LQ Newbie
 
Registered: Aug 2008
Posts: 13

Original Poster
Rep: Reputation: 0
and how do i do that?
sorry abt my questions.. im just 2 weeks into unix..

well to be frank im an Electronics engineer, so .. very new to programming itself..
 
Old 08-22-2008, 03:29 AM   #8
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Have a look here.
 
Old 08-22-2008, 04:56 AM   #9
MaRock
LQ Newbie
 
Registered: Aug 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks ! that was great information
 
Old 08-22-2008, 02:48 PM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by MaRock View Post
T1.what kind of signal would the alarm be ? (sound, bit pattern, flag?)

2. Is it possible to mutate the alarm signal to a required type?
i.e. if it by default gives a sound signal, can it be made to jsut raise a flag instead?

3. Can u pls give a sample code bit , to showcase this imolementation?

4. And how do i integrate this C program with my shell Script?
Sorry, I mis-spoke slightly. You monitor the directory and not the file itself. In any case:
Code:
#define _GNU_SOURCE

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>


static void alarm_handler(int);
static void write_handler(int);


int main(int argc, char *argv[])
{
	signal(SIGALRM, &alarm_handler); //register a handler for the alarm
	signal(SIGIO, &write_handler);   //register a handler for the write notification


	//sign up for notification for each directory given on the command line

	int I = 1;
	for (; I < argc; I++)
	{
	int next_directory = open(argv[I], O_RDONLY);

	if (next_directory == -1)
	 {
	fprintf(stderr, "%s: file open error: %s", argv[0], strerror(errno));
	continue;
	 }

	if (fcntl(next_directory, F_NOTIFY, DN_MODIFY | DN_MULTISHOT) == -1)
	 {
	fprintf(stderr, "%s: file monitor error: %s", argv[0], strerror(errno));
	continue;
	 }
	}


	//set the alarm (though by now a write notification should have started it)

	alarm(1);


	//wait indefinitely for the alarm to go off

	while (1) sleep(1);


	return 1;
}


static void alarm_handler(int sSignal)
{
	if (sSignal == SIGALRM)
	{
	fprintf(stderr, "all file writes are complete!\n");
	exit(0);
	}
}


static void write_handler(int sSignal)
{
	//reset the alarm

	if (sSignal == SIGIO) alarm(1);
}
"Signal" has a very specific meaning on *nix systems. It's an interrupt sent to a process that normally stops whatever the process is doing at the time to execute a predetermined routine meant to handle the signal.

You can compile the above program, assuming the file's name is monitor.c, as follows:
Code:
gcc monitor.c -o monitor
You can test it by doing this:
Code:
{ dd if=/dev/urandom of=testing bs=1K count=32K & }; ./monitor .
You'll receive a message and the program monitor will exit once writing stops. Make sure to delete the file testing since it will be 32MB.
ta0kira

Last edited by ta0kira; 08-22-2008 at 02:56 PM.
 
Old 08-25-2008, 06:38 AM   #11
MaRock
LQ Newbie
 
Registered: Aug 2008
Posts: 13

Original Poster
Rep: Reputation: 0
thanks ta0kira...!
 
  


Reply

Tags
inotify, lsof


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
How to know if a folder is being written with a file at any point of time? MaRock Linux - Newbie 8 08-25-2008 06:40 AM
LXer: This time, it'll be a Wikipedia written by experts LXer Syndicated Linux News 0 07-15-2006 11:54 PM
How can I point a users public_html to a folder on a webserver outside the firewall? Durham Linux - Networking 5 05-13-2006 04:36 AM
First time installing, mount point? smagee12 Linux - General 1 10-23-2005 01:24 PM
how to run the script from /etc/rc.d/ini.d/somefile. at statup shahidawan Linux - General 1 05-13-2005 05:23 AM


All times are GMT -5. The time now is 09:49 AM.

Main Menu
Advertisement
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