dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / trignote.c
CommitLineData
1479465f
GJ
1/*
2 * libdpkg - Debian packaging suite library routines
3 * trignote.c - trigger note handling
4 *
5 * Copyright © 2007 Canonical Ltd
6 * Written by Ian Jackson <ijackson@chiark.greenend.org.uk>
7 * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
8 *
9 * This is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <config.h>
24#include <compat.h>
25
26#include <string.h>
27
28#include <dpkg/dpkg.h>
29#include <dpkg/dpkg-db.h>
30#include <dpkg/pkg.h>
31#include <dpkg/pkg-list.h>
32#include <dpkg/dlist.h>
33#include <dpkg/triglib.h>
34
35/*
36 * Note: This is also called from fields.c where *pend is a temporary!
37 *
38 * trig is not copied!
39 */
40bool
41trig_note_pend_core(struct pkginfo *pend, const char *trig)
42{
43 struct trigpend *tp;
44
45 for (tp = pend->trigpend_head; tp; tp = tp->next)
46 if (strcmp(tp->name, trig) == 0)
47 return false;
48
49 tp = nfmalloc(sizeof(*tp));
50 tp->name = trig;
51 tp->next = pend->trigpend_head;
52 pend->trigpend_head = tp;
53
54 return true;
55}
56
57/*
58 * trig is not copied!
59 *
60 * @retval true For done.
61 * @retval false For already noted.
62 */
63bool
64trig_note_pend(struct pkginfo *pend, const char *trig)
65{
66 if (!trig_note_pend_core(pend, trig))
67 return false;
68
69 if (pend->trigaw.head)
70 pkg_set_status(pend, PKG_STAT_TRIGGERSAWAITED);
71 else
72 pkg_set_status(pend, PKG_STAT_TRIGGERSPENDING);
73
74 return true;
75}
76
77/*
78 * Note: This is called also from fields.c where *aw is a temporary
79 * but pend is from pkg_db_find()!
80 *
81 * @retval true For done.
82 * @retval false For already noted.
83 */
84bool
85trig_note_aw(struct pkginfo *pend, struct pkginfo *aw)
86{
87 struct trigaw *ta;
88
89 /* We search through aw's list because that's probably shorter. */
90 for (ta = aw->trigaw.head; ta; ta = ta->sameaw.next)
91 if (ta->pend == pend)
92 return false;
93
94 ta = nfmalloc(sizeof(*ta));
95 ta->aw = aw;
96 ta->pend = pend;
97 ta->samepend_next = pend->othertrigaw_head;
98 pend->othertrigaw_head = ta;
99 LIST_LINK_TAIL_PART(aw->trigaw, ta, sameaw);
100
101 return true;
102}
103
104static struct pkg_list *trig_awaited_pend_head;
105
106void
107trig_awaited_pend_enqueue(struct pkginfo *pend)
108{
109 struct pkg_list *tp;
110
111 for (tp = trig_awaited_pend_head; tp; tp = tp->next)
112 if (tp->pkg == pend)
113 return;
114
115 pkg_list_prepend(&trig_awaited_pend_head, pend);
116}
117
118void
119trig_awaited_pend_foreach(trig_awaited_pend_foreach_func *func)
120{
121 struct pkg_list *tp;
122
123 for (tp = trig_awaited_pend_head; tp; tp = tp->next)
124 if (!tp->pkg->trigpend_head)
125 func(tp->pkg);
126}
127
128void
129trig_awaited_pend_free(void)
130{
131 pkg_list_free(trig_awaited_pend_head);
132 trig_awaited_pend_head = NULL;
133}