Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 9 Mar 2002 20:20:01 -0800 (PST)
From:      Mark Hannon <markhannon@optushome.com.au>
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/35451: PATCH: pkg_add -r able to save local copy to PKG_SAVEDIR
Message-ID:  <200203100420.g2A4K1m48321@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/35451; it has been noted by GNATS.

From: Mark Hannon <markhannon@optushome.com.au>
To: freebsd-gnats-submit@FreeBSD.org, markhannon@optushome.com.au
Cc:  
Subject: Re: bin/35451: PATCH: pkg_add -r able to save local copy to PKG_SAVEDIR
Date: Sun, 10 Mar 2002 15:12:08 +1100

 This is a multi-part message in MIME format.
 --------------B4919200D4BC990CD7DFF7DB
 Content-Type: text/plain; charset=us-ascii
 Content-Transfer-Encoding: 7bit
 
 Cleaned up version of the patches, taking into account comments from
 Simon Schubert.
 
 Rgds/mark
 --------------B4919200D4BC990CD7DFF7DB
 Content-Type: text/plain; charset=us-ascii;
  name="pkg_install.2"
 Content-Transfer-Encoding: 7bit
 Content-Disposition: inline;
  filename="pkg_install.2"
 
 Index: pkg_install/lib/file.c
 ===================================================================
 RCS file: /home/ncvs/src/usr.sbin/pkg_install/lib/file.c,v
 retrieving revision 1.40.2.9
 diff -c -r1.40.2.9 file.c
 *** pkg_install/lib/file.c	2001/10/23 09:16:04	1.40.2.9
 --- pkg_install/lib/file.c	2002/03/10 04:02:03
 ***************
 *** 151,156 ****
 --- 151,160 ----
       int pfd[2], pstat, r, w;
       char *hint;
       int fd;
 +     int do_copy, fd_copy, no_bytes_written;
 +     char *contents_name_ptr, *newline_ptr, *pkg_name_ptr, *pkg_savedir;		  
 +     char contents_line[8192], pkg_long_name[FILENAME_MAX], pkg_short_name[FILENAME_MAX];
 +     FILE *contents_file;
   
       rp = NULL;
       /* Special tip that sysinstall left for us */
 ***************
 *** 196,201 ****
 --- 200,225 ----
       else
   	strcpy(fname, spec);
   
 +     /* 
 +      * Check if PKG_SAVEDIR is set, if so then save a copy of the 
 +      * package to that directory.  During the download process the
 +      * filename is set to package_name.tgz (sans version info).
 +      * Subsequent to installation, when a +CONTENTS file is available,
 +      * the package is renamed to package_name-version.tgz
 +      */
 +     pkg_savedir = getenv("PKG_SAVEDIR");
 +     if (pkg_savedir) { 
 + 	pkg_name_ptr = strrchr(fname, '/');
 + 	if ( ( strlen(pkg_savedir) + strlen(pkg_name_ptr) + 1 ) > 
 + 		sizeof(pkg_short_name) ){
 + 	    fprintf(stderr, "Error: PKG_SAVEDIR/package_name.tgz is too long!\n");
 + 	    return NULL;
 + 	}
 + 	strcpy(pkg_short_name, pkg_savedir);
 + 	strcat(pkg_short_name, "/");
 + 	strcat(pkg_short_name, ++pkg_name_ptr); 
 +     }
 + 
       if ((ftp = fetchGetURL(fname, Verbose ? "v" : NULL)) == NULL) {
   	printf("Error: FTP Unable to get %s: %s\n",
   	       fname, fetchLastErrString);
 ***************
 *** 227,243 ****
   	execl("/usr/bin/tar", "tar", Verbose ? "-xzvf" : "-xzf", "-", 0);
   	_exit(2);
       }
       close(pfd[0]);
       for (;;) {
 ! 	if ((r = fread(buf, 1, sizeof buf, ftp)) < 1)
 ! 	    break;
 ! 	if ((w = write(pfd[1], buf, r)) != r)
 ! 	    break;
       }
       if (ferror(ftp))
   	warn("warning: error reading from server");
       fclose(ftp);
       close(pfd[1]);
       if (w == -1)
   	warn("warning: error writing to tar");
       tpid = waitpid(tpid, &pstat, 0);
 --- 251,329 ----
   	execl("/usr/bin/tar", "tar", Verbose ? "-xzvf" : "-xzf", "-", 0);
   	_exit(2);
       }
 +     
       close(pfd[0]);
 +    
 +     /*
 +      * Check if local copy is being made, if so then create a local file
 +      * to store the download in.
 +      */ 
 +     do_copy = 0;
 +     if (pkg_savedir) {
 + 	if ((fd_copy = open(pkg_short_name, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 
 + 			S_IRWXU|S_IRGRP|S_IROTH)) != -1 ){
 + 	    do_copy = 1;
 + 	} else {
 + 	    fprintf(stderr, "warning: unable to save %s\n", pkg_short_name);
 + 	}
 +     }
 + 
       for (;;) {
 !       if ((r = fread(buf, 1, sizeof buf, ftp)) < 1)
 ! 	break;
 !       if ((w = write(pfd[1], buf, r)) != r)
 ! 	break;
 !       if ( do_copy && ( (no_bytes_written = write(fd_copy, buf, r) ) != r) )
 ! 	break;
       }
 + 
       if (ferror(ftp))
   	warn("warning: error reading from server");
       fclose(ftp);
       close(pfd[1]);
 + 
 +     /*
 +      * In case of a local copy having been made then open the +CONTENTS
 +      * file, obtain the full package and version name and then rename 
 +      * the previously downloaded (shortform) package file.
 +      */
 +     if ( do_copy == 1 ){
 + 	if (no_bytes_written == -1)
 + 	    warn("warning: error writing %s", pkg_short_name);
 + 	close(fd_copy);
 + 	/* Use the short form as basis for the longform package name */
 + 	strcpy(pkg_long_name, pkg_short_name);
 + 	/* Remove the original short form package name */
 + 	pkg_name_ptr = strrchr(pkg_long_name, '/');
 + 	*(++pkg_name_ptr) = '\0';
 + 	/* Open the package +CONTENTS file and look for the real pkgname */
 + 	if ((contents_file = fopen(CONTENTS_FNAME, "r")) != NULL){
 + 	    while (fgets(contents_line, 8192, contents_file) != NULL){
 + 		/* Look for @name in line and use to rename package file */
 + 		if (((contents_name_ptr = strstr(contents_line, "@name ")) != NULL) &&
 + 			(strlen(contents_name_ptr) > 5) &&
 + 			(strlen(pkg_long_name) + strlen(contents_name_ptr) + 4) < 
 + 			sizeof(pkg_long_name)){
 + 		    /* Move to start of the actual package name */
 + 		    contents_name_ptr += 6;
 + 		    if ( ( newline_ptr = strrchr(contents_name_ptr, '\n') ) != NULL)
 + 			*(newline_ptr) = '\0'; /* Remove carriage return */ 
 + 		    /* Add packagename-version.tgz */
 + 		    strcat(pkg_long_name, contents_name_ptr);
 + 		    strcat(pkg_long_name, ".tgz");
 + 		    /* Actually rename file */
 + 		    if (rename(pkg_short_name, pkg_long_name) == -1)
 + 			warn("warning: unable to rename %s -> %s", 
 + 				pkg_short_name, pkg_long_name);
 + 		    break;
 + 		}
 + 	    }
 + 	} else {
 + 	    warn("warning: unable to open +CONTENTS file to rename %s -> %s", 
 + 		    pkg_short_name, pkg_long_name);
 + 	}
 +     }
 + 
       if (w == -1)
   	warn("warning: error writing to tar");
       tpid = waitpid(tpid, &pstat, 0);
 
 --------------B4919200D4BC990CD7DFF7DB--
 

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




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