Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite list #59

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dev/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
extern int create_spm(char* path,struct package* pkg);
extern int create_ecmp(char* path,struct package* pkg);

char* names[5] = {"pkg1","pkg2","pkg3","pkg4","pkg5"};

Check warning on line 24 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:24:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'names' is non-const and globally accessible, consider making it const
char* versions[5] = {"1.1.0","2.0.8","6.8.7","7.0","5.20"};

Check warning on line 25 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:25:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'versions' is non-const and globally accessible, consider making it const
char* types[5] = {"bin","src","src","bin","src"};

Check warning on line 26 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:26:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'types' is non-const and globally accessible, consider making it const

#define l_d_count 5
#define l_f_count 12
char* l_dirs[l_d_count] = {"b","b/d","s","s/j","s/j/k"};

Check warning on line 30 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:30:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'l_dirs' is non-const and globally accessible, consider making it const
char* l_files[l_f_count] = {"w","b/d/e","a","d","b/y","b/c","b/f","s/j/k/z","s/j/k/x","s/j/k/c","s/j/k/v","s/j/k/b"};

Check warning on line 31 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:31:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'l_files' is non-const and globally accessible, consider making it const

char** list_of_stuff = NULL;

Check warning on line 33 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:33:8 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'list_of_stuff' is non-const and globally accessible, consider making it const

Check warning on line 33 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:33:8 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'list_of_stuff' provides global access to a non-const object; consider making the pointed-to data 'const'
int list_of_stuff_count = 0;

Check warning on line 34 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:34:5 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'list_of_stuff_count' is non-const and globally accessible, consider making it const

int test_data ();
int test_ecmp();
Expand Down Expand Up @@ -70,7 +70,7 @@

if (strcmp(argv[1], "data") == 0) {
return test_data();
} else if (strcmp(argv[1], "ecmp") == 0) {

Check warning on line 73 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:73:7 [readability-else-after-return]

do not use 'else' after 'return'
return test_ecmp();
} else if (strcmp(argv[1], "all") == 0) {
int ret = 0;
Expand All @@ -79,7 +79,7 @@
return ret;
} else if (strcmp(argv[1], "make") == 0) {
int EXIT = 0;
EXIT += test_make(argv[2]);

Check warning on line 82 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:82:27 [clang-diagnostic-incompatible-pointer-types-discards-qualifiers]

passing 'const char *' to parameter of type 'char *' discards qualifiers
printf("Leaks: %d\n", check_leaks());
return EXIT;
}
Expand All @@ -102,6 +102,10 @@
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;
Expand Down Expand Up @@ -434,3 +438,9 @@
strcat(string,list[i]);
return string;
}
int list() {
list_installed();
}
int count() {
printf("%d", count_installed());
}
181 changes: 86 additions & 95 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,105 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#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"));

//shame that print_all_data uses msg, this could have been so clean
sqlite3_stmt *stmt;
char *zErrMsg = 0;
int rc;

// 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;
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>

#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;
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);

// 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));
}
if (num_files != NULL)
*num_files = file_count;

// Check if the SQL query was successful
if (rc != SQLITE_DONE) {
msg(ERROR, "SQL error: %s", sqlite3_errmsg(INSTALLED_DB));
return -1;
}

dbg(2, "%d packages installed", count_installed());
return 0;
if (file_count == 0)
return NULL;

return files_array;
}

//count installed
int count_installed()
{
int count = 0;
int count_installed(const char *directory) {
DIR *dirp;
struct dirent *dp;
int fileCount = 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);
dirp = opendir(directory);
if (dirp == NULL) {
perror("Error opening directory");
return OPEN_ERROR;
}

// Check if the SQL query was successful
if (rc != SQLITE_DONE) {
msg(ERROR, "Error executing statement: %s\n", sqlite3_errmsg(INSTALLED_DB));
return -1;
while ((dp = readdir(dirp)) != NULL) {
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
fileCount++;
}
}

if (errno != 0) {
perror("Error reading directory");
closedir(dirp);
return READ_ERROR;
}

return count;
closedir(dirp);
return fileCount;
}

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++;
}
free(remote);
}

// Check if the SQL query was successful
if (rc != SQLITE_DONE) {
msg(ERROR, "SQL error: %s", sqlite3_errmsg(ALL_DB));
return -1;
}
int list_installed() {
char* path = "/var/cccp/data/spm";
DIR *d;
struct dirent *dir;
int count = 0;
d = opendir(path);

msg(WARNING, "found %d packages that match %s", _found, in);
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
}

return 0;
}
return count;
}
Loading