Detect when a new file has been created

Asked

Viewed 89 times

1

Details: I have an app where:

  1. I have an AJAX request with Jquery triggered every time new users are entered passing the data needed for a PHP file that receives them via POST and creates a JSON file with these dice.

  2. The JSON file data will be interpreted with C language using libjson so that they are then stored in the database sqlite.

Question: How to make the C language "wait" and create a warning when this new file is generated so that another C function can automatically grab it and play in the second process?

  • 2

    In C language you can’t do that efficiently. What you can do 'and use other new file detection mechanisms and, with these mechanisms, run a compiled C program. I think your best solution is to do the PHP that receives the POST run the pre-compiled program.

  • 3

    if you are on Linux, this may help you: (http://stackoverflow.com/questions/18692134/continuously-monitor-a-directory-in-linux-and-notify-when-a-new-file-is-availabl) or (http://unix.stackexchange.com/questions/24952/script-monitor-folder-new-files)

  • Because the database has to be accessed with the C language?

  • @MYS, the site is also being written in C language with the help of Mongoose, the database as well. (Language is a requirement of those who requested the project for its efficiency when it comes to embedded software)

2 answers

3

I’ll assume you’re dealing with the environment linux. Here is a very interesting implementation for this problem: File Events Notification

Basically a thread is created to monitor a directory while a running process keeps it alive. Note that the thread can "hear" several different states. Being these:

  • FILE_ACCESS: File/Directory monitored was accessed
  • FILE_MODIFIED: File/Directory monitored has been modified
  • FILE_ATTRIB: Monitored file/directory had a modified attribute
  • FILE_NOFOLLOW: Do not follow symmetrical links
  • FILE_DELETE: File/Directory has been deleted
  • FILE_RENAME_TO: File/Directory has been renamed
  • FILE_RENAME_FROM: File/Directory has been renamed
  • UNMOUNTED: File/Directory has been disassembled
  • MOUNTEDOVER: File/Directory has been mounted

I believe these events already solve your case. In the code the treatment for each event was via if and else, but I suggest that a switch to facilitate maintainability.

  • Rhuan Karlus, interesting: I didn’t know this library (+1). I’ll take a look.

2

If it is in linux: in these situations I usually use the inotify library. There is inotify interface for various programming languages. (see man inotify for information on the C interface).

Below is an example in Perl that waits for someone to create a file in the "Dir" folder and calls a command (for example your database tidying program) when this happens.

#!/usr/bin/perl

 use Linux::Inotify2;

 my $inotify = new Linux::Inotify2 or die "unable to inotify: $!";

 $inotify->watch ("Dir", IN_CREATE, # ... IN_MODIFY,
    sub { my $e = shift;
          my $name = $e->fullname;
          system("ls -la '$name'");             ##  ==> chamar o teu programa C
          print "$name was created\n" if $e->IN_CREATE; }
    );

 1 while $inotify->poll;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.