aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2020-07-31 16:24:59 +0200
committerJan200101 <sentrycraft123@gmail.com>2020-07-31 16:24:59 +0200
commitaabfdfe3fcbcf718b30872c98568581d7e7e6cca (patch)
treeb8b1d94814c9d339c6d6bb019f1fff9e4ccb2406
parent83e4ea0f70ba75f337615ee847f85d4be630686b (diff)
downloadpolecat-aabfdfe3fcbcf718b30872c98568581d7e7e6cca.tar.gz
polecat-aabfdfe3fcbcf718b30872c98568581d7e7e6cca.zip
implement config dirs and unpack tars to them0.1.1
-rw-r--r--Makefile10
-rw-r--r--src/config.c44
-rw-r--r--src/config.h11
-rw-r--r--src/main.c18
-rw-r--r--src/tar.c102
-rw-r--r--src/tar.h6
-rw-r--r--src/wine.c24
7 files changed, 201 insertions, 14 deletions
diff --git a/Makefile b/Makefile
index bae52c5..0361e08 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
# GENERAL VARIABLES
NAME := polecat
-VERSION := 0.1.0
+VERSION := 0.1.1
TARGET ?= debug
DEBUG := 0
ifeq ($(TARGET),debug)
@@ -45,17 +45,17 @@ ifeq ($(DEBUG),0)
else
COMMONFLAGS += -g
endif
-CFLAGS := $(COMMONFLAGS) -Wall `$(PKGCONFIG) json-c --cflags` `$(CURLCONFIG) --cflags`
-LDFLAGS := `$(PKGCONFIG) json-c --libs` `$(CURLCONFIG) --libs`
+CFLAGS := $(COMMONFLAGS) -Wall `$(PKGCONFIG) json-c --cflags` `$(PKGCONFIG) libarchive --cflags` `$(CURLCONFIG) --cflags`
+LDFLAGS := `$(PKGCONFIG) json-c --libs` `$(PKGCONFIG) libarchive --libs` `$(CURLCONFIG) --libs`
DEFINES := -DNAME=\"$(NAME)\" -DVERSION=\"$(VERSION)\"
ifeq ($(DEBUG),1)
DEFINES += -DDEBUG
endif
# SOURCE CODE AND OBJECT FILES
-CC_SRC_FILES := $(wildcard $(SRC_DIR)/*.c)
+CC_SRC_FILES := $(wildcard $(SRC_DIR)/*.c)
-OBJ_FILES := $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/$(TARGET)/%.o, $(CC_SRC_FILES))
+OBJ_FILES := $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/$(TARGET)/%.o, $(CC_SRC_FILES))
# TARGETS
diff --git a/src/config.c b/src/config.c
new file mode 100644
index 0000000..f691648
--- /dev/null
+++ b/src/config.c
@@ -0,0 +1,44 @@
+#include <string.h>
+#include <stdio.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "config.h"
+#include "common.h"
+
+void getXDGDir(const char* envvar, const char* homeext, char* config)
+{
+ char* xdg_var = getenv(envvar);
+
+ if (xdg_var)
+ {
+ strcpy(config, xdg_var);
+ }
+ else
+ {
+ char* home = getenv("HOME");
+ strcpy(config, home);
+ strcat(config, homeext);
+ }
+}
+
+void getConfigDir(char* config)
+{
+ getXDGDir("XDG_CONFIG_HOME", "/.config/" NAME, config);
+}
+
+void getDataDir(char* config)
+{
+ getXDGDir("XDG_DATA_HOME", "/.local/share/" NAME, config);
+}
+
+void makeDir(const char* path)
+{
+ struct stat st = {0};
+
+ if (stat(path, &st) == -1)
+ {
+ mkdir(path, 0755);
+ }
+} \ No newline at end of file
diff --git a/src/config.h b/src/config.h
new file mode 100644
index 0000000..cba95d8
--- /dev/null
+++ b/src/config.h
@@ -0,0 +1,11 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#include <stdlib.h>
+
+void getConfigDir(char* config);
+void getDataDir(char* config);
+
+void makeDir(const char* path);
+
+#endif \ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 2e7655d..f1775e3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,10 +5,11 @@
#include "wine.h"
#include "dxvk.h"
#include "common.h"
+#include "config.h"
const static struct Command main_commands[] = {
{ .name = "wine", .func = wine, .description = "manage wine versions" },
- { .name = "dxvk", .func = dxvk, .description = "manage dxvk versions" },
+ { .name = "dxvk", .func = dxvk, .description = "manage dxvk versions (TODO)" },
{ .name = "info", .func = main_info, .description = "show some information about polecat" },
{ .name = "help", .func = main_help, .description = "displays this message" },
};
@@ -29,10 +30,19 @@ int main(int argc, char** argv)
int main_info(int argc, char** argv)
{
- printf("Version:\t\t%s\n"
- "User-Agent:\t\t%s/%s\n",
+ char cfgdir[256];
+ char datadir[256];
+
+ getConfigDir(cfgdir);
+ getDataDir(datadir);
+
+ printf("version:\t\t%s\n"
+ "user-Agent:\t\t%s/%s\n"
+ "config dir\t\t%s\n"
+ "data dir\t\t%s\n",
VERSION,
- NAME, VERSION);
+ NAME, VERSION,
+ cfgdir, datadir);
return 0;
}
diff --git a/src/tar.c b/src/tar.c
new file mode 100644
index 0000000..3f2983b
--- /dev/null
+++ b/src/tar.c
@@ -0,0 +1,102 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <archive.h>
+#include <archive_entry.h>
+#include <fcntl.h>
+
+static int copy_data(struct archive* ar, struct archive* aw)
+{
+ int r;
+ const void *buff;
+ size_t size;
+ la_int64_t offset;
+
+ for (;;) {
+ r = archive_read_data_block(ar, &buff, &size, &offset);
+ if (r == ARCHIVE_EOF)
+ return (ARCHIVE_OK);
+ if (r < ARCHIVE_OK)
+ return (r);
+ r = archive_write_data_block(aw, buff, size, offset);
+ if (r < ARCHIVE_OK) {
+ fprintf(stderr, "%s\n", archive_error_string(aw));
+ return (r);
+ }
+ }
+}
+
+void extract(const char* filename, const char* outputdir)
+{
+ char cwd[256];
+ getcwd(cwd, sizeof(cwd));
+
+ if (chdir(outputdir) < 0)
+ {
+ fprintf(stderr, "Cannot change to %s\n", outputdir);
+ return;
+ }
+
+ struct archive* a;
+ struct archive* ext;
+ struct archive_entry* entry;
+ int flags, r;
+
+ /* Select which attributes we want to restore. */
+ flags = ARCHIVE_EXTRACT_TIME;
+ flags |= ARCHIVE_EXTRACT_PERM;
+ flags |= ARCHIVE_EXTRACT_ACL;
+ flags |= ARCHIVE_EXTRACT_FFLAGS;
+
+ a = archive_read_new();
+ archive_read_support_format_all(a);
+ archive_read_support_compression_all(a);
+ ext = archive_write_disk_new();
+ archive_write_disk_set_options(ext, flags);
+ archive_write_disk_set_standard_lookup(ext);
+ if ((r = archive_read_open_filename(a, filename, 10240))) return;
+
+ for (;;)
+ {
+ r = archive_read_next_header(a, &entry);
+ if (r == ARCHIVE_EOF)
+ {
+ break;
+ }
+
+ if (r < ARCHIVE_OK)
+ {
+ fprintf(stderr, "%s\n", archive_error_string(a));
+ }
+
+ if (r < ARCHIVE_WARN)
+ {
+ return;
+ }
+
+ r = archive_write_header(ext, entry);
+ if (r < ARCHIVE_OK)
+ {
+ fprintf(stderr, "%s\n", archive_error_string(ext));
+ }
+ else if (archive_entry_size(entry) > 0)
+ {
+ r = copy_data(a, ext);
+ if (r < ARCHIVE_OK)
+ fprintf(stderr, "%s\n", archive_error_string(ext));
+ if (r < ARCHIVE_WARN)
+ return;
+ }
+
+ r = archive_write_finish_entry(ext);
+ if (r < ARCHIVE_OK)
+ fprintf(stderr, "%s\n", archive_error_string(ext));
+ if (r < ARCHIVE_WARN)
+ return;
+ }
+ archive_read_close(a);
+ archive_read_free(a);
+ archive_write_close(ext);
+ archive_write_free(ext);
+
+ if (cwd != NULL) chdir(cwd);
+} \ No newline at end of file
diff --git a/src/tar.h b/src/tar.h
new file mode 100644
index 0000000..4235583
--- /dev/null
+++ b/src/tar.h
@@ -0,0 +1,6 @@
+#ifndef TAR_H
+#define TAR_H
+
+void extract(const char* filename, const char* outputdir);
+
+#endif \ No newline at end of file
diff --git a/src/wine.c b/src/wine.c
index 2c304ed..82ab655 100644
--- a/src/wine.c
+++ b/src/wine.c
@@ -2,10 +2,13 @@
#include <string.h>
#include <json.h>
#include <libgen.h>
+#include <unistd.h>
#include "wine.h"
#include "net.h"
+#include "tar.h"
#include "common.h"
+#include "config.h"
const static struct Command wine_commands[] = {
{ .name = "install", .func = wine_install, .description = "download and install a wine version from lutris" },
@@ -26,7 +29,6 @@ int wine(int argc, char** argv)
return wine_help(argc, argv);
}
-
int wine_install(int argc, char** argv)
{
if (argc == 4)
@@ -42,7 +44,7 @@ int wine_install(int argc, char** argv)
if (choice > json_object_array_length(versions) - 1 || choice < 0)
{
- printf("`%i' is not a valid ID\n\nrun `polecat wine list' to get a valid ID", choice);
+ fprintf(stderr, "`%i' is not a valid ID\n\nrun `polecat wine list' to get a valid ID\n", choice);
}
else
{
@@ -51,10 +53,22 @@ int wine_install(int argc, char** argv)
json_object_object_get_ex(value, "url", &url);
char* name = basename((char*)json_object_get_string(url));
+
+ char datadir[256];
+ char downloadpath[256];
+
+ getDataDir(datadir);
+ makeDir(datadir);
+
+ strcpy(downloadpath, datadir);
+ strcat(downloadpath, "/");
+ strcat(downloadpath, name);
- printf("Downloading %s", name);
- downloadFile(json_object_get_string(url), name);
- printf("\nDone\n");
+ fprintf(stderr, "Downloading %s\n", name);
+ downloadFile(json_object_get_string(url), downloadpath);
+ fprintf(stderr, "Extracting %s\n", name);
+ extract(downloadpath, datadir);
+ fprintf(stderr, "Done\n");
}
}
}