1998-10-03 20:24:07 +04:00
|
|
|
/* $NetBSD: file.c,v 1.16 1998/10/03 16:24:08 hubertf Exp $ */
|
1997-06-05 16:59:18 +04:00
|
|
|
|
1997-10-17 18:53:18 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-06-05 12:54:23 +04:00
|
|
|
#ifndef lint
|
1997-06-05 16:59:18 +04:00
|
|
|
#if 0
|
1997-10-16 04:31:32 +04:00
|
|
|
static const char *rcsid = "from FreeBSD Id: file.c,v 1.29 1997/10/08 07:47:54 charnier Exp";
|
1997-06-05 16:59:18 +04:00
|
|
|
#else
|
1998-10-03 20:24:07 +04:00
|
|
|
__RCSID("$NetBSD: file.c,v 1.16 1998/10/03 16:24:08 hubertf Exp $");
|
1997-06-05 16:59:18 +04:00
|
|
|
#endif
|
1997-06-05 12:54:23 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lib.h"
|
1998-07-06 10:56:06 +04:00
|
|
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
1998-10-03 20:24:07 +04:00
|
|
|
#include <assert.h>
|
1997-10-16 04:31:32 +04:00
|
|
|
#include <err.h>
|
1997-06-05 12:54:23 +04:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
1998-07-09 20:47:26 +04:00
|
|
|
FILE *
|
|
|
|
ftpGetURL(char *url, char *user, char *passwd, int *retcode);
|
|
|
|
|
1997-06-05 12:54:23 +04:00
|
|
|
/* Quick check to see if a file exists */
|
|
|
|
Boolean
|
|
|
|
fexists(char *fname)
|
|
|
|
{
|
|
|
|
struct stat dummy;
|
|
|
|
if (!lstat(fname, &dummy))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Quick check to see if something is a directory */
|
|
|
|
Boolean
|
|
|
|
isdir(char *fname)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
1998-04-23 14:40:04 +04:00
|
|
|
if (lstat(fname, &sb) != FAIL && S_ISDIR(sb.st_mode))
|
1997-06-05 12:54:23 +04:00
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1998-08-28 03:37:35 +04:00
|
|
|
/* Check if something is a link to a directory */
|
|
|
|
Boolean
|
|
|
|
islinktodir(char *fname)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (lstat(fname, &sb) != FAIL && S_ISLNK(sb.st_mode))
|
|
|
|
if (stat(fname, &sb) != FAIL && S_ISDIR(sb.st_mode))
|
|
|
|
return TRUE; /* link to dir! */
|
|
|
|
else
|
|
|
|
return FALSE; /* link to non-dir */
|
|
|
|
else
|
|
|
|
return FALSE; /* non-link */
|
|
|
|
}
|
|
|
|
|
1997-06-05 12:54:23 +04:00
|
|
|
/* Check to see if file is a dir, and is empty */
|
|
|
|
Boolean
|
|
|
|
isemptydir(char *fname)
|
|
|
|
{
|
1998-08-28 03:37:35 +04:00
|
|
|
if (isdir(fname) || islinktodir(fname)) {
|
1997-06-05 12:54:23 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Boolean
|
|
|
|
isfile(char *fname)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check to see if file is a file and is empty. If nonexistent or not
|
|
|
|
a file, say "it's empty", otherwise return TRUE if zero sized. */
|
|
|
|
Boolean
|
|
|
|
isemptyfile(char *fname)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode)) {
|
|
|
|
if (sb.st_size != 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns TRUE if file is a URL specification */
|
|
|
|
Boolean
|
|
|
|
isURL(char *fname)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* working.
|
|
|
|
*/
|
|
|
|
if (!fname)
|
|
|
|
return FALSE;
|
|
|
|
while (isspace(*fname))
|
|
|
|
++fname;
|
|
|
|
if (!strncmp(fname, "ftp://", 6))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the host part of a URL */
|
|
|
|
char *
|
|
|
|
fileURLHost(char *fname, char *where, int max)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
while (isspace(*fname))
|
|
|
|
++fname;
|
|
|
|
/* Don't ever call this on a bad URL! */
|
|
|
|
fname += strlen("ftp://");
|
|
|
|
/* Do we have a place to stick our work? */
|
|
|
|
if ((ret = where) != NULL) {
|
|
|
|
while (*fname && *fname != '/' && max--)
|
|
|
|
*where++ = *fname++;
|
|
|
|
*where = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* If not, they must really want us to stomp the original string */
|
|
|
|
ret = fname;
|
|
|
|
while (*fname && *fname != '/')
|
|
|
|
++fname;
|
|
|
|
*fname = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the filename part of a URL */
|
|
|
|
char *
|
|
|
|
fileURLFilename(char *fname, char *where, int max)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
while (isspace(*fname))
|
|
|
|
++fname;
|
|
|
|
/* Don't ever call this on a bad URL! */
|
|
|
|
fname += strlen("ftp://");
|
|
|
|
/* Do we have a place to stick our work? */
|
|
|
|
if ((ret = where) != NULL) {
|
|
|
|
while (*fname && *fname != '/')
|
|
|
|
++fname;
|
|
|
|
if (*fname == '/') {
|
|
|
|
while (*fname && max--)
|
|
|
|
*where++ = *fname++;
|
|
|
|
}
|
|
|
|
*where = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* If not, they must really want us to stomp the original string */
|
|
|
|
while (*fname && *fname != '/')
|
|
|
|
++fname;
|
|
|
|
return fname;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try and fetch a file by URL, returning the directory name for where
|
|
|
|
* it's unpacked, if successful.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
fileGetURL(char *base, char *spec)
|
|
|
|
{
|
1998-07-06 10:56:06 +04:00
|
|
|
char host[MAXHOSTNAMELEN], file[FILENAME_MAX];
|
|
|
|
char pword[MAXHOSTNAMELEN + 40], *uname, *cp, *rp;
|
1997-06-05 12:54:23 +04:00
|
|
|
char fname[FILENAME_MAX];
|
|
|
|
char pen[FILENAME_MAX];
|
|
|
|
struct passwd *pw;
|
|
|
|
FILE *ftp;
|
|
|
|
pid_t tpid;
|
|
|
|
int i, status;
|
|
|
|
char *hint;
|
|
|
|
|
|
|
|
rp = NULL;
|
|
|
|
/* Special tip that sysinstall left for us */
|
|
|
|
hint = getenv("PKG_ADD_BASE");
|
|
|
|
if (!isURL(spec)) {
|
|
|
|
if (!base && !hint)
|
|
|
|
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. */
|
|
|
|
if (base) {
|
|
|
|
strcpy(fname, base);
|
|
|
|
/* Advance back two slashes to get to the root of the package hierarchy */
|
|
|
|
cp = strrchr(fname, '/');
|
|
|
|
if (cp) {
|
|
|
|
*cp = '\0'; /* chop name */
|
|
|
|
cp = strrchr(fname, '/');
|
|
|
|
}
|
|
|
|
if (cp) {
|
|
|
|
*(cp + 1) = '\0';
|
|
|
|
strcat(cp, "All/");
|
|
|
|
strcat(cp, spec);
|
|
|
|
strcat(cp, ".tgz");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Otherwise, we've been given an environment variable hinting at the right location from sysinstall */
|
|
|
|
strcpy(fname, hint);
|
|
|
|
strcat(fname, spec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(fname, spec);
|
1998-07-06 10:56:06 +04:00
|
|
|
cp = fileURLHost(fname, host, MAXHOSTNAMELEN);
|
1997-06-05 12:54:23 +04:00
|
|
|
if (!*cp) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("URL `%s' has bad host part!", fname);
|
1997-06-05 12:54:23 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = fileURLFilename(fname, file, FILENAME_MAX);
|
|
|
|
if (!*cp) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("URL `%s' has bad filename part!", fname);
|
1997-06-05 12:54:23 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Maybe change to ftp if this doesn't work */
|
|
|
|
uname = "anonymous";
|
|
|
|
|
|
|
|
/* Make up a convincing "password" */
|
|
|
|
pw = getpwuid(getuid());
|
|
|
|
if (!pw) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("can't get user name for ID %d", getuid());
|
1997-06-05 12:54:23 +04:00
|
|
|
strcpy(pword, "joe@");
|
|
|
|
}
|
|
|
|
else {
|
1998-07-06 10:56:06 +04:00
|
|
|
char me[MAXHOSTNAMELEN + 1];
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1998-07-06 10:56:06 +04:00
|
|
|
gethostname(me, sizeof me);
|
|
|
|
me[sizeof(me) - 1] = '\0';
|
|
|
|
snprintf(pword, sizeof pword, "%s@%s", pw->pw_name, me);
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
if (Verbose)
|
|
|
|
printf("Trying to fetch %s.\n", fname);
|
|
|
|
ftp = ftpGetURL(fname, uname, pword, &status);
|
|
|
|
if (ftp) {
|
|
|
|
pen[0] = '\0';
|
|
|
|
if ((rp = make_playpen(pen, 0)) != NULL) {
|
1998-10-02 01:16:26 +04:00
|
|
|
rp=strdup(pen); /* XXX be safe for nested calls */
|
1997-06-05 12:54:23 +04:00
|
|
|
if (Verbose)
|
|
|
|
printf("Extracting from FTP connection into %s\n", pen);
|
|
|
|
tpid = fork();
|
|
|
|
if (!tpid) {
|
|
|
|
dup2(fileno(ftp), 0);
|
|
|
|
i = execl("/usr/bin/tar", "tar", Verbose ? "-xzvf" : "-xzf", "-", 0);
|
|
|
|
exit(i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int pstat;
|
|
|
|
|
|
|
|
fclose(ftp);
|
|
|
|
tpid = waitpid(tpid, &pstat, 0);
|
|
|
|
if (Verbose)
|
|
|
|
printf("tar command returns %d status\n", WEXITSTATUS(pstat));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("Error: Unable to construct a new playpen for FTP!\n");
|
|
|
|
fclose(ftp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("Error: FTP Unable to get %s: %s\n",
|
|
|
|
fname,
|
1998-07-09 20:47:26 +04:00
|
|
|
status ? "Error while performing FTP" :
|
|
|
|
hstrerror(h_errno));
|
1997-06-05 12:54:23 +04:00
|
|
|
return rp;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
fileFindByPath(char *base, char *fname)
|
|
|
|
{
|
|
|
|
static char tmp[FILENAME_MAX];
|
|
|
|
char *cp;
|
|
|
|
|
1998-10-03 20:24:07 +04:00
|
|
|
if (ispkgpattern(fname)) {
|
|
|
|
if (cp=findbestmatchingname(".",fname)) {
|
|
|
|
strcpy (tmp, cp);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if (fexists(fname) && isfile(fname)) {
|
|
|
|
strcpy(tmp, fname);
|
|
|
|
return tmp;
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
1998-10-03 20:24:07 +04:00
|
|
|
|
1997-06-05 12:54:23 +04:00
|
|
|
if (base) {
|
|
|
|
strcpy(tmp, base);
|
|
|
|
|
1997-10-16 04:55:01 +04:00
|
|
|
cp = strrchr(tmp, '/');
|
1997-06-05 12:54:23 +04:00
|
|
|
if (cp) {
|
|
|
|
*cp = '\0'; /* chop name */
|
1997-10-16 04:55:01 +04:00
|
|
|
cp = strrchr(tmp, '/');
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
if (cp) {
|
|
|
|
*(cp + 1) = '\0';
|
|
|
|
strcat(cp, "All/");
|
|
|
|
strcat(cp, fname);
|
1997-10-16 04:55:01 +04:00
|
|
|
strcat(cp, ".tgz");
|
1998-10-03 20:24:07 +04:00
|
|
|
if (ispkgpattern(tmp)) {
|
|
|
|
cp=findbestmatchingname(dirname_of(tmp), basename_of(tmp));
|
|
|
|
if (cp) {
|
|
|
|
char *s;
|
|
|
|
s=strrchr(tmp,'/');
|
|
|
|
assert(s != NULL);
|
|
|
|
strcpy(s+1, cp);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fexists(tmp)) {
|
1997-06-05 12:54:23 +04:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
1998-10-03 20:24:07 +04:00
|
|
|
}
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
|
|
|
|
cp = getenv("PKG_PATH");
|
|
|
|
while (cp) {
|
|
|
|
char *cp2 = strsep(&cp, ":");
|
|
|
|
|
|
|
|
snprintf(tmp, FILENAME_MAX, "%s/%s.tgz", cp2 ? cp2 : cp, fname);
|
1998-10-03 20:24:07 +04:00
|
|
|
if (ispkgpattern(tmp)) {
|
|
|
|
char *s;
|
|
|
|
s = findbestmatchingname(dirname_of(tmp), basename_of(tmp));
|
|
|
|
if (s){
|
|
|
|
char *t;
|
|
|
|
t=strrchr(tmp, '/');
|
|
|
|
strcpy(t+1, s);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
if (fexists(tmp) && isfile(tmp)) {
|
1997-06-05 12:54:23 +04:00
|
|
|
return tmp;
|
|
|
|
}
|
1998-10-03 20:24:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-06-05 12:54:23 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
fileGetContents(char *fname)
|
|
|
|
{
|
|
|
|
char *contents;
|
|
|
|
struct stat sb;
|
|
|
|
int fd;
|
|
|
|
|
1998-03-27 15:17:58 +03:00
|
|
|
if (stat(fname, &sb) == FAIL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "can't stat '%s'", fname);
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
|
|
|
|
contents = (char *)malloc(sb.st_size + 1);
|
|
|
|
fd = open(fname, O_RDONLY, 0);
|
1998-03-27 15:17:58 +03:00
|
|
|
if (fd == FAIL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "unable to open '%s' for reading", fname);
|
|
|
|
}
|
|
|
|
if (read(fd, contents, sb.st_size) != sb.st_size) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "short read on '%s' - did not get %qd bytes",
|
1997-10-19 13:39:35 +04:00
|
|
|
fname, (long long)sb.st_size);
|
1998-03-27 15:17:58 +03:00
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
close(fd);
|
|
|
|
contents[sb.st_size] = '\0';
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
1997-10-16 04:50:20 +04:00
|
|
|
/* Takes a filename and package name, returning (in "try") the canonical "preserve"
|
|
|
|
* name for it.
|
|
|
|
*/
|
|
|
|
Boolean
|
|
|
|
make_preserve_name(char *try, int max, char *name, char *file)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1997-06-05 12:54:23 +04:00
|
|
|
/* Write the contents of "str" to a file */
|
|
|
|
void
|
|
|
|
write_file(char *name, char *str)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
fp = fopen(name, "w");
|
1998-03-27 15:17:58 +03:00
|
|
|
if (!fp) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "cannot fopen '%s' for writing", name);
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
len = strlen(str);
|
1998-03-27 15:17:58 +03:00
|
|
|
if (fwrite(str, 1, len, fp) != len) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "short fwrite on '%s', tried to write %d bytes",
|
1997-10-16 04:31:32 +04:00
|
|
|
name, len);
|
1998-03-27 15:17:58 +03:00
|
|
|
}
|
|
|
|
if (fclose(fp)) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "failure to fclose '%s'", name);
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
copy_file(char *dir, char *fname, char *to)
|
|
|
|
{
|
|
|
|
char cmd[FILENAME_MAX];
|
|
|
|
|
|
|
|
if (fname[0] == '/')
|
|
|
|
snprintf(cmd, FILENAME_MAX, "cp -p -r %s %s", fname, to);
|
|
|
|
else
|
|
|
|
snprintf(cmd, FILENAME_MAX, "cp -p -r %s/%s %s", dir, fname, to);
|
1998-03-27 15:17:58 +03:00
|
|
|
if (vsystem(cmd)) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "could not perform '%s'", cmd);
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
move_file(char *dir, char *fname, char *to)
|
|
|
|
{
|
|
|
|
char cmd[FILENAME_MAX];
|
|
|
|
|
|
|
|
if (fname[0] == '/')
|
|
|
|
snprintf(cmd, FILENAME_MAX, "mv %s %s", fname, to);
|
|
|
|
else
|
|
|
|
snprintf(cmd, FILENAME_MAX, "mv %s/%s %s", dir, fname, to);
|
1998-03-27 15:17:58 +03:00
|
|
|
if (vsystem(cmd)) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "could not perform '%s'", cmd);
|
|
|
|
}
|
1997-06-05 12:54:23 +04: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
|
|
|
|
copy_hierarchy(char *dir, char *fname, Boolean to)
|
|
|
|
{
|
|
|
|
char cmd[FILENAME_MAX * 3];
|
|
|
|
|
|
|
|
if (!to) {
|
|
|
|
/* If absolute path, use it */
|
|
|
|
if (*fname == '/')
|
|
|
|
dir = "/";
|
|
|
|
snprintf(cmd, FILENAME_MAX * 3, "tar cf - -C %s %s | tar xpf -",
|
|
|
|
dir, fname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snprintf(cmd, FILENAME_MAX * 3, "tar cf - %s | tar xpf - -C %s",
|
|
|
|
fname, dir);
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Using '%s' to copy trees.\n", cmd);
|
|
|
|
#endif
|
1998-03-27 15:17:58 +03:00
|
|
|
if (system(cmd)) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "copy_file: could not perform '%s'", cmd);
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unpack a tar file */
|
|
|
|
int
|
|
|
|
unpack(char *pkg, char *flist)
|
|
|
|
{
|
|
|
|
char args[10], suffix[80], *cp;
|
|
|
|
|
|
|
|
args[0] = '\0';
|
|
|
|
/*
|
|
|
|
* Figure out by a crude heuristic whether this or not this is probably
|
|
|
|
* compressed.
|
|
|
|
*/
|
|
|
|
if (strcmp(pkg, "-")) {
|
1997-10-18 15:05:34 +04:00
|
|
|
cp = strrchr(pkg, '.');
|
1997-06-05 12:54:23 +04:00
|
|
|
if (cp) {
|
|
|
|
strcpy(suffix, cp + 1);
|
1997-10-18 15:05:34 +04:00
|
|
|
if (strchr(suffix, 'z') || strchr(suffix, 'Z'))
|
1997-06-05 12:54:23 +04:00
|
|
|
strcpy(args, "-z");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(args, "z");
|
|
|
|
strcat(args, "xpf");
|
|
|
|
if (vsystem("tar %s %s %s", args, pkg, flist ? flist : "")) {
|
1997-10-16 04:31:32 +04:00
|
|
|
warnx("tar extract of %s failed!", pkg);
|
1997-06-05 12:54:23 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Using fmt, replace all instances of:
|
|
|
|
*
|
|
|
|
* %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
|
|
|
|
format_cmd(char *buf, char *fmt, char *dir, char *name)
|
|
|
|
{
|
|
|
|
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';
|
|
|
|
}
|
1998-07-09 20:47:26 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* This is as ftpGetURL from FreeBSD's ftpio.c, except that it uses
|
|
|
|
* NetBSD's ftp command to do all FTP, which will DTRT for proxies,
|
|
|
|
* etc.
|
|
|
|
*/
|
|
|
|
FILE *
|
|
|
|
ftpGetURL(char *url, char *user, char *passwd, int *retcode)
|
|
|
|
{
|
|
|
|
FILE *ftp;
|
|
|
|
pid_t pid_ftp;
|
|
|
|
int p[2];
|
|
|
|
|
|
|
|
*retcode=0;
|
|
|
|
|
|
|
|
if( pipe(p) < 0){
|
|
|
|
*retcode = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_ftp = fork();
|
|
|
|
if(pid_ftp < 0){
|
|
|
|
*retcode = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if(pid_ftp == 0){
|
|
|
|
/* child */
|
|
|
|
dup2(p[1],1);
|
|
|
|
close(p[1]);
|
|
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, ">>> ftp -o - %s\n",url);
|
|
|
|
execl("/usr/bin/ftp","ftp","-V","-o","-",url,NULL);
|
|
|
|
exit(1);
|
|
|
|
}else{
|
|
|
|
/* parent */
|
|
|
|
ftp = fdopen(p[0],"r");
|
|
|
|
|
|
|
|
close(p[1]);
|
|
|
|
|
|
|
|
if(ftp < 0){
|
|
|
|
*retcode = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ftp;
|
|
|
|
}
|