Date: Fri, 1 Mar 2002 22:04:34 +1100 (EST) From: Mark Hannon <markhannon@optushome.com.au> To: FreeBSD-gnats-submit@freebsd.org Subject: bin/35451: PATCH: pkg_add -r able to save local copy to PKG_SAVEDIR Message-ID: <200203011104.g21B4Yf80402@doorway.homeip.net>
next in thread | raw e-mail | index | archive | help
>Number: 35451 >Category: bin >Synopsis: PATCH: pkg_add -r able to save local copy to PKG_SAVEDIR >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Fri Mar 01 03:10:02 PST 2002 >Closed-Date: >Last-Modified: >Originator: Mark Hannon >Release: FreeBSD 4.5-RELEASE i386 >Organization: - >Environment: System: FreeBSD tbird.home.lan 4.5-RELEASE FreeBSD 4.5-RELEASE #0: Mon Jan 28 14:31:56 GMT 2002 murray@builder.freebsdmall.com:/usr/src/sys/compile/GENERIC i386 >Description: Included patch adds ability to save a local copy of the downloaded pkg.tgz file when using the pkg_add -r command. Pkg.tgz is downloaded to $PKG_SAVEDIR >How-To-Repeat: >Fix: Index: pkg_install/add/pkg_add.1 =================================================================== RCS file: /home/ncvs/src/usr.sbin/pkg_install/add/pkg_add.1,v retrieving revision 1.35.2.10 diff -c -r1.35.2.10 pkg_add.1 *** pkg_install/add/pkg_add.1 2001/12/14 16:48:11 1.35.2.10 --- pkg_install/add/pkg_add.1 2002/02/22 12:23:02 *************** *** 406,411 **** --- 406,416 ---- .Pa /usr/tmp with sufficient space. .Pp + If set, the environment variable + .Ev PKG_SAVEDIR + specifies a directory to which a copy of a remotely fetched package + will be saved. + .Pp The environment variable .Ev PACKAGEROOT specifies an alternate location for 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/02/22 12:23:22 *************** *** 151,156 **** --- 151,170 ---- int pfd[2], pstat, r, w; char *hint; int fd; + /* + * Additions to handle saving of local copy + */ + int do_copy; /* Set to 1 if saving local copy */ + int fd_copy; /* File Descriptor for local copy */ + int no_bytes_written; + char *contents_name_ptr; + char *newline_ptr; + char *pkg_name_ptr; + char *pkg_savedir; + char contents_line[8192]; + char pkg_long_name[FILENAME_MAX]; + char pkg_short_name[FILENAME_MAX]; + FILE *contents_file; rp = NULL; /* Special tip that sysinstall left for us */ *************** *** 196,201 **** --- 210,230 ---- else strcpy(fname, spec); + /* + * Check if PKG_SAVEDIR is set, if so then save a copy of the + * package to that directory. + */ + pkg_savedir = getenv("PKG_SAVEDIR"); + if (pkg_savedir) { + /* + * Set copy_short_name to $PKG_SAVEDIR/pkg_name.tgz + */ + strcpy(pkg_short_name, pkg_savedir); + strcat(pkg_short_name, "/"); + pkg_name_ptr = strrchr(fname, '/'); + 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); --- 256,327 ---- execl("/usr/bin/tar", "tar", Verbose ? "-xzvf" : "-xzf", "-", 0); _exit(2); } + close(pfd[0]); + + do_copy = 0; /* Default = no local copy */ + if (pkg_savedir) { /* If saving local copy then ... */ + 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; /* make local copy of package tar file */ + } else { + fprintf(stderr, "warning: unable to save %s\n", pkg_short_name); + do_copy = 0; + } + } + 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 rename the + * tar file to pkgname-version.tgz if needed. + */ + if ( do_copy == 1 ){ + if (no_bytes_written == -1) + warn("warning: error writing %s", pkg_short_name); + close(fd_copy); + /* + * 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 ){ + contents_name_ptr += 6; /* Move to start of actual name */ + strcpy(pkg_long_name, pkg_short_name); + pkg_name_ptr = strrchr(pkg_long_name, '/'); + *(++pkg_name_ptr) = '\0'; /* Remove the short base name .... */ + strcat(pkg_long_name, contents_name_ptr); + if ( ( newline_ptr = strrchr(pkg_long_name, '\n') ) != NULL) + *(newline_ptr) = '\0'; /* Remove carriage return */ + strcat(pkg_long_name, ".tgz"); + if ( rename( pkg_short_name, pkg_long_name) == -1) + warn("warning: unable to rename %s -> %s", + pkg_short_name, pkg_long_name); + break; + } + } + } + } + if (w == -1) warn("warning: error writing to tar"); tpid = waitpid(tpid, &pstat, 0); >Release-Note: >Audit-Trail: >Unformatted: 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?200203011104.g21B4Yf80402>