Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 5 Jul 2005 10:32:05 GMT
From:      soc-andrew <soc-andrew@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 79593 for review
Message-ID:  <200507051032.j65AW5Z0072360@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=79593

Change 79593 by soc-andrew@soc-andrew_serv on 2005/07/05 10:31:48

	Implement a callback for when the next file is opened for reading by libarchive.

Affected files ...

.. //depot/projects/soc2005/bsdinstaller/src/lib/bsdinstaller/installer/dist.c#3 edit
.. //depot/projects/soc2005/bsdinstaller/src/lib/bsdinstaller/installer/extract.c#2 edit
.. //depot/projects/soc2005/bsdinstaller/src/lib/bsdinstaller/installer/extract.h#2 edit

Differences ...

==== //depot/projects/soc2005/bsdinstaller/src/lib/bsdinstaller/installer/dist.c#3 (text+ko) ====

@@ -41,6 +41,8 @@
 static char base[PATH_MAX];
 static char location[PATH_MAX];
 
+static void dist_change_file_callback(int, char *);
+
 /*
  * Process a .inf file
  * dir is the directory relative to base, eg src
@@ -139,6 +141,15 @@
   free(f);
 }
 
+static void
+dist_change_file_callback(int pcnt, char *name) {
+  printf("%d%%", pcnt);
+  if (name)
+    printf(" %s", name);
+
+  putchar('\n');
+}
+
 void
 dist_extract(const char *dist) {
   struct inf_file * inf;
@@ -166,7 +177,7 @@
   archive_read_support_compression_all(a);
   archive_read_support_format_all(a);
   if(read_open_split(a, basename, (inf->pieces / 26) + 'a',
-                     (inf->pieces % 26) + 'a') != ARCHIVE_OK) {
+         (inf->pieces % 26) + 'a', dist_change_file_callback) != ARCHIVE_OK) {
     printf("ERROR: %s\n", archive_error_string(a));
     archive_read_finish(a);
     inf_free(inf);

==== //depot/projects/soc2005/bsdinstaller/src/lib/bsdinstaller/installer/extract.c#2 (text+ko) ====

@@ -24,8 +24,11 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <sys/param.h>
+
 #include <archive.h>
 #include <errno.h>
+#include <libgen.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -35,11 +38,12 @@
 #define block_size 10240
 
 struct file_list {
-	char  *basename;   /* The name of the file without the last 2 characters */
+	char  *basename;   /* Name of the file without the last 2 characters */
 	char   end[2];     /* The characters to end on */
 	char   current[2]; /* The current characters to use */
 	FILE  *fd;
 	void  *buff;
+	void   (*new_file)(int, char*);
 };
 
 static int open_current(struct archive *a, struct file_list *fl);
@@ -53,28 +57,29 @@
  */
 static int
 open_current(struct archive *a, struct file_list *fl) {
-  char *file = malloc(strlen(fl->basename) + 4);
-  if (file == NULL) {
-    archive_set_error(a, ENOMEM, "No memory");
-    return ARCHIVE_FATAL;
-  }
+  char file[PATH_MAX];
 
   if (fl->fd != NULL)
     fclose(fl->fd);
 
-  sprintf(file, "%s%c%c", fl->basename, fl->current[0], fl->current[1]);
+  snprintf(file, PATH_MAX, "%s%c%c", fl->basename, fl->current[0], fl->current[1]);
 
   fl->fd = fopen(file, "r");
 
+  if(fl->new_file != NULL) {
+    int file_no = (fl->current[0] - 'a') * 26 + (fl->current[1] - 'a');
+    int total = (fl->end[0] - 'a') * 26 + (fl->end[1] - 'a');
+    snprintf(file, PATH_MAX, "%s%c%c", basename(fl->basename),
+             fl->current[0], fl->current[1]);
+    (*fl->new_file)((int)(((double)file_no/total)*100), file);
+  }
+
   if (fl->fd == NULL) {
     /* XXX use archive_set_error() */
     archive_set_error(a, ENOENT, "File not found %s", file);
-    free(file);
     return ARCHIVE_FATAL;
   }
 
-  free(file);
-
   return ARCHIVE_OK;
 }
 
@@ -132,9 +137,13 @@
 
 static int
 close_callback(struct archive *a __unused, void *client_data) {
-  fclose(((struct file_list*)client_data)->fd);
+  struct file_list *fl = client_data;
+
+  if(fl->new_file != NULL) {
+    (*fl->new_file)(100, NULL);
+  }
 
-  struct file_list *fl = client_data;
+  fclose(fl->fd);
 
   free(fl->buff);
   free(fl->basename);
@@ -148,7 +157,7 @@
  * extract them in a similar way to archive_read_open_fd
  */
 int
-read_open_split(struct archive *a, const char *base_name, char end_0, char end_1) {
+read_open_split(struct archive *a, const char *base_name, char end_0, char end_1, void (*nf)(int, char*)) {
   struct file_list *fl;
 
   fl = malloc(sizeof(struct file_list));
@@ -157,6 +166,8 @@
     return ARCHIVE_FATAL;
   }
 
+  fl->new_file = nf;
+
   fl->current[0] = 'a';
   fl->current[1] = 'a';
 
@@ -171,3 +182,4 @@
 
   return ARCHIVE_OK;
 }
+

==== //depot/projects/soc2005/bsdinstaller/src/lib/bsdinstaller/installer/extract.h#2 (text+ko) ====

@@ -27,6 +27,6 @@
 #ifndef _EXTRACT_H_
 #define _EXTRACT_H_
 
-int read_open_split(struct archive *a, const char *base_name, char end_0, char end_1);
+int read_open_split(struct archive *a, const char *base_name, char end_0, char end_1, void (*nf)(int, char*));
 
 #endif /* _EXTRACT_H_ */



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