mpm

morphux package manager
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/mpm

commit b13a569f75754b08e13cfba027a351ced9b0345a
parent a4520bfb2884a1a1bd47931f3f94f7ecc7016c57
Author: Louis Solofrizzo <louis@ne02ptzero.me>
Date:   Sun,  4 Jan 2015 01:48:21 +0100

Main program done !

Diffstat:
Msrc/Makefile.am | 7+++++--
Msrc/Makefile.in | 13++++++++++---
Asrc/download.c | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/functions.c | 1+
Msrc/install.c | 7++++++-
Msrc/parse.c | 2++
Asrc/resume.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/unpack.c | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 250 insertions(+), 6 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am @@ -5,5 +5,8 @@ mpm_SOURCES= main.c \ package.c \ install.c \ functions.c \ - parse.c -AM_LDFLAGS = -lcurl + parse.c \ + resume.c \ + download.c \ + unpack.c +AM_LDFLAGS = -lcurl -lm diff --git a/src/Makefile.in b/src/Makefile.in @@ -64,7 +64,8 @@ am__installdirs = "$(DESTDIR)$(bindir)" PROGRAMS = $(bin_PROGRAMS) am_mpm_OBJECTS = main.$(OBJEXT) help.$(OBJEXT) options.$(OBJEXT) \ package.$(OBJEXT) install.$(OBJEXT) functions.$(OBJEXT) \ - parse.$(OBJEXT) + parse.$(OBJEXT) resume.$(OBJEXT) download.$(OBJEXT) \ + unpack.$(OBJEXT) mpm_OBJECTS = $(am_mpm_OBJECTS) mpm_LDADD = $(LDADD) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) @@ -174,9 +175,12 @@ mpm_SOURCES = main.c \ package.c \ install.c \ functions.c \ - parse.c + parse.c \ + resume.c \ + download.c \ + unpack.c -AM_LDFLAGS = -lcurl +AM_LDFLAGS = -lcurl -lm all: all-am .SUFFIXES: @@ -261,6 +265,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/download.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/functions.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/help.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/install.Po@am__quote@ @@ -268,6 +273,8 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/options.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/package.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parse.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/resume.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/unpack.Po@am__quote@ .c.o: @am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/download.c b/src/download.c @@ -0,0 +1,77 @@ +#include "../inc/mpm.h" + +static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) { + struct FtpFile *out = (struct FtpFile *)stream; + if (out && !out->stream) { + out->stream=fopen(out->filename, "wb"); + if(!out->stream) + return -1; + } + return fwrite(buffer, size, nmemb, out->stream); +} + +static char *getTarName(char *url) { + char **tmp = ft_strsplit(url, '/'); + int i; + + for (i = 0; tmp[i]; i++); + return strjoin("/tmp/", tmp[i - 1]); +} + +static int progress_func(void* ptr, double TotalToDownload, double NowDownloaded, + double TotalToUpload, double NowUploaded) { + + int totaldotz = 60; + double fractiondownloaded = NowDownloaded / TotalToDownload; + int dotz = round(fractiondownloaded * totaldotz); + int ii = 0; + + printf("%3.0f%% [",fractiondownloaded*100); + for (; ii < dotz; ii++) { + if (ii == dotz - 1) + printf(">"); + else + printf("="); + } + for (; ii < totaldotz; ii++) { + printf(" "); + } + printf("] \r"); + fflush(stdout); + return 0; +} + +void download(mpm_pinfo *p) { + CURL *curl; + CURLcode res; + struct FtpFile ftpfile = { + getTarName(p->url), + NULL + }; + curl_global_init(CURL_GLOBAL_DEFAULT); + curl = curl_easy_init(); + while (p) { + info(strjoin("Downloading ", p->name)); + printf("=> %s\n", getTarName(p->url)); + curl_easy_setopt(curl, CURLOPT_URL, p->url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); + curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_func); + res = curl_easy_perform(curl); + printf("\n"); + if (ftpfile.stream) + fclose(ftpfile.stream); + if (CURLE_OK != res) { + printf(">> %d\n", res); + error("Download failed"); + } + curl_easy_cleanup(curl); + fflush(stdout); + printf("\n"); + success("Done !"); + unpacking(getTarName(p->url), p); + p = p->next; + } + curl_global_cleanup(); +} diff --git a/src/functions.c b/src/functions.c @@ -8,6 +8,7 @@ char *strjoin(char *s1, char *s2) { return NULL; join = memcpy(join, s1, strlen(s1)); memcpy(join + strlen(s1), s2, strlen(s2)); + join[strlen(s1) + strlen(s2)] = 0; return join; } diff --git a/src/install.c b/src/install.c @@ -53,9 +53,14 @@ mpm_pinfo *get_info(char *name, mpm_pinfo *packages) { void install(mpm_package *p) { mpm_pinfo *packages; + + if (getuid()) + error("MPM install must run as a root user !"); while (p) { packages = get_info(p->package, packages); p = p->next; } - print_packages(packages); + /*print_packages(packages);*/ + resume(packages); + download(packages); } diff --git a/src/parse.c b/src/parse.c @@ -40,6 +40,8 @@ mpm_pinfo *parse(char *str, mpm_pinfo *packages) { pack_tmp->after = return_string(tmp[1]); } else if (!strcmp(tmp[0], "authors")) { pack_tmp->after = return_string(tmp[1]); + } else if (!strcmp(tmp[0], "size")) { + pack_tmp->size = return_string(tmp[1]); } } if (i == 0) diff --git a/src/resume.c b/src/resume.c @@ -0,0 +1,60 @@ +#include "../inc/mpm.h" + +static void print_line(int col) { + int i; + + for (i = 0; i < col; i++) { + printf("-"); + } + printf("\n"); +} + +static void menu(int col) { + int i; + + printf(" \033[1;38mPACKAGE"); + col -= 8; + for (i = col; i > (col / 2 + 8); i--) + printf(" "); + printf("VERSION"); + i -= 7; + for (; i > 6; i--) + printf(" "); + printf("SIZE \033[0m\n"); +} + +static void print_package_info(mpm_pinfo *p, int col) { + int i, tmp = col; + while (p) { + col = tmp; + printf(" %s", p->name); + col -= strlen(p->name); + for (i = col; i > (col / 2 + 7); i--) + printf(" "); + printf("%s", p->version); + i -= strlen(p->version); + for (; i > strlen(p->size) + 2; i--) + printf(" "); + printf("%s \n", p->size); + p = p->next; + } +} + +int resume(mpm_pinfo *p) { + struct winsize w; + char buffer[1]; + + printf("\033[0;33mRESUME:\033[0m\n"); + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + print_line(w.ws_col); + menu(w.ws_col); + print_line(w.ws_col); + print_package_info(p, w.ws_col); + print_line(w.ws_col); + write(1, "\033[1;32m-->\033[0m Sound's good to you ? [y/n] ", 43); + read(0, buffer, 2); + if (buffer[0] == 'y' || buffer[0] == '\n') + return 0; + else + error("Aborting"); +} diff --git a/src/unpack.c b/src/unpack.c @@ -0,0 +1,89 @@ +#include "../inc/mpm.h" + +static char *get_extract_directory(void) { + int fd = open("/tmp/mpm.log", O_RDONLY); + char buffer[50]; + char **result; + + read(fd, buffer, 50); + result = ft_strsplit(buffer, '/'); + close(fd); + remove("/tmp/mpm.log"); + return strjoin("/tmp/", result[0]); +} + +extern char **environ; + +void configure(char *path, mpm_pinfo *p) { + info("Running configure.."); + char **configure = ft_strsplit(p->configure, ' '); + int pid, status; + + configure[0] = strjoin(path, configure[0]); + if (!(pid = fork())) { + int fd = open("/dev/null", O_WRONLY); + dup2(fd, 1); + execve(configure[0], configure, environ); + } + waitpid(pid, &status, 0); + if (status > 0) + error("Configure return an error"); + info("Done."); +} + +void make(mpm_pinfo *p) { + info("Running make..."); + char **make = ft_strsplit(p->make, ' '); + int pid, status; + + make[0] = strjoin("/usr/bin/", make[0]); + if (!(pid = fork())) { + int fd = open("/dev/null", O_WRONLY); + dup2(fd, 1); + execve(make[0], make, environ); + } + waitpid(pid, &status, 0); + if (status > 0) + error("Make return an error"); + info("Done."); +} + +void make_install(mpm_pinfo *p) { + info("Install the package..."); + char **install = ft_strsplit(p->install, ' '); + int pid, status; + + install[0] = strjoin("/usr/bin/", install[0]); + if (!(pid = fork())) { + int fd = open("/dev/null", O_WRONLY); + dup2(fd, 1); + execve(install[0], install, environ); + } + waitpid(pid, &status, 0); + if (status > 0) + error("Installation return an error"); + info("Done."); +} + +void unpacking(char *path, mpm_pinfo *p) { + char *extract_dir; + int pid; + + info("Unpacking ..."); + if (!(pid = fork())){ + int fd = open("/tmp/mpm.log", O_WRONLY | O_CREAT, 0777); + dup2(fd, 1); + execl("/bin/tar", "/bin/tar", "xvf", path, "-C", "/tmp/", NULL); + } + waitpid(pid, &pid, 0); + success("Done."); + extract_dir = get_extract_directory(); + info("Making build directory"); + mkdir("/tmp/build", 0777); + info("Changing directory"); + chdir("/tmp/build"); + configure(extract_dir, p); + make(p); + make_install(p); + success("Package installed !"); +}