Skip to content

Instantly share code, notes, and snippets.

@StefanCristian
Last active August 29, 2015 14:04
Show Gist options
  • Save StefanCristian/7aa34f31ec012bc2694a to your computer and use it in GitHub Desktop.
Save StefanCristian/7aa34f31ec012bc2694a to your computer and use it in GitHub Desktop.
A inotify libs testcase
/*Licensed under Creative Commons 0 Public Domain (CC0)*/
/*There is no Copyright nor author, it's a general and assembled public work*/
/*AS the License states, any other mixation, redistribution, and so forth of this work will not have any author.*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/inotify.h>
#include <dirent.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int main( char * argv[] )
{
int length, i = 0;
int FF;
int WW;
char buffer[EVENT_BUF_LEN];
FF = inotify_init();
if ( FF < 0 ) {
perror("inotify_init");
}
WW = inotify_add_watch( FF, "/tmp", IN_CREATE | IN_DELETE );
length = read( FF, buffer, EVENT_BUF_LEN );
if ( length != 0 ) {
printf("Event number %d has happened, program is going to boom\n", length);
}
if ( length < 0 ) {
perror( "read" );
}
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
if ( event->mask & IN_CREATE ) {
if ( event->mask & IN_ISDIR ) {
printf( "New directory %s created.\n", event->name );
}
else {
printf( "New file %s created.\n", event->name );
#define EMP 1024
char InitialDir[] = "/tmp";
char SNewFILE[EMP] = {0};
sprintf(SNewFILE, "%s/%s", InitialDir, "regularfile");
struct stat st_buf;
stat(SNewFILE, &st_buf);
if (S_ISDIR(st_buf.st_mode))
{
printf("All okay, go on\n");
continue;
} else {
printf("Not okay\n");
}
if (S_ISREG (st_buf.st_mode)) {
printf("Indeed, it`s regular\n");
} else {
printf("Why dafuck it isn`t.\n");
}
char InitialFile[EMP] = {0};
sprintf(InitialFile,"%s/%s", InitialDir, "regularfile");
int src_fd, dst_fd;
DIR *d;
struct dirent *dir;
d = opendir("/tmp");
char *dst_path;
char *src_path;
char DestPath[] = "/home/work/utilfiles";
if (d) {
while ((dir = readdir(d)) != NULL) {
printf("There is a flaw %s\n", dir->d_name);
}
} else {
printf("Big FAILURE\n");
}
FILE* RReadFile = fopen(InitialFile,"r");
if (RReadFile) {
char DestFileName[EMP] = {0};
sprintf(DestFileName, "%s/%s", DestPath, "regularfile");
FILE* DWritenFile = fopen(DestFileName, "w");
if (DWritenFile) {
char buffer[EMP] = {0};
while(fgets(buffer, EMP, RReadFile)) {
fputs(buffer, DWritenFile);
}
fclose(DWritenFile);
} else {
printf("Does not work bud\n");
}
fclose(RReadFile);
} else {
printf("Doesnt work neither\n");
}
closedir(d);
}
}
else if ( event->mask & IN_DELETE ) {
if ( event->mask & IN_ISDIR ) {
printf( "Directory %s deleted.\n", event->name );
}
else {
printf( "File %s deleted.\n", event->name );
}
}
}
i += EVENT_SIZE + event->len;
}
inotify_rm_watch( FF, WW );
close( FF );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment