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

New repo stuff #57

Draft
wants to merge 3 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
38 changes: 30 additions & 8 deletions dev/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@


#define STATIC

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

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 23 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:23: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 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 'versions' is non-const and globally accessible, consider making it const
char* types[5] = {"bin","src","src","bin","src"};

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 '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 29 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:29: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 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_files' is non-const and globally accessible, consider making it const

char** list_of_stuff = NULL;

Check warning on line 32 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:32: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 32 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:32: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 33 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:33: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 All @@ -42,7 +41,6 @@
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 @@
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 @@ -70,7 +68,7 @@

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

Check warning on line 71 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:71: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,12 +77,10 @@
return ret;
} else if (strcmp(argv[1], "make") == 0) {
int EXIT = 0;
EXIT += test_make(argv[2]);

Check warning on line 80 in dev/test.c

View workflow job for this annotation

GitHub Actions / c-linter

dev/test.c:80: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;
}
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 @@
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 @@
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
@@ -1,4 +1,4 @@
#include <curl/curl.h>

Check failure on line 1 in src/get.c

View workflow job for this annotation

GitHub Actions / c-linter

src/get.c:1:10 [clang-diagnostic-error]

'curl/curl.h' file not found
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
Expand Down Expand Up @@ -69,6 +69,18 @@
// 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);
}
}
}
Loading