1 /* pkg_alternatives.c - the opkg package management system
3 Copyright (C) 2017 Yousong Zhou
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2, or (at
8 your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
17 #include <sys/types.h> /* stat */
21 #include "libbb/libbb.h"
22 #include "opkg_message.h"
25 #include "pkg_alternatives.h"
26 #include "sprintf_alloc.h"
28 static int pkg_alternatives_update_path(pkg_t *pkg, const pkg_vec_t *installed, const char *path)
30 struct pkg_alternatives *pkg_alts;
31 struct pkg_alternative *the_alt = NULL;
37 for (i = 0; i < installed->len; i++) {
38 pkg_t *pkg = installed->pkgs[i];
39 pkg_alts = pkg_get_ptr(pkg, PKG_ALTERNATIVES);
43 for (j = 0; j < pkg_alts->nalts; j++) {
44 struct pkg_alternative *alt = pkg_alts->alts[j];
46 if (strcmp(path, alt->path))
48 if (!the_alt || the_alt->prio < alt->prio) {
55 /* path is assumed to be an absolute one */
56 sprintf_alloc(&path_in_dest, "%s%s", the_pkg->dest->root_dir, &path[1]);
63 r = lstat(path_in_dest, &sb);
67 if (!S_ISLNK(sb.st_mode)) {
68 opkg_msg(ERROR, "%s exists but is not a symlink\n", path_in_dest);
72 realpath = xreadlink(path_in_dest);
73 if (realpath && strcmp(realpath, the_alt->altpath))
76 } else if (errno != ENOENT) {
79 r = symlink(the_alt->altpath, path_in_dest);
81 opkg_msg(INFO, "failed symlinking %s -> %s\n", path_in_dest, the_alt->altpath);
92 int pkg_alternatives_update(pkg_t * pkg)
96 struct pkg_alternatives *pkg_alts;
97 struct pkg_alternative *alt = NULL;
100 pkg_alts = pkg_get_ptr(pkg, PKG_ALTERNATIVES);
104 installed = pkg_vec_alloc();
105 pkg_hash_fetch_all_installed(installed);
106 for (i = 0; i < pkg_alts->nalts; i++) {
107 alt = pkg_alts->alts[i];
108 r |= pkg_alternatives_update_path(pkg, installed, alt->path);
110 pkg_vec_free(installed);