From 6ee347da12f77c21b4076ba5e667db0a7b6098b2 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 3 Jul 2024 20:25:42 +0900 Subject: [PATCH] Handle symbolic links separately Previous implementation could loop forever following a symbolic link that goes backwards. This commit makes symbolic links an "unhandled file type" for now. --- src/parser.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/parser.c b/src/parser.c index ffaba0c..b54bcdd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -23,8 +23,10 @@ #include #include "platforms.h" -#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX +#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX || \ + SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN #include +#include #include #include #endif @@ -252,8 +254,10 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames( SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX for (char **iter = parsed->working_files; iter && *iter; ++iter) { struct stat st; - stat(*iter, &st); - if ((st.st_mode & S_IFMT) == S_IFREG) { + fstatat(AT_FDCWD, *iter, &st, AT_SYMLINK_NOFOLLOW); + if ((st.st_mode & S_IFMT) == S_IFLNK) { + // Is a symbolic link. TODO handle this. + } else if ((st.st_mode & S_IFMT) == S_IFREG) { // Is a regular file. int len = strlen(*iter) + 1; char *filename = malloc(len); @@ -291,8 +295,10 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames( char *combined_path = malloc(combined_size); snprintf(combined_path, combined_size, "%s/%s", next, dir_entry->d_name); - stat(combined_path, &st); - if ((st.st_mode & S_IFMT) == S_IFREG) { + fstatat(AT_FDCWD, combined_path, &st, AT_SYMLINK_NOFOLLOW); + if ((st.st_mode & S_IFMT) == S_IFLNK) { + // Is a symbolic link. TODO handle this. + } else if ((st.st_mode & S_IFMT) == S_IFREG) { // Is a file. if (simple_archiver_hash_map_get(hash_map, combined_path, combined_size - 1) == NULL) { @@ -308,7 +314,7 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames( // Is a directory. simple_archiver_list_add_front(dir_list, combined_path, NULL); } else { - // Unhandled type. + // Unhandled type. TODO handle this. free(combined_path); } } @@ -320,7 +326,7 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames( } } } else { - // Unhandled type. + // Unhandled type. TODO handle this. } } #endif