diff options
author | jkh <jkh@FreeBSD.org> | 1995-10-25 15:38:37 +0000 |
---|---|---|
committer | jkh <jkh@FreeBSD.org> | 1995-10-25 15:38:37 +0000 |
commit | d4d9564530f574c891a76bc0ca7af4c9275e9442 (patch) | |
tree | 38f96dd54eeecc8a263b88c6c73dd075dcbadb38 /usr.sbin/pkg_install/lib | |
parent | a31022b303ea5ec7c096c6165e363e3fd1058fee (diff) | |
download | FreeBSD-src-d4d9564530f574c891a76bc0ca7af4c9275e9442.zip FreeBSD-src-d4d9564530f574c891a76bc0ca7af4c9275e9442.tar.gz |
Bring forward my changes from 2.1
Diffstat (limited to 'usr.sbin/pkg_install/lib')
-rw-r--r-- | usr.sbin/pkg_install/lib/file.c | 136 | ||||
-rw-r--r-- | usr.sbin/pkg_install/lib/lib.h | 9 | ||||
-rw-r--r-- | usr.sbin/pkg_install/lib/pen.c | 124 | ||||
-rw-r--r-- | usr.sbin/pkg_install/lib/str.c | 12 |
4 files changed, 188 insertions, 93 deletions
diff --git a/usr.sbin/pkg_install/lib/file.c b/usr.sbin/pkg_install/lib/file.c index 829fa12..86e0005 100644 --- a/usr.sbin/pkg_install/lib/file.c +++ b/usr.sbin/pkg_install/lib/file.c @@ -1,5 +1,5 @@ #ifndef lint -static const char *rcsid = "$Id: file.c,v 1.15 1995/08/01 07:16:50 jkh Exp $"; +static const char *rcsid = "$Id: file.c,v 1.10.4.6 1995/10/15 14:08:40 jkh Exp $"; #endif /* @@ -103,6 +103,8 @@ isURL(char *fname) * also be looking for here, but for now I'll just be happy to get ftp * working. */ + if (!fname) + return FALSE; while (isspace(*fname)) ++fname; if (!strncmp(fname, "ftp://", 6)) @@ -164,24 +166,58 @@ fileURLFilename(char *fname, char *where, int max) #define HOSTNAME_MAX 64 /* - * Try and fetch a file by URL, returning the fd of open - * file if fetched successfully. + * Try and fetch a file by URL, returning the directory name for where + * it's unpacked, if successful. */ char * -fileGetURL(char *fname) +fileGetURL(char *base, char *spec) { char host[HOSTNAME_MAX], file[FILENAME_MAX], dir[FILENAME_MAX]; - char pword[HOSTNAME_MAX + 40], *uname, *cp; - static char tmpl[40]; + char pword[HOSTNAME_MAX + 40], *uname, *cp, *rp, *tmp; + char fname[511]; + char pen[FILENAME_MAX]; struct passwd *pw; FTP_t ftp; + pid_t tpid; int fd, fd2, i, len = 0; char ch; time_t start, stop; - if (!isURL(fname)) - return NULL; - + rp = NULL; + if (!isURL(spec)) { + int len; + + if (!base) + return NULL; + /* We've been given an existing URL (that's known-good) and now we need + to construct a composite one out of that and the basename we were + handed as a dependency. */ + strncpy(fname, base, 511); + fname[511] = '\0'; + cp = strrchr(fname, '/'); + if (cp) { + *cp = '\0'; /* Eliminate the filename portion */ + len = strlen(fname); + /* Special case for the all category */ + if (len > 3 && !strcmp(cp - 3, "All")) + sprintf(cp, "/%s", spec); + else { + /* Replace category with All */ + if ((cp = strrchr(fname, '/')) != NULL) { + strcat(cp + 1, "All/"); + strcat(cp + 4, spec); + } + else { + strcat(fname, "All/"); + strcat(fname, spec); + } + } + } + else + return NULL; + } + else + strcpy(fname, spec); ftp = FtpInit(); cp = fileURLHost(fname, host, HOSTNAME_MAX); if (!*cp) { @@ -206,9 +242,9 @@ fileGetURL(char *fname) } else snprintf(pword, HOSTNAME_MAX + 40, "%s@%s", pw->pw_name, host); - if (Verbose) - printf("Trying to fetch %s from %s.\n", file, host); + if (Verbose) + printf("Trying to log into %s as %s.\n", host, uname); FtpOpen(ftp, host, uname, pword); if (getenv("FTP_PASSIVE_MODE")) FtpPassive(ftp, TRUE); @@ -223,41 +259,77 @@ fileGetURL(char *fname) } FtpBinary(ftp, TRUE); if (Verbose) printf("FTP: trying to get %s\n", basename_of(file)); - fd = FtpGet(ftp, basename_of(file)); - if (fd < 0) - return NULL; - if ((cp = getenv("PKG_TMPDIR")) != NULL) - sprintf(tmpl, "%s/instpkg-XXXXXX.tgz", cp); - else - strcpy(tmpl, "/var/tmp/instpkg-XXXXXX.tgz"); - fd2 = mkstemp(tmpl); - if (fd2 < 0) { - whinge("Unable to create a temporary file for ftp: %s", tmpl); - return NULL; - } - if (Verbose) printf("FTP: Trying to copy from ftp connection to temporary: %s\n", tmpl); - (void)time(&start); - while (read(fd, &ch, 1) == 1) { - ++len; - write(fd2, &ch, 1); + tmp = basename_of(file); + if (!strstr(tmp, ".tgz")) + tmp = strconcat(tmp, ".tgz"); + fd = FtpGet(ftp, tmp); + if (fd >= 0) { + pen[0] = '\0'; + if (rp = make_playpen(pen, 0)) { + if (Verbose) + printf("Extracting from FTP connection into %s\n", pen); + tpid = fork(); + if (!tpid) { + dup2(fd, 0); + i = execl("/usr/bin/tar", "tar", Verbose ? "-xzvf" : "-xzf", "-", 0); + if (Verbose) + printf("tar command returns %d status\n", i); + exit(i); + } + else { + int pstat; + + close(fd); + tpid = waitpid(tpid, &pstat, 0); + } + } + else + printf("Error: Unable to construct a new playpen for FTP!\n"); } - (void)time(&stop); - if (Verbose) printf("FTP: Read %d bytes from connection, %d elapsed seconds.\n%FTP: Average transfer rate: %d bytes/second.\n", len, stop - start, len / ((stop - start) + 1)); + else + printf("Error: FTP Unable to get %s\n", basename_of(file)); FtpEOF(ftp); FtpClose(ftp); - return tmpl; + return rp; } char * -fileFindByPath(char *fname) +fileFindByPath(char *base, char *fname) { static char tmp[FILENAME_MAX]; char *cp; + int len; if (fexists(fname) && isfile(fname)) { strcpy(tmp, fname); return tmp; } + if (base) { + strcpy(tmp, base); + cp = strchr(tmp, '/'); + len = strlen(tmp); + + if (cp) { + /* Special case for the all category */ + if (len > 3 && !strncmp(cp - 3, "All/", 4)) + strcat(cp + 1, fname); + else { + *cp = '\0'; + /* Replace category with All */ + if ((cp = strrchr(tmp, '/')) != NULL) { + strcat(cp + 1, "All/"); + strcat(cp, fname); + } + else { + strcat(tmp, "All/"); + strcat(tmp, fname); + } + } + } + if (fexists(tmp)) + return tmp; + } + cp = getenv("PKG_PATH"); while (cp) { char *cp2 = strsep(&cp, ":"); diff --git a/usr.sbin/pkg_install/lib/lib.h b/usr.sbin/pkg_install/lib/lib.h index a5c2fa5..d0e770b 100644 --- a/usr.sbin/pkg_install/lib/lib.h +++ b/usr.sbin/pkg_install/lib/lib.h @@ -1,4 +1,4 @@ -/* $Id: lib.h,v 1.17 1995/07/30 01:44:45 ache Exp $ */ +/* $Id: lib.h,v 1.18 1995/08/26 10:15:12 jkh Exp $ */ /* * FreeBSD install - a package for the installation and maintainance @@ -107,8 +107,8 @@ typedef struct _pack Package; int vsystem(const char *, ...); void cleanup(int); char *make_playpen(char *, size_t); -void leave_playpen(void); char *where_playpen(void); +void leave_playpen(char *); size_t min_free(char *); /* String */ @@ -118,6 +118,7 @@ Boolean suffix(char *, char *); void nuke_suffix(char *); void str_lowercase(char *); char *basename_of(char *); +char *strconcat(char *, char *); /* File */ Boolean fexists(char *); @@ -125,10 +126,10 @@ Boolean isdir(char *); Boolean isfile(char *); Boolean isempty(char *); Boolean isURL(char *); -char *fileGetURL(char *); +char *fileGetURL(char *, char *); char *fileURLFilename(char *, char *, int); char *fileURLHost(char *, char *, int); -char *fileFindByPath(char *); +char *fileFindByPath(char *, char *); char *fileGetContents(char *); void write_file(char *, char *); void copy_file(char *, char *, char *); diff --git a/usr.sbin/pkg_install/lib/pen.c b/usr.sbin/pkg_install/lib/pen.c index d242ddd..5d07e6e 100644 --- a/usr.sbin/pkg_install/lib/pen.c +++ b/usr.sbin/pkg_install/lib/pen.c @@ -1,5 +1,5 @@ #ifndef lint -static const char *rcsid = "$Id: pen.c,v 1.17 1995/08/26 18:36:27 jkh Exp $"; +static const char *rcsid = "$Id: pen.c,v 1.13.4.6 1995/10/23 12:33:43 jkh Exp $"; #endif /* @@ -28,29 +28,41 @@ static const char *rcsid = "$Id: pen.c,v 1.17 1995/08/26 18:36:27 jkh Exp $"; #include <sys/mount.h> /* For keeping track of where we are */ -static char Cwd[FILENAME_MAX]; -static char Pen[FILENAME_MAX]; -extern char *PlayPen; +static char Current[FILENAME_MAX]; +static char Previous[FILENAME_MAX]; + +char * +where_playpen(void) +{ + return Current; +} /* Find a good place to play. */ static char * -find_play_pen(size_t sz) +find_play_pen(char *pen, size_t sz) { char *cp; struct stat sb; - if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz)) - sprintf(Pen, "%s/instmp.XXXXXX", cp); + if (pen[0] && stat(pen, &sb) != FAIL && (min_free(pen) >= sz)) + return pen; + else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz)) + sprintf(pen, "%s/instmp.XXXXXX", cp); else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz)) - sprintf(Pen, "%s/instmp.XXXXXX", cp); + sprintf(pen, "%s/instmp.XXXXXX", cp); else if (stat("/var/tmp", &sb) != FAIL && min_free("/var/tmp") >= sz) - strcpy(Pen, "/var/tmp/instmp.XXXXXX"); + strcpy(pen, "/var/tmp/instmp.XXXXXX"); else if (stat("/tmp", &sb) != FAIL && min_free("/tmp") >= sz) - strcpy(Pen, "/tmp/instmp.XXXXXX"); - else if ((stat("/usr/tmp", &sb) == SUCCESS | mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz) - strcpy(Pen, "/usr/tmp/instmp.XXXXXX"); - else barf("Can't find enough temporary space to extract the files, please set\nyour PKG_TMPDIR environment variable to a location with at least %d bytes\nfree.", sz); - return Pen; + strcpy(pen, "/tmp/instmp.XXXXXX"); + else if ((stat("/usr/tmp", &sb) == SUCCESS || mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz) + strcpy(pen, "/usr/tmp/instmp.XXXXXX"); + else { + barf("Can't find enough temporary space to extract the files, please set\n" + "your PKG_TMPDIR environment variable to a location with at least %d bytes\n" + "free.", sz); + return NULL; + } + return pen; } /* @@ -60,62 +72,62 @@ find_play_pen(size_t sz) char * make_playpen(char *pen, size_t sz) { - if (!pen) - PlayPen = find_play_pen(sz); - else { - strcpy(Pen, pen); - PlayPen = Pen; + char *tmp; + + if (!find_play_pen(pen, sz)) + return NULL; + + if (!mktemp(pen)) { + barf("Can't mktemp '%s'.", pen); + return NULL; } - if (!getcwd(Cwd, FILENAME_MAX)) - upchuck("getcwd"); - if (!mktemp(Pen)) - barf("Can't mktemp '%s'.", Pen); - if (mkdir(Pen, 0755) == FAIL) - barf("Can't mkdir '%s'.", Pen); - if (Verbose) - { + if (mkdir(pen, 0755) == FAIL) { + barf("Can't mkdir '%s'.", pen); + return NULL; + } + if (Verbose) { if (sz) - fprintf(stderr, "Projected package size: %d bytes, -free temp space: %d bytes in %s\n", (int)sz, min_free(Pen), Pen); + fprintf(stderr, "Requested space: %d bytes, free space: %d bytes in %s\n", (int)sz, min_free(pen), pen); } - if (min_free(Pen) < sz) { - rmdir(Pen); - barf("Not enough free space to create `%s'.\nPlease set your PKG_TMPDIR -environment variable to a location with more space and\ntry the command -again.", Pen); - PlayPen = NULL; + if (min_free(pen) < sz) { + rmdir(pen); + barf("Not enough free space to create: `%s'\n" + "Please set your PKG_TMPDIR environment variable to a location\n" + "with more space and\ntry the command again.", pen); + return NULL; } - if (chdir(Pen) == FAIL) - barf("Can't chdir to '%s'.", Pen); - return Cwd; + if (Current[0]) + strcpy(Previous, Current); + else if (!getcwd(Previous, FILENAME_MAX)) { + upchuck("getcwd"); + return NULL; + } + if (chdir(pen) == FAIL) + barf("Can't chdir to '%s'.", pen); + strcpy(Current, pen); + return Previous; } /* Convenience routine for getting out of playpen */ void -leave_playpen(void) +leave_playpen(char *save) { void (*oldsig)(int); /* Don't interrupt while we're cleaning up */ oldsig = signal(SIGINT, SIG_IGN); - if (Cwd[0]) { - if (chdir(Cwd) == FAIL) - barf("Can't chdir back to '%s'.", Cwd); - if (vsystem("rm -rf %s", Pen)) - fprintf(stderr, "Couldn't remove temporary dir '%s'\n", Pen); - Cwd[0] = '\0'; + if (Previous[0] && chdir(Previous) == FAIL) + barf("Can't chdir back to '%s'.", Previous); + else if (Current[0]) { + if (vsystem("rm -rf %s", Current)) + whinge("Couldn't remove temporary dir '%s'", Current); + strcpy(Current, Previous); } - signal(SIGINT, oldsig); -} - -/* Accessor function for telling us where the pen is */ -char * -where_playpen(void) -{ - if (Cwd[0]) - return Pen; + if (save) + strcpy(Previous, save); else - return NULL; + Previous[0] = '\0'; + signal(SIGINT, oldsig); } size_t @@ -123,8 +135,6 @@ min_free(char *tmpdir) { struct statfs buf; - if (!tmpdir) - tmpdir = Pen; if (statfs(tmpdir, &buf) != 0) { perror("Error in statfs"); return -1; diff --git a/usr.sbin/pkg_install/lib/str.c b/usr.sbin/pkg_install/lib/str.c index 3e39780..a9f1300 100644 --- a/usr.sbin/pkg_install/lib/str.c +++ b/usr.sbin/pkg_install/lib/str.c @@ -35,6 +35,18 @@ basename_of(char *str) return basename; } +char * +strconcat(char *s1, char *s2) +{ + static char tmp[FILENAME_MAX]; + + tmp[0] = '\0'; + strncpy(tmp, s1 ? s1 : s2, FILENAME_MAX); + if (s1 && s2) + strncat(tmp, s2, FILENAME_MAX - strlen(tmp)); + return tmp; +} + /* Get a string parameter as a file spec or as a "contents follow -" spec */ char * get_dash_string(char **str) |