2 * FreeBSD install - a package for the installation and maintainance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
17 * Routines used to do various operations with dependencies
18 * among installed packages.
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
29 void list_deps(const char *pkgname
, char **pkgs
, char *listed
,
30 char *check_loop
, char **newpkgs
, int *nrnewpkgs
,
34 * Sort given NULL-terminated list of installed packages (pkgs) in
35 * such a way that if package A depends on package B then after
36 * sorting A will be listed before B no matter how they were
37 * originally positioned in the list.
39 * Works by performing a recursive depth-first search on the
47 int nrpkgs
, nrnewpkgs
;
48 char *listed
, *check_loop
, **newpkgs
;
51 if (pkgs
[0] == NULL
|| pkgs
[1] == NULL
)
55 while (pkgs
[nrpkgs
]) nrpkgs
++;
56 listed
= alloca(nrpkgs
);
58 warnx("%s(): alloca() failed", __func__
);
62 check_loop
= alloca(nrpkgs
);
63 if (check_loop
== NULL
) {
64 warnx("%s(): alloca() failed", __func__
);
67 bzero(check_loop
,nrpkgs
);
68 newpkgs
= alloca(nrpkgs
*sizeof(char*));
69 if (newpkgs
== NULL
) {
70 warnx("%s(): alloca() failed", __func__
);
75 for (i
= 0; pkgs
[i
]; i
++) if (!listed
[i
]) {
77 cp
= strchr(pkgs
[i
], ':');
80 list_deps(pkgs
[i
],pkgs
,listed
,check_loop
,newpkgs
,&nrnewpkgs
,&err_cnt
);
84 newpkgs
[nrnewpkgs
] = pkgs
[i
];
88 if (nrnewpkgs
!= nrpkgs
) {
89 fprintf(stderr
,"This shouldn't happen, and indicates a huge error in the code.\n");
92 for (i
= 0; i
< nrnewpkgs
; i
++) pkgs
[i
] = newpkgs
[i
];
98 * This recursive function lists the dependencies (that is, the
99 * "required-by"s) for pkgname, putting them into newpkgs.
102 void list_deps(const char *pkgname
, char **pkgs
, char *listed
,
103 char *check_loop
, char **newpkgs
, int *nrnewpkgs
,
108 struct reqr_by_entry
*rb_entry
;
109 struct reqr_by_head
*rb_list
;
111 if (isinstalledpkg(pkgname
) <= 0)
114 errcode
= requiredby(pkgname
, &rb_list
, FALSE
, TRUE
);
118 * We put rb_list into an argv style NULL terminated list,
119 * because requiredby uses some static storage, and list_deps
120 * is a recursive function.
123 rbtmp
= rb
= alloca((errcode
+ 1) * sizeof(*rb
));
125 warnx("%s(): alloca() failed", __func__
);
129 STAILQ_FOREACH(rb_entry
, rb_list
, link
) {
130 *rbtmp
= alloca(strlen(rb_entry
->pkgname
) + 1);
131 if (*rbtmp
== NULL
) {
132 warnx("%s(): alloca() failed", __func__
);
136 strcpy(*rbtmp
, rb_entry
->pkgname
);
141 for (i
= 0; rb
[i
]; i
++)
142 for (j
= 0; pkgs
[j
]; j
++) if (!listed
[j
]) {
143 cp
= strchr(pkgs
[j
], ':');
146 if (strcmp(rb
[i
], pkgs
[j
]) == 0) { /*match */
148 * Try to avoid deadlock if package A depends on B which in
149 * turn depends on C and C due to an error depends on A.
150 * It Should Never Happen[tm] in real life.
153 warnx("dependency loop detected for package %s", pkgs
[j
]);
158 list_deps(pkgs
[j
],pkgs
,listed
,check_loop
,newpkgs
,nrnewpkgs
,err_cnt
);
160 newpkgs
[*nrnewpkgs
] = pkgs
[j
];
170 * Load +REQUIRED_BY file and return a list with names of
171 * packages that require package reffered to by `pkgname'.
173 * Optionally check that packages listed there are actually
174 * installed and filter out those that don't (filter == TRUE).
176 * strict argument controls whether the caller want warnings
177 * to be emitted when there are some non-fatal conditions,
178 * i.e. package doesn't have +REQUIRED_BY file or some packages
179 * listed in +REQUIRED_BY don't exist.
181 * Result returned in the **list, while return value is equal
182 * to the number of entries in the resulting list. Print error
183 * message and return -1 on error.
186 requiredby(const char *pkgname
, struct reqr_by_head
**list
, Boolean strict
, Boolean filter
)
189 char fbuf
[FILENAME_MAX
], fname
[FILENAME_MAX
];
191 struct reqr_by_entry
*rb_entry
;
192 static struct reqr_by_head rb_list
= STAILQ_HEAD_INITIALIZER(rb_list
);
195 /* Deallocate any previously allocated space */
196 while (!STAILQ_EMPTY(&rb_list
)) {
197 rb_entry
= STAILQ_FIRST(&rb_list
);
198 STAILQ_REMOVE_HEAD(&rb_list
, link
);
202 if (isinstalledpkg(pkgname
) <= 0) {
204 warnx("no such package '%s' installed", pkgname
);
208 snprintf(fname
, sizeof(fname
), "%s/%s/%s", LOG_DIR
, pkgname
,
210 fp
= fopen(fname
, "r");
212 /* Probably pkgname doesn't have any packages that depend on it */
214 warnx("couldn't open dependency file '%s'", fname
);
219 while (fgets(fbuf
, sizeof(fbuf
), fp
) != NULL
) {
220 if (fbuf
[strlen(fbuf
) - 1] == '\n')
221 fbuf
[strlen(fbuf
) - 1] = '\0';
222 if (filter
== TRUE
&& isinstalledpkg(fbuf
) <= 0) {
224 warnx("package '%s' is recorded in the '%s' but isn't "
225 "actually installed", fbuf
, fname
);
229 rb_entry
= malloc(sizeof(*rb_entry
));
230 if (rb_entry
== NULL
) {
231 warnx("%s(): malloc() failed", __func__
);
235 strlcpy(rb_entry
->pkgname
, fbuf
, sizeof(rb_entry
->pkgname
));
236 STAILQ_INSERT_TAIL(&rb_list
, rb_entry
, link
);