10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*8781Sro@techfak.uni-bielefeld.de * Common Development and Distribution License (the "License").
6*8781Sro@techfak.uni-bielefeld.de * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*8781Sro@techfak.uni-bielefeld.de
220Sstevel@tonic-gate /*
23*8781Sro@techfak.uni-bielefeld.de * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <fcntl.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <strings.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <dirent.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include "list.h"
390Sstevel@tonic-gate #include "protodir.h"
400Sstevel@tonic-gate #include "arch.h"
410Sstevel@tonic-gate
420Sstevel@tonic-gate static pkg_list *packages[HASH_SIZE];
430Sstevel@tonic-gate
440Sstevel@tonic-gate #define HASH(name) (hash(name) % HASH_SIZE)
450Sstevel@tonic-gate
460Sstevel@tonic-gate int
processed_package(const char * pkgname)470Sstevel@tonic-gate processed_package(const char *pkgname)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate int bucket;
500Sstevel@tonic-gate pkg_list *tmp;
510Sstevel@tonic-gate
520Sstevel@tonic-gate bucket = HASH(pkgname);
530Sstevel@tonic-gate for (tmp = packages[bucket]; tmp != NULL; tmp = tmp->next) {
540Sstevel@tonic-gate if (strcmp(tmp->pkg_name, pkgname) == 0)
550Sstevel@tonic-gate return (1);
560Sstevel@tonic-gate }
570Sstevel@tonic-gate return (0);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate
600Sstevel@tonic-gate void
mark_processed(const char * pkgname)610Sstevel@tonic-gate mark_processed(const char *pkgname)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate int bucket;
640Sstevel@tonic-gate pkg_list *tmp;
650Sstevel@tonic-gate
660Sstevel@tonic-gate bucket = HASH(pkgname);
670Sstevel@tonic-gate tmp = malloc(sizeof (pkg_list));
680Sstevel@tonic-gate bzero(tmp, sizeof (pkg_list));
690Sstevel@tonic-gate (void) strcpy(tmp->pkg_name, pkgname);
700Sstevel@tonic-gate tmp->next = packages[bucket];
710Sstevel@tonic-gate packages[bucket] = tmp;
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
740Sstevel@tonic-gate static pkg_list *
add_dependency(pkg_list * dependlist,const char * pkgname)750Sstevel@tonic-gate add_dependency(pkg_list *dependlist, const char *pkgname)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate pkg_list *tmp;
780Sstevel@tonic-gate pkg_list *pkg;
790Sstevel@tonic-gate
800Sstevel@tonic-gate pkg = malloc(sizeof (pkg_list));
810Sstevel@tonic-gate bzero(pkg, sizeof (pkg_list));
820Sstevel@tonic-gate (void) strcpy(pkg->pkg_name, pkgname);
830Sstevel@tonic-gate
840Sstevel@tonic-gate /* easy case */
850Sstevel@tonic-gate if (dependlist == NULL)
860Sstevel@tonic-gate return (pkg);
870Sstevel@tonic-gate /* insert at end, since the order matters */
880Sstevel@tonic-gate for (tmp = dependlist; tmp->next != NULL; tmp = tmp->next) {
890Sstevel@tonic-gate /* NULL */
900Sstevel@tonic-gate }
910Sstevel@tonic-gate tmp->next = pkg;
920Sstevel@tonic-gate return (dependlist);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate static void
free_dependency_list(pkg_list * dependlist)960Sstevel@tonic-gate free_dependency_list(pkg_list *dependlist)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate pkg_list *tmp;
990Sstevel@tonic-gate
1000Sstevel@tonic-gate while (dependlist) {
1010Sstevel@tonic-gate tmp = dependlist;
1020Sstevel@tonic-gate dependlist = dependlist->next;
1030Sstevel@tonic-gate tmp->next = NULL;
1040Sstevel@tonic-gate free(tmp);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate #ifdef DEBUG
1090Sstevel@tonic-gate void
print_dependencies(const char * pkgname,pkg_list * dependlist)1100Sstevel@tonic-gate print_dependencies(const char *pkgname, pkg_list *dependlist)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate pkg_list *tmp;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate fprintf(stderr, "%s:", pkgname);
1150Sstevel@tonic-gate for (tmp = dependlist; tmp != NULL; tmp = tmp->next)
1160Sstevel@tonic-gate fprintf(stderr, " %s", tmp->pkg_name);
1170Sstevel@tonic-gate fprintf(stderr, "\n");
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate #endif
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate static char *suffix_list[] = {
1220Sstevel@tonic-gate #if defined(__i386)
1230Sstevel@tonic-gate ".i",
1240Sstevel@tonic-gate #elif defined(__sparc)
1250Sstevel@tonic-gate ".c",
1260Sstevel@tonic-gate ".d",
1270Sstevel@tonic-gate ".m",
1280Sstevel@tonic-gate ".u",
129*8781Sro@techfak.uni-bielefeld.de ".v",
1300Sstevel@tonic-gate #else
1310Sstevel@tonic-gate #error "Unknown architecture."
1320Sstevel@tonic-gate #endif
1330Sstevel@tonic-gate NULL,
1340Sstevel@tonic-gate };
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate static pkg_list *
find_dependencies(const char * pkgname,const char * parentdir)1370Sstevel@tonic-gate find_dependencies(const char *pkgname, const char *parentdir)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate char dependfile[MAXPATHLEN + 1];
1400Sstevel@tonic-gate char pkgdir[MAXPATHLEN + 1];
1410Sstevel@tonic-gate char buf[BUFSIZ];
1420Sstevel@tonic-gate char deppkg[MAXNAME];
1430Sstevel@tonic-gate char archpkg[MAXNAME];
1440Sstevel@tonic-gate struct stat sbuf;
1450Sstevel@tonic-gate FILE *fp;
1460Sstevel@tonic-gate pkg_list *dependlist = NULL;
1470Sstevel@tonic-gate char **suffixes;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate (void) sprintf(dependfile, "%s/%s/depend", parentdir, pkgname);
1500Sstevel@tonic-gate fp = fopen(dependfile, "r");
1510Sstevel@tonic-gate if (fp == NULL) {
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate * depend won't exist in ON packages until a build
1540Sstevel@tonic-gate * has been done, but it would be nice if you didn't have
1550Sstevel@tonic-gate * to do that. So try the generic depend file that those
1560Sstevel@tonic-gate * packages would copy in during the build.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate (void) sprintf(dependfile, "%s/common_files/depend", parentdir);
1590Sstevel@tonic-gate fp = fopen(dependfile, "r");
1600Sstevel@tonic-gate if (fp == NULL)
1610Sstevel@tonic-gate return (NULL);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate while (fgets(buf, BUFSIZ, fp) != NULL) {
1640Sstevel@tonic-gate if ((buf[0] == '\0') || (buf[0] == '#') || isspace(buf[0]))
1650Sstevel@tonic-gate continue;
1660Sstevel@tonic-gate /* we only care about prerequisites */
1670Sstevel@tonic-gate if (buf[0] != 'P')
1680Sstevel@tonic-gate continue;
1690Sstevel@tonic-gate (void) sscanf(buf, "P %s", deppkg);
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * We have to be careful with some of the packages that are
1720Sstevel@tonic-gate * listed as dependencies but exist under a different name -
1730Sstevel@tonic-gate * SUNWcar is good, because it's actually SUNWcar.{c,d,i,m,u}.
1740Sstevel@tonic-gate * What do we do there? We can't just go for all the '.'
1750Sstevel@tonic-gate * packages, since on x86 we only want the .i one, and on sparc
1760Sstevel@tonic-gate * we want everything _but_ .i. Maybe
1770Sstevel@tonic-gate *
1780Sstevel@tonic-gate * I think perhaps what we do is, if we don't find a package
1790Sstevel@tonic-gate * dependency, on intel we append '.i' and try for that, and on
1800Sstevel@tonic-gate * sparc we try the other extensions. Any we find get added.
1810Sstevel@tonic-gate *
1820Sstevel@tonic-gate * Note also we're quiet on failures. This is because you might
1830Sstevel@tonic-gate * be dependant on some outside package.
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate (void) sprintf(pkgdir, "%s/%s", parentdir, deppkg);
1860Sstevel@tonic-gate if (stat(pkgdir, &sbuf) == -1) {
1870Sstevel@tonic-gate if (errno != ENOENT) {
1880Sstevel@tonic-gate continue;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate for (suffixes = &suffix_list[0]; *suffixes != NULL;
1910Sstevel@tonic-gate suffixes++) {
1920Sstevel@tonic-gate (void) sprintf(archpkg, "%s%s", deppkg,
1930Sstevel@tonic-gate *suffixes);
1940Sstevel@tonic-gate (void) sprintf(pkgdir, "%s/%s", parentdir,
1950Sstevel@tonic-gate archpkg);
1960Sstevel@tonic-gate if (stat(pkgdir, &sbuf) == -1) {
1970Sstevel@tonic-gate continue;
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate if (!S_ISDIR(sbuf.st_mode)) {
2000Sstevel@tonic-gate continue;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate /* found one */
2030Sstevel@tonic-gate dependlist = add_dependency(dependlist,
2040Sstevel@tonic-gate archpkg);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate if (!S_ISDIR(sbuf.st_mode)) {
2080Sstevel@tonic-gate continue;
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate dependlist = add_dependency(dependlist, deppkg);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate (void) fclose(fp);
2130Sstevel@tonic-gate return (dependlist);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate int
process_dependencies(const char * pkgname,const char * parentdir,elem_list * list,int verbose)2170Sstevel@tonic-gate process_dependencies(const char *pkgname, const char *parentdir,
2180Sstevel@tonic-gate elem_list *list, int verbose)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate int count = 0;
2210Sstevel@tonic-gate char pkgdir[MAXPATHLEN + 1];
2220Sstevel@tonic-gate pkg_list *dependlist;
2230Sstevel@tonic-gate pkg_list *tmp;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate dependlist = find_dependencies(pkgname, parentdir);
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * print_dependencies(pkgname, dependlist);
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate if (dependlist == NULL)
2300Sstevel@tonic-gate return (0);
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate for (tmp = dependlist; tmp != NULL; tmp = tmp->next) {
2330Sstevel@tonic-gate (void) sprintf(pkgdir, "%s/%s", parentdir, tmp->pkg_name);
2340Sstevel@tonic-gate count += process_package_dir(tmp->pkg_name, pkgdir, list,
2350Sstevel@tonic-gate verbose);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate free_dependency_list(dependlist);
2390Sstevel@tonic-gate return (count);
2400Sstevel@tonic-gate }
241