XBPS Library API  0.19
The X Binary Package System
rpool.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 <sys/utsname.h>
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <libgen.h>
31 #include <string.h>
32 #include <errno.h>
33 
34 #include "xbps_api_impl.h"
35 
36 /**
37  * @file lib/rpool.c
38  * @brief Repository pool routines
39  * @defgroup repopool Repository pool functions
40  */
41 
42 int HIDDEN
43 xbps_rpool_init(struct xbps_handle *xhp)
44 {
45  prop_dictionary_t repod, d = NULL;
46  size_t i, ntotal = 0, nmissing = 0;
47  const char *repouri;
48  char *plist;
49  int rv = 0;
50 
51  if (xhp->repo_pool != NULL)
52  return 0;
53  else if (xhp->cfg == NULL)
54  return ENOTSUP;
55 
56  xhp->repo_pool = prop_array_create();
57  if (xhp->repo_pool == NULL)
58  return ENOMEM;
59 
60  for (i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
61  repouri = cfg_getnstr(xhp->cfg, "repositories", i);
62  ntotal++;
63  /*
64  * If index file is not there, skip.
65  */
66  plist = xbps_pkg_index_plist(xhp, repouri);
67  if (plist == NULL) {
68  rv = errno;
69  goto out;
70  }
71  if (access(plist, R_OK) == -1) {
72  xbps_dbg_printf(xhp, "[rpool] `%s' cannot be "
73  "internalized: %s\n", repouri, strerror(errno));
74  nmissing++;
75  continue;
76  }
77  repod = prop_dictionary_internalize_from_zfile(plist);
78  free(plist);
79  if (prop_object_type(repod) != PROP_TYPE_DICTIONARY) {
80  xbps_dbg_printf(xhp, "[rpool] `%s' cannot be "
81  "internalized: %s\n", repouri, strerror(errno));
82  nmissing++;
83  continue;
84  }
85  /*
86  * Register repository into the array.
87  */
88  if ((d = prop_dictionary_create()) == NULL) {
89  rv = ENOMEM;
90  prop_object_release(repod);
91  goto out;
92  }
93  if (!prop_dictionary_set_cstring_nocopy(d, "uri", repouri)) {
94  rv = EINVAL;
95  prop_object_release(repod);
96  prop_object_release(d);
97  goto out;
98  }
99  if (!prop_dictionary_set(d, "index", repod)) {
100  rv = EINVAL;
101  prop_object_release(repod);
102  prop_object_release(d);
103  goto out;
104  }
105  prop_object_release(repod);
106  if (!prop_array_add(xhp->repo_pool, d)) {
107  rv = EINVAL;
108  prop_object_release(d);
109  goto out;
110  }
111  xbps_dbg_printf(xhp, "[rpool] `%s' registered.\n", repouri);
112  }
113  if (ntotal - nmissing == 0) {
114  /* no repositories available, error out */
115  rv = ENOTSUP;
116  goto out;
117  }
118 
119  prop_array_make_immutable(xhp->repo_pool);
120  xbps_dbg_printf(xhp, "[rpool] initialized ok.\n");
121 out:
122  if (rv != 0)
123  xbps_rpool_release(xhp);
124 
125  return rv;
126 
127 }
128 
129 void HIDDEN
130 xbps_rpool_release(struct xbps_handle *xhp)
131 {
132  prop_dictionary_t d;
133  size_t i;
134  const char *uri;
135 
136  if (xhp->repo_pool == NULL)
137  return;
138 
139  for (i = 0; i < prop_array_count(xhp->repo_pool); i++) {
140  d = prop_array_get(xhp->repo_pool, i);
141  if (xhp->flags & XBPS_FLAG_DEBUG) {
142  prop_dictionary_get_cstring_nocopy(d, "uri", &uri);
143  xbps_dbg_printf(xhp, "[rpool] unregistered "
144  "repository '%s'\n", uri);
145  }
146  prop_object_release(d);
147  }
148  prop_object_release(xhp->repo_pool);
149  xhp->repo_pool = NULL;
150  xbps_dbg_printf(xhp, "[rpool] released ok.\n");
151 }
152 
153 int
154 xbps_rpool_sync(struct xbps_handle *xhp, const char *file, const char *uri)
155 {
156  const char *repouri;
157  size_t i;
158 
159  if (xhp->cfg == NULL)
160  return ENOTSUP;
161 
162  for (i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
163  repouri = cfg_getnstr(xhp->cfg, "repositories", i);
164  /* If argument was set just process that repository */
165  if (uri && strcmp(repouri, uri))
166  continue;
167 
168  if (xbps_rindex_sync(xhp, repouri, file) == -1) {
169  xbps_dbg_printf(xhp,
170  "[rpool] `%s' failed to fetch `%s': %s\n",
171  repouri, file,
172  fetchLastErrCode == 0 ? strerror(errno) :
174  continue;
175  }
176  }
177  return 0;
178 }
179 
180 int
182  int (*fn)(struct xbps_rindex *, void *, bool *),
183  void *arg)
184 {
185  prop_dictionary_t d;
186  struct xbps_rindex rpi;
187  size_t i;
188  int rv = 0;
189  bool done = false;
190 
191  assert(fn != NULL);
192  /* Initialize repository pool */
193  if ((rv = xbps_rpool_init(xhp)) != 0) {
194  if (rv == ENOTSUP) {
195  xbps_dbg_printf(xhp,
196  "[rpool] empty repository list.\n");
197  } else if (rv != ENOENT && rv != ENOTSUP) {
198  xbps_dbg_printf(xhp,
199  "[rpool] couldn't initialize: %s\n",
200  strerror(rv));
201  }
202  return rv;
203  }
204  /* Iterate over repository pool */
205  for (i = 0; i < prop_array_count(xhp->repo_pool); i++) {
206  d = prop_array_get(xhp->repo_pool, i);
207  prop_dictionary_get_cstring_nocopy(d, "uri", &rpi.uri);
208  rpi.repod = prop_dictionary_get(d, "index");
209  rpi.xhp = xhp;
210  rv = (*fn)(&rpi, arg, &done);
211  if (rv != 0 || done)
212  break;
213  }
214 
215  return rv;
216 }