XBPS Library API 20240111
The X Binary Package System
repo_sync.c
1/*-
2 * Copyright (c) 2009-2014 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/stat.h>
27
28#include <errno.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
34#include "xbps_api_impl.h"
35#include "fetch.h"
36
37char HIDDEN *
38xbps_get_remote_repo_string(const char *uri)
39{
40 struct url *url;
41 size_t i;
42 char *p;
43
44 if ((url = fetchParseURL(uri)) == NULL)
45 return NULL;
46
47 /*
48 * Replace '.' ':' and '/' characters with underscores, so that
49 * provided URL:
50 *
51 * http://nocturno.local:8080/repo/x86_64
52 *
53 * becomes:
54 *
55 * http___nocturno_local_8080_repo_x86_64
56 */
57 if (url->port != 0)
58 p = xbps_xasprintf("%s://%s:%u%s", url->scheme,
59 url->host, url->port, url->doc);
60 else
61 p = xbps_xasprintf("%s://%s%s", url->scheme,
62 url->host, url->doc);
63
64 fetchFreeURL(url);
65 for (i = 0; i < strlen(p); i++) {
66 if (p[i] == '.' || p[i] == '/' || p[i] == ':')
67 p[i] = '_';
68 }
69
70 return p;
71}
72
73/*
74 * Returns -1 on error, 0 if transfer was not necessary (local/remote
75 * size and/or mtime match) and 1 if downloaded successfully.
76 */
77int HIDDEN
78xbps_repo_sync(struct xbps_handle *xhp, const char *uri)
79{
80 mode_t prev_umask;
81 const char *arch, *fetchstr = NULL;
82 char *repodata, *lrepodir, *uri_fixedp;
83 int rv = 0;
84
85 assert(uri != NULL);
86
87 /* ignore non remote repositories */
89 return 0;
90
91 uri_fixedp = xbps_get_remote_repo_string(uri);
92 if (uri_fixedp == NULL)
93 return -1;
94
95 if (xhp->target_arch)
96 arch = xhp->target_arch;
97 else
98 arch = xhp->native_arch;
99
100 /*
101 * Full path to repository directory to store the plist
102 * index file.
103 */
104 lrepodir = xbps_xasprintf("%s/%s", xhp->metadir, uri_fixedp);
105 free(uri_fixedp);
106 /*
107 * Create repodir in metadir.
108 */
109 prev_umask = umask(022);
110 if ((rv = xbps_mkpath(lrepodir, 0755)) == -1) {
111 if (errno != EEXIST) {
112 xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL,
113 errno, NULL, "[reposync] failed "
114 "to create repodir `%s': %s", lrepodir,
115 strerror(errno));
116 umask(prev_umask);
117 free(lrepodir);
118 return rv;
119 }
120 }
121 if (chdir(lrepodir) == -1) {
122 xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL, errno, NULL,
123 "[reposync] failed to change dir to repodir `%s': %s",
124 lrepodir, strerror(errno));
125 umask(prev_umask);
126 free(lrepodir);
127 return -1;
128 }
129 free(lrepodir);
130 /*
131 * Remote repository plist index full URL.
132 */
133 repodata = xbps_xasprintf("%s/%s-repodata", uri, arch);
134
135 /* reposync start cb */
136 xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC, 0, repodata, NULL);
137 /*
138 * Download plist index file from repository.
139 */
140 if ((rv = xbps_fetch_file(xhp, repodata, NULL)) == -1) {
141 /* reposync error cb */
142 fetchstr = xbps_fetch_error_string();
143 xbps_set_cb_state(xhp, XBPS_STATE_REPOSYNC_FAIL,
144 fetchLastErrCode != 0 ? fetchLastErrCode : errno, NULL,
145 "[reposync] failed to fetch file `%s': %s",
146 repodata, fetchstr ? fetchstr : strerror(errno));
147 } else if (rv == 1)
148 rv = 0;
149 umask(prev_umask);
150
151 free(repodata);
152
153 return rv;
154}
const char * xbps_fetch_error_string(void)
Definition download.c:89
int xbps_fetch_file(struct xbps_handle *xhp, const char *uri, const char *flags)
Definition download.c:362
char metadir[XBPS_MAXPATH+sizeof(XBPS_META_PATH)]
Definition xbps.h:664
const char * target_arch
Definition xbps.h:631
char native_arch[64]
Definition xbps.h:671
Generic XBPS structure handler for initialization.
Definition xbps.h:550
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
int xbps_mkpath(const char *path, mode_t mode)
Definition mkpath.c:42
bool xbps_repository_is_remote(const char *uri)
Definition util.c:66