2 * dpkg-trigger - trigger management utility
4 * Copyright © 2007 Canonical Ltd.
5 * Written by Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2014 Guillem Jover <guillem@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/>.
25 #include <sys/types.h>
36 #include <dpkg/i18n.h>
37 #include <dpkg/dpkg.h>
38 #include <dpkg/dpkg-db.h>
39 #include <dpkg/debug.h>
40 #include <dpkg/options.h>
41 #include <dpkg/trigdeferred.h>
42 #include <dpkg/triglib.h>
43 #include <dpkg/pkg-spec.h>
45 static const char printforhelp
[] = N_(
46 "Type dpkg-trigger --help for help about this utility.");
49 printversion(const char *const *argv
)
51 printf(_("Debian %s package trigger utility version %s.\n"),
52 dpkg_get_progname(), PACKAGE_RELEASE
);
55 "This is free software; see the GNU General Public License version 2 or\n"
56 "later for copying conditions. There is NO warranty.\n"));
58 m_output(stdout
, _("<standard output>"));
64 usage(const char *const *argv
)
67 "Usage: %s [<option>...] <trigger-name>\n"
68 " %s [<option>...] <command>\n"
69 "\n"), dpkg_get_progname(), dpkg_get_progname());
73 " --check-supported Check if the running dpkg supports triggers.\n"
77 " -?, --help Show this help message.\n"
78 " --version Show the version.\n"
83 " --admindir=<directory> Use <directory> instead of %s.\n"
84 " --root=<directory> Use <directory> instead of %s.\n"
85 " --by-package=<package> Override trigger awaiter (normally set\n"
87 " --await Package needs to await the processing.\n"
88 " --no-await No package needs to await the processing.\n"
89 " --no-act Just test - don't actually change anything.\n"
90 "\n"), ADMINDIR
, "/");
92 m_output(stdout
, _("<standard output>"));
97 static int opt_act
= 1;
98 static int opt_await
= 1;
99 static const char *opt_bypackage
;
101 static const char *activate
;
102 static bool done_trig
, ctrig
;
105 yespackage(const char *awname
)
107 trigdef_update_printf(" %s", awname
);
111 parse_awaiter_package(void)
113 struct dpkg_error err
= DPKG_ERROR_INIT
;
119 if (opt_bypackage
== NULL
) {
120 const char *pkgname
, *archname
;
122 pkgname
= getenv("DPKG_MAINTSCRIPT_PACKAGE");
123 archname
= getenv("DPKG_MAINTSCRIPT_ARCH");
124 if (pkgname
== NULL
|| archname
== NULL
)
125 badusage(_("must be called from a maintainer script"
126 " (or with a --by-package option)"));
128 pkg
= pkg_spec_find_pkg(pkgname
, archname
, &err
);
129 } else if (strcmp(opt_bypackage
, "-") == 0) {
132 pkg
= pkg_spec_parse_pkg(opt_bypackage
, &err
);
135 /* Normalize the bypackage name if there was no error. */
137 opt_bypackage
= pkg_name(pkg
, pnaw_same
);
143 tdm_add_trig_begin(const char *trig
)
145 ctrig
= strcmp(trig
, activate
) == 0;
146 trigdef_update_printf("%s", trig
);
147 if (!ctrig
|| done_trig
)
149 yespackage(opt_bypackage
);
154 tdm_add_package(const char *awname
)
156 if (ctrig
&& strcmp(awname
, opt_bypackage
) == 0)
162 tdm_add_trig_end(void)
164 trigdef_update_printf("\n");
167 static const struct trigdefmeths tdm_add
= {
168 .trig_begin
= tdm_add_trig_begin
,
169 .package
= tdm_add_package
,
170 .trig_end
= tdm_add_trig_end
,
174 do_trigger(const char *const *argv
)
177 enum trigdef_update_flags tduf
;
178 enum trigdef_update_status tdus
;
180 if (!*argv
|| argv
[1])
181 badusage(_("takes one argument, the trigger name"));
183 badname
= parse_awaiter_package();
185 badusage(_("illegal awaited package name '%.250s': %.250s"),
186 opt_bypackage
, badname
);
189 badname
= trig_name_is_illegal(activate
);
191 badusage(_("invalid trigger name '%.250s': %.250s"),
194 trigdef_set_methods(&tdm_add
);
196 tduf
= TDUF_NO_LOCK_OK
;
198 tduf
|= TDUF_WRITE
| TDUF_WRITE_IF_EMPTY
;
199 tdus
= trigdef_update_start(tduf
);
203 trigdef_update_printf("%s %s\n", activate
, opt_bypackage
);
204 trigdef_process_done();
211 do_check(const char *const *argv
)
213 enum trigdef_update_status uf
;
216 badusage(_("--%s takes no arguments"), "check-supported");
218 uf
= trigdef_update_start(TDUF_NO_LOCK_OK
);
220 case TDUS_ERROR_NO_DIR
:
221 notice(_("triggers data directory not yet created"));
223 case TDUS_ERROR_NO_DEFERRED
:
224 notice(_("trigger records not yet in existence"));
227 case TDUS_ERROR_EMPTY_DEFERRED
:
230 internerr("unknown trigdef_update_start return value '%d'", uf
);
234 static const struct cmdinfo cmdinfo_trigger
=
235 ACTION("trigger", 0, 0, do_trigger
);
237 static const struct cmdinfo cmdinfos
[] = {
238 ACTION("check-supported", 0, 0, do_check
),
239 ACTION("help", '?', 0, usage
),
240 ACTION("version", 0, 0, printversion
),
242 { "admindir", 0, 1, NULL
, NULL
, set_admindir
, 0 },
243 { "root", 0, 1, NULL
, NULL
, set_root
, 0 },
244 { "by-package", 'f', 1, NULL
, &opt_bypackage
},
245 { "await", 0, 0, &opt_await
, NULL
, NULL
, 1 },
246 { "no-await", 0, 0, &opt_await
, NULL
, NULL
, 0 },
247 { "no-act", 0, 0, &opt_act
, NULL
, NULL
, 0 },
252 main(int argc
, const char *const *argv
)
256 dpkg_locales_init(PACKAGE
);
257 dpkg_program_init("dpkg-trigger");
258 dpkg_options_parse(&argv
, cmdinfos
, printforhelp
);
260 debug(dbg_general
, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
263 setaction(&cmdinfo_trigger
, NULL
);
265 ret
= cipaction
->action(argv
);