2024-02-08 09:01:44 -05:00
|
|
|
/*
|
|
|
|
* 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>
|
2024-10-02 17:30:43 -04:00
|
|
|
#include <dirent.h>
|
2024-07-21 22:35:38 -04:00
|
|
|
#include <errno.h>
|
2024-02-08 09:01:44 -05:00
|
|
|
#include <libconfig.h>
|
2024-07-21 22:35:38 -04:00
|
|
|
#include <locale.h>
|
2024-12-15 11:57:08 -05:00
|
|
|
#include <openssl/sha.h>
|
2024-02-08 09:01:44 -05:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2024-10-02 17:30:43 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2024-02-08 09:01:44 -05:00
|
|
|
#include <unistd.h>
|
2024-07-21 22:35:38 -04:00
|
|
|
#include <wchar.h>
|
2024-02-08 09:01:44 -05:00
|
|
|
|
2024-10-20 20:48:49 -04:00
|
|
|
#include "config.h"
|
2024-12-15 11:57:08 -05:00
|
|
|
#include "include/globals.h"
|
2024-10-20 20:48:49 -04:00
|
|
|
|
2024-12-05 17:41:44 -05:00
|
|
|
#define BUFFER_SIZE 1024
|
2024-10-22 10:56:37 -04:00
|
|
|
#define MAX_CHILDREN 64
|
2024-12-05 17:41:44 -05:00
|
|
|
#define MAX_SIZE 256
|
2024-10-22 10:56:37 -04:00
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
/*
|
|
|
|
* Global Variables
|
|
|
|
*
|
|
|
|
* Descriptions are given as comments after the variable declaration.
|
|
|
|
* DEFINED IN: globals.h
|
|
|
|
*
|
|
|
|
*/
|
2024-02-08 09:01:44 -05:00
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
config_t cfg; /* Context for libconfig */
|
|
|
|
config_setting_t *setting; /* Pointer for setting */
|
|
|
|
const char str; /* Unsure what this does, will possibly remove later */
|
2024-10-02 17:30:43 -04:00
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
int GLACIER_ALLOW_SERVICES; /* Declaration of GLACIER_ALLOW_SERVICES as given in glacier.cfg */
|
|
|
|
char GLACIER_ALLOWED_LICENSES; /* Declaration of GLACIER_ALLOWED_LICENSES as given in glacier.cfg */
|
|
|
|
int GLACIER_DO_INT_CHECK; /* Declaration of GLACIER_DO_INT_CHECK as given in glacier.cfg */
|
|
|
|
int GLACIER_VERBOSE; /* Declaration of GLACIER_VERBOSE as given in glacier.cfg */
|
2024-02-08 09:01:44 -05:00
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
const char *GLACIER_REPO; /* Declaration of GLACIER_REPO as defined in profile.cfg */
|
|
|
|
const char *GLACIER_ARCH; /* Declaration of GLACIER_ARCH as defined in profile.cfg */
|
|
|
|
const char *GLACIER_TARGET; /* Declaration of GLACIER_TARGET as defined in profile.cfg */
|
|
|
|
const char *GLACIER_LOCALDB; /* Declaration of GLACIER_LOCALDB as defined in profile.cfg */
|
|
|
|
const char *GLACIER_SYSTEM_PROFILE; /* Declaration of GLACIER_SYSTEM_PROFILE as defined in profile.cfg */
|
2024-10-18 18:00:17 -04:00
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
/* Required runtime files */
|
2024-10-02 17:30:43 -04:00
|
|
|
const char *runtime_files[] = {
|
|
|
|
"/etc/glacier.cfg",
|
|
|
|
"/etc/glacier/call-hooks",
|
|
|
|
"/etc/make.conf"
|
|
|
|
};
|
2024-02-08 09:01:44 -05:00
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
FILE *expected_hash; /* File pointer for expected hash */
|
|
|
|
FILE *pkg; /* File pointer for package hash */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* END GLOBAL VARIABLES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* node
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Definition of node type.
|
|
|
|
* DEFINED IN: data.h
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-10-22 10:56:37 -04:00
|
|
|
struct node {
|
|
|
|
char *data;
|
|
|
|
struct node *children[MAX_CHILDREN];
|
|
|
|
int numChildren;
|
|
|
|
};
|
|
|
|
|
2024-02-08 09:01:44 -05:00
|
|
|
/*
|
|
|
|
* infolog
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Output a stylized info message.
|
|
|
|
* PARAMETERS: char MSG[]
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: log.h
|
2024-02-08 09:01:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
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[]
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: log.h
|
2024-02-08 09:01:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
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[]
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: log.h
|
2024-02-08 09:01:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
errlog(char MSG[])
|
|
|
|
{
|
|
|
|
if (MSG == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, COL_RED "[x]" COL_RESET " %s\n", MSG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-03 11:37:10 -04:00
|
|
|
/*
|
|
|
|
* successlog
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Output a stylized success message.
|
|
|
|
* PARAMETERS: char MSG[]
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: log.h
|
2024-10-03 11:37:10 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
successlog(char MSG[])
|
|
|
|
{
|
|
|
|
if (MSG == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-05 08:52:24 -05:00
|
|
|
printf(COL_GREEN "[*]" COL_RESET " %s\n", MSG);
|
2024-10-03 11:37:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:01:44 -05:00
|
|
|
/*
|
|
|
|
* runtime_exists
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Check if necesary runtime files exist.
|
|
|
|
* PARAMETERS: None.
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: runtime.h
|
2024-02-08 09:01:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
int
|
2024-12-05 17:41:44 -05:00
|
|
|
runtime_exists(void)
|
2024-02-08 09:01:44 -05:00
|
|
|
{
|
|
|
|
int f;
|
|
|
|
|
2024-12-05 17:41:44 -05:00
|
|
|
for (f = 0; f < sizeof(runtime_files); f++) {
|
2024-02-08 09:01:44 -05:00
|
|
|
if (access(runtime_files[f], F_OK) == 0) {
|
2024-10-20 17:00:18 -04:00
|
|
|
continue;
|
2024-02-08 09:01:44 -05:00
|
|
|
} else {
|
2024-12-05 17:41:44 -05:00
|
|
|
/* might keep this message, might not, idk */
|
|
|
|
/* errlog("Runtime files are missing, cannot continue with operation.");
|
2024-10-20 17:00:18 -04:00
|
|
|
printf(COL_RED "[x]" COL_RESET " The following files are missing:\n");
|
2024-12-05 17:41:44 -05:00
|
|
|
printf(COL_RED "[x]" COL_RESET " \t%s\n", runtime_files[f]); */
|
|
|
|
return 0;
|
2024-02-08 09:01:44 -05:00
|
|
|
}
|
|
|
|
}
|
2024-12-05 17:41:44 -05:00
|
|
|
return 1;
|
2024-02-08 09:01:44 -05:00
|
|
|
}
|
|
|
|
|
2024-10-08 13:36:28 -04:00
|
|
|
/*
|
|
|
|
* is_process_root
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Check if process is running as root.
|
2024-11-12 09:06:26 -05:00
|
|
|
* PARAMETERS: char MSG[]
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: runtime.h
|
2024-10-08 13:36:28 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2024-12-05 17:41:44 -05:00
|
|
|
is_process_root(void)
|
2024-10-08 13:36:28 -04:00
|
|
|
{
|
|
|
|
if (getuid() != 0) {
|
2024-12-05 17:41:44 -05:00
|
|
|
return 0; /* process is not running as root */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1; /* process is running as root */
|
2024-10-08 13:36:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:01:44 -05:00
|
|
|
/*
|
|
|
|
* init_config
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Initialize libconfig.
|
|
|
|
* PARAMETERS: None.
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: config.h
|
2024-02-08 09:01:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2024-12-05 17:41:44 -05:00
|
|
|
init_config(void)
|
2024-02-08 09:01:44 -05:00
|
|
|
{
|
|
|
|
config_init(&cfg);
|
|
|
|
|
2024-10-18 17:58:23 -04:00
|
|
|
if (! config_read_file(&cfg, runtime_files[0])) {
|
2024-02-08 09:01:44 -05:00
|
|
|
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
|
|
|
|
config_error_line(&cfg), config_error_text(&cfg));
|
|
|
|
config_destroy(&cfg);
|
|
|
|
return(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2024-10-20 20:45:50 -04:00
|
|
|
if (LG_VERBOSE == 1) {
|
|
|
|
infolog("Initialized libconfig");
|
|
|
|
}
|
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
return 0;
|
2024-02-08 09:01:44 -05:00
|
|
|
}
|
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
/*
|
|
|
|
* die_config
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Kill libconfig.
|
|
|
|
* PARAMETERS: None.
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: config.h
|
2024-10-02 17:30:43 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-02-08 09:01:44 -05:00
|
|
|
int
|
2024-12-05 17:41:44 -05:00
|
|
|
die_config(void)
|
2024-02-08 09:01:44 -05:00
|
|
|
{
|
|
|
|
config_destroy(&cfg);
|
2024-10-20 20:45:50 -04:00
|
|
|
|
|
|
|
if (LG_VERBOSE == 1) {
|
|
|
|
infolog("Destroyed libconfig");
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:01:44 -05:00
|
|
|
return(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
/*
|
|
|
|
* load_all_from_config
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Loads all settings from the Glacier config file.
|
|
|
|
* PARAMETERS: None.
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: config.h
|
2024-10-02 17:30:43 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-10-18 17:58:23 -04:00
|
|
|
int
|
2024-12-05 17:41:44 -05:00
|
|
|
load_all_from_config(void)
|
2024-10-18 17:58:23 -04:00
|
|
|
{
|
2024-12-28 13:04:11 -05:00
|
|
|
/* this is probably really ugly but it works */
|
|
|
|
if (! config_lookup_bool(&cfg, "GLACIER_DO_INT_CHECK", &GLACIER_DO_INT_CHECK)) { return 1; }
|
|
|
|
if (! config_lookup_bool(&cfg, "GLACIER_VERBOSE", &GLACIER_VERBOSE)) { return 1; }
|
2024-10-18 17:58:23 -04:00
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
return 0;
|
2024-10-18 17:58:23 -04:00
|
|
|
}
|
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
/*
|
|
|
|
* load_all_from_profile
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Loads all settings from the Glacier system profile.
|
|
|
|
* PARAMETERS: None.
|
|
|
|
* DEFINED IN: config.h
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
int
|
2024-12-28 13:04:11 -05:00
|
|
|
load_all_from_profile(void)
|
2024-02-08 09:01:44 -05:00
|
|
|
{
|
2024-12-28 13:04:11 -05:00
|
|
|
if (! config_lookup_string(&cfg, "GLACIER_REPO", &GLACIER_REPO)) { return 1; }
|
|
|
|
if (! config_lookup_string(&cfg, "GLACIER_ARCH", &GLACIER_ARCH)) { return 1; }
|
|
|
|
if (! config_lookup_string(&cfg, "GLACIER_TARGET", &GLACIER_TARGET)) { return 1; }
|
|
|
|
if (! config_lookup_string(&cfg, "GLACIER_SYSTEM_PROFILE", &GLACIER_SYSTEM_PROFILE)) { return 1; }
|
|
|
|
|
|
|
|
return 0;
|
2024-07-21 22:35:38 -04:00
|
|
|
}
|
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
/*
|
|
|
|
* load_setting_from_config
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Load a specified setting from the Glacier config file.
|
|
|
|
* PARAMETERS: char SETTING[]
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: config.h
|
2024-10-02 17:30:43 -04:00
|
|
|
*
|
|
|
|
*/
|
2024-07-21 22:35:38 -04:00
|
|
|
|
2024-10-18 17:58:23 -04:00
|
|
|
/* int
|
2024-10-02 17:30:43 -04:00
|
|
|
load_setting_from_config(char SETTING[])
|
2024-10-18 17:58:23 -04:00
|
|
|
{} */
|
2024-07-21 22:35:38 -04:00
|
|
|
|
2024-10-22 10:56:37 -04:00
|
|
|
/*
|
|
|
|
* create_node
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Create a dependency tree node.
|
|
|
|
* PARAMETERS: char *data
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-10-22 10:56:37 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct node
|
|
|
|
*create_node(char *data)
|
|
|
|
{
|
|
|
|
struct node *newNode = (struct node*)malloc(sizeof(struct node));
|
|
|
|
newNode->data = strdup(data);
|
|
|
|
newNode->numChildren = 0;
|
|
|
|
return newNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2024-12-05 17:41:44 -05:00
|
|
|
* add_child
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Add a child node to a parent node.
|
|
|
|
* PARAMETERS: struct node *parent, struct node *child
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-12-05 17:41:44 -05:00
|
|
|
*
|
|
|
|
*/
|
2024-10-22 10:56:37 -04:00
|
|
|
|
|
|
|
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
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-10-22 10:56:37 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
/*
|
|
|
|
* queue
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Definition of queue type.
|
|
|
|
* DEFINED IN: data.h
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-12-05 17:41:44 -05:00
|
|
|
typedef struct {
|
|
|
|
int items[MAX_SIZE];
|
|
|
|
int front;
|
|
|
|
int rear;
|
|
|
|
} queue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* init_queue
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Initialize a queue.
|
|
|
|
* PARAMETERS: queue *q
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-12-05 17:41:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
init_queue(queue *q)
|
|
|
|
{
|
|
|
|
q -> front = -1;
|
|
|
|
q -> rear = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* queue_is_empty
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Check if queue is empty.
|
|
|
|
* PARAMETERS: struct node *root, int level
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-12-05 17:41:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool queue_is_empty(queue *q) { return (q -> front == q -> rear -1); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* queue_is_full
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Check if queue is full.
|
|
|
|
* PARAMETERS: queue *q
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-12-05 17:41:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool queue_is_full(queue *q) { return (q -> rear == MAX_SIZE); }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* enqueue
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Enqueue an element at the back of the queue.
|
|
|
|
* PARAMETERS: queue *q, int value
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-12-05 17:41:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
enqueue (queue *q, int value)
|
|
|
|
{
|
|
|
|
if (queue_is_full(q)) {
|
|
|
|
printf("Queue is full\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
q -> items[q -> rear] = value;
|
|
|
|
q -> rear++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dequeue
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Dequeue the element at the front of the queue.
|
|
|
|
* PARAMETERS: queue *q, int value
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-12-05 17:41:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
dequeue(queue *q)
|
|
|
|
{
|
|
|
|
if (queue_is_empty(q)) {
|
|
|
|
printf("Queue is empty\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
q -> front++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* peek
|
|
|
|
*
|
|
|
|
* DESCRIPTION: View the element at the front of the queue.
|
|
|
|
* PARAMETERS: struct node *root, int level
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-12-05 17:41:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
peek(queue *q)
|
|
|
|
{
|
|
|
|
if (queue_is_empty(q)) {
|
|
|
|
printf("Queue is empty\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return q -> items[q -> front + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* print_queue
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Print the queue.
|
|
|
|
* PARAMETERS: queue *q
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: data.h
|
2024-12-05 17:41:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
print_queue(queue *q)
|
|
|
|
{
|
|
|
|
if (queue_is_empty(q)) {
|
|
|
|
printf("Queue is empty\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Current Queue: ");
|
|
|
|
|
|
|
|
for (int i = q -> front + 1; i < q -> rear; i++) {
|
|
|
|
printf("%d ", q -> items[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
/*
|
|
|
|
* mkworkspace
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Creates a new Glacier workspace in /tmp.
|
|
|
|
* PARAMETERS: None.
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: pkgops.h
|
2024-10-02 17:30:43 -04:00
|
|
|
*
|
|
|
|
*/
|
2024-07-21 22:35:38 -04:00
|
|
|
|
|
|
|
int
|
2024-12-05 17:41:44 -05:00
|
|
|
mkworkspace(void)
|
2024-07-21 22:35:38 -04:00
|
|
|
{
|
2024-10-02 17:30:43 -04:00
|
|
|
DIR* workspace = opendir("/tmp/glacier-workspace");
|
|
|
|
if (workspace) {
|
2024-12-05 17:41:44 -05:00
|
|
|
/* infolog("Not creating new workspace, valid workspace already exists."); */
|
|
|
|
return 0;
|
2024-10-02 17:30:43 -04:00
|
|
|
} else if (ENOENT == errno) {
|
2024-12-05 17:41:44 -05:00
|
|
|
/* infolog("Creating new Glacier workspace..."); */
|
2024-10-02 17:30:43 -04:00
|
|
|
mkdir("/tmp/glacier-workspace", 0777);
|
2024-12-05 17:41:44 -05:00
|
|
|
return 1;
|
2024-10-02 17:30:43 -04:00
|
|
|
} else {
|
2024-12-05 17:41:44 -05:00
|
|
|
if (LG_VERBOSE == 1) {
|
|
|
|
errlog("in mkworkspace()");
|
|
|
|
errlog("mkdir() failed to run");
|
|
|
|
}
|
|
|
|
return -1;
|
2024-10-02 17:30:43 -04:00
|
|
|
}
|
2024-07-21 22:35:38 -04:00
|
|
|
}
|
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
/*
|
|
|
|
* prepare_pkg
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Copies a package archive from the localdb to the workspace, and unpacks it.
|
|
|
|
* PARAMETERS: char PACKAGE[]
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: pkgops.h
|
2024-10-02 17:30:43 -04:00
|
|
|
*
|
|
|
|
*/
|
2024-07-21 22:35:38 -04:00
|
|
|
|
|
|
|
int
|
2024-10-02 17:30:43 -04:00
|
|
|
prepare_pkg(char PACKAGE[])
|
2024-07-21 22:35:38 -04:00
|
|
|
{
|
2024-10-02 17:30:43 -04:00
|
|
|
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;
|
2024-07-21 22:35:38 -04:00
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
pkg_old = fopen(PACKAGE, "r");
|
|
|
|
pkg_new = fopen(PKG_NEW, "a+");
|
2024-02-08 09:01:44 -05:00
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
contents = fgetc(pkg_old);
|
2024-07-21 22:35:38 -04:00
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
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);
|
2024-07-21 22:35:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-02 17:30:43 -04:00
|
|
|
/*
|
|
|
|
* run_make_task
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Runs a make task in current working directory
|
|
|
|
* PARAMETERS: char TASK[]
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: pkgops.h
|
2024-10-02 17:30:43 -04:00
|
|
|
*
|
|
|
|
*/
|
2024-07-21 22:35:38 -04:00
|
|
|
|
|
|
|
int
|
2024-10-02 17:30:43 -04:00
|
|
|
run_make_task(char TASK[])
|
2024-07-21 22:35:38 -04:00
|
|
|
{
|
2024-10-02 17:30:43 -04:00
|
|
|
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;
|
2024-07-21 22:35:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-05 17:41:44 -05:00
|
|
|
/*
|
|
|
|
* compare_file_hash
|
|
|
|
*
|
|
|
|
* DESCRIPTION: Compare two file hashes
|
|
|
|
* PARAMETERS: char ORIG_HASH[], char FILE[]
|
2024-12-28 13:04:11 -05:00
|
|
|
* DEFINED IN: security.h
|
2024-12-05 17:41:44 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-12-28 13:04:11 -05:00
|
|
|
/*
|
2024-12-05 17:41:44 -05:00
|
|
|
int
|
|
|
|
compare_file_hash(char ORIG_HASH[], char FILE[])
|
|
|
|
{
|
2024-12-15 11:57:08 -05:00
|
|
|
FILE *pkg;
|
2024-12-05 17:41:44 -05:00
|
|
|
|
2024-12-15 11:57:08 -05:00
|
|
|
pkg = fopen(FILE, "rb");
|
|
|
|
if (pkg == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char hash[SHA256_DIGEST_LENGTH];
|
|
|
|
SHA256_CTX sha256;
|
|
|
|
|
|
|
|
SHA256_Init(&sha256);
|
|
|
|
|
|
|
|
const int hashBufferSize = 1024;
|
|
|
|
unsigned char hashBuffer[hashBufferSize];
|
|
|
|
int bytesRead;
|
|
|
|
|
|
|
|
while ((byesRead = fread(hashBuffer, 1, hashBufferSize, pkg)) != 0) {
|
|
|
|
SHA256_Update(&sha256, hashBuffer, bytesRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
SHA256_Final(hash, &sha256);
|
|
|
|
fclose(pkg);
|
|
|
|
|
|
|
|
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
|
|
|
|
printf("%02x", hash[i]);
|
|
|
|
}
|
2024-12-05 17:41:44 -05:00
|
|
|
}
|
2024-12-28 13:04:11 -05:00
|
|
|
*/
|