1993-08-26 01:19:55 +00:00
|
|
|
/*
|
|
|
|
* FreeBSD install - a package for the installation and maintainance
|
|
|
|
* of non-core utilities.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Jordan K. Hubbard
|
|
|
|
* 18 July 1993
|
|
|
|
*
|
|
|
|
* Miscellaneous file access utilities.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-04-01 09:39:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
#include "lib.h"
|
1997-10-08 07:48:21 +00:00
|
|
|
#include <err.h>
|
2000-06-27 11:00:07 +00:00
|
|
|
#include <fetch.h>
|
1995-04-26 15:08:23 +00:00
|
|
|
#include <pwd.h>
|
1995-08-01 09:49:27 +00:00
|
|
|
#include <time.h>
|
1996-06-20 18:33:55 +00:00
|
|
|
#include <sys/wait.h>
|
1993-08-26 01:19:55 +00:00
|
|
|
|
|
|
|
/* Quick check to see if a file exists */
|
|
|
|
Boolean
|
2001-10-10 06:58:42 +00:00
|
|
|
fexists(const char *fname)
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
1995-06-24 10:12:59 +00:00
|
|
|
struct stat dummy;
|
|
|
|
if (!lstat(fname, &dummy))
|
1993-08-26 01:19:55 +00:00
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-09-18 07:41:48 +00:00
|
|
|
/* Quick check to see if something is a directory or symlink to a directory */
|
1993-08-26 01:19:55 +00:00
|
|
|
Boolean
|
2001-10-10 06:58:42 +00:00
|
|
|
isdir(const char *fname)
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
2000-02-08 06:36:08 +00:00
|
|
|
if (lstat(fname, &sb) != FAIL && S_ISDIR(sb.st_mode))
|
1993-08-26 01:19:55 +00:00
|
|
|
return TRUE;
|
2001-08-02 18:20:27 +00:00
|
|
|
else if (lstat(strconcat(fname, "/."), &sb) != FAIL && S_ISDIR(sb.st_mode))
|
2000-02-19 08:58:54 +00:00
|
|
|
return TRUE;
|
1993-08-26 01:19:55 +00:00
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-09-18 07:41:48 +00:00
|
|
|
/* Check to see if file is a dir or symlink to a dir, and is empty */
|
1993-08-26 01:19:55 +00:00
|
|
|
Boolean
|
2001-10-10 06:58:42 +00:00
|
|
|
isemptydir(const char *fname)
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
|
|
|
if (isdir(fname)) {
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
|
|
|
|
|
|
|
dirp = opendir(fname);
|
|
|
|
if (!dirp)
|
|
|
|
return FALSE; /* no perms, leave it alone */
|
|
|
|
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
|
|
|
|
if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
|
|
|
|
closedir(dirp);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)closedir(dirp);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-09-18 07:41:48 +00:00
|
|
|
/*
|
|
|
|
* Returns TRUE if file is a regular file or symlink pointing to a regular
|
|
|
|
* file
|
|
|
|
*/
|
1995-07-30 01:44:45 +00:00
|
|
|
Boolean
|
2001-10-10 06:58:42 +00:00
|
|
|
isfile(const char *fname)
|
1995-07-30 01:44:45 +00:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-09-18 07:41:48 +00:00
|
|
|
/*
|
|
|
|
* Check to see if file is a file or symlink pointing to a file and is empty.
|
|
|
|
* If nonexistent or not a file, say "it's empty", otherwise return TRUE if
|
|
|
|
* zero sized.
|
|
|
|
*/
|
1994-12-06 00:51:50 +00:00
|
|
|
Boolean
|
2001-10-10 06:58:42 +00:00
|
|
|
isemptyfile(const char *fname)
|
1994-12-06 00:51:50 +00:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode)) {
|
|
|
|
if (sb.st_size != 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-09-18 07:41:48 +00:00
|
|
|
/* Returns TRUE if file is a symbolic link. */
|
|
|
|
Boolean
|
2001-10-10 06:58:42 +00:00
|
|
|
issymlink(const char *fname)
|
2000-09-18 07:41:48 +00:00
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
if (lstat(fname, &sb) != FAIL && S_ISLNK(sb.st_mode))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1995-04-26 15:08:23 +00:00
|
|
|
/* Returns TRUE if file is a URL specification */
|
|
|
|
Boolean
|
2001-10-10 06:58:42 +00:00
|
|
|
isURL(const char *fname)
|
1995-04-26 15:08:23 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* I'm sure there are other types of URL specifications that I could
|
|
|
|
* also be looking for here, but for now I'll just be happy to get ftp
|
2000-06-27 11:00:07 +00:00
|
|
|
* and http working.
|
1995-04-26 15:08:23 +00:00
|
|
|
*/
|
1995-10-25 15:38:37 +00:00
|
|
|
if (!fname)
|
|
|
|
return FALSE;
|
1995-04-26 15:08:23 +00:00
|
|
|
while (isspace(*fname))
|
|
|
|
++fname;
|
2000-06-27 11:00:07 +00:00
|
|
|
if (!strncmp(fname, "ftp://", 6) || !strncmp(fname, "http://", 7))
|
1995-04-26 15:08:23 +00:00
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HOSTNAME_MAX 64
|
|
|
|
/*
|
1995-10-25 15:38:37 +00:00
|
|
|
* Try and fetch a file by URL, returning the directory name for where
|
|
|
|
* it's unpacked, if successful.
|
1995-04-26 15:08:23 +00:00
|
|
|
*/
|
|
|
|
char *
|
2001-10-10 06:58:42 +00:00
|
|
|
fileGetURL(const char *base, const char *spec)
|
1995-04-26 15:08:23 +00:00
|
|
|
{
|
2001-01-22 12:01:55 +00:00
|
|
|
char *cp, *rp;
|
1995-11-12 04:55:40 +00:00
|
|
|
char fname[FILENAME_MAX];
|
1995-10-25 15:38:37 +00:00
|
|
|
char pen[FILENAME_MAX];
|
2000-10-29 15:39:51 +00:00
|
|
|
char buf[8192];
|
1996-06-20 18:33:55 +00:00
|
|
|
FILE *ftp;
|
1995-10-25 15:38:37 +00:00
|
|
|
pid_t tpid;
|
2001-10-10 06:58:42 +00:00
|
|
|
int pfd[2], pstat, r, w;
|
1995-11-12 04:55:40 +00:00
|
|
|
char *hint;
|
2001-06-11 14:18:26 +00:00
|
|
|
int fd;
|
1995-04-26 15:08:23 +00:00
|
|
|
|
1995-10-25 15:38:37 +00:00
|
|
|
rp = NULL;
|
1995-11-12 04:55:40 +00:00
|
|
|
/* Special tip that sysinstall left for us */
|
|
|
|
hint = getenv("PKG_ADD_BASE");
|
1995-10-25 15:38:37 +00:00
|
|
|
if (!isURL(spec)) {
|
1995-11-12 04:55:40 +00:00
|
|
|
if (!base && !hint)
|
1995-10-25 15:38:37 +00:00
|
|
|
return NULL;
|
2001-05-17 10:12:45 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
1995-11-12 04:55:40 +00:00
|
|
|
if (base) {
|
|
|
|
strcpy(fname, base);
|
2001-05-17 10:12:45 +00:00
|
|
|
/*
|
|
|
|
* Advance back two slashes to get to the root of the package
|
|
|
|
* hierarchy
|
|
|
|
*/
|
1995-11-12 04:55:40 +00:00
|
|
|
cp = strrchr(fname, '/');
|
|
|
|
if (cp) {
|
|
|
|
*cp = '\0'; /* chop name */
|
|
|
|
cp = strrchr(fname, '/');
|
1995-10-25 15:38:37 +00:00
|
|
|
}
|
1995-11-12 04:55:40 +00:00
|
|
|
if (cp) {
|
|
|
|
*(cp + 1) = '\0';
|
|
|
|
strcat(cp, "All/");
|
|
|
|
strcat(cp, spec);
|
1996-10-14 19:41:44 +00:00
|
|
|
strcat(cp, ".tgz");
|
1995-11-12 04:55:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
2001-05-17 10:12:45 +00:00
|
|
|
/*
|
|
|
|
* Otherwise, we've been given an environment variable hinting
|
|
|
|
* at the right location from sysinstall
|
|
|
|
*/
|
1995-11-12 04:55:40 +00:00
|
|
|
strcpy(fname, hint);
|
|
|
|
strcat(fname, spec);
|
1999-05-12 06:06:49 +00:00
|
|
|
strcat(fname, ".tgz");
|
1995-10-25 15:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(fname, spec);
|
1995-04-26 15:08:23 +00:00
|
|
|
|
2001-06-11 14:18:26 +00:00
|
|
|
if ((ftp = fetchGetURL(fname, Verbose ? "v" : NULL)) == NULL) {
|
2000-06-27 11:00:07 +00:00
|
|
|
printf("Error: FTP Unable to get %s: %s\n",
|
|
|
|
fname, fetchLastErrString);
|
1995-04-26 15:08:23 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-27 11:00:07 +00:00
|
|
|
|
|
|
|
if (isatty(0) || Verbose)
|
|
|
|
printf("Fetching %s...", fname), fflush(stdout);
|
|
|
|
pen[0] = '\0';
|
2000-10-29 15:39:51 +00:00
|
|
|
if ((rp = make_playpen(pen, 0)) == NULL) {
|
2000-06-27 11:00:07 +00:00
|
|
|
printf("Error: Unable to construct a new playpen for FTP!\n");
|
2000-10-29 15:39:51 +00:00
|
|
|
fclose(ftp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (pipe(pfd) == -1) {
|
|
|
|
warn("pipe()");
|
|
|
|
cleanup(0);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if ((tpid = fork()) == -1) {
|
|
|
|
warn("pipe()");
|
|
|
|
cleanup(0);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if (!tpid) {
|
|
|
|
dup2(pfd[0], 0);
|
2001-06-13 11:55:40 +00:00
|
|
|
for (fd = getdtablesize() - 1; fd >= 3; --fd)
|
2001-06-11 14:18:26 +00:00
|
|
|
close(fd);
|
2002-08-24 20:04:17 +00:00
|
|
|
execl("/usr/bin/tar", "tar", Verbose ? "-xzpvf" : "-xzpf", "-",
|
2001-07-09 09:24:06 +00:00
|
|
|
(char *)0);
|
2000-10-29 15:39:51 +00:00
|
|
|
_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;
|
|
|
|
}
|
2001-10-10 06:58:42 +00:00
|
|
|
if (ferror(ftp))
|
|
|
|
warn("warning: error reading from server");
|
2000-06-27 11:00:07 +00:00
|
|
|
fclose(ftp);
|
2000-10-29 15:39:51 +00:00
|
|
|
close(pfd[1]);
|
|
|
|
if (w == -1)
|
|
|
|
warn("warning: error writing to tar");
|
|
|
|
tpid = waitpid(tpid, &pstat, 0);
|
|
|
|
if (Verbose)
|
|
|
|
printf("tar command returns %d status\n", WEXITSTATUS(pstat));
|
2000-06-27 11:00:07 +00:00
|
|
|
if (rp && (isatty(0) || Verbose))
|
|
|
|
printf(" Done.\n");
|
1995-10-25 15:38:37 +00:00
|
|
|
return rp;
|
1995-04-26 15:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2001-10-10 06:58:42 +00:00
|
|
|
fileFindByPath(const char *base, const char *fname)
|
1995-04-26 15:08:23 +00:00
|
|
|
{
|
|
|
|
static char tmp[FILENAME_MAX];
|
|
|
|
char *cp;
|
2002-08-04 21:27:57 +00:00
|
|
|
const char *suffixes[] = {".tgz", ".tar", ".tbz", NULL};
|
2001-01-22 12:01:55 +00:00
|
|
|
int i;
|
1995-04-26 15:08:23 +00:00
|
|
|
|
1995-07-30 01:44:45 +00:00
|
|
|
if (fexists(fname) && isfile(fname)) {
|
1995-05-10 23:00:16 +00:00
|
|
|
strcpy(tmp, fname);
|
|
|
|
return tmp;
|
|
|
|
}
|
1995-10-25 15:38:37 +00:00
|
|
|
if (base) {
|
|
|
|
strcpy(tmp, base);
|
|
|
|
|
1997-10-18 05:54:17 +00:00
|
|
|
cp = strrchr(tmp, '/');
|
1995-10-25 15:38:37 +00:00
|
|
|
if (cp) {
|
1995-11-12 04:55:40 +00:00
|
|
|
*cp = '\0'; /* chop name */
|
1997-10-18 05:54:17 +00:00
|
|
|
cp = strrchr(tmp, '/');
|
1995-11-12 04:55:40 +00:00
|
|
|
}
|
2001-01-22 12:01:55 +00:00
|
|
|
if (cp)
|
|
|
|
for (i = 0; suffixes[i] != NULL; i++) {
|
|
|
|
*(cp + 1) = '\0';
|
|
|
|
strcat(cp, "All/");
|
|
|
|
strcat(cp, fname);
|
|
|
|
strcat(cp, suffixes[i]);
|
|
|
|
if (fexists(tmp))
|
|
|
|
return tmp;
|
|
|
|
}
|
1995-10-25 15:38:37 +00:00
|
|
|
}
|
|
|
|
|
1995-04-26 15:08:23 +00:00
|
|
|
cp = getenv("PKG_PATH");
|
|
|
|
while (cp) {
|
|
|
|
char *cp2 = strsep(&cp, ":");
|
1995-05-30 03:57:47 +00:00
|
|
|
|
2001-01-22 12:01:55 +00:00
|
|
|
for (i = 0; suffixes[i] != NULL; i++) {
|
|
|
|
snprintf(tmp, FILENAME_MAX, "%s/%s%s", cp2 ? cp2 : cp, fname, suffixes[i]);
|
|
|
|
if (fexists(tmp) && isfile(tmp))
|
|
|
|
return tmp;
|
|
|
|
}
|
1995-04-26 15:08:23 +00:00
|
|
|
}
|
1995-05-10 23:00:16 +00:00
|
|
|
return NULL;
|
1995-04-26 15:08:23 +00:00
|
|
|
}
|
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
char *
|
2001-10-10 06:58:42 +00:00
|
|
|
fileGetContents(const char *fname)
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
|
|
|
char *contents;
|
|
|
|
struct stat sb;
|
|
|
|
int fd;
|
|
|
|
|
1998-09-11 07:26:58 +00:00
|
|
|
if (stat(fname, &sb) == FAIL) {
|
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: can't stat '%s'", __func__, fname);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
|
|
|
|
contents = (char *)malloc(sb.st_size + 1);
|
|
|
|
fd = open(fname, O_RDONLY, 0);
|
1998-09-11 07:26:58 +00:00
|
|
|
if (fd == FAIL) {
|
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: unable to open '%s' for reading", __func__, fname);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
|
|
|
if (read(fd, contents, sb.st_size) != sb.st_size) {
|
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: short read on '%s' - did not get %qd bytes", __func__,
|
2001-10-10 06:58:42 +00:00
|
|
|
fname, (long long)sb.st_size);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
close(fd);
|
|
|
|
contents[sb.st_size] = '\0';
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2001-05-17 10:12:45 +00:00
|
|
|
/*
|
|
|
|
* Takes a filename and package name, returning (in "try") the
|
|
|
|
* canonical "preserve" name for it.
|
1997-07-01 06:13:51 +00:00
|
|
|
*/
|
|
|
|
Boolean
|
2001-10-10 06:58:42 +00:00
|
|
|
make_preserve_name(char *try, int max, const char *name, const char *file)
|
1997-07-01 06:13:51 +00:00
|
|
|
{
|
|
|
|
int len, i;
|
|
|
|
|
|
|
|
if ((len = strlen(file)) == 0)
|
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
i = len - 1;
|
|
|
|
strncpy(try, file, max);
|
|
|
|
if (try[i] == '/') /* Catch trailing slash early and save checking in the loop */
|
|
|
|
--i;
|
|
|
|
for (; i; i--) {
|
|
|
|
if (try[i] == '/') {
|
|
|
|
try[i + 1]= '.';
|
|
|
|
strncpy(&try[i + 2], &file[i + 1], max - i - 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!i) {
|
|
|
|
try[0] = '.';
|
|
|
|
strncpy(try + 1, file, max - 1);
|
|
|
|
}
|
|
|
|
/* I should probably be called rude names for these inline assignments */
|
|
|
|
strncat(try, ".", max -= strlen(try));
|
|
|
|
strncat(try, name, max -= strlen(name));
|
|
|
|
strncat(try, ".", max--);
|
|
|
|
strncat(try, "backup", max -= 6);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
/* Write the contents of "str" to a file */
|
|
|
|
void
|
2001-10-10 06:58:42 +00:00
|
|
|
write_file(const char *name, const char *str)
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2001-10-10 06:58:42 +00:00
|
|
|
size_t len;
|
1993-08-26 01:19:55 +00:00
|
|
|
|
|
|
|
fp = fopen(name, "w");
|
1998-09-11 07:26:58 +00:00
|
|
|
if (!fp) {
|
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: cannot fopen '%s' for writing", __func__, name);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
len = strlen(str);
|
1998-09-11 07:26:58 +00:00
|
|
|
if (fwrite(str, 1, len, fp) != len) {
|
|
|
|
cleanup(0);
|
2002-05-11 03:48:49 +00:00
|
|
|
errx(2, "%s: short fwrite on '%s', tried to write %ld bytes",
|
2002-05-11 04:17:55 +00:00
|
|
|
__func__, name, (long)len);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
|
|
|
if (fclose(fp)) {
|
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: failure to fclose '%s'", __func__, name);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-10-10 06:58:42 +00:00
|
|
|
copy_file(const char *dir, const char *fname, const char *to)
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
1993-09-06 23:28:42 +00:00
|
|
|
char cmd[FILENAME_MAX];
|
|
|
|
|
|
|
|
if (fname[0] == '/')
|
2000-03-15 09:18:12 +00:00
|
|
|
snprintf(cmd, FILENAME_MAX, "cp -r %s %s", fname, to);
|
1993-09-06 23:28:42 +00:00
|
|
|
else
|
2000-03-15 09:18:12 +00:00
|
|
|
snprintf(cmd, FILENAME_MAX, "cp -r %s/%s %s", dir, fname, to);
|
1998-09-11 07:26:58 +00:00
|
|
|
if (vsystem(cmd)) {
|
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: could not perform '%s'", __func__, cmd);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
|
|
|
|
1995-04-22 13:58:44 +00:00
|
|
|
void
|
2001-10-10 06:58:42 +00:00
|
|
|
move_file(const char *dir, const char *fname, const char *to)
|
1995-04-22 13:58:44 +00:00
|
|
|
{
|
|
|
|
char cmd[FILENAME_MAX];
|
|
|
|
|
|
|
|
if (fname[0] == '/')
|
1995-04-26 15:08:23 +00:00
|
|
|
snprintf(cmd, FILENAME_MAX, "mv %s %s", fname, to);
|
1995-04-22 13:58:44 +00:00
|
|
|
else
|
1995-04-26 15:08:23 +00:00
|
|
|
snprintf(cmd, FILENAME_MAX, "mv %s/%s %s", dir, fname, to);
|
1998-09-11 07:26:58 +00:00
|
|
|
if (vsystem(cmd)) {
|
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: could not perform '%s'", __func__, cmd);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1995-04-22 13:58:44 +00:00
|
|
|
}
|
|
|
|
|
1993-08-26 01:19:55 +00:00
|
|
|
/*
|
|
|
|
* Copy a hierarchy (possibly from dir) to the current directory, or
|
|
|
|
* if "to" is TRUE, from the current directory to a location someplace
|
|
|
|
* else.
|
|
|
|
*
|
|
|
|
* Though slower, using tar to copy preserves symlinks and everything
|
|
|
|
* without me having to write some big hairy routine to do it.
|
|
|
|
*/
|
|
|
|
void
|
2001-10-10 06:58:42 +00:00
|
|
|
copy_hierarchy(const char *dir, const char *fname, Boolean to)
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
|
|
|
char cmd[FILENAME_MAX * 3];
|
|
|
|
|
|
|
|
if (!to) {
|
|
|
|
/* If absolute path, use it */
|
|
|
|
if (*fname == '/')
|
|
|
|
dir = "/";
|
1995-04-26 15:08:23 +00:00
|
|
|
snprintf(cmd, FILENAME_MAX * 3, "tar cf - -C %s %s | tar xpf -",
|
|
|
|
dir, fname);
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
|
|
|
else
|
1995-04-26 15:08:23 +00:00
|
|
|
snprintf(cmd, FILENAME_MAX * 3, "tar cf - %s | tar xpf - -C %s",
|
|
|
|
fname, dir);
|
1993-09-05 04:54:24 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Using '%s' to copy trees.\n", cmd);
|
|
|
|
#endif
|
1998-09-11 07:26:58 +00:00
|
|
|
if (system(cmd)) {
|
|
|
|
cleanup(0);
|
2002-05-11 04:17:55 +00:00
|
|
|
errx(2, "%s: could not perform '%s'", __func__, cmd);
|
1998-09-11 07:26:58 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unpack a tar file */
|
|
|
|
int
|
2001-10-10 06:58:42 +00:00
|
|
|
unpack(const char *pkg, const char *flist)
|
1993-08-26 01:19:55 +00:00
|
|
|
{
|
2001-10-10 06:58:42 +00:00
|
|
|
char args[10], suff[80], *cp;
|
1993-08-26 01:19:55 +00:00
|
|
|
|
1996-03-12 06:12:43 +00:00
|
|
|
args[0] = '\0';
|
1993-08-26 01:19:55 +00:00
|
|
|
/*
|
|
|
|
* Figure out by a crude heuristic whether this or not this is probably
|
2001-01-22 12:01:55 +00:00
|
|
|
* compressed and whichever compression utility was used (gzip or bzip2).
|
1993-08-26 01:19:55 +00:00
|
|
|
*/
|
1996-03-12 06:12:43 +00:00
|
|
|
if (strcmp(pkg, "-")) {
|
2000-10-22 09:53:27 +00:00
|
|
|
cp = strrchr(pkg, '.');
|
1996-03-12 06:12:43 +00:00
|
|
|
if (cp) {
|
2001-10-10 06:58:42 +00:00
|
|
|
strcpy(suff, cp + 1);
|
|
|
|
if (strchr(suff, 'z') || strchr(suff, 'Z')) {
|
|
|
|
if (strchr(suff, 'b'))
|
2001-01-22 12:01:55 +00:00
|
|
|
strcpy(args, "-y");
|
|
|
|
else
|
|
|
|
strcpy(args, "-z");
|
|
|
|
}
|
1996-03-12 06:12:43 +00:00
|
|
|
}
|
1993-08-26 01:19:55 +00:00
|
|
|
}
|
1996-03-12 06:12:43 +00:00
|
|
|
else
|
1998-12-05 06:29:03 +00:00
|
|
|
strcpy(args, "-z");
|
|
|
|
strcat(args, " -xpf");
|
2000-01-04 05:08:58 +00:00
|
|
|
if (vsystem("tar %s '%s' %s", args, pkg, flist ? flist : "")) {
|
1997-10-08 07:48:21 +00:00
|
|
|
warnx("tar extract of %s failed!", pkg);
|
1993-08-26 01:19:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1993-09-18 03:39:50 +00:00
|
|
|
|
2001-05-17 10:12:45 +00:00
|
|
|
/*
|
|
|
|
* Using fmt, replace all instances of:
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1993-09-18 03:39:50 +00:00
|
|
|
* %F With the parameter "name"
|
|
|
|
* %D With the parameter "dir"
|
|
|
|
* %B Return the directory part ("base") of %D/%F
|
|
|
|
* %f Return the filename part of %D/%F
|
|
|
|
*
|
|
|
|
* Does not check for overflow - caution!
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
2001-10-10 06:58:42 +00:00
|
|
|
format_cmd(char *buf, const char *fmt, const char *dir, const char *name)
|
1993-09-18 03:39:50 +00:00
|
|
|
{
|
|
|
|
char *cp, scratch[FILENAME_MAX * 2];
|
|
|
|
|
|
|
|
while (*fmt) {
|
|
|
|
if (*fmt == '%') {
|
|
|
|
switch (*++fmt) {
|
|
|
|
case 'F':
|
|
|
|
strcpy(buf, name);
|
|
|
|
buf += strlen(name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
strcpy(buf, dir);
|
|
|
|
buf += strlen(dir);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
sprintf(scratch, "%s/%s", dir, name);
|
|
|
|
cp = &scratch[strlen(scratch) - 1];
|
|
|
|
while (cp != scratch && *cp != '/')
|
|
|
|
--cp;
|
|
|
|
*cp = '\0';
|
|
|
|
strcpy(buf, scratch);
|
|
|
|
buf += strlen(scratch);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
sprintf(scratch, "%s/%s", dir, name);
|
|
|
|
cp = &scratch[strlen(scratch) - 1];
|
|
|
|
while (cp != scratch && *(cp - 1) != '/')
|
|
|
|
--cp;
|
|
|
|
strcpy(buf, cp);
|
|
|
|
buf += strlen(cp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*buf++ = *fmt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*buf++ = *fmt++;
|
|
|
|
}
|
|
|
|
*buf = '\0';
|
|
|
|
}
|