LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-15-2002, 07:30 AM   #1
saravanan1979
Member
 
Registered: Jan 2002
Posts: 163

Rep: Reputation: 30
Linux Equivalent for Windows API functions


Hello
Can anyone tell me if there is any Linux Equivalent for Windows NT API fuinctions which help us to identify when a
file in a directory is added,Edited or Deleted.I need this sort of interface very badly.
 
Old 06-15-2002, 10:49 AM   #2
Cyth
Member
 
Registered: Mar 2001
Location: Sweden, Lund
Distribution: Slackware
Posts: 68

Rep: Reputation: 15
I don't know what glibc offers but I know KDE have an excellent technology called KDirWatchers which fulfills your need. If you want to use the KDE framework(which will solve this problem) I suggest you drop in on #kde or #kde-users on OPN or join the kde mailinglist(send an mail with "subscribe" as subject to kde-devel-request@mail.kde.org).

You should also check out http://developer.kde.org where all you need to get familiar with KDirWatch is documented.

cheers,
Cyth
 
Old 06-15-2002, 11:07 AM   #3
Ztyx
Member
 
Registered: Dec 2001
Location: Stockholm, Sweden
Distribution: Ubuntu, Kubuntu and Debian
Posts: 338

Rep: Reputation: 30
To check if a file is added or removed you can simply dump the file listing to a file. When then checking if a file is added or removed you can then just compare your dump-file with the actual file listing.
A simple way to do a file listing is running by the following at shell (I don't know how much you know, so I write some extra):
------------
ls > your_dump_file
------------
If you're writing your program in C/C++ you may do the command above by calling:
------------
#include <stdlib.h>
....
system("ls > your_dump_file");
------------
When you're then going to check the last time the file was modified you then call this function (you'll actually have to compare every file to check if it's been modified or not):
------------
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

int stat(const char *path, struct stat *buf);
------------
Where "path" is the file you'd like to get information about and "buf" is a pointer a "struct stat"-type where it will save all the info about the file.

The struct "stat" contains a lot of other information about files. If you're interrested in them all try "man 2 stat". Anyway, the "struct stat" has a member called "st_mtime" which is of the type "time_t" (numbers of seconds since 1970). That's the variable you'll have compare with your dump-file.
(I forgot to tell you above that you'll also have to dump the last modification times for each file :-) )

So, now you have something to do :-)
If you don't like the shell-solution of dumping a file and instead would like me to send the solution on how to list a directory inside a C-program I can do that :-)

Good luck,

Jens Rantil, student i Sweden
 
Old 06-15-2002, 02:10 PM   #4
Cyth
Member
 
Registered: Mar 2001
Location: Sweden, Lund
Distribution: Slackware
Posts: 68

Rep: Reputation: 15
Me judging Saravanan's post; it looks like he/her wants to know *when*, to get notified when the something changes. One solution is to construct a loop which performs what you suggested and then compares the data so see if something had changed. But that is really ugly.

What are you looking for, savanan?

cheers,
Cyth
 
Old 06-15-2002, 04:47 PM   #5
geoffm33
Member
 
Registered: May 2002
Distribution: RH 7.3 - YDL 2.3
Posts: 63

Rep: Reputation: 15
Please try not to double post....there have been solutions offered in both this thread and this one but you need to tell us why they won't work so we can find the right solution.

thanks!
 
Old 06-17-2002, 10:10 AM   #6
saravanan1979
Member
 
Registered: Jan 2002
Posts: 163

Original Poster
Rep: Reputation: 30
Dear Cyth
U assumption was right, i want to nnow whenever a file is being added into a directory.Whenver a new file is added i will check for certain contents in the file and if the conditions are satisfied i will move the file to anothere directory.

Dear Ztyx
I am actually calling the shell program from a PHP script.
system will do,stat is fine it will only give some information about the file/directory,but how do i compare dump_file with the directory to find out if a new file is added

Please revert with details
Saravanana
 
Old 06-17-2002, 12:58 PM   #7
Ztyx
Member
 
Registered: Dec 2001
Location: Stockholm, Sweden
Distribution: Ubuntu, Kubuntu and Debian
Posts: 338

Rep: Reputation: 30
This is an php-example on how to check if a file i added. The file "dump_file.txt" is the file you dumped the list of files last time last time. That file is being used to compare with the new file list.
To use this code below you must first use chdir() to change to the directory where you're going to compare the files...
------------------------------
PHP Code:
function isStringInFile($file$string)
{
  
$fp fopen($file"r");

  
$found_a_file false;

  while (
$teststring fgets($fp4096)) {
    if (
$teststring == $string)
      
$found_a_file true;
  {

  
fclose($fp);

  return 
$found_a_file;
}

// Here goes the actual code
// First dump the files which exists at this moment
system("ls > /tmp/a_temp_file.txt");

$fp2 fopen("/tmp/a_temp_file.txt""r");

while (
$file_name fgets($fp24096)) {
  if (
isStringInFile("dump_file.txt"$file_name) == false) {
    echo(
"The file $file_name has been added recently");
  }
}

fclose($fp2);
unlink("/tmp/a_temp_file.txt");

// After the comparation you just dump the directory to file again
system("ls > dump_file.txt"); 
-----------------------------------

This doesn't have code for the modification of files. If you'd like to check which files that have been deleted you just run my code above except that you compare the new old file dump with new one instead. Got it?

I hope this helps or perhaps this isn't the solution you're looking for...

Jens Rantil

Last edited by Ztyx; 06-18-2002 at 06:40 AM.
 
Old 06-18-2002, 01:03 AM   #8
saravanan1979
Member
 
Registered: Jan 2002
Posts: 163

Original Poster
Rep: Reputation: 30
Dear Jens Rantil
i got ur idea of trapping a new file nbeing added it is only the lack of ,y knowledge in Linux that bought me down.Ur logic is good, as of know i will carry on with this logic to find out if a file is modifed we will get the size of the file and compare it,meanwhile if i find some other optimal solution i will inform u also.I have decided to learn the linux commands in and out.
Regards
Saravanan
 
Old 06-18-2002, 02:12 AM   #9
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Well scanning the contents of the complete directory every x seconds to see if there are any changes is a kind of expensive operation. The modified time of a directory is automatically changes when something in the directory changes (Ex. file is added, file is removed, file is modified). Once the timestamp is changed then you can scan the directory contents to find out exactly which file is modified, added or deleted.

In c you could check the directory timestamp with a simple stat command. I don't know how you would do that with perl but I'm sure it wouldn't be to hard to do.
 
Old 06-18-2002, 03:15 AM   #10
saravanan1979
Member
 
Registered: Jan 2002
Posts: 163

Original Poster
Rep: Reputation: 30
Dear Mik
I am to going to check the directory every xx seconds i will be checking the directory every x days and check if any new file has been added.I will tell u my exact requirement.
I need to check if a user has received a mail if he has i need to read he mail and do some manipulations,i think the solution provided by Rantil serves the purpose anyway i have started imoplementing his logic,i
 
Old 06-18-2002, 06:39 AM   #11
Ztyx
Member
 
Registered: Dec 2001
Location: Stockholm, Sweden
Distribution: Ubuntu, Kubuntu and Debian
Posts: 338

Rep: Reputation: 30
Just so you know. There might be better solutions although due my lack of knowledge about any such library in linux I'd write my program in the way described above.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
windows api in a linux distro???? ronanio Linux - Games 2 10-04-2005 08:57 AM
X Windows equivalent API of Win32 APIs montylee Programming 2 03-08-2005 06:19 AM
If Win32 is for WIndows than What is the API for Linux ? indian Programming 14 01-07-2005 02:28 PM
Need help finding linux equivalent to windows API functions: SHBrowseForFolder....etc mike1 Programming 2 11-11-2004 06:48 AM
Does Linux have API-like functions solartear Programming 8 04-03-2002 12:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:32 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
Open Source Consulting | Domain Registration