libglacier/libglacier.c

497 lines
9.6 KiB
C

/*
* libglacier.c - Backend C library for Glacier
*
* This file is part of Glacier.
*
* Glacier is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* Glacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Glacier. If
* not, see <https://www.gnu.org/licenses/>.
*/
#include <color.h>
#include <dirent.h>
#include <errno.h>
#include <libconfig.h>
#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wchar.h>
#include "config.h"
#define MAX_CHILDREN 64
/* Global variables */
config_t cfg;
config_setting_t *setting;
const char str;
int GLACIER_ALLOW_SERVICES;
char GLACIER_ALLOWED_LICENSES;
int GLACIER_DO_INT_CHECK;
int GLACIER_VERBOSE;
const char *GLACIER_REPO;
const char *GLACIER_ARCH;
const char *GLACIER_TARGET;
const char *GLACIER_SYSTEM_PROFILE;
const char *runtime_files[] = {
"/etc/glacier.cfg",
"/etc/glacier/call-hooks",
"/etc/make.conf"
};
struct node {
char *data;
struct node *children[MAX_CHILDREN];
int numChildren;
};
/*
* infolog
*
* DESCRIPTION: Output a stylized info message.
* PARAMETERS: char MSG[]
* DEFINED IN: glacier_log.h
*
*/
void
infolog(char MSG[])
{
if (MSG == NULL) {
return;
}
printf(COL_BLUE "[i]" COL_RESET " %s\n", MSG);
return;
}
/*
* warnlog
*
* DESCRIPTION: Output a stylized warning message.
* Parameters: char MSG[]
* DEFINED IN: glacier_log.h
*
*/
void
warnlog(char MSG[])
{
if (MSG == NULL) {
return;
}
printf(COL_YELLOW "[!]" COL_RESET " %s\n", MSG);
return;
}
/*
* errlog
*
* DESCRIPTION: Output a stylized error message.
* PARAMETERS: char MSG[]
* DEFINED IN: glacier_log.h
*
*/
void
errlog(char MSG[])
{
if (MSG == NULL) {
return;
}
fprintf(stderr, COL_RED "[x]" COL_RESET " %s\n", MSG);
return;
}
/*
* successlog
*
* DESCRIPTION: Output a stylized success message.
* PARAMETERS: char MSG[]
* DEFINED IN: glacier_log.h
*
*/
void
successlog(char MSG[])
{
const wchar_t w_check = 0x2713;
setlocale(LC_CTYPE, "");
if (MSG == NULL) {
return;
}
printf(COL_GREEN "[%lc]" COL_RESET " %s\n", w_check, MSG);
return;
}
/*
* runtime_exists
*
* DESCRIPTION: Check if necesary runtime files exist.
* PARAMETERS: None.
* DEFINED IN: glacier_runtime.h
*
*/
int
runtime_exists()
{
int f;
for (f = 0; f < 3; f++) {
if (access(runtime_files[f], F_OK) == 0) {
continue;
} else {
errlog("Runtime files are missing, cannot continue with operation.");
printf(COL_RED "[x]" COL_RESET " The following files are missing:\n");
printf(COL_RED "[x]" COL_RESET " \t%s\n", runtime_files[f]);
return 1;
}
}
return 0;
}
/*
* is_process_root
*
* DESCRIPTION: Check if process is running as root.
* PARAMETERS: None.
* DEFINED IN: glacier_runtime.h
*
*/
int
is_process_root()
{
if (getuid() != 0) {
errlog("Failed to open package index: permission denied. Are you root?");
exit(1);
}
}
/*
* init_config
*
* DESCRIPTION: Initialize libconfig.
* PARAMETERS: None.
* DEFINED IN: glacier_config.h
*
*/
int
init_config()
{
config_init(&cfg);
if (! config_read_file(&cfg, runtime_files[0])) {
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return(EXIT_FAILURE);
}
if (LG_VERBOSE == 1) {
infolog("Initialized libconfig");
}
return 0;
}
/*
* die_config
*
* DESCRIPTION: Kill libconfig.
* PARAMETERS: None.
* DEFINED IN: glacier_config.h
*
*/
int
die_config()
{
config_destroy(&cfg);
if (LG_VERBOSE == 1) {
infolog("Destroyed libconfig");
}
return(EXIT_SUCCESS);
}
/*
* load_all_from_config
*
* DESCRIPTION: Loads all settings from the Glacier config file.
* PARAMETERS: None.
* DEFINED IN: glacier_config.h
*
*/
int
load_all_from_config()
{
config_lookup_bool(&cfg, "GLACIER_DO_INT_CHECK", &GLACIER_DO_INT_CHECK);
config_lookup_bool(&cfg, "GLACIER_VERBOSE", &GLACIER_VERBOSE);
config_lookup_string(&cfg, "GLACIER_REPO", &GLACIER_REPO);
config_lookup_string(&cfg, "GLACIER_ARCH", &GLACIER_ARCH);
config_lookup_string(&cfg, "GLACIER_TARGET", &GLACIER_TARGET);
config_lookup_string(&cfg, "GLACIER_SYSTEM_PROFILE", &GLACIER_SYSTEM_PROFILE);
}
/* issues with this idk
int
load_all_from_config()
{
if(config_lookup_bool(&cfg, "GLACIER_ALLOW_SERVICES", &GLACIER_ALLOW_SERVICES))
printf("Services allowed: %s\n", GLACIER_ALLOW_SERVICES ? "true" : "false");
else
warnlog("GLACIER_SERVICES_ALLOWED is not defined in glacier.cfg.");
if(config_lookup_bool(&cfg, "GLACIER_DO_INT_CHECK", &GLACIER_DO_INT_CHECK))
printf("Integrity checking enabled: %s\n", GLACIER_DO_INT_CHECK ? "true" : "false");
else
warnlog("GLACIER_DO_INT_CHECK is not defined in glacier.cfg.");
if(config_lookup_bool(&cfg, "GLACIER_VERBOSE", &GLACIER_VERBOSE))
printf("Verbose logging: %s\n", GLACIER_VERBOSE ? "true" : "false");
else
warnlog("GLACIER_VERBOSE is not defined in glacier.cfg.");
}
*/
/*
* load_setting_from_config
*
* DESCRIPTION: Load a specified setting from the Glacier config file.
* PARAMETERS: char SETTING[]
* DEFINED IN: glacier_config.h
*
*/
/* int
load_setting_from_config(char SETTING[])
{} */
/*
* create_node
*
* DESCRIPTION: Create a dependency tree node.
* PARAMETERS: char *data
* DEFINED IN: glacier_data.h
*
*/
struct node
*create_node(char *data)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = strdup(data);
newNode->numChildren = 0;
return newNode;
}
/*
* add_child
*
* DESCRIPTION: Add a child node to a parent node.
* PARAMETERS: struct node *parent, struct node *child
* DEFINED IN: glacier_data.h
*
*/
void
add_child(struct node *parent, struct node *child)
{
if (parent->numChildren < MAX_CHILDREN) {
parent->children[parent->numChildren++] = child;
} else {
if (LG_VERBOSE == 1) { errlog("Maximum number of children exceeded"); }
exit(1);
}
}
/*
* print_tree
*
* DESCRIPTION: Print a dependency tree.
* PARAMETERS: struct node *root, int level
* DEFINED IN: glacier_data.h
*
*/
void
print_tree(struct node *root, int level)
{
if (root == NULL) {
return;
}
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%s\n", root->data);
for (int i = 0; i < root->numChildren; i++) {
print_tree(root->children[i], level + 1);
}
}
/*
* mkworkspace
*
* DESCRIPTION: Creates a new Glacier workspace in /tmp.
* PARAMETERS: None.
* DEFINED IN: glacier_pkgops.h
*
*/
int
mkworkspace()
{
DIR* workspace = opendir("/tmp/glacier-workspace");
if (workspace) {
infolog("Not creating new workspace, valid workspace already exists.");
} else if (ENOENT == errno) {
infolog("Creating new Glacier workspace...");
mkdir("/tmp/glacier-workspace", 0777);
} else {
printf("LIBRARY ERROR: opendir() failed\n");
return 2;
}
}
/*
* prepare_pkg
*
* DESCRIPTION: Copies a package archive from the localdb to the workspace, and unpacks it.
* PARAMETERS: char PACKAGE[]
* DEFINED IN: glacier_pkgops.h
*
*/
int
prepare_pkg(char PACKAGE[])
{
if (PACKAGE == NULL) {
printf(COL_RED "[x] " COL_RESET "Package '%s' does not exist in the local database.\n", PACKAGE);
errlog("Ensure your local database is up to date and try again.");
errlog("This can be done by running 'glacier-update-pkgdb' as root.");
return 1;
} else {
char PKG_NEW[512];
strcat(PKG_NEW, "/tmp/glacier-workspace/");
strcat(PKG_NEW, PACKAGE);
strcat(PKG_NEW, ".tar");
FILE *pkg_old, *pkg_new;
char filename[100], contents;
pkg_old = fopen(PACKAGE, "r");
pkg_new = fopen(PKG_NEW, "a+");
contents = fgetc(pkg_old);
while (contents != EOF) {
fputc(contents, pkg_new);
contents = fgetc(pkg_old);
}
fclose(pkg_old);
fclose(pkg_new);
char *tar_args[] = {
"/bin/tar", /* This should be changed to /glacier/bin/tar later on */
"-xvf",
PKG_NEW,
NULL,
};
execvp(
"/bin/tar", /* Above comment applies here */
tar_args
);
if (errno != 0) {
printf(COL_RED "[x] " COL_RESET "Error while unpacking archive for package %s.\n", PACKAGE);
return errno;
}
remove(PKG_NEW);
char pkg_dir[512];
strcat(pkg_dir, "/tmp/glacier-workspace/");
strcat(pkg_dir, PACKAGE);
chdir(pkg_dir);
}
}
/* download_package_sources() is deprecated and will be removed in a later release. */
/* int
download_package_sources(char PACKAGE[])
{
printf(COL_BLUE "[i] " COL_RESET "Downloading package archive for %s...\n", PACKAGE);
if (GLACIER_SYSTEM_PROFILE == "x86-musl") {
execl("/glacier/tools/wget", "https://git.everestlinux.org/EverestLinux/epkgs-x86-musl/", NULL);
} else {
printf(COL_RED "[x] " COL_RESET "System profile '%s' is not supported.\n", GLACIER_SYSTEM_PROFILE);
}
} */
void
TEST_chdir(char dir[])
{
char *buf;
chdir(dir);
buf = (char *) malloc(100*sizeof(char));
getcwd(buf, 100);
printf("%s\n", buf);
}
/*
* run_make_task
*
* DESCRIPTION: Runs a make task in current working directory
* PARAMETERS: char TASK[]
* DEFINED IN: glacier_pkgops.h
*
*/
int
run_make_task(char TASK[])
{
char *build_args[] = {
"/bin/make", /* This should be changed to /glacier/bin/make later on */
TASK,
NULL
};
execvp(
"/bin/make", /* Above comment applies here */
build_args
);
if (errno != 0) {
errlog("An error occurred while running the make task.");
return errno;
}
}