Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 15 Dec 2009 15:52:43 +0100
From:      Baptiste Daroussin <baptiste.daroussin@gmail.com>
To:        freebsd-current@freebsd.org
Subject:   Small patches for pkg_add
Message-ID:  <20091215145243.GF1016@wicklow.lan>

next in thread | raw e-mail | index | archive | help

--ADZbWkCsHQ7r3kzd
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

I've written a small patch for pkg_install that removes the system call to tar
in pkg_add command replacing it by some libarchive code.

I'm planning to do some cleanup by time to time sending small patches on
pkg_install. My very first goal is to remove most of the system call, and to
understand the way the code works, not to rewrite everything.

Here is the first patch :
- modification of the unpack function to use libarchive to extract data
- it does not modify the way things works: it does exactly the same thing
  without the system call
- I also removed two unused function from lib/file.c

It is my first patch for src, I don't know the good process for that.
Hope to send more patches soon.

This is not another attempt to rewrite pkgtools, I just want to improve them the
time that people working on the different rewrites finish their work.

the patch is join to this mail, or can be found at the following link:
http://etoilebsd.net/~bapt/pkgtools-libarchive-patch1.patch

regards
Bapt

--ADZbWkCsHQ7r3kzd
Content-Type: text/x-diff; charset=iso-8859-1
Content-Disposition: attachment; filename="pkgtools-libarchive-patch1.patch"
Content-Transfer-Encoding: 8bit

Index: delete/Makefile
===================================================================
--- delete/Makefile	(révision 199967)
+++ delete/Makefile	(copie de travail)
@@ -9,6 +9,6 @@
 WFORMAT?=	1
 
 DPADD=	${LIBINSTALL} ${LIBMD}
-LDADD=	${LIBINSTALL} -lmd
+LDADD=	${LIBINSTALL} -lmd -larchive
 
 .include <bsd.prog.mk>
Index: create/Makefile
===================================================================
--- create/Makefile	(révision 199967)
+++ create/Makefile	(copie de travail)
@@ -9,6 +9,6 @@
 WFORMAT?=	1
 
 DPADD=	${LIBINSTALL} ${LIBMD}
-LDADD=	${LIBINSTALL} -lmd
+LDADD=	${LIBINSTALL} -lmd -larchive
 
 .include <bsd.prog.mk>
Index: version/Makefile
===================================================================
--- version/Makefile	(révision 199967)
+++ version/Makefile	(copie de travail)
@@ -9,7 +9,7 @@
 WFORMAT?=	1
 
 DPADD=	${LIBINSTALL} ${LIBFETCH} ${LIBMD}
-LDADD=	${LIBINSTALL} -lfetch -lmd
+LDADD=	${LIBINSTALL} -lfetch -lmd -larchive
 
 test:
 	sh ${.CURDIR}/test-pkg_version.sh
Index: lib/file.c
===================================================================
--- lib/file.c	(révision 199967)
+++ lib/file.c	(copie de travail)
@@ -22,10 +22,13 @@
 __FBSDID("$FreeBSD$");
 
 #include "lib.h"
+#include <archive.h>
+#include <archive_entry.h>
 #include <err.h>
 #include <pwd.h>
 #include <time.h>
 #include <sys/wait.h>
+#include <stdbool.h>
 
 /* Quick check to see if a file exists */
 Boolean
@@ -293,68 +296,49 @@
     }
 }
 
-/*
- * Copy a hierarchy (possibly from dir) to the current directory, or
- * if "to" is TRUE, from the current directory to a location someplace
- * else.
- *
- * Though slower, using tar to copy preserves symlinks and everything
- * without me having to write some big hairy routine to do it.
- */
-void
-copy_hierarchy(const char *dir, const char *fname, Boolean to)
-{
-    char cmd[FILENAME_MAX * 3];
-
-    if (!to) {
-	/* If absolute path, use it */
-	if (*fname == '/')
-	    dir = "/";
-	snprintf(cmd, FILENAME_MAX * 3, "/usr/bin/tar cf - -C %s %s | /usr/bin/tar xpf -",
-		 dir, fname);
-    }
-    else
-	snprintf(cmd, FILENAME_MAX * 3, "/usr/bin/tar cf - %s | /usr/bin/tar xpf - -C %s",
-		 fname, dir);
-#ifdef DEBUG
-    printf("Using '%s' to copy trees.\n", cmd);
-#endif
-    if (system(cmd)) {
-	cleanup(0);
-	errx(2, "%s: could not perform '%s'", __func__, cmd);
-    }
-}
-
 /* Unpack a tar file */
 int
 unpack(const char *pkg, const char *flist)
 {
-    const char *comp, *cp;
-    char suff[80];
-
-    comp = "";
-    /*
-     * Figure out by a crude heuristic whether this or not this is probably
-     * compressed and whichever compression utility was used (gzip or bzip2).
-     */
-    if (strcmp(pkg, "-")) {
-	cp = strrchr(pkg, '.');
-	if (cp) {
-	    strcpy(suff, cp + 1);
-	    if (strchr(suff, 'z') || strchr(suff, 'Z')) {
-		if (strchr(suff, 'b'))
-		    comp = "-j";
-		else
-		    comp = "-z";
-	    }
-	}
-    }
+    int r;
+    bool whole_archive=false;
+    struct archive *a = archive_read_new();
+    struct archive_entry *entry;
+    const int archive_flags = ARCHIVE_EXTRACT_OWNER| ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_TIME;
+    if(flist == NULL)
+	whole_archive=true;
     else
-	comp = "-j";
-    if (vsystem("/usr/bin/tar -xp %s -f '%s' %s", comp, pkg, flist ? flist : "")) {
-	warnx("tar extract of %s failed!", pkg);
+	if(strlen(flist) == 0)
+	    whole_archive=true;
+    archive_read_support_compression_all(a);
+    archive_read_support_format_all(a);
+    r = archive_read_open_filename(a, pkg, 16384);
+    if (r != ARCHIVE_OK) {
+	archive_read_finish(a);
 	return 1;
-    }
+    } 
+    if(!whole_archive) {
+	while ( ( r = archive_read_next_header(a, &entry)) == ARCHIVE_OK) {
+	    if (strcmp(archive_entry_pathname(entry), flist)==0) {
+		r = archive_read_extract(a, entry, archive_flags);
+		if (r != ARCHIVE_OK) {
+		    warnx("tar extract of %s failed", pkg);
+		    archive_read_finish(a);
+		    return 1;
+		}
+	    } else
+		archive_read_data_skip(a);
+	}
+    } else
+	while ((archive_read_next_header(a, &entry)) == ARCHIVE_OK) {
+	    r = archive_read_extract(a, entry, archive_flags);
+	    if (r != ARCHIVE_OK) {
+		warnx("tar extract of %s failed", pkg);
+		archive_read_finish(a);
+		return 1;
+	    }
+	}
+    archive_read_finish(a);
     return 0;
 }
 
Index: lib/lib.h
===================================================================
--- lib/lib.h	(révision 199967)
+++ lib/lib.h	(copie de travail)
@@ -189,7 +189,6 @@
 void		write_file(const char *, const char *);
 void		copy_file(const char *, const char *, const char *);
 void		move_file(const char *, const char *, const char *);
-void		copy_hierarchy(const char *, const char *, Boolean);
 int		delete_hierarchy(const char *, Boolean, Boolean);
 int		unpack(const char *, const char *);
 void		format_cmd(char *, int, const char *, const char *, const char *);
Index: add/perform.c
===================================================================
--- add/perform.c	(révision 199967)
+++ add/perform.c	(copie de travail)
@@ -128,7 +128,7 @@
 		    warnx("can't stat package file '%s'", pkg_fullname);
 		    goto bomb;
 		}
-		sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
+		sprintf(extract_contents, "%s", CONTENTS_FNAME);
 		extract = extract_contents;
 	    }
 	    else {
Index: add/Makefile
===================================================================
--- add/Makefile	(révision 199967)
+++ add/Makefile	(copie de travail)
@@ -9,6 +9,6 @@
 WFORMAT?=	1
 
 DPADD=	${LIBINSTALL} ${LIBFETCH} ${LIBMD}
-LDADD=	${LIBINSTALL} -lfetch -lmd
+LDADD=	${LIBINSTALL} -lfetch -lmd -larchive
 
 .include <bsd.prog.mk>
Index: info/Makefile
===================================================================
--- info/Makefile	(révision 199967)
+++ info/Makefile	(copie de travail)
@@ -9,6 +9,6 @@
 WFORMAT?=	1
 
 DPADD=	${LIBINSTALL} ${LIBFETCH} ${LIBMD}
-LDADD=	${LIBINSTALL} -lfetch -lmd
+LDADD=	${LIBINSTALL} -lfetch -lmd -larchive
 
 .include <bsd.prog.mk>
Index: updating/Makefile
===================================================================
--- updating/Makefile	(révision 199967)
+++ updating/Makefile	(copie de travail)
@@ -9,6 +9,6 @@
 WFORMAT?= 1
 
 DPADD=	${LIBINSTALL} ${LIBFETCH} ${LIBMD}
-LDADD=	${LIBINSTALL} -lfetch -lmd
+LDADD=	${LIBINSTALL} -lfetch -lmd -larchive
 
 .include <bsd.prog.mk>

--ADZbWkCsHQ7r3kzd--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20091215145243.GF1016>