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

added list, update and upgrade #27

Merged
merged 2 commits into from
Nov 1, 2023
Merged
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
3 changes: 2 additions & 1 deletion include/libspm.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ struct package
//test
int list_installed();
int count_installed();
int get_installed(int index);
int update();
int upgrade();

//end test

Expand Down
2 changes: 1 addition & 1 deletion src/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ char* get(struct package* i_pkg, const char* out_path)
void sync()
{
// Download the "all.db" file to the specified path
downloadRepo("all.db", getenv("SOVIET_ALL_DB_PATH"));
downloadRepo("all.db", getenv("ALL_DB"));
}
110 changes: 59 additions & 51 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,72 @@

//will print the content of INSTALLED_DB
int list_installed()
{
msg(INFO, "listing installed packages from %s", getenv("INSTALLED_DB"));
if(0 == 1)
{
sqlite3_stmt *stmt;
int rc;
{
msg(INFO, "listing installed packages from %s", getenv("INSTALLED_DB"));

// Prepare the SQL query
const char *sql = "idfk";
rc = sqlite3_prepare_v2(getenv(INSTALLED_DB), sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
msg(ERROR, "SQL error: %s -- %d", sqlite3_errmsg(INSTALLED_DB), rc);
return 1;
}
//shame that print_all_data uses msg, this could have been so clean
sqlite3_stmt *stmt;
char *zErrMsg = 0;
int rc;

// Bind the value for the parameter in the SQL query
sqlite3_bind_text(stmt, 1, "idk", -1, SQLITE_STATIC);

// Execute the PRINT statement or smth.
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
msg(ERROR, "Error executing PRINT statement or smth: %s\n", sqlite3_errmsg(INSTALLED_DB));
return -1;
}

// Finalize the statement.
rc = sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
msg(ERROR, "Error finalizing PRINT statement or smth: %s\n", sqlite3_errmsg(INSTALLED_DB));
return -1;
}
// 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);
sqlite3_free(zErrMsg);
return 1;
}

return 0;
}
// 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));
}

// Check if the SQL query was successful
if (rc != SQLITE_DONE) {
fprintf(stderr, "SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
return -1;
}

//will return the ammount of packages installed INSTALLED_DB
msg(INFO, "%d packages installed", count_installed());
return 0;
}

//count installed
int count_installed()
{
int count = 1;

// do sql magic here

msg(INFO, "there are %d installed packages", count);

return count;
}
{
int count;

//will print the content of INSTALLED_DB at a specific index
int get_installed(int index)
{
int count = 0;
char* out = "test";
//out = sql magic

msg(INFO, "%s is installed at %d", out, index);

return count;
sqlite3_stmt *stmt;
char *zErrMsg = 0;
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", zErrMsg);
sqlite3_free(zErrMsg);
return 1;
}
// Execute the SQL query
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
count = (int)sqlite3_column_int(stmt, 0);
}

if (rc != SQLITE_DONE) {
msg(ERROR, "Error executing statement: %s\n", sqlite3_errmsg(INSTALLED_DB));
return -1;
}
// Check if the SQL query was successful
if (rc != SQLITE_DONE) {
fprintf(stderr, "SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
return -1;
}

return count;
}

139 changes: 139 additions & 0 deletions src/update.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include <stdio.h>
#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 update()
{
msg(INFO, "fetching updates");

sqlite3_stmt *stmt;
char *zErrMsg = 0;
int rc;
int new_version_found = 0;

// Prepare the SQL query
const char *sql = "SELECT Name, Version FROM Packages";
rc = sqlite3_prepare_v2(INSTALLED_DB, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
msg(ERROR, "SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
return 1;
}

// Execute the SQL query
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
struct package* local = calloc(1, sizeof(struct package));
struct package* remote = calloc(1, sizeof(struct package));
local->name = (char*)sqlite3_column_text(stmt, 0);
local->version = (char*)sqlite3_column_text(stmt, 1);
msg(ERROR, "don't ask why this is here");
remote->name = local->name;
retrieve_data_repo(ALL_DB, remote);
if(remote->version == NULL)
{
msg(ERROR, "No package %s exists in repo", local->name);
}
else
{
if(strcmp(local->version, remote->version) != 0)
{
msg(INFO, "package %s is at version %s, available version is %s", local->name, local->version, remote->version);
new_version_found = 1;
}
}
free(local);
free(remote);
}

// Check if the SQL query was successful
if (rc != SQLITE_DONE) {
fprintf(stderr, "SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
return -1;
}
if(new_version_found != 0)
{
msg(WARNING, "new version found for one or more packages, use --upgrade to upgrade");
}
else
{
msg(WARNING, "all packages are up to date");
}

return 0;
}

int upgrade()
{
msg(INFO, "upgrading");

sqlite3_stmt *stmt;
char *zErrMsg = 0;
int rc;
int new_version_installed = 0;

// Prepare the SQL query
const char *sql = "SELECT Name, Version FROM Packages";
rc = sqlite3_prepare_v2(INSTALLED_DB, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
msg(ERROR, "SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
return 1;
}

// Execute the SQL query
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
struct package* local = calloc(1, sizeof(struct package));
struct package* remote = calloc(1, sizeof(struct package));
local->name = (char*)sqlite3_column_text(stmt, 0);
local->version = (char*)sqlite3_column_text(stmt, 1);
msg(ERROR, "don't ask why this is here");
remote->name = local->name;
retrieve_data_repo(ALL_DB, remote);
if(remote->version == NULL)
{
msg(ERROR, "No package %s exists in repo", local->name);
}
else
{
if(strcmp(local->version, remote->version) != 0)
{
msg(INFO, "upgrading %s from %s to %s", local->name, local->version, remote->version);
uninstall(local->name);
char* format = get(local, local->name);

if (format == NULL) {
msg(ERROR, "Failed to download package %s", local->name);
return 1;
}

f_install_package_source(local->name, 0, format);
new_version_installed = 1;
}
}
free(local);
free(remote);
}

// Check if the SQL query was successful
if (rc != SQLITE_DONE) {
fprintf(stderr, "SQL error: %s", zErrMsg);
sqlite3_free(zErrMsg);
return -1;
}
if(new_version_installed == 0)
{
msg(WARNING, "all packages are up to date");
}

return 0;
}