diff options
-rw-r--r-- | dirwatch.c | 96 |
1 files changed, 78 insertions, 18 deletions
@@ -8,11 +8,10 @@ #include <sys/types.h> #include <limits.h> - -#define WATCH_DIR "/tmp" - #define BUF_SIZE (10 * (sizeof(struct inotify_event) + NAME_MAX + 1)) +int debug=0; + char *path2name( char *s ) { int i; @@ -23,7 +22,6 @@ char *path2name( char *s ) if ( s == NULL ) return NULL; - i = len; while ( i > 0 ) { @@ -66,7 +64,8 @@ void ftp_curl_upload( char *fpath, char *fevent, const char *u ) //printf("%s\n", path2name(f)); snprintf(buf, buf_size,"curl -T %s %s%s\n", fpath, u, fevent); system( buf ); - printf( "%s", buf ); + if (debug) + printf( "%s", buf ); } //takes str1+str2 and create new string with str1+str2 @@ -115,25 +114,85 @@ int main( int argc, char **argv ) char *p; ssize_t num_read; pid_t pid; + int daemonise=1; + //options to ge from argv char *watch_dir=NULL; char *send_url=NULL; - if ( argc != 3 ) + FILE *pid_f=NULL; + + //getopt stuff + int c; + + opterr = 0; + + while ( (c = getopt(argc, argv, "w:u:fd")) != -1 ) { - printf("%s [DIR_TO_WATCH] [CURL_URL_TO_UPLOAD]\n",argv[0]); - return 0; + switch ( c ) + { + //dir path to watch + case 'w': + watch_dir = optarg; + break; + //url to upload with curl + case 'u': + send_url = optarg; + break; + //dont daemonise + case 'f': + daemonise = 0; + break; + //debuging add some extra stuff to print + case 'd': + debug = 1; + break; + case '?': + printf("Run: %s [OPTIONS] ... \n", argv[0]); + printf("-w [DIR] watch dir\n"); + printf("-u [URL] url to upload\n"); + printf("-f foreground mode\n"); + printf("-d debug info\n"); + printf("-? show help\n"); + return -1; + break; + case ':': + printf("Missing options be carefull\n"); + break; + default: + printf("Unknown option\n"); + return -1; + } } - watch_dir = argv[1]; - send_url = argv[2]; + if ( watch_dir == NULL ) + { + printf("Need -w option\n"); + return -1; + } + if ( send_url == NULL ) + { + printf("Needed -u option\n"); + return -1; + } //make process daemon - if ( daemon(1,0) < -1 ) + if ( daemonise == 1 ) { - printf("Cannot become daemon\n"); - return -1; + if ( access( "/tmp/dirwatch.pid", F_OK ) != -1 ) + { + printf("PID file allready excists kill da process or rm file\n "); + return -1; + } + if ( daemon(1,0) < -1 ) + { + printf("Cannot become daemon\n"); + return -1; + } + pid_f = fopen("/tmp/dirwatch.pid","w"); + fprintf( pid_f, "%d", getpid() ); + fclose( pid_f ); } notify_fd = inotify_init(); @@ -144,10 +203,10 @@ int main( int argc, char **argv ) } //register dir to watch - fret = inotify_add_watch( notify_fd, watch_dir, IN_CREATE ); + fret = inotify_add_watch( notify_fd, watch_dir, IN_CREATE|IN_MOVED_TO ); if ( fret == -1 ) { - printf("Cannot add inotify watch\n"); + printf("Cannot add inotify watch %s\n", watch_dir); return -1; } @@ -171,11 +230,13 @@ int main( int argc, char **argv ) event = (struct inotify_event *)p; //if someone added file notify that - if (event->mask & IN_CREATE) + if ((event->mask & IN_CREATE) || + (event->mask & IN_MOVED_TO) ) { if ( !(event->mask & IN_ISDIR) ) { - printf("CREATE_FILE %s \n", event->name); + if (debug) + printf("CREATE_FILE|MOVED_TO %s \n", event->name); char *fpath = strncat_n( 1024, watch_dir, event->name ); if ( fpath != NULL ) ftp_curl_upload( fpath, event->name, send_url ); @@ -187,7 +248,6 @@ int main( int argc, char **argv ) } } - close( notify_fd ); return 0; |