LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-23-2014, 06:30 PM   #1
MichaelStein
LQ Newbie
 
Registered: Apr 2014
Posts: 14

Rep: Reputation: Disabled
Using actime in stat.h file to record the time a file was accessed.


Good day all,

I have been working on a program that monitors(during runtime) for when a file is accessed by the user.

Here is the code:
Code:
void LogFiles::Write(string filename)
{	
	struct stat attrib; //Creating a file attribute structure
	stat(filename.c_str(), &attrib); //get the attributes of filename.txt
	time_t lasttime = attrib.st_atime;
	time_t newtime = lasttime;
	
	/*Compare the previous time the file was accessed to the current time it was accessed. Loops until they differ. If they differ, exit the loop and record to the console. Note: the times will only differ if the user accesses the file during runtime.*/
	while( newtime == lasttime )
	{
		stat(filename.c_str(), &attrib);
		newtime = attrib.st_atime;
	} 
	cout << "User accessed file " << filename " at: " << ctime(&(newtime)) << endl;
}
The code ALMOST works. However, upon running the program, I noticed that it only records to the console if I access AND MODIFY the file, and then access the file again. Otherwise, it seems never to exit the while loop.

I have given it some thought and I realised that the "newtime" variable/value only changes if the file is modified and then accessed.

1. Does anyone know why this is?
2. Does anyone have a suggestion how I can fix this?
Many thanks
 
Old 04-23-2014, 06:45 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
check the filesystem mount - it may have atime updates disabled for speed.

The other thing is you might want to use inotify functions instead. What you are doing is polling the file, inotify provides for watch lists that the kernel uses to generate events to notify the process when things change.

Granted, it really depends on how fast you want to be notified... and the amount of CPU time you have to spend polling (which without a sleep in the loop, will be a lot).
 
1 members found this post helpful.
Old 04-23-2014, 07:09 PM   #3
MichaelStein
LQ Newbie
 
Registered: Apr 2014
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post

The other thing is you might want to use inotify functions instead. What you are doing is polling the file, inotify provides for watch lists that the kernel uses to generate events to notify the process when things change.

Granted, it really depends on how fast you want to be notified... and the amount of CPU time you have to spend polling (which without a sleep in the loop, will be a lot).
Thank you for replying.

I tried to use inotify for this problem. Here is the code I used for it:

Code:
int length, i = 0;
	/*Initialises a file descriptor.*/	
	int fd = inotify_init();
	/*Initialises a watch descriptor.*/
	int wd = 0;
	
			/*Adds a watch*/
	        	wd = inotify_add_watch(fd, filename.c_str(), IN_ACCESS);
    		}  
	       			
		/*Calls read() to wait for alert*/
        	length = read(fd, buffer, BUF_LEN);
		
		
        	if(length < 0)
        	{
       	   		perror("read");
        	}
		
		/*Initialises an array of events and sets them to the buffer offset*/
	 	struct inotify_event* event = (struct inotify_event * ) &buffer[0];
		
		/*Set the event mask IN_ACCESS and writes the occurrence to the console*/
		if(event->mask & IN_ACCESS)
		{			
			 cout << event->name << endl;
		}
However, I found that it printed to the console all the file names within the directory - not just the name of the directory itself.
 
Old 04-23-2014, 09:45 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Well? Any access to any file within the directory would be an access to the directory. Just doing a ls command will cause an access for each file in the directory...

Thats what happens when you put a watch on a directory. I thought you wanted a specific file.
 
1 members found this post helpful.
  


Reply

Tags
c++, stat


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
Is there a way to know the last time Apache accessed a file? WhisperiN Linux - Server 8 03-10-2010 11:06 PM
Can I check who has accessed a file recently? davidstvz Linux - Security 4 06-29-2009 12:20 PM
Is there any way to tell which user last accessed a file? sixerjman Programming 2 10-19-2006 02:28 AM
file created/accessed dates plainkeyman Linux - General 4 10-27-2005 08:34 PM
c++ file processing -- how to remove a record from a file sharonyiisl Programming 4 09-26-2004 04:54 AM


All times are GMT -5. The time now is 10:46 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration