#include #include #include #include #include #include #include #define BUFFERSIZE 4096 char READBUFFER[BUFFERSIZE]; #include int TraversePath(char *path) { struct dirent *d = NULL; DIR *dir = NULL; /* pointer to directory head*/ char buf[255]={0}; /* buffer to store the complete file/dir name*/ struct stat statbuf; /* to obtain the statistics of file/dir */ int retval =0; /* to hold the return value*/ int fd = 0; memset(&statbuf,0,sizeof(struct stat)); retval = stat(path,&statbuf); /* if the stat returned success and path provided is of valid directory*/ if(S_ISDIR(statbuf.st_mode) && (retval==0)) { dir = opendir(path); /* open the directory*/ /* reads entry one by one*/ while((d = readdir(dir)) != NULL) { if((strcmp(d->d_name,".")!=0) && (strcmp(d->d_name,"..")!=0)) { sprintf(buf,"%s/%s",path,d->d_name); retval = stat(buf,&statbuf); if(retval == 0) { if(!S_ISDIR(statbuf.st_mode)) { /* This is file - read from this, Since read ahead * will itself bring 128KB, so we can just read 4KB * to start with */ fd = open(buf,O_RDONLY,(mode_t)777); if(fd) { read(fd, READBUFFER, BUFFERSIZE); close(fd); } } else { /* This is a directory, recursive search in it once all files are read */ TraversePath(buf); } } else { perror("stat failed\n"); } } } } else { perror("Failed"); } return retval; } int main(int argc, char **argv) { struct timeval rv; struct timeval rv1; int stat_time = 0; if(argc < 2) { printf("./TraversePath \n"); return 0; } //Traverse the complete path inside timing unit gettimeofday(&rv, 0); TraversePath(argv[1]); gettimeofday(&rv1, 0); stat_time = (rv1.tv_sec * 1000 + rv1.tv_usec / 1000) - (rv.tv_sec * 1000 + rv.tv_usec / 1000); printf(" Traversed Path : %s \n", argv[1]); printf(" Time Taken : %d msec \n",stat_time); return 0; }