From f9dc20576ebd347633305b82caf5273ffb88678b Mon Sep 17 00:00:00 2001 From: Bill Gates Date: Sun, 17 Mar 2024 18:58:31 +0000 Subject: [PATCH 1/3] stuff --- include/libspm.h | 9 ++++++ makefile | 2 +- src/get.c | 16 ++++++++-- src/repo.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100755 src/repo.c diff --git a/include/libspm.h b/include/libspm.h index efd69b8..89d5b61 100755 --- a/include/libspm.h +++ b/include/libspm.h @@ -393,6 +393,15 @@ long get_locations(char*** locations, const char* loc_dir); */ int check_dependencies(char ** dependencies,int dependenciesCount); +#define MAX_URL_LENGTH 100 + +typedef struct { + char name[MAX_URL_LENGTH]; + char url[MAX_URL_LENGTH]; +} Repos; + +Repos* read_sources_list(const char* filename, int* num_repos); +void clone_repositories(Repos* repositories, int num_repos, const char* clone_directory); diff --git a/makefile b/makefile index be7feee..bbbe0ac 100755 --- a/makefile +++ b/makefile @@ -29,7 +29,7 @@ SDIR = src -CFLAGS = -Wall -g -fPIC -O2 -Wextra -L./bin -Iinclude +CFLAGS = -Wno-error -Wno-format-security -g -fPIC -O2 -L./bin -Iinclude LIBS = lib/* -lcurl -lsqlite3 -lm diff --git a/src/get.c b/src/get.c index d3d4b1e..fc46d80 100755 --- a/src/get.c +++ b/src/get.c @@ -69,6 +69,18 @@ char* get(struct package* i_pkg, const char* out_path) // Function to synchronize the local repository with a remote repository void sync() { - // Download the "all.db" file to the specified path - downloadRepo("all.db", getenv("ALL_DB")); + const char* filename = "/var/cccp/data/sources.list"; + int num_repos; + Repos* repositories = read_sources_list(filename, &num_repos); + if (repositories != NULL) { + char* clone_directory = getenv("SOVIET_REPOS"); + if (clone_directory == NULL) { + fprintf(stderr, "SOVIET_REPOS environment variable not set\n"); + free(repositories); + return EXIT_FAILURE; + } + clone_repositories(repositories, num_repos, clone_directory); + free(repositories); + } + return 0; } diff --git a/src/repo.c b/src/repo.c new file mode 100755 index 0000000..e2cbda1 --- /dev/null +++ b/src/repo.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include + +#define MAX_URL_LENGTH 100 + +typedef struct { + char name[MAX_URL_LENGTH]; + char url[MAX_URL_LENGTH]; +} Repos; + +Repos* read_sources_list(const char* filename, int* num_repos) { + FILE* file = fopen(filename, "r"); + if (file == NULL) { + perror("Error opening file"); + return NULL; + } + + int num_lines = 0; + char ch; + while ((ch = fgetc(file)) != EOF) { + if (ch == '\n') + num_lines++; + } + fseek(file, 0, SEEK_SET); + + Repos* repositories = (Repos*)malloc(num_lines * sizeof(Repos)); + if (repositories == NULL) { + perror("Error allocating memory"); + fclose(file); + return NULL; + } + + *num_repos = 0; + char line[MAX_URL_LENGTH * 2]; + while (fgets(line, sizeof(line), file) != NULL) { + if (sscanf(line, "%s = %s", repositories[*num_repos].name, repositories[*num_repos].url) == 2) { + (*num_repos)++; + } + } + + fclose(file); + + return repositories; +} + +void clone_repositories(Repos* repositories, int num_repos, const char* clone_directory) { + char clone_command[MAX_URL_LENGTH + 150]; // Increased to accommodate directory path + + for (int i = 0; i < num_repos; i++) { + char destination[MAX_URL_LENGTH + 50]; + sprintf(destination, "%s/%s", clone_directory, repositories[i].name); + + pid_t pid = fork(); + + if (pid == -1) { + perror("Fork failed"); + exit(EXIT_FAILURE); + } else if (pid == 0) { + sprintf(clone_command, "git clone %s %s", repositories[i].url, destination); + if (system(clone_command) == -1) { + perror("Error cloning repository"); + exit(EXIT_FAILURE); + } + exit(EXIT_SUCCESS); + } else { + int status; + waitpid(pid, &status, 0); + if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) { + fprintf(stderr, "Error cloning repository '%s'\n", repositories[i].name); + } + } + } +} From 0aa96957bccaf975f1c6b2089b51a01d718696c6 Mon Sep 17 00:00:00 2001 From: Bill Gates Date: Sun, 17 Mar 2024 19:25:41 +0000 Subject: [PATCH 2/3] better stuff --- src/repo.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/repo.c b/src/repo.c index e2cbda1..7fde7c3 100755 --- a/src/repo.c +++ b/src/repo.c @@ -37,7 +37,7 @@ Repos* read_sources_list(const char* filename, int* num_repos) { *num_repos = 0; char line[MAX_URL_LENGTH * 2]; while (fgets(line, sizeof(line), file) != NULL) { - if (sscanf(line, "%s = %s", repositories[*num_repos].name, repositories[*num_repos].url) == 2) { + if (sscanf(line, "%99s = %99s", repositories[*num_repos].name, repositories[*num_repos].url) == 2) { (*num_repos)++; } } @@ -52,7 +52,7 @@ void clone_repositories(Repos* repositories, int num_repos, const char* clone_di for (int i = 0; i < num_repos; i++) { char destination[MAX_URL_LENGTH + 50]; - sprintf(destination, "%s/%s", clone_directory, repositories[i].name); + snprintf(destination, sizeof(destination), "%s/%s", clone_directory, repositories[i].name); pid_t pid = fork(); @@ -61,17 +61,20 @@ void clone_repositories(Repos* repositories, int num_repos, const char* clone_di exit(EXIT_FAILURE); } else if (pid == 0) { sprintf(clone_command, "git clone %s %s", repositories[i].url, destination); - if (system(clone_command) == -1) { - perror("Error cloning repository"); + if (system(clone_command) != 0) { + fprintf(stderr, "Error cloning repository '%s'\n", repositories[i].name); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); - } else { - int status; - waitpid(pid, &status, 0); - if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) { - fprintf(stderr, "Error cloning repository '%s'\n", repositories[i].name); - } } } -} + + // Wait for all child processes to finish + int status; + pid_t wpid; + while ((wpid = wait(&status)) > 0) { + if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) { + fprintf(stderr, "Error cloning repository (pid: %d)\n", wpid); + } + } +} \ No newline at end of file From 555655b5ec3eaac07190b12cdf6e5d9e767fe9a6 Mon Sep 17 00:00:00 2001 From: Bill Gates Date: Sun, 17 Mar 2024 19:46:07 +0000 Subject: [PATCH 3/3] it works(kinda) --- dev/test.c | 38 ++++++++++++++++++++++++++++++-------- dev/test.list | 2 ++ include/libspm.h | 23 ++++++++++++++++++----- src/repo.c | 3 +++ 4 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 dev/test.list diff --git a/dev/test.c b/dev/test.c index 30dd0be..88c54fd 100644 --- a/dev/test.c +++ b/dev/test.c @@ -14,7 +14,6 @@ #define STATIC - extern int open_spm(char* path,struct package* pkg); extern int open_ecmp(char* path,struct package* pkg); @@ -42,7 +41,6 @@ int test_config(); int test_make(char* spm_path); char* assemble(char** list,int count); - int main(int argc, char const *argv[]) { dbg(1, "started spm-test"); @@ -58,7 +56,7 @@ int main(int argc, char const *argv[]) DEBUG_UNIT = NULL; if (argc < 2 || strcmp(argv[1], "help") == 0) { - printf("Usage: %s [data|ecmp|all|make|install|uninstall|move|help|split|config|get]\n", argv[0]); + printf("Usage: %s [data|ecmp|all|make|install|uninstall|move|help|split|config|get|update]\n", argv[0]); return 0; } @@ -82,9 +80,7 @@ int main(int argc, char const *argv[]) EXIT += test_make(argv[2]); printf("Leaks: %d\n", check_leaks()); return EXIT; - } - else if (strcmp(argv[1],"install") == 0) - { + } else if (strcmp(argv[1],"install") == 0) { dbg(1, "installing"); init(); install_package_source(argv[2], 0); @@ -102,12 +98,14 @@ 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], "update") == 0) { + // Call the update function + update(); + return 0; } else { printf("Invalid argument\n"); return 1; } - - } int test_move() @@ -434,3 +432,27 @@ char* assemble(char** list,int count) strcat(string,list[i]); return string; } + +int update() { + const char* filename = "dev/test.list"; // Change this to your input file + const char* clone_directory = "/tmp/repos"; // Change this to your desired directory + + printf("Reading repositories from file: %s\n", filename); + int num_repos; + Repos* repositories = read_sources_list(filename, &num_repos); + if (repositories == NULL) { + fprintf(stderr, "Failed to read repositories from file.\n"); + return EXIT_FAILURE; + } + + printf("Cloning repositories to directory: %s\n", clone_directory); + clone_repositories(repositories, num_repos, clone_directory); + + // Clean up + free(repositories); + + printf("Update completed successfully.\n"); + + return EXIT_SUCCESS; +} + diff --git a/dev/test.list b/dev/test.list new file mode 100644 index 0000000..ed7d6e8 --- /dev/null +++ b/dev/test.list @@ -0,0 +1,2 @@ +repo1 = https://github.com/Soviet-Linux/libspm +repo2 = https://github.com/Soviet-Linux/libspm diff --git a/include/libspm.h b/include/libspm.h index 89d5b61..0a57203 100755 --- a/include/libspm.h +++ b/include/libspm.h @@ -393,18 +393,31 @@ long get_locations(char*** locations, const char* loc_dir); */ int check_dependencies(char ** dependencies,int dependenciesCount); + #define MAX_URL_LENGTH 100 typedef struct { char name[MAX_URL_LENGTH]; char url[MAX_URL_LENGTH]; } Repos; +// Function to read repository sources from a file +/* +Accepts: +- const char* filename: Path to the file containing repository sources. +- int* num_repos: Pointer to an integer to store the number of repositories. +Returns: +- Repos*: An array of repository structures. +*/ Repos* read_sources_list(const char* filename, int* num_repos); -void clone_repositories(Repos* repositories, int num_repos, const char* clone_directory); - - - - +// Function to clone repositories +/* +Accepts: +- Repos* repositories: An array of repository structures. +- int num_repos: The number of repositories. +- const char* clone_directory: The directory where repositories will be cloned. +Returns: None +*/ +void clone_repositories(Repos* repositories, int num_repos, const char* clone_directory); \ No newline at end of file diff --git a/src/repo.c b/src/repo.c index 7fde7c3..4c1aa8f 100755 --- a/src/repo.c +++ b/src/repo.c @@ -50,7 +50,9 @@ Repos* read_sources_list(const char* filename, int* num_repos) { void clone_repositories(Repos* repositories, int num_repos, const char* clone_directory) { char clone_command[MAX_URL_LENGTH + 150]; // Increased to accommodate directory path + printf("Cloning repositories:\n"); for (int i = 0; i < num_repos; i++) { + printf("Repository Name: %s, URL: %s\n", repositories[i].name, repositories[i].url); char destination[MAX_URL_LENGTH + 50]; snprintf(destination, sizeof(destination), "%s/%s", clone_directory, repositories[i].name); @@ -61,6 +63,7 @@ void clone_repositories(Repos* repositories, int num_repos, const char* clone_di exit(EXIT_FAILURE); } else if (pid == 0) { sprintf(clone_command, "git clone %s %s", repositories[i].url, destination); + printf("Command: %s\n", clone_command); if (system(clone_command) != 0) { fprintf(stderr, "Error cloning repository '%s'\n", repositories[i].name); exit(EXIT_FAILURE);