1999-08-24 04:48:37 +04:00
|
|
|
/* $NetBSD: file.c,v 1.32 1999/08/24 00:48:39 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
|
1999-08-24 04:48:37 +04:00
|
|
|
__RCSID("$NetBSD: file.c,v 1.32 1999/08/24 00:48:39 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-11-03 01:40:11 +03:00
|
|
|
#include <fcntl.h>
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* This is as ftpGetURL from FreeBSD's ftpio.c, except that it uses
|
1998-10-08 16:15:24 +04:00
|
|
|
* NetBSD's ftp command to do all FTP, which will DTRT for proxies,
|
|
|
|
* etc.
|
|
|
|
*/
|
|
|
|
static FILE *
|
|
|
|
ftpGetURL(char *url, int *retcode)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
FILE *ftp;
|
|
|
|
pid_t pid_ftp;
|
|
|
|
int p[2];
|
1998-10-08 16:15:24 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
*retcode = 0;
|
1998-10-08 16:15:24 +04:00
|
|
|
|
|
|
|
if (pipe(p) < 0) {
|
|
|
|
*retcode = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_ftp = fork();
|
|
|
|
if (pid_ftp < 0) {
|
|
|
|
*retcode = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (pid_ftp == 0) {
|
|
|
|
/* child */
|
1999-08-24 04:48:37 +04:00
|
|
|
dup2(p[1], 1);
|
1998-10-08 16:15:24 +04:00
|
|
|
close(p[1]);
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
fprintf(stderr, ">>> ftp -o - %s\n", url);
|
|
|
|
execl("/usr/bin/ftp", "ftp", "-V", "-o", "-", url, NULL);
|
1998-10-08 16:15:24 +04:00
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
/* parent */
|
1999-08-24 04:48:37 +04:00
|
|
|
ftp = fdopen(p[0], "r");
|
1998-10-08 16:15:24 +04:00
|
|
|
|
|
|
|
close(p[1]);
|
|
|
|
|
|
|
|
if (ftp == (FILE *) NULL) {
|
|
|
|
*retcode = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ftp;
|
|
|
|
}
|
1998-07-09 20:47:26 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Quick check to see if a file exists
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
Boolean
|
|
|
|
fexists(char *fname)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
struct stat dummy;
|
|
|
|
if (!lstat(fname, &dummy))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Quick check to see if something is a directory
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
Boolean
|
|
|
|
isdir(char *fname)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
struct stat sb;
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
if (lstat(fname, &sb) != FAIL && S_ISDIR(sb.st_mode))
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Check if something is a link to a directory
|
|
|
|
*/
|
1998-08-28 03:37:35 +04:00
|
|
|
Boolean
|
|
|
|
islinktodir(char *fname)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
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 */
|
1998-08-28 03:37:35 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Check to see if file is a dir, and is empty
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
Boolean
|
|
|
|
isemptydir(char *fname)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
if (isdir(fname) || islinktodir(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;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
return FALSE;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Check if something is a regular file
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
Boolean
|
|
|
|
isfile(char *fname)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
struct stat sb;
|
|
|
|
if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
Boolean
|
|
|
|
isemptyfile(char *fname)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
struct stat sb;
|
|
|
|
if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode)) {
|
|
|
|
if (sb.st_size != 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/* This struct defines the leading part of a valid URL name */
|
1999-08-19 18:12:34 +04:00
|
|
|
typedef struct url_t {
|
1999-08-24 04:48:37 +04:00
|
|
|
char *u_s; /* the leading part of the URL */
|
|
|
|
int u_len; /* its length */
|
|
|
|
} url_t;
|
|
|
|
|
|
|
|
/* A table of valid leading strings for URLs */
|
|
|
|
static url_t urls[] = {
|
|
|
|
{"ftp://", 6},
|
|
|
|
{"http://", 7},
|
|
|
|
{NULL}
|
1999-08-19 18:12:34 +04:00
|
|
|
};
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Returns length of leading part of any URL from urls table, or -1
|
|
|
|
*/
|
1999-08-19 18:12:34 +04:00
|
|
|
int
|
|
|
|
URLlength(char *fname)
|
1997-06-05 12:54:23 +04:00
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
url_t *up;
|
|
|
|
int i;
|
1999-08-19 18:12:34 +04:00
|
|
|
|
|
|
|
if (fname != (char *) NULL) {
|
1999-08-24 04:48:37 +04:00
|
|
|
for (i = 0; isspace((unsigned char) *fname); i++) {
|
1999-08-19 18:12:34 +04:00
|
|
|
fname++;
|
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
for (up = urls; up->u_s; up++) {
|
1999-08-19 18:12:34 +04:00
|
|
|
if (strncmp(fname, up->u_s, up->u_len) == 0) {
|
|
|
|
return i + up->u_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Returns the host part of a URL
|
|
|
|
*/
|
|
|
|
char *
|
1997-06-05 12:54:23 +04:00
|
|
|
fileURLHost(char *fname, char *where, int max)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
char *ret;
|
|
|
|
int i;
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-23 19:17:03 +04:00
|
|
|
if ((i = URLlength(fname)) < 0) { /* invalid URL? */
|
1999-08-19 18:12:34 +04:00
|
|
|
errx(1, "fileURLhost called with a bad URL: `%s'", fname);
|
|
|
|
}
|
|
|
|
fname += i;
|
|
|
|
/* 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';
|
1997-06-05 12:54:23 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Returns the filename part of a URL
|
|
|
|
*/
|
|
|
|
char *
|
1997-06-05 12:54:23 +04:00
|
|
|
fileURLFilename(char *fname, char *where, int max)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
char *ret;
|
|
|
|
int i;
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-23 19:17:03 +04:00
|
|
|
if ((i = URLlength(fname)) < 0) { /* invalid URL? */
|
1999-08-19 18:12:34 +04:00
|
|
|
errx(1, "fileURLhost called with a bad URL: `%s'", fname);
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
1999-08-19 18:12:34 +04:00
|
|
|
fname += i;
|
|
|
|
/* 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;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try and fetch a file by URL, returning the directory name for where
|
|
|
|
* it's unpacked, if successful.
|
|
|
|
*/
|
1999-08-24 04:48:37 +04:00
|
|
|
char *
|
1997-06-05 12:54:23 +04:00
|
|
|
fileGetURL(char *base, char *spec)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
char host[MAXHOSTNAMELEN], file[FILENAME_MAX];
|
|
|
|
char *cp, *rp;
|
|
|
|
char fname[FILENAME_MAX];
|
|
|
|
char pen[FILENAME_MAX];
|
|
|
|
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 (!IS_URL(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);
|
|
|
|
cp = fileURLHost(fname, host, MAXHOSTNAMELEN);
|
|
|
|
if (!*cp) {
|
|
|
|
warnx("URL `%s' has bad host part!", fname);
|
1997-06-05 12:54:23 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
cp = fileURLFilename(fname, file, FILENAME_MAX);
|
|
|
|
if (!*cp) {
|
|
|
|
warnx("URL `%s' has bad filename part!", fname);
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
if (Verbose)
|
|
|
|
printf("Trying to fetch %s.\n", fname);
|
|
|
|
ftp = ftpGetURL(fname, &status);
|
|
|
|
if (ftp) {
|
|
|
|
pen[0] = '\0';
|
|
|
|
if ((rp = make_playpen(pen, sizeof(pen), 0)) != NULL) {
|
|
|
|
rp = strdup(pen); /* be safe for nested calls */
|
|
|
|
if (Verbose)
|
|
|
|
printf("Extracting from FTP connection into %s\n", pen);
|
|
|
|
tpid = fork();
|
|
|
|
if (!tpid) {
|
|
|
|
dup2(fileno(ftp), 0);
|
|
|
|
i = execl(TAR_FULLPATHNAME, TAR_CMD, Verbose ? "-xzvf" : "-xzf", "-", 0);
|
|
|
|
err(i, TAR_FULLPATHNAME " failed");
|
|
|
|
} else {
|
|
|
|
int pstat;
|
|
|
|
|
|
|
|
fclose(ftp);
|
|
|
|
tpid = waitpid(tpid, &pstat, 0);
|
|
|
|
if (Verbose)
|
|
|
|
printf("%s command returns %d status\n", TAR_CMD, WEXITSTATUS(pstat));
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
printf("Error: Unable to construct a new playpen for FTP!\n");
|
1997-06-05 12:54:23 +04:00
|
|
|
fclose(ftp);
|
1999-08-24 04:48:37 +04:00
|
|
|
} else
|
|
|
|
printf("Error: FTP Unable to get %s: %s\n",
|
|
|
|
fname,
|
|
|
|
status ? "Error while performing FTP" :
|
|
|
|
hstrerror(h_errno));
|
|
|
|
return rp;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-01-26 17:26:23 +03:00
|
|
|
/*
|
|
|
|
* Look for filename/pattern "fname" in
|
|
|
|
* - current dir, and if not found there, look
|
|
|
|
* - $base/../All
|
|
|
|
* - all dirs in $PKG_PATH
|
|
|
|
*/
|
1999-08-24 04:48:37 +04:00
|
|
|
char *
|
1997-06-05 12:54:23 +04:00
|
|
|
fileFindByPath(char *base, char *fname)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
static char tmp[FILENAME_MAX];
|
|
|
|
char *cp;
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
if (ispkgpattern(fname)) {
|
|
|
|
if ((cp = findbestmatchingname(".", fname)) != NULL) {
|
|
|
|
strcpy(tmp, cp);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fexists(fname) && isfile(fname)) {
|
|
|
|
strcpy(tmp, fname);
|
|
|
|
return tmp;
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
if (base) {
|
|
|
|
strcpy(tmp, base);
|
|
|
|
|
|
|
|
cp = strrchr(tmp, '/');
|
1998-10-03 20:24:07 +04:00
|
|
|
if (cp) {
|
1999-08-24 04:48:37 +04:00
|
|
|
*cp = '\0'; /* chop name */
|
|
|
|
cp = strrchr(tmp, '/');
|
1998-10-03 20:24:07 +04:00
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
if (cp) {
|
|
|
|
*(cp + 1) = '\0';
|
|
|
|
strcat(cp, "All/");
|
|
|
|
strcat(cp, fname);
|
|
|
|
strcat(cp, ".tgz");
|
|
|
|
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)) {
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
1999-01-26 17:26:23 +03:00
|
|
|
}
|
1998-10-03 20:24:07 +04:00
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
cp = getenv("PKG_PATH");
|
|
|
|
while (cp) {
|
|
|
|
char *cp2 = strsep(&cp, ":");
|
|
|
|
|
|
|
|
(void) snprintf(tmp, sizeof(tmp), "%s/%s.tgz", cp2 ? cp2 : cp, fname);
|
|
|
|
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)) {
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
1998-10-03 20:24:07 +04:00
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
|
|
|
|
return NULL;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-01-26 17:47:32 +03:00
|
|
|
/*
|
1999-08-24 04:48:37 +04:00
|
|
|
* Expect "fname" to point at a +CONTENTS file, and read it into
|
|
|
|
* the buffer returned.
|
1999-01-26 17:47:32 +03:00
|
|
|
*/
|
1999-08-24 04:48:37 +04:00
|
|
|
char *
|
1997-06-05 12:54:23 +04:00
|
|
|
fileGetContents(char *fname)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
char *contents;
|
|
|
|
struct stat sb;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (stat(fname, &sb) == FAIL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "can't stat '%s'", fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
contents = (char *) malloc((size_t) (sb.st_size) + 1);
|
|
|
|
fd = open(fname, O_RDONLY, 0);
|
|
|
|
if (fd == FAIL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "unable to open '%s' for reading", fname);
|
|
|
|
}
|
|
|
|
if (read(fd, contents, (size_t) sb.st_size) != (size_t) sb.st_size) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "short read on '%s' - did not get %qd bytes",
|
|
|
|
fname, (long long) sb.st_size);
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
contents[(size_t) sb.st_size] = '\0';
|
|
|
|
return contents;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Takes a filename and package name, returning (in "try") the canonical
|
1999-01-26 17:47:32 +03:00
|
|
|
* "preserve" name for it.
|
1997-10-16 04:50:20 +04:00
|
|
|
*/
|
|
|
|
Boolean
|
1998-10-08 16:15:24 +04:00
|
|
|
make_preserve_name(char *try, size_t max, char *name, char *file)
|
1997-10-16 04:50:20 +04:00
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
int len, i;
|
1997-10-16 04:50:20 +04:00
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
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;
|
|
|
|
}
|
1997-10-16 04:50:20 +04:00
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
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-10-16 04:50:20 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Write the contents of "str" to a file
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
void
|
|
|
|
write_file(char *name, char *str)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
size_t len;
|
|
|
|
FILE *fp;
|
1997-06-05 12:54:23 +04:00
|
|
|
|
1998-10-08 16:15:24 +04:00
|
|
|
if ((fp = fopen(name, "w")) == (FILE *) NULL) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "cannot fopen '%s' for writing", name);
|
|
|
|
}
|
|
|
|
len = strlen(str);
|
|
|
|
if (fwrite(str, 1, len, fp) != len) {
|
|
|
|
cleanup(0);
|
1998-10-13 14:00:09 +04:00
|
|
|
errx(2, "short fwrite on '%s', tried to write %ld bytes",
|
1999-08-24 04:48:37 +04:00
|
|
|
name, (long) len);
|
1998-10-08 16:15:24 +04: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)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
char cmd[FILENAME_MAX];
|
|
|
|
|
|
|
|
if (fname[0] == '/')
|
|
|
|
(void) snprintf(cmd, sizeof(cmd), "cp -p -r %s %s", fname, to);
|
|
|
|
else
|
|
|
|
(void) snprintf(cmd, sizeof(cmd), "cp -p -r %s/%s %s", dir, fname, to);
|
|
|
|
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)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
char cmd[FILENAME_MAX];
|
|
|
|
|
|
|
|
if (fname[0] == '/')
|
|
|
|
(void) snprintf(cmd, sizeof(cmd), "mv %s %s", fname, to);
|
|
|
|
else
|
|
|
|
(void) snprintf(cmd, sizeof(cmd), "mv %s/%s %s", dir, fname, to);
|
|
|
|
if (vsystem(cmd)) {
|
|
|
|
cleanup(0);
|
|
|
|
errx(2, "could not perform '%s'", cmd);
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
/*
|
|
|
|
* Unpack a tar file
|
|
|
|
*/
|
1997-06-05 12:54:23 +04:00
|
|
|
int
|
|
|
|
unpack(char *pkg, char *flist)
|
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
char args[10], suff[80], *cp;
|
|
|
|
|
|
|
|
args[0] = '\0';
|
|
|
|
/*
|
|
|
|
* Figure out by a crude heuristic whether this or not this is probably
|
|
|
|
* compressed.
|
|
|
|
*/
|
|
|
|
if (strcmp(pkg, "-")) {
|
|
|
|
cp = strrchr(pkg, '.');
|
|
|
|
if (cp) {
|
|
|
|
strcpy(suff, cp + 1);
|
|
|
|
if (strchr(suff, 'z') || strchr(suff, 'Z'))
|
|
|
|
strcpy(args, "-z");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
strcpy(args, "z");
|
|
|
|
strcat(args, "xpf");
|
|
|
|
if (vsystem("%s %s %s %s", TAR_CMD, args, pkg, flist ? flist : "")) {
|
|
|
|
warnx("%s extract of %s failed!", TAR_CMD, pkg);
|
|
|
|
return 1;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
return 0;
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
|
|
|
|
1998-10-08 16:57:58 +04:00
|
|
|
/*
|
|
|
|
* Using fmt, replace all instances of:
|
1999-08-24 04:48:37 +04:00
|
|
|
*
|
1997-06-05 12:54:23 +04: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
|
1999-08-24 04:48:37 +04:00
|
|
|
*
|
1998-10-08 16:57:58 +04:00
|
|
|
* Check that no overflows can occur.
|
1997-06-05 12:54:23 +04:00
|
|
|
*/
|
|
|
|
void
|
1998-10-08 16:57:58 +04:00
|
|
|
format_cmd(char *buf, size_t size, char *fmt, char *dir, char *name)
|
1997-06-05 12:54:23 +04:00
|
|
|
{
|
1999-08-24 04:48:37 +04:00
|
|
|
char scratch[FILENAME_MAX * 2];
|
1998-10-08 16:57:58 +04:00
|
|
|
char *bufp;
|
|
|
|
char *cp;
|
|
|
|
|
1999-08-24 04:48:37 +04:00
|
|
|
for (bufp = buf; (int) (bufp - buf) < size && *fmt;) {
|
1998-10-08 16:57:58 +04:00
|
|
|
if (*fmt == '%') {
|
|
|
|
switch (*++fmt) {
|
|
|
|
case 'F':
|
1999-08-24 04:48:37 +04:00
|
|
|
strnncpy(bufp, size - (int) (bufp - buf), name, strlen(name));
|
1998-10-08 16:57:58 +04:00
|
|
|
bufp += strlen(bufp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
1999-08-24 04:48:37 +04:00
|
|
|
strnncpy(bufp, size - (int) (bufp - buf), dir, strlen(dir));
|
1998-10-08 16:57:58 +04:00
|
|
|
bufp += strlen(bufp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
(void) snprintf(scratch, sizeof(scratch), "%s/%s", dir, name);
|
|
|
|
if ((cp = strrchr(scratch, '/')) == (char *) NULL) {
|
|
|
|
cp = scratch;
|
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
strnncpy(bufp, size - (int) (bufp - buf), scratch, (size_t) (cp - scratch));
|
1998-10-08 16:57:58 +04:00
|
|
|
bufp += strlen(bufp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
(void) snprintf(scratch, sizeof(scratch), "%s/%s", dir, name);
|
|
|
|
if ((cp = strrchr(scratch, '/')) == (char *) NULL) {
|
|
|
|
cp = scratch;
|
|
|
|
} else {
|
|
|
|
cp++;
|
|
|
|
}
|
1999-08-24 04:48:37 +04:00
|
|
|
strnncpy(bufp, size - (int) (bufp - buf), cp, strlen(cp));
|
1998-10-08 16:57:58 +04:00
|
|
|
bufp += strlen(bufp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*bufp++ = *fmt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++fmt;
|
|
|
|
} else {
|
|
|
|
*bufp++ = *fmt++;
|
|
|
|
}
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|
1998-10-08 16:57:58 +04:00
|
|
|
*bufp = '\0';
|
1997-06-05 12:54:23 +04:00
|
|
|
}
|