aboutsummaryrefslogtreecommitdiff
path: root/archive.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2019-12-18 01:14:48 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2019-12-18 01:14:48 -0500
commit2be3d5c5d905bd748b8ce511033065fa5a83a59c (patch)
tree740de985535e765c37deb61dd87b03e131c5d0bb /archive.c
parentfa992c8655f2fe27a97fe0e6768800a356de3744 (diff)
downloadspmc-2be3d5c5d905bd748b8ce511033065fa5a83a59c.tar.gz
Split up functions into different source files
Diffstat (limited to 'archive.c')
-rw-r--r--archive.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/archive.c b/archive.c
new file mode 100644
index 0000000..19269a8
--- /dev/null
+++ b/archive.c
@@ -0,0 +1,65 @@
+#include "spm.h"
+
+/**
+ * Extract a single file from a tar archive into a directory
+ *
+ * @param archive path to tar archive
+ * @param filename known path inside the archive to extract
+ * @param destination where to extract file to (must exist)
+ * @return
+ */
+int tar_extract_file(const char *archive, const char* filename, const char *destination) {
+ Process *proc = NULL;
+ int status;
+ char cmd[PATH_MAX];
+
+ sprintf(cmd, "tar xf %s -C %s %s 2>&1", archive, destination, filename);
+ shell(&proc, SHELL_OUTPUT, cmd);
+ if (!proc) {
+ fprintf(SYSERROR);
+ return -1;
+ }
+
+ status = proc->returncode;
+ shell_free(proc);
+
+ return status;
+}
+
+int tar_extract_archive(const char *_archive, const char *_destination) {
+ Process *proc = NULL;
+ int status;
+ char cmd[PATH_MAX];
+
+ char *archive = strdup(_archive);
+ if (!archive) {
+ fprintf(SYSERROR);
+ return -1;
+ }
+ char *destination = strdup(_destination);
+ if (!destination) {
+ fprintf(SYSERROR);
+ return -1;
+ }
+
+ // sanitize archive
+ strchrdel(archive, "&;|");
+ // sanitize destination
+ strchrdel(destination, "&;|");
+
+ sprintf(cmd, "tar xf %s -C %s 2>&1", archive, destination);
+ shell(&proc, SHELL_OUTPUT, cmd);
+ if (!proc) {
+ fprintf(SYSERROR);
+ free(archive);
+ free(destination);
+ return -1;
+ }
+
+ status = proc->returncode;
+ shell_free(proc);
+ free(archive);
+ free(destination);
+ return status;
+}
+