Skip to content
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
38 changes: 30 additions & 8 deletions dev/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@


#define STATIC

extern int open_spm(char* path,struct package* pkg);
extern int open_ecmp(char* path,struct package* pkg);

Expand Down Expand Up @@ -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");
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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()
Expand Down Expand Up @@ -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;
}

2 changes: 2 additions & 0 deletions dev/test.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
repo1 = https://github.com/Soviet-Linux/libspm
repo2 = https://github.com/Soviet-Linux/libspm
24 changes: 23 additions & 1 deletion include/libspm.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,30 @@ 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);

// 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);
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 14 additions & 2 deletions src/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
83 changes: 83 additions & 0 deletions src/repo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#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, "%99s = %99s", 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

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

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);
printf("Command: %s\n", clone_command);
if (system(clone_command) != 0) {
fprintf(stderr, "Error cloning repository '%s'\n", repositories[i].name);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
}

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