po: Update German man pages translation
[dpkg.git] / dselect / method.cc
blobed7e83470f0a10ad9acf4eb6f13de90a5ef38617
1 /*
2 * dselect - Debian package maintenance user interface
3 * method.cc - access method handling
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2001,2002 Wichert Akkerman <wakkerma@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/file.h>
28 #include <sys/wait.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <dirent.h>
35 #include <signal.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <stdio.h>
40 #include <dpkg/i18n.h>
41 #include <dpkg/dpkg.h>
42 #include <dpkg/dpkg-db.h>
43 #include <dpkg/subproc.h>
44 #include <dpkg/command.h>
46 #include "dselect.h"
47 #include "method.h"
49 static const char *const methoddirectories[]= {
50 LIBDIR "/" METHODSDIR,
51 LOCALLIBDIR "/" METHODSDIR,
52 nullptr
55 static char *methodlockfile = nullptr;
56 static int methlockfd= -1;
58 static void
59 sthfailed(const char * reasoning)
61 curseson();
62 clear();
63 printw("\n\n%s: %s\n", DSELECT, reasoning);
64 attrset(A_BOLD);
65 addstr(_("\nPress <enter> to continue."));
66 attrset(A_NORMAL);
67 refresh(); getch();
70 static void cu_unlockmethod(int, void**) {
71 struct flock fl;
73 if (methodlockfile == nullptr)
74 internerr("method lock file is nullptr");
75 if (methlockfd < 0)
76 internerr("method lock fd is %d < 0", methlockfd);
77 fl.l_type=F_UNLCK; fl.l_whence= SEEK_SET; fl.l_start=fl.l_len=0;
78 if (fcntl(methlockfd, F_SETLK, &fl) < 0)
79 sthfailed(_("cannot unlock access method area"));
82 static enum urqresult ensureoptions(void) {
83 dselect_option *newoptions;
84 int nread;
86 if (!options) {
87 newoptions = nullptr;
88 nread= 0;
89 for (const char *const *ccpp = methoddirectories; *ccpp; ccpp++)
90 readmethods(*ccpp, &newoptions, &nread);
91 if (!newoptions) {
92 sthfailed(_("no access methods are available"));
93 return urqr_fail;
95 options= newoptions;
96 noptions= nread;
98 return urqr_normal;
101 static enum urqresult lockmethod(void) {
102 struct flock fl;
104 if (methodlockfile == nullptr)
105 methodlockfile = dpkg_db_get_path(METHLOCKFILE);
107 if (methlockfd < 0) {
108 methlockfd= open(methodlockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
109 if (methlockfd < 0) {
110 if ((errno == EPERM) || (errno == EACCES)) {
111 sthfailed(_("requested operation requires superuser privilege"));
112 return urqr_fail;
114 sthfailed(_("cannot open or create access method lockfile"));
115 return urqr_fail;
118 fl.l_type=F_WRLCK; fl.l_whence=SEEK_SET; fl.l_start=fl.l_len=0;
119 if (fcntl(methlockfd, F_SETLK, &fl) < 0) {
120 if (errno == EACCES || errno == EAGAIN) {
121 sthfailed(_("the access method area is already locked"));
122 return urqr_fail;
124 sthfailed(_("cannot lock access method area"));
125 return urqr_fail;
127 push_cleanup(cu_unlockmethod, ~0, 0);
128 return urqr_normal;
131 static urqresult
132 falliblesubprocess(struct command *cmd)
134 pid_t pid;
135 int i, c;
137 cursesoff();
139 subproc_signals_ignore(cmd->name);
141 pid = subproc_fork();
142 if (pid == 0) {
143 subproc_signals_cleanup(0, nullptr);
144 command_exec(cmd);
147 fprintf(stderr, "\n");
149 i = subproc_reap(pid, cmd->name, SUBPROC_WARN);
151 subproc_signals_restore();
153 if (i == 0) {
154 sleep(1);
155 return urqr_normal;
157 fprintf(stderr,_("Press <enter> to continue.\n"));
158 m_output(stderr, _("<standard error>"));
159 do {
160 c = fgetc(stdin);
161 } while ((c == EOF && errno == EINTR) || (c != '\n' && c != EOF));
162 if (c == EOF)
163 ohshite(_("error reading acknowledgement of program failure message"));
164 return urqr_fail;
167 static urqresult runscript(const char *exepath, const char *name) {
168 urqresult ur;
170 ur= ensureoptions(); if (ur != urqr_normal) return ur;
171 ur=lockmethod(); if (ur != urqr_normal) return ur;
172 getcurrentopt();
174 if (coption) {
175 struct command cmd;
177 strcpy(coption->meth->pathinmeth,exepath);
179 command_init(&cmd, coption->meth->path, name);
180 command_add_args(&cmd, exepath, dpkg_db_get_dir(),
181 coption->meth->name, coption->name, nullptr);
182 ur = falliblesubprocess(&cmd);
183 command_destroy(&cmd);
184 } else {
185 sthfailed(_("no access method is selected or configured"));
186 ur= urqr_fail;
188 pop_cleanup(ehflag_normaltidy);
190 return ur;
193 urqresult urq_update(void) {
194 return runscript(METHODUPDATESCRIPT,_("update available list script"));
197 urqresult urq_install(void) {
198 return runscript(METHODINSTALLSCRIPT,_("installation script"));
201 static urqresult rundpkgauto(const char *name, const char *dpkgmode) {
202 urqresult ur;
203 struct command cmd;
205 command_init(&cmd, DPKG, name);
206 command_add_args(&cmd, DPKG, "--admindir", dpkg_db_get_dir(), "--pending",
207 dpkgmode, nullptr);
209 cursesoff();
210 printf(_("running %s %s ...\n"), "dpkg --pending", dpkgmode);
211 fflush(stdout);
212 ur = falliblesubprocess(&cmd);
213 command_destroy(&cmd);
215 return ur;
218 urqresult urq_remove(void) {
219 return rundpkgauto("dpkg --remove","--remove");
222 urqresult urq_config(void) {
223 return rundpkgauto("dpkg --configure","--configure");
226 urqresult urq_setup(void) {
227 quitaction qa;
228 urqresult ur;
230 ur= ensureoptions(); if (ur != urqr_normal) return ur;
231 ur=lockmethod(); if (ur != urqr_normal) return ur;
232 getcurrentopt();
234 curseson();
235 methodlist *l= new methodlist();
236 qa= l->display();
237 delete l;
239 if (qa == qa_quitchecksave) {
240 struct command cmd;
242 strcpy(coption->meth->pathinmeth,METHODSETUPSCRIPT);
244 command_init(&cmd, coption->meth->path, _("query/setup script"));
245 command_add_args(&cmd, METHODSETUPSCRIPT, dpkg_db_get_dir(),
246 coption->meth->name, coption->name, nullptr);
247 ur = falliblesubprocess(&cmd);
248 command_destroy(&cmd);
249 if (ur == urqr_normal) writecurrentopt();
250 } else {
251 ur= urqr_fail;
254 pop_cleanup(ehflag_normaltidy);
255 return ur;