aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2015-04-06 18:44:21 +0900
committerFreeArtMan <dos21h@gmail.com>2015-04-06 18:44:21 +0900
commit665719f12710b7c541a95fa9bc578f9c662c52a7 (patch)
tree3284d9dcd08bdc6c15728addb4bb37dea8936155
parent5154b6843e35c8b8de35a04146bfec22dc6912dc (diff)
downloaddirwatch-665719f12710b7c541a95fa9bc578f9c662c52a7.tar.gz
dirwatch-665719f12710b7c541a95fa9bc578f9c662c52a7.zip
Initial commit
-rw-r--r--.gitignore1
-rw-r--r--Makefile2
-rw-r--r--dirwatch.c194
3 files changed, 197 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0e79848
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+dirwatch
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..34ad5aa
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,2 @@
+make:
+ gcc dirwatch.c -o dirwartch \ No newline at end of file
diff --git a/dirwatch.c b/dirwatch.c
new file mode 100644
index 0000000..4671d4d
--- /dev/null
+++ b/dirwatch.c
@@ -0,0 +1,194 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/inotify.h>
+#include <sys/types.h>
+#include <limits.h>
+
+
+#define WATCH_DIR "/tmp"
+
+#define BUF_SIZE (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))
+
+char *path2name( char *s )
+{
+ int i;
+ size_t len = strlen(s);
+ char *p=NULL;
+ char *name=NULL;
+
+ if ( s == NULL )
+ return NULL;
+
+
+ i = len;
+ while ( i > 0 )
+ {
+ if ( s[len-i] == '/' )
+ {
+ p = &s[len-i];
+ break;
+ }
+ i--;
+ }
+ name = malloc( len-i+1 );
+ if ( name == NULL )
+ return NULL;
+ memcpy( name, p, len-i );
+ name[ len-i ] = 0;
+ return name;
+}
+
+// filename with use to upload plain filename
+// url of dir where to upload with ending "/"
+void ftp_curl_upload( char *fpath, char *fevent, const char *u )
+{
+ const int buf_size = 1024;
+ char buf[buf_size];
+ if ( fpath == NULL )
+ {
+ printf("file NULL\n" );
+ return;
+ }
+ if ( fevent == NULL )
+ {
+ printf("file event NULL\n");
+ return;
+ }
+ if ( u == NULL )
+ {
+ printf("url NULL\n");
+ return;
+ }
+ //printf("%s\n", path2name(f));
+ snprintf(buf, buf_size,"curl -T %s %s%s\n", fpath, u, fevent);
+ system( buf );
+ printf( "%s", buf );
+}
+
+//takes str1+str2 and create new string with str1+str2
+//should be freed after
+char *strncat_n( size_t len, const char *str1, const char *str2)
+{
+ int i,j;
+
+ //join all to one nice while loop?
+ size_t s1_l = strlen( str1 );
+ size_t s2_l = strlen( str2 );
+ size_t tot_l = s1_l + s2_l;
+ char *res = NULL;
+ if ( tot_l >= len ) tot_l = len-1;
+
+ res = malloc( tot_l+1 ); memset( res,0,tot_l);
+ if ( res == NULL )
+ return NULL;
+
+ i = 0;
+ while( i < s1_l )
+ {
+ res[i] = str1[i];
+ i++;
+ }
+ printf("%s\n",res);
+
+ j = 0;
+ while ( j <= s2_l )
+ {
+ res[i+j] = str2[j];
+ j++;
+ }
+ printf("%s\n",res);
+ res[tot_l] = 0x0;
+ return res;
+}
+
+int main( int argc, char **argv )
+{
+ int fret;
+ int i; //cycle counter
+ int notify_fd; //inotudy fd
+ char ev_buffer[BUF_SIZE]; //buffer for events
+ struct inotify_event *event;
+ char *p;
+ ssize_t num_read;
+ pid_t pid;
+
+ char *watch_dir=NULL;
+ char *send_url=NULL;
+
+ if ( argc != 3 )
+ {
+ printf("%s [DIR_TO_WATCH] [CURL_URL_TO_UPLOAD]\n",argv[0]);
+ return 0;
+ }
+
+ watch_dir = argv[1];
+ send_url = argv[2];
+
+
+ //make process daemon
+ if ( daemon(1,0) < -1 )
+ {
+ printf("Cannot become daemon\n");
+ return -1;
+ }
+
+ notify_fd = inotify_init();
+ if ( notify_fd == -1 )
+ {
+ printf("Cannot init inotify\n");
+ return -1;
+ }
+
+ //register dir to watch
+ fret = inotify_add_watch( notify_fd, watch_dir, IN_CREATE );
+ if ( fret == -1 )
+ {
+ printf("Cannot add inotify watch\n");
+ return -1;
+ }
+
+ for (;;)
+ {
+ num_read = read( notify_fd, ev_buffer, BUF_SIZE );
+ if ( num_read == 0 )
+ {
+ printf("inotify read 0!\n");
+ break;
+ }
+ if ( num_read == -1 )
+ {
+ printf("error while read inotify\n" );
+ break;
+ }
+
+ for (p = ev_buffer;
+ p < ev_buffer+num_read;)
+ {
+ event = (struct inotify_event *)p;
+
+ //if someone added file notify that
+ if (event->mask & IN_CREATE)
+ {
+ if ( !(event->mask & IN_ISDIR) )
+ {
+ printf("CREATE_FILE %s \n", event->name);
+ char *fpath = strncat_n( 1024, watch_dir, event->name );
+ if ( fpath != NULL )
+ ftp_curl_upload( fpath, event->name, send_url );
+ free( fpath );
+ }
+ }
+
+ p += sizeof( struct inotify_event ) + event->len;
+ }
+ }
+
+
+ close( notify_fd );
+
+ return 0;
+} \ No newline at end of file