diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/Makefile b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/Makefile new file mode 100644 index 0000000..aaf986e --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/Makefile @@ -0,0 +1,4 @@ +all: + gcc -pthread ./getfiles.c -o getfiles-simple ; + gcc -pthread ./getfiles-size.c -o getfiles-size ; + gcc -pthread ./longest_path.c -o get-longest-path ; diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/get-longest-path b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/get-longest-path new file mode 100755 index 0000000..e73d438 Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/get-longest-path differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles-simple b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles-simple new file mode 100755 index 0000000..36dd16c Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles-simple differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles-size b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles-size new file mode 100755 index 0000000..c92688d Binary files /dev/null and b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles-size differ diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles-size.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles-size.c new file mode 100644 index 0000000..6e80a8f --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles-size.c @@ -0,0 +1,110 @@ + +#include +#include + +#include +#include +#include +#include +#include + +int NTHREADS; +int counter; +unsigned long long size=0; +void *listFilesRecursively(void* path); +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; + +int main(int argc,char** argv) { + DIR *d; + struct dirent *dir; + char* path=argv[1]; + /* + d = opendir("node_modules"); + if (d) { + while ((dir = readdir(d)) != NULL) { + if (dir->d_type == DT_DIR) { + printf("D %s\n", dir->d_name); + }else if (dir->d_type == DT_REG) { + printf("F %s\n", dir->d_name); + } + } + closedir(d); + } + */ + listFilesRecursively(path); + printf("count %d\n", counter); + + printf("size %lld\n", size); + return(0); +} + + +int walk_dir(char *dname, char *pattern, int spec) +{ +} + +/** + * Lists all files and sub-directories recursively + * considering path as base path. + */ +void *listFilesRecursively(void *basePath_pass) +{ + char* basePath=(char*) basePath_pass; + char path[1000]; + struct dirent *dp; + DIR *dir = opendir(basePath); + + pthread_t tid[2]; + + int i = 0; + int err; + pthread_t* thread_id=(pthread_t*)malloc(1); + int ret1,ret2; + + NTHREADS = 0; + + // Unable to open directory stream + if (!dir) + return(0); + + while ((dp = readdir(dir)) != NULL) + { + if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) + { + //printf("%s\n", dp->d_name); + if (dp->d_type == DT_DIR) { + printf("D %s\n", dp->d_name); + // Construct new path from our base path + strcpy(path, basePath); + strncat(path, "/",1); + strcat(path, dp->d_name); + + thread_id=(pthread_t*) realloc(thread_id,(NTHREADS+1)*sizeof(pthread_t)); + err = pthread_create(&thread_id[NTHREADS], NULL, listFilesRecursively, path ); + if (err != 0) + printf("\ncan't create thread :[%s]", strerror(err)); + else + printf("\n Thread created successfully\n"); + pthread_join( thread_id[NTHREADS], NULL); + NTHREADS++; + // listFilesRecursively(path); + }else if (dp->d_type == DT_REG) { + printf("F %s\n", dp->d_name); + struct stat st; + stat(basePath, &st); + size += st.st_size; + counter++; + } + + } + } +/* + for(int j=0; j < NTHREADS; j++) + { + pthread_join( thread_id[j], NULL); + } +*/ + closedir(dir); + //printf("count %d\n", counter); +} + diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles.c new file mode 100644 index 0000000..0410cb1 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/getfiles.c @@ -0,0 +1,103 @@ + +#include +#include + +#include +#include +#include +#include + +int NTHREADS; +int counter; +void *listFilesRecursively(void* path); +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; + +int main(int argc,char** argv) { + DIR *d; + struct dirent *dir; + char* path=argv[1]; + /* + d = opendir("node_modules"); + if (d) { + while ((dir = readdir(d)) != NULL) { + if (dir->d_type == DT_DIR) { + printf("D %s\n", dir->d_name); + }else if (dir->d_type == DT_REG) { + printf("F %s\n", dir->d_name); + } + } + closedir(d); + } + */ + listFilesRecursively(path); + printf("count %d\n", counter); + return(0); +} + + +int walk_dir(char *dname, char *pattern, int spec) +{ +} + +/** + * Lists all files and sub-directories recursively + * considering path as base path. + */ +void *listFilesRecursively(void *basePath_pass) +{ + char* basePath=(char*) basePath_pass; + char path[1000]; + struct dirent *dp; + DIR *dir = opendir(basePath); + + pthread_t tid[2]; + + int i = 0; + int err; + pthread_t* thread_id=(pthread_t*)malloc(1); + int ret1,ret2; + + NTHREADS = 0; + + // Unable to open directory stream + if (!dir) + return(0); + + while ((dp = readdir(dir)) != NULL) + { + if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) + { + //printf("%s\n", dp->d_name); + if (dp->d_type == DT_DIR) { + printf("D %s\n", dp->d_name); + // Construct new path from our base path + strcpy(path, basePath); + strncat(path, "/",1); + strcat(path, dp->d_name); + + thread_id=(pthread_t*) realloc(thread_id,(NTHREADS+1)*sizeof(pthread_t)); + err = pthread_create(&thread_id[NTHREADS], NULL, listFilesRecursively, path ); + if (err != 0) + printf("\ncan't create thread :[%s]", strerror(err)); + else + printf("\n Thread created successfully\n"); + pthread_join( thread_id[NTHREADS], NULL); + NTHREADS++; + // listFilesRecursively(path); + }else if (dp->d_type == DT_REG) { + printf("F %s\n", dp->d_name); + counter++; + } + + } + } +/* + for(int j=0; j < NTHREADS; j++) + { + pthread_join( thread_id[j], NULL); + } +*/ + closedir(dir); + //printf("count %d\n", counter); +} + diff --git a/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/longest_path.c b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/longest_path.c new file mode 100644 index 0000000..638a9f9 --- /dev/null +++ b/install/usr/share/swarmlab.io/sec/project/courses/pthread/getfiles/longest_path.c @@ -0,0 +1,120 @@ + +#include +#include + +#include +#include +#include +#include +#include +#define PATH_MAX 4096 + +int NTHREADS; +int counter; +//ftiaxnoume metavliti gia to megalytero path me megethos oso to max path size poy sto linux einai 4096 +char longest_path[PATH_MAX+1]; +//ftiaxnoume metavliti gia na apothikeuei to megethos tona arxeion +unsigned long long size=0; +void *listFilesRecursively(void* path); +//kanoume initialize to mutex gia na kleidosoume tous workers +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; + +int main(int argc,char** argv) { + DIR *d; + struct dirent *dir; + //diavazoume to arxeio san parametro + //prosoxh einai sth thesi 1 kai oxi 0 + char* path=argv[1]; + listFilesRecursively(path); + printf("count %d\n", counter); + + printf("size %lld\n", size); + + printf("longest path is %s\n",longest_path); + return(0); +} + + +int walk_dir(char *dname, char *pattern, int spec) +{ +} + +/** + * Lists all files and sub-directories recursively + * considering path as base path. + */ +void *listFilesRecursively(void *basePath_pass) +{ + char* basePath=(char*) basePath_pass; + char path[1000]; + struct dirent *dp; + DIR *dir = opendir(basePath); + + pthread_t tid[2]; + + int i = 0; + int err; + pthread_t* thread_id=(pthread_t*)malloc(1); + pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; + int ret1,ret2; + + NTHREADS = 0; + + // Unable to open directory stream + if (!dir) + return(0); + + //diavazoume oso yparxoyn katalogoi + while ((dp = readdir(dir)) != NULL) + { + if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) + { + if (dp->d_type == DT_DIR) { + printf("D %s\n", dp->d_name); + // Construct new path from our base path + printf("path %s\n",path); + strcpy(path, basePath); + printf("path %s\n",path); + strncat(path, "/",1); + printf("path %s\n",path); + strcat(path, dp->d_name); + printf("path %s\n",path); + + + printf("path %s\n",path); + printf("base path %s\n",basePath); + + thread_id=(pthread_t*) realloc(thread_id,(NTHREADS+1)*sizeof(pthread_t)); + err = pthread_create(&thread_id[NTHREADS], NULL, listFilesRecursively, path ); + if (err != 0) + printf("\ncan't create thread :[%s]", strerror(err)); + else + printf("\n Thread created successfully\n"); + pthread_join( thread_id[NTHREADS], NULL); + NTHREADS++; + }else if (dp->d_type == DT_REG) { + printf("F %s\n", dp->d_name); + //kaloume stat gia na paroyme to size + struct stat st; + stat(basePath, &st); + //prosthtoume to size sthn global metavliti + size += st.st_size; + + //kleidonoyme prin tin sygrisi gia na apofygoyme lathi + pthread_mutex_lock( &mutex1 ); + //sygrinoyme to current path me to longest path + //kai an einai megalytero to allazoyme + if(strlen(longest_path)<=strlen(basePath)) + strcpy(longest_path,path); + //anoigoyme pali to mutex gia na mpoyn oi alloi + pthread_mutex_unlock( &mutex1 ); + + + counter++; + } + + } + } + closedir(dir); +} +