From 87496d169aae0a7e5200d3c2a93cc8fe7af86f00 Mon Sep 17 00:00:00 2001 From: Bill Gates Date: Sun, 17 Mar 2024 22:32:16 +0000 Subject: [PATCH 1/2] rewrite... again --- dev/test.c | 10 ++++ src/list.c | 166 +++++++++++++++++++++++------------------------------ 2 files changed, 81 insertions(+), 95 deletions(-) diff --git a/dev/test.c b/dev/test.c index 30dd0be..25be9f6 100644 --- a/dev/test.c +++ b/dev/test.c @@ -102,6 +102,10 @@ int main(int argc, char const *argv[]) return test_config(); } else if (strcmp(argv[1], "get") == 0) { return test_get(); + } else if (strcmp(argv[1], "list") == 0) { + return list_installed(); + } else if (strcmp(argv[1], "count") == 0) { + return count(); } else { printf("Invalid argument\n"); return 1; @@ -434,3 +438,9 @@ char* assemble(char** list,int count) strcat(string,list[i]); return string; } +int list() { + list_installed(); +} +int count() { + count_installed(); +} \ No newline at end of file diff --git a/src/list.c b/src/list.c index 4b96935..de99214 100644 --- a/src/list.c +++ b/src/list.c @@ -2,114 +2,90 @@ #include #include #include -#include "sqlite3.h" // SQLite database library - -// Include necessary headers -#include "libspm.h" -#include "cutils.h" - -//should probably add there to the header when we are done - -//will print the content of INSTALLED_DB -int list_installed() -{ - dbg(2, "listing installed packages from %s", getenv("INSTALLED_DB")); +#include +#include + +#define MAX_PATH_LENGTH 1024 + +char **getAllFiles(const char *path, int *num_files) { + char **files_array = NULL; + int file_count = 0; + + DIR *dir; + struct dirent *entry; + struct stat stat_buf; + + if (!(dir = opendir(path))) + return NULL; + + while ((entry = readdir(dir)) != NULL) { + if (entry->d_type == DT_DIR) { + char next_path[MAX_PATH_LENGTH]; + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + continue; + snprintf(next_path, sizeof(next_path), "%s/%s", path, entry->d_name); + getAllFiles(next_path, num_files); + } else { + char full_path[MAX_PATH_LENGTH]; + snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name); + if (stat(full_path, &stat_buf) == 0 && S_ISREG(stat_buf.st_mode)) { + files_array = (char **)realloc(files_array, (file_count + 1) * sizeof(char *)); + files_array[file_count] = (char *)malloc(MAX_PATH_LENGTH * sizeof(char)); + // Extract the last directory name from the path + char *last_dir = strrchr(path, '/'); + if (last_dir != NULL) + last_dir++; // Move past the '/' + else + last_dir = (char *)path; // No '/' found, use the path itself + snprintf(files_array[file_count], MAX_PATH_LENGTH, "%s/%s", last_dir, entry->d_name); + file_count++; + } + } + } + closedir(dir); - //shame that print_all_data uses msg, this could have been so clean - sqlite3_stmt *stmt; - char *zErrMsg = 0; - int rc; + if (num_files != NULL) + *num_files = file_count; - // Prepare the SQL query - const char *sql = "SELECT Name, Version, Type FROM Packages"; - rc = sqlite3_prepare_v2(INSTALLED_DB, sql, -1, &stmt, NULL); - if (rc != SQLITE_OK) { - msg(ERROR, "SQL error: %s", zErrMsg); // compiler doesn't complain about this but it might be bad - sqlite3_free(zErrMsg); - return 1; - } + if (file_count == 0) + return NULL; - // Execute the SQL query - while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { - printf("\x1b[31;1;1m %s \x1b[0m %s - %s \n", sqlite3_column_text(stmt, 0), sqlite3_column_text(stmt, 1), sqlite3_column_text(stmt, 2)); - } + return files_array; +} - // Check if the SQL query was successful - if (rc != SQLITE_DONE) { - msg(ERROR, "SQL error: %s", sqlite3_errmsg(INSTALLED_DB)); - return -1; +int count_installed(char *basePath) { + int file_count = 0; + DIR * dirp; + struct dirent * entry; + while ((entry = readdir(basePath)) != NULL) { + if (entry->d_type == DT_REG) { /* If the entry is a regular file */ + file_count++; + } } - - dbg(2, "%d packages installed", count_installed()); - return 0; + closedir(basePath); } -//count installed -int count_installed() -{ - int count = 0; - sqlite3_stmt *stmt; - int rc; - - // Prepare the SQL query - const char *sql = "SELECT COUNT(*) FROM Packages"; - rc = sqlite3_prepare_v2(INSTALLED_DB, sql, -1, &stmt, NULL); - if (rc != SQLITE_OK) { - msg(ERROR, "SQL error: %s", sqlite3_errmsg(INSTALLED_DB)); - return 1; - } - // Execute the SQL query - while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { - count = (int)sqlite3_column_int(stmt, 0); - } +int list_installed(const char *path) { + int count = 0; + DIR *dir; + struct dirent *entry; - // Check if the SQL query was successful - if (rc != SQLITE_DONE) { - msg(ERROR, "Error executing statement: %s\n", sqlite3_errmsg(INSTALLED_DB)); + // Open directory + if ((dir = opendir(path)) == NULL) { + perror("opendir"); return -1; } - - return count; -} - -int search(char* in) -{ - msg(INFO, "searching for %s", in); - - sqlite3_stmt *stmt; - int rc; - int _found = 0; - - // Prepare the SQL query - const char *sql = "SELECT Name, Section FROM Packages"; - rc = sqlite3_prepare_v2(ALL_DB, sql, -1, &stmt, NULL); - if (rc != SQLITE_OK) { - msg(ERROR, "SQL error: %s", sqlite3_errmsg(ALL_DB)); - return 1; - } - - // Execute the SQL query - while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) { - struct package* remote = calloc(1, sizeof(struct package)); - remote->name = (char*)sqlite3_column_text(stmt, 0); - - if(strstr(remote->name, in) != 0) - { - printf("found \x1b[31;1;1m %s \x1b[0m in %s \n", remote->name, (char*)sqlite3_column_text(stmt, 1)); - _found++; + // Iterate through directory entries + while ((entry = readdir(dir)) != NULL) { + // Ignore current directory and parent directory entries + if (entry->d_type == DT_REG) { + count++; } - free(remote); - } - - // Check if the SQL query was successful - if (rc != SQLITE_DONE) { - msg(ERROR, "SQL error: %s", sqlite3_errmsg(ALL_DB)); - return -1; } - msg(WARNING, "found %d packages that match %s", _found, in); - + closedir(dir); + printf("%s", count); return 0; -} +} \ No newline at end of file From 15ea58ce043a154967d82e0301f25bda1bba9f81 Mon Sep 17 00:00:00 2001 From: Bill Gates Date: Mon, 18 Mar 2024 08:50:06 +0000 Subject: [PATCH 2/2] IT WORKS --- dev/test.c | 2 +- src/list.c | 65 +++++++++++++++++++++++++++++++++--------------------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/dev/test.c b/dev/test.c index 25be9f6..d9ba6e1 100644 --- a/dev/test.c +++ b/dev/test.c @@ -442,5 +442,5 @@ int list() { list_installed(); } int count() { - count_installed(); + printf("%d", count_installed()); } \ No newline at end of file diff --git a/src/list.c b/src/list.c index de99214..277923d 100644 --- a/src/list.c +++ b/src/list.c @@ -4,8 +4,11 @@ #include #include #include +#include #define MAX_PATH_LENGTH 1024 +#define OPEN_ERROR -1 +#define READ_ERROR -2 // You can define appropriate error codes char **getAllFiles(const char *path, int *num_files) { char **files_array = NULL; @@ -53,39 +56,51 @@ char **getAllFiles(const char *path, int *num_files) { return files_array; } -int count_installed(char *basePath) { - int file_count = 0; - DIR * dirp; - struct dirent * entry; - while ((entry = readdir(basePath)) != NULL) { - if (entry->d_type == DT_REG) { /* If the entry is a regular file */ - file_count++; +int count_installed(const char *directory) { + DIR *dirp; + struct dirent *dp; + int fileCount = 0; + + dirp = opendir(directory); + if (dirp == NULL) { + perror("Error opening directory"); + return OPEN_ERROR; + } + + while ((dp = readdir(dirp)) != NULL) { + if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) { + fileCount++; } } - closedir(basePath); + + if (errno != 0) { + perror("Error reading directory"); + closedir(dirp); + return READ_ERROR; + } + + closedir(dirp); + return fileCount; } -int list_installed(const char *path) { +int list_installed() { + char* path = "/var/cccp/data/spm"; + DIR *d; + struct dirent *dir; int count = 0; - DIR *dir; - struct dirent *entry; - - // Open directory - if ((dir = opendir(path)) == NULL) { - perror("opendir"); - return -1; - } + d = opendir(path); - // Iterate through directory entries - while ((entry = readdir(dir)) != NULL) { - // Ignore current directory and parent directory entries - if (entry->d_type == DT_REG) { - count++; + if (d) { + while ((dir = readdir(d)) != NULL) { + if (dir->d_type == DT_REG) // Check if it's a regular file + count++; } + closedir(d); + } else { + printf("Error: Unable to open directory %s\n", path); + return -1; // Return -1 to indicate an error } - closedir(dir); - printf("%s", count); - return 0; + return count; } \ No newline at end of file