Skip to content
Newer
Older
100644 223 lines (204 sloc) 5.78 KB
15fe615 @bapt Add the forgotten set.c :)
bapt authored
1 /*
2 * Copyright (c) 2012 Baptiste Daroussin <bapt@FreeBSD.org>
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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <err.h>
28 #include <stdio.h>
29 #include <stdbool.h>
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
30 #include <string.h>
15fe615 @bapt Add the forgotten set.c :)
bapt authored
31 #include <sysexits.h>
32 #include <unistd.h>
33
34 #include <pkg.h>
35
36 #include "pkgcli.h"
37
41c8593 @infracaninophile Make some constants explicitly unsigned
infracaninophile authored
38 #define AUTOMATIC 1U<<0
39 #define ORIGIN 1U<<1
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
40
15fe615 @bapt Add the forgotten set.c :)
bapt authored
41 void
42 usage_set(void)
43 {
e7b9006 @evilgjb Update set.c:usage_set() to match pkg-set.8
evilgjb authored
44 fprintf(stderr, "usage: pkg set [-a] [-A [01]] [-o <oldorigin>:<neworigin>] [-y] [-xXg] <pkg-name>\n\n");
15fe615 @bapt Add the forgotten set.c :)
bapt authored
45 fprintf(stderr, "For more information see 'pkg help set'. \n");
46 }
47
48 int
49 exec_set(int argc, char **argv)
50 {
51 struct pkgdb *db = NULL;
52 struct pkgdb_it *it = NULL;
53 struct pkg *pkg = NULL;
54 int ch;
55 int i;
12addcf @bdrewery Fix `pkg set -a1` not confirming for every package
bdrewery authored
56 bool yes_flag = false;
ea7b1cd @bapt pkg set -o now change origin for a port and everything that depends o…
bapt authored
57 bool yes = yes_flag;
15fe615 @bapt Add the forgotten set.c :)
bapt authored
58 match_t match = MATCH_EXACT;
59 int newautomatic = -1;
60 bool automatic = false;
61 const char *errstr;
62 const char *name;
63 const char *version;
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
64 char *neworigin = NULL;
65 char *oldorigin = NULL;
66 unsigned int loads = PKG_LOAD_BASIC;
67 unsigned int sets = 0;
15fe615 @bapt Add the forgotten set.c :)
bapt authored
68
10eda77 @bdrewery pkg-set: Rename -a to -A and add -a for all packages
bdrewery authored
69 while ((ch = getopt(argc, argv, "ayA:kxXgo:")) != -1) {
15fe615 @bapt Add the forgotten set.c :)
bapt authored
70 switch (ch) {
76319fc @wca style(9): fix switch statements, line lengths while I'm here
wca authored
71 case 'y':
72 yes_flag = true;
73 break;
74 case 'a':
75 match = MATCH_ALL;
76 break;
77 case 'x':
78 match = MATCH_REGEX;
79 break;
80 case 'X':
81 match = MATCH_EREGEX;
82 break;
83 case 'g':
84 match = MATCH_GLOB;
85 break;
86 case 'A':
87 sets |= AUTOMATIC;
88 newautomatic = strtonum(optarg, 0, 1, &errstr);
89 if (errstr)
90 errx(EX_USAGE, "Wrong value for -A. "
91 "Expecting 0 or 1, got: %s (%s)",
92 optarg, errstr);
93 break;
94 case 'o':
95 sets |= ORIGIN;
96 loads |= PKG_LOAD_DEPS;
97 match = MATCH_ALL;
98 oldorigin = strdup(optarg);
99 neworigin = strrchr(oldorigin, ':');
100 if (neworigin == NULL) {
101 free(oldorigin);
102 errx(EX_USAGE, "Wrong format for -o. "
103 "Expecting oldorigin:neworigin, got: %s",
104 optarg);
105 }
106 *neworigin = '\0';
107 neworigin++;
108 if (strrchr(oldorigin, '/') == NULL ||
109 strrchr(neworigin, '/') == NULL) {
110 free(oldorigin);
111 errx(EX_USAGE,
112 "Bad origin format, got: %s", optarg);
113 }
114 break;
115 default:
c6a26c2 @dnaeon Fix a possible leak in pkg/set.c
dnaeon authored
116 if (oldorigin != NULL)
117 free(oldorigin);
118
76319fc @wca style(9): fix switch statements, line lengths while I'm here
wca authored
119 usage_set();
120 return (EX_USAGE);
15fe615 @bapt Add the forgotten set.c :)
bapt authored
121 }
122 }
123
124 argc -= optind;
125 argv += optind;
126
10eda77 @bdrewery pkg-set: Rename -a to -A and add -a for all packages
bdrewery authored
127 if ((argc < 1 && match != MATCH_ALL) || (newautomatic == -1 && neworigin == NULL)) {
15fe615 @bapt Add the forgotten set.c :)
bapt authored
128 usage_set();
129 return (EX_USAGE);
130 }
131
132 if (geteuid() != 0) {
ee0b3ff Try to unify usage() output and several other output from pkg(8)
Lars Engels authored
133 warnx("Modifying local database can only be done as root");
15fe615 @bapt Add the forgotten set.c :)
bapt authored
134 return (EX_NOPERM);
135 }
136
137 if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK)
138 return (EX_IOERR);
139
ea7b1cd @bapt pkg set -o now change origin for a port and everything that depends o…
bapt authored
140 if (!yes_flag)
141 pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes_flag);
142
143 if (oldorigin != NULL) {
144 yes = yes_flag;
145 match = MATCH_ALL;
146 if ((it = pkgdb_query(db, oldorigin, MATCH_EXACT)) == NULL) {
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
147 pkgdb_close(db);
148 return (EX_IOERR);
149 }
150
151 if (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) != EPKG_OK) {
2a9b57f @bapt Allow pkg set -o even if oldorigin is not installed (fixes #273)
bapt authored
152 pkg = NULL;
153 /* fprintf(stderr, "%s not installed\n", oldorigin);
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
154 free(oldorigin);
155 pkgdb_it_free(it);
156 pkgdb_close(db);
2a9b57f @bapt Allow pkg set -o even if oldorigin is not installed (fixes #273)
bapt authored
157 return (EX_SOFTWARE);*/
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
158 }
2a9b57f @bapt Allow pkg set -o even if oldorigin is not installed (fixes #273)
bapt authored
159 if (pkg != NULL)
160 pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version);
161 if (!yes) {
162 if (pkg != NULL)
163 yes = query_yesno("Change origin from %s to %s for %s-%s? [y/N]: ",
164 oldorigin, neworigin, name, version);
165 else
166 yes = query_yesno("Change origin from %s to %s for all dependencies? "
167 "[y/N]: ", oldorigin, neworigin);
168 }
169 if (pkg != NULL && yes) {
ea7b1cd @bapt pkg set -o now change origin for a port and everything that depends o…
bapt authored
170 if (pkgdb_set(db, pkg, PKG_SET_ORIGIN, neworigin) != EPKG_OK)
0c3a249 @infracaninophile Use sysexits
infracaninophile authored
171 return (EX_IOERR);
ea7b1cd @bapt pkg set -o now change origin for a port and everything that depends o…
bapt authored
172 }
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
173 pkgdb_it_free(it);
174 }
15fe615 @bapt Add the forgotten set.c :)
bapt authored
175 i = 0;
176 do {
177 if ((it = pkgdb_query(db, argv[i], match)) == NULL) {
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
178 if (oldorigin != NULL)
179 free(oldorigin);
15fe615 @bapt Add the forgotten set.c :)
bapt authored
180 pkgdb_close(db);
181 return (EX_IOERR);
182 }
183
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
184 while (pkgdb_it_next(it, &pkg, loads) == EPKG_OK) {
ea7b1cd @bapt pkg set -o now change origin for a port and everything that depends o…
bapt authored
185 yes = yes_flag;
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
186 if ((sets & AUTOMATIC) == AUTOMATIC) {
187 pkg_get(pkg, PKG_AUTOMATIC, &automatic);
188 if (automatic == newautomatic)
189 continue;
190 if (!yes) {
191 pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version);
192 if (newautomatic)
193 yes = query_yesno("Mark %s-%s as automatically installed? [y/N]: ", name, version);
194 else
195 yes = query_yesno("Mark %s-%s as not automatically installed? [y/N]: ", name, version);
196 }
197 if (yes)
ea7b1cd @bapt pkg set -o now change origin for a port and everything that depends o…
bapt authored
198 pkgdb_set(db, pkg, PKG_SET_AUTOMATIC, newautomatic);
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
199 }
200 if ((sets & ORIGIN) == ORIGIN) {
201 struct pkg_dep *d = NULL;
202 while (pkg_deps(pkg, &d) == EPKG_OK) {
ea7b1cd @bapt pkg set -o now change origin for a port and everything that depends o…
bapt authored
203 /*
ee0b3ff Try to unify usage() output and several other output from pkg(8)
Lars Engels authored
204 * Do not query user when he has already
205 * been queried.
ea7b1cd @bapt pkg set -o now change origin for a port and everything that depends o…
bapt authored
206 */
207 if (pkgdb_set(db, pkg, PKG_SET_DEPORIGIN, oldorigin, neworigin) != EPKG_OK)
0c3a249 @infracaninophile Use sysexits
infracaninophile authored
208 return (EX_IOERR);
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
209 }
15fe615 @bapt Add the forgotten set.c :)
bapt authored
210 }
211 }
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
212 pkgdb_it_free(it);
15fe615 @bapt Add the forgotten set.c :)
bapt authored
213 i++;
214 } while (i < argc);
215
333eec4 @bapt pkg set -o oldorigin:neworigin mypkg
bapt authored
216 if (oldorigin != NULL)
217 free(oldorigin);
218 pkg_free(pkg);
219 pkgdb_close(db);
220
0c3a249 @infracaninophile Use sysexits
infracaninophile authored
221 return (EX_OK);
15fe615 @bapt Add the forgotten set.c :)
bapt authored
222 }
Something went wrong with that request. Please try again.