PERFORCE change 178192 for review

Garrett Cooper gcooper at FreeBSD.org
Thu May 13 04:57:43 UTC 2010


http://p4web.freebsd.org/@@178192?ac=10

Change 178192 by gcooper at starr-bastion on 2010/05/13 04:56:45

	
	Add unpack_to_buffer API for unpacking a file from a package to a buffer.
	
	The way that this is called today is backwards, but it will be sorted out
	eventually.
	
	Also update comments here and there to remove implicitness and to make
	things more concrete.

Affected files ...

.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/file.c#7 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/pkg.h#3 edit

Differences ...

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/file.c#7 (text+ko) ====

@@ -344,6 +344,58 @@
 				 ARCHIVE_EXTRACT_FFLAGS|ARCHIVE_EXTRACT_XATTR)
 
 /*
+ * Unpack a single file, denoted by file, to a buffer; this call uses
+ * unpack_file_to_fd to first open the file, and once that has been completed
+ * it opens the file and proceeds to read it into the buffer -- either
+ * specified by buffer if buffer is not NULL, or attempts to allocate memory
+ * which will need to be freed by the user at a later date.
+ *
+ * Returns an address to a buffer with the contents of *file if successful, or
+ * returns NULL on failure.
+ */
+char*
+unpack_file_to_buffer(char *buffer, const char *pkg, const char *file)
+{
+
+	FILE *fd = NULL;
+	char *buf = buffer;
+	struct stat sb;
+
+	if ((fd = unpack_file_to_fd(pkg, file)) != NULL) {
+
+		if (fstat(fileno(fd), &sb) == 0) {
+
+			/*
+			 * User either passed in a non-NULL value or we need
+			 * to malloc on the fly and let the user deal with it
+			 * later.
+			 */
+			if (buf != NULL)
+				buf = malloc(sb.st_size);
+			if (buf != NULL) {
+
+				if (fread(buf, sb.st_size, 1, fd) !=
+				    sb.st_size) {
+
+					/* 
+					 * Don't try to free user specified
+					 * memory.
+					 */
+					if (buffer == NULL)
+						free(buf);
+
+				}	
+
+			}
+
+		}
+	}
+
+	return buffer;
+
+}
+
+/*
  * Unpack a single file from a tar-file to a file descriptor; this is written
  * like so as an optimization to abbreviate the extract to *open step, as well
  * as to reduce the number of required steps needed when unpacking a tarball on
@@ -361,8 +413,7 @@
  *
  * [The return code info will eventually be...]
  *
- * Return -1 on failure, a value greater than 0 on success [in accordance with
- * open(2)].
+ * Return -1 on failure, a value greater than 0 on success.
  */
 FILE*
 unpack_file_to_fd(const char *pkg, const char *file)
@@ -474,7 +525,8 @@
  * Return 0 on success, 1 on failure
  *
  * NOTE: the exit code is 0 / 1 so that this can be fed directly into exit
- * when doing piped tar commands for copying hierarchies *hint*, *hint*.
+ * when doing piped tar commands for copying hierarchies *hint*, *hint* -- this
+ * use may be short-lived though, so don't depend on the return value, mmk?
  */
 int
 unpack(const char *pkg, const char *file_expr)

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/pkg.h#3 (text+ko) ====

@@ -189,7 +189,8 @@
 void		copy_hierarchy(const char *, const char *, Boolean);
 int		delete_hierarchy(const char *, Boolean, Boolean);
 int		unpack(const char *, const char *);
-FILE*		unpack_file_to_fd(const char *pkg, const char *file);
+char*		unpack_file_to_buffer(char *, const char *, const char *);
+FILE*		unpack_file_to_fd(const char *, const char *);
 void		format_cmd(char *, int, const char *, const char *, const char *);
 
 /* Msg */


More information about the p4-projects mailing list