Downloads, unpacks, doesn't get the right directory currently.

This commit is contained in:
Tristan Smith 2024-08-31 15:45:10 -04:00
parent 34e0c13f1f
commit 351f7c076f
No known key found for this signature in database
GPG key ID: 0858A9B022DE8ECE
7 changed files with 198 additions and 17 deletions

3
.gitignore vendored
View file

@ -1,3 +1,6 @@
src/*.o src/*.o
*.o *.o
caur caur
*.tar.gz
*.bak
cleanup.sh

93
src/build.c Normal file
View file

@ -0,0 +1,93 @@
#include "caur.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
int setup_cache_directory(char *cache_dir, size_t size) {
const char *home = getenv("HOME");
if (home == NULL) {
fprintf(stderr, "Could not determine the user's home directory.\n");
return 1;
}
snprintf(cache_dir, size, "%s/.config/caur", home);
struct stat st = {0};
if (stat(cache_dir, &st) == -1) {
if (mkdir(cache_dir, 0755) == -1) {
fprintf(stderr, "Failed to create cache directory: %s\n", strerror(errno));
return 1;
} else {
printf("Cache directory created: %s\n", cache_dir);
}
} else {
printf("Cache directory already exists: %s\n", cache_dir);
}
return 0;
}
int build_package(const char *package_name) {
char cache_dir[1024];
char tar_file[1024];
char unpack_dir[1024];
char build_command[1024];
int ret;
// Set up the cache directory
if (setup_cache_directory(cache_dir, sizeof(cache_dir)) != 0) {
return 1;
}
// Path to the downloaded .tar.gz file
ret = snprintf(tar_file, sizeof(tar_file), "%s/%s.tar.gz", cache_dir, package_name);
if ((size_t)ret >= sizeof(tar_file)) {
fprintf(stderr, "Warning: tar_file path was truncated.\n");
return 1;
}
// Check if the tar.gz file exists
struct stat st;
if (stat(tar_file, &st) != 0) {
fprintf(stderr, "Error: The file %s does not exist. Please check the download process.\n", tar_file);
return 1;
}
// Create directory to unpack the files into
ret = snprintf(unpack_dir, sizeof(unpack_dir), "%s/%s", cache_dir, package_name);
if ((size_t)ret >= sizeof(unpack_dir)) {
fprintf(stderr, "Warning: unpack_dir path was truncated.\n");
return 1;
}
mkdir(unpack_dir, 0755);
// Unpack the downloaded .tar.gz file
if (unpack_tar_gz(tar_file, unpack_dir) != 0) {
return 1;
}
// Change directory to unpacked location and build the package
ret = snprintf(build_command, sizeof(build_command), "cd %s && makepkg -si", unpack_dir);
if ((size_t)ret >= sizeof(build_command)) {
fprintf(stderr, "Warning: build_command was truncated.\n");
return 1;
}
int result = system(build_command);
if (result == -1) {
fprintf(stderr, "Failed to execute build command\n");
return 1;
} else if (WIFEXITED(result) && WEXITSTATUS(result) != 0) {
fprintf(stderr, "Build failed with exit status %d.\n", WEXITSTATUS(result));
return 1;
}
printf("Package %s built and installed successfully.\n", package_name);
return 0;
}

View file

@ -4,13 +4,30 @@
#include <stdio.h> #include <stdio.h>
#include <curl/curl.h> #include <curl/curl.h>
// Define the URL for the AUR package and the file extension
#define AUR_PACKAGE_URL "https://aur.archlinux.org/cgit/aur.git/snapshot/"
#define AUR_PACKAGE_URL_END ".tar.gz"
// Function to write data to file // Function to write data to file
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream);
// Function to download a package from the AUR // Function to download a package from the AUR
int download_package(const char *package_name); int download_package(const char *package_name, const char *cache_dir);
// Function to perform a system upgrade // Function to perform a system upgrade
int perform_system_upgrade(); int perform_system_upgrade();
// Function to create cache/build directory
int setup_cache_directory(char *cache_dir, size_t size);
// Function to unpack archived .tar.gz file
// TODO: Look into seeing if other archive formats are used
int unpack_tar_gz(const char *filename, const char *destination);
// Function to build package
int build_package(const char *package_name);
// Reading user config file
FILE *open_config_file(const char *cache_dir);
#endif // CAUR_H #endif // CAUR_H

21
src/config.c Normal file
View file

@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
FILE *open_config_file(const char *cache_dir) {
char config_file[512];
// Construct the full path to the config file
if ((size_t)snprintf(config_file, sizeof(config_file), "%s/config", cache_dir) >= sizeof(config_file)) {
fprintf(stderr, "config_file was truncated\n");
return NULL;
}
// Attempt to open the config file
FILE *file = fopen(config_file, "r");
if (file == NULL) {
fprintf(stderr, "Could not open config file: %s\n", config_file);
return NULL;
}
return file;
}

View file

@ -1,23 +1,24 @@
#include "caur.h" #include "caur.h"
#include <string.h> #include <stdio.h>
#include <stdlib.h>
#define AUR_PACKAGE_URL "https://aur.archlinux.org/cgit/aur.git/snapshot/" #include <curl/curl.h>
#define AUR_PACKAGE_URL_END ".tar.gz" #include <sys/stat.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream); size_t written = fwrite(ptr, size, nmemb, stream);
return written; return written;
} }
int download_package(const char *package_name) { int download_package(const char *package_name, const char *cache_dir) {
CURL *curl; CURL *curl;
FILE *fp; FILE *fp;
CURLcode res; CURLcode res;
char url[256]; char url[256];
char outfilename[256]; char outfilename[512];
// Construct the URL and output file path
snprintf(url, sizeof(url), "%s%s%s", AUR_PACKAGE_URL, package_name, AUR_PACKAGE_URL_END); snprintf(url, sizeof(url), "%s%s%s", AUR_PACKAGE_URL, package_name, AUR_PACKAGE_URL_END);
snprintf(outfilename, sizeof(outfilename), "%s%s", package_name, AUR_PACKAGE_URL_END); snprintf(outfilename, sizeof(outfilename), "%s/%s%s", cache_dir, package_name, AUR_PACKAGE_URL_END);
curl = curl_easy_init(); curl = curl_easy_init();
if (curl) { if (curl) {
@ -38,6 +39,15 @@ int download_package(const char *package_name) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return 1; return 1;
} }
// Check if the file was downloaded correctly
struct stat st;
if (stat(outfilename, &st) != 0) {
fprintf(stderr, "Downloaded file %s not found.\n", outfilename);
return 1;
}
printf("Downloaded file %s successfully, size: %ld bytes\n", outfilename, st.st_size);
} else { } else {
fprintf(stderr, "Failed to init curl\n"); fprintf(stderr, "Failed to init curl\n");
return 1; return 1;

View file

@ -1,4 +1,4 @@
// Version: 0.1.1 // Version: 0.1.5
// date created 8-31-24 // date created 8-31-24
#include "caur.h" #include "caur.h"
@ -12,7 +12,14 @@
// #include <json-c/json.h> // Parsing packing information from the AUR packages // #include <json-c/json.h> // Parsing packing information from the AUR packages
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char cache_dir[1024];
// Setup cache directory
if (setup_cache_directory(cache_dir, sizeof(cache_dir)) != 0) {
return 1;
}
// Perform system upgrade if fewer characters than 1 are entered.
if (argc < 2) { if (argc < 2) {
printf("Performing system upgrade...\n"); printf("Performing system upgrade...\n");
return perform_system_upgrade(); return perform_system_upgrade();
@ -22,18 +29,20 @@ int main(int argc, char *argv[]) {
const char *package_name = argv[1]; const char *package_name = argv[1];
// Call download function with provided package name // Call download function with provided package name
if (download_package(package_name) == 0) { if (download_package(package_name, cache_dir) == 0) {
printf("Package %s downloaded successfully\n", package_name); printf("Package %s downloaded successfully\n", package_name);
// Build and install the package
if (build_package(package_name) == 0) {
printf("Successfully built and installed %s!\n", package_name);
} else {
printf("Building package %s failed.\n", package_name);
}
} else { } else {
printf("Failed to download package %s\n", package_name); printf("Failed to download package %s\n", package_name);
} }
return 0; return 0;
/* const char *package_name = "neofetch";
if (download_package(package_name) == 0) {
printf("Package downloaded successfully\n");
} else {
printf("Failed to download package");
} */
} }

28
src/unpack.c Normal file
View file

@ -0,0 +1,28 @@
#include <stdlib.h> // For system()
#include <stdio.h> // For printf(), fprintf()
#include <sys/wait.h> // System calls need to CTFD
#include "caur.h"
int unpack_tar_gz(const char *filename, const char *destination) {
char command[1024];
int ret = snprintf(command, sizeof(command), "tar -xzf %s --strip-components=1 -C %s", filename, destination);
if ((size_t)ret >= sizeof(command)) {
fprintf(stderr, "Warning: tar command was truncated.\n");
return 1;
}
int result = system(command);
if (result == -1) {
fprintf(stderr, "Failed to execute tar command\n");
return 1;
} else if (WIFEXITED(result) && WEXITSTATUS(result) != 0) {
fprintf(stderr, "Unpacking failed with exit status %d.\n", WEXITSTATUS(result));
return 1;
}
printf("Successfully unpacked %s to %s\n", filename, destination);
return 0;
}