XBPS Library API  0.19
The X Binary Package System
package_remove_obsoletes.c
1 /*-
2  * Copyright (c) 2009-2012 Juan Romero Pardines.
3  * All rights reserved.
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  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <unistd.h>
32 
33 #include "xbps_api_impl.h"
34 
35 prop_array_t
36 xbps_find_pkg_obsoletes(struct xbps_handle *xhp,
37  prop_dictionary_t instd,
38  prop_dictionary_t newd)
39 {
40  prop_array_t array, array2, obsoletes;
41  prop_object_t obj, obj2;
42  prop_string_t oldstr, newstr;
43  size_t i, x;
44  const char *array_str = "files";
45  const char *oldhash;
46  char *file;
47  int rv = 0;
48  bool found, dodirs, dolinks, docffiles;
49 
50  dodirs = dolinks = docffiles = false;
51 
52  assert(prop_object_type(instd) == PROP_TYPE_DICTIONARY);
53  assert(prop_object_type(newd) == PROP_TYPE_DICTIONARY);
54 
55  obsoletes = prop_array_create();
56  assert(obsoletes);
57 
58 again:
59  array = prop_dictionary_get(instd, array_str);
60  if (array == NULL || prop_array_count(array) == 0)
61  goto out1;
62 
63  /*
64  * Iterate over files list from installed package.
65  */
66  for (i = 0; i < prop_array_count(array); i++) {
67  found = false;
68  obj = prop_array_get(array, i);
69  if (prop_object_type(obj) != PROP_TYPE_DICTIONARY)
70  continue;
71  oldstr = prop_dictionary_get(obj, "file");
72  if (oldstr == NULL)
73  continue;
74 
75  file = xbps_xasprintf(".%s",
76  prop_string_cstring_nocopy(oldstr));
77 
78  if ((strcmp(array_str, "files") == 0) ||
79  (strcmp(array_str, "conf_files") == 0)) {
80  prop_dictionary_get_cstring_nocopy(obj,
81  "sha256", &oldhash);
82  rv = xbps_file_hash_check(file, oldhash);
83  if (rv == ENOENT || rv == ERANGE) {
84  /*
85  * Skip unexistent and files that do not
86  * match the hash.
87  */
88  free(file);
89  continue;
90  }
91  }
92  array2 = prop_dictionary_get(newd, array_str);
93  if (array2 && prop_array_count(array2)) {
94  for (x = 0; x < prop_array_count(array2); x++) {
95  obj2 = prop_array_get(array2, x);
96  newstr = prop_dictionary_get(obj2, "file");
97  assert(newstr);
98  /*
99  * Skip files with same path.
100  */
101  if (prop_string_equals(oldstr, newstr)) {
102  found = true;
103  break;
104  }
105  }
106  }
107  if (found) {
108  free(file);
109  continue;
110  }
111  /*
112  * Do not add required symlinks for the
113  * system transition to /usr.
114  */
115  if ((strcmp(file, "./bin") == 0) ||
116  (strcmp(file, "./bin/") == 0) ||
117  (strcmp(file, "./sbin") == 0) ||
118  (strcmp(file, "./sbin/") == 0) ||
119  (strcmp(file, "./lib") == 0) ||
120  (strcmp(file, "./lib/") == 0) ||
121  (strcmp(file, "./lib64/") == 0) ||
122  (strcmp(file, "./lib64") == 0)) {
123  free(file);
124  continue;
125  }
126  /*
127  * Obsolete found, add onto the array.
128  */
129  xbps_dbg_printf(xhp, "found obsolete: %s (%s)\n",
130  file, array_str);
131 
132  prop_array_add_cstring(obsoletes, file);
133  free(file);
134  }
135 out1:
136  if (!dolinks) {
137  dolinks = true;
138  array_str = "links";
139  goto again;
140  } else if (!docffiles) {
141  docffiles = true;
142  array_str = "conf_files";
143  goto again;
144  } else if (!dodirs) {
145  dodirs = true;
146  array_str = "dirs";
147  goto again;
148  }
149 
150  return obsoletes;
151 }