c55febc6c4f2223bc07ea71983b85024a68e9a57
[dragonfly.git] / usr.sbin / pkg_install / lib / deps.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
3  * of non-core utilities.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * Maxim Sobolev
15  * 14 March 2001
16  *
17  * Routines used to do various operations with dependencies
18  * among installed packages.
19  *
20  * $FreeBSD: src/usr.sbin/pkg_install/lib/deps.c,v 1.11 2004/06/29 18:59:19 eik Exp $
21  * $DragonFly: src/usr.sbin/pkg_install/lib/Attic/deps.c,v 1.3 2004/07/30 04:46:13 dillon Exp $
22  */
23
24 #include "lib.h"
25 #include <err.h>
26 #include <stdio.h>
27
28 /*
29  * Sort given NULL-terminated list of installed packages (pkgs) in
30  * such a way that if package A depends on package B then after
31  * sorting A will be listed before B no matter how they were
32  * originally positioned in the list.
33  */
34 int
35 sortdeps(char **pkgs)
36 {
37     char *tmp;
38     int i, j, loop_cnt;
39     int err_cnt = 0;
40
41     if (pkgs[0] == NULL || pkgs[1] == NULL)
42         return (0);
43
44     for (i = 0; pkgs[i + 1]; i++) {
45         /*
46          * Check to see if any other package in pkgs[i+1:] depends
47          * on pkgs[i] and swap those two packages if so.
48          */
49         loop_cnt = 0;
50         for (j = i + 1; pkgs[j]; j++) {
51             if (chkifdepends(pkgs[j], pkgs[i]) == 1) {
52                 /*
53                  * Try to avoid deadlock if package A depends on B which in
54                  * turn depends on C and C due to an error depends on A.
55                  * Use ugly but simple method, becase it Should Never
56                  * Happen[tm] in the real life anyway.
57                  */
58                 if (loop_cnt > 4096) {
59                     warnx("dependency loop detected for package %s", pkgs[j]);
60                     err_cnt++;
61                     break;
62                 }
63                 loop_cnt++;
64                 tmp = pkgs[i];
65                 pkgs[i] = pkgs[j];
66                 pkgs[j] = tmp;
67                 /*
68                  * Another iteration requred to check if new pkgs[i]
69                  * itself has any packages that depend on it
70                  */
71                 j = i + 1;
72             }
73         }
74     }
75     return err_cnt;
76 }
77
78 /*
79  * Check to see if pkgname1 depends on pkgname2.
80  * Returns 1 if depends, 0 if not, and -1 if error occured.
81  */ 
82 int
83 chkifdepends(const char *pkgname1, const char *pkgname2)
84 {
85     char *cp1, *cp2;
86     int errcode;
87     struct reqr_by_entry *rb_entry;
88     struct reqr_by_head *rb_list;
89
90     cp2 = strchr(pkgname2, ':');
91     if (cp2 != NULL)
92         *cp2 = '\0';
93     cp1 = strchr(pkgname1, ':');
94     if (cp1 != NULL)
95         *cp1 = '\0';
96
97     errcode = 0;
98     /* Check that pkgname2 is actually installed */
99     if (isinstalledpkg(pkgname2) <= 0)
100         goto exit;
101
102     errcode = requiredby(pkgname2, &rb_list, FALSE, TRUE);
103     if (errcode < 0)
104         goto exit;
105
106     errcode = 0;
107     STAILQ_FOREACH(rb_entry, rb_list, link) {
108         if (strcmp(rb_entry->pkgname, pkgname1) == 0) { /* match */
109             errcode = 1;
110             break;
111         }
112     }
113
114 exit:
115     if (cp1 != NULL)
116         *cp1 = ':';
117     if (cp2 != NULL)
118         *cp2 = ':';
119     return errcode;
120 }
121
122 /*
123  * Load +REQUIRED_BY file and return a list with names of
124  * packages that require package reffered to by `pkgname'.
125  *
126  * Optionally check that packages listed there are actually
127  * installed and filter out those that don't (filter == TRUE).
128  *
129  * strict argument controls whether the caller want warnings
130  * to be emitted when there are some non-fatal conditions,
131  * i.e. package doesn't have +REQUIRED_BY file or some packages
132  * listed in +REQUIRED_BY don't exist.
133  *
134  * Result returned in the **list, while return value is equal
135  * to the number of entries in the resulting list. Print error
136  * message and return -1 on error.
137  */
138 int
139 requiredby(const char *pkgname, struct reqr_by_head **list, Boolean strict, Boolean filter)
140 {
141     FILE *fp;
142     char fbuf[FILENAME_MAX], fname[FILENAME_MAX];
143     int retval;
144     struct reqr_by_entry *rb_entry;
145     static struct reqr_by_head rb_list = STAILQ_HEAD_INITIALIZER(rb_list);
146
147     *list = &rb_list;
148     /* Deallocate any previously allocated space */
149     while (!STAILQ_EMPTY(&rb_list)) {
150         rb_entry = STAILQ_FIRST(&rb_list);
151         STAILQ_REMOVE_HEAD(&rb_list, link);
152         free(rb_entry);
153     }
154
155     if (isinstalledpkg(pkgname) <= 0) {
156         if (strict == TRUE)
157             warnx("no such package '%s' installed", pkgname);
158         return -1;
159     }
160
161     snprintf(fname, sizeof(fname), "%s/%s/%s", LOG_DIR, pkgname,
162              REQUIRED_BY_FNAME);
163     fp = fopen(fname, "r");
164     if (fp == NULL) {
165         /* Probably pkgname doesn't have any packages that depend on it */
166         if (strict == TRUE)
167             warnx("couldn't open dependency file '%s'", fname);
168         return 0;
169     }
170
171     retval = 0;
172     while (fgets(fbuf, sizeof(fbuf), fp) != NULL) {
173         if (fbuf[strlen(fbuf) - 1] == '\n')
174             fbuf[strlen(fbuf) - 1] = '\0';
175         if (filter == TRUE && isinstalledpkg(fbuf) <= 0) {
176             if (strict == TRUE)
177                 warnx("package '%s' is recorded in the '%s' but isn't "
178                       "actually installed", fbuf, fname);
179             continue;
180         }
181         retval++;
182         rb_entry = malloc(sizeof(*rb_entry));
183         if (rb_entry == NULL) {
184             warnx("%s(): malloc() failed", __func__);
185             retval = -1;
186             break;
187         }
188         strlcpy(rb_entry->pkgname, fbuf, sizeof(rb_entry->pkgname));
189         STAILQ_INSERT_TAIL(&rb_list, rb_entry, link);
190     }
191     fclose(fp);
192
193     return retval;
194 }