Skip to content

Commit 6b3b7e8

Browse files
committed
Added passing the needed info through an environment variable in bencode serialization. Can be further extended, if we need more info.
1 parent 55cdc52 commit 6b3b7e8

File tree

11 files changed

+304
-7
lines changed

11 files changed

+304
-7
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bin_PROGRAMS = \
2727

2828
dpkg_SOURCES = \
2929
archives.c archives.h \
30+
bencode.c \
3031
cleanup.c \
3132
configure.c \
3233
depcon.c \
@@ -38,12 +39,14 @@ dpkg_SOURCES = \
3839
help.c \
3940
main.c main.h \
4041
packages.c \
42+
pascal_str.c \
4143
perpkgstate.c \
4244
remove.c \
4345
script.c \
4446
select.c \
4547
selinux.c \
4648
trigproc.c \
49+
triggerer_info_passthrough.c \
4750
unpack.c \
4851
update.c \
4952
verify.c \

src/archives.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "main.h"
6262
#include "archives.h"
6363
#include "filters.h"
64+
#include "triggerer_info_passthrough.h"
6465

6566
static inline void
6667
fd_writeback_init(int fd)
@@ -1478,6 +1479,7 @@ archivefiles(const char *const *argv)
14781479
int i;
14791480
jmp_buf ejbuf;
14801481
enum modstatdb_rw msdbflags;
1482+
struct passed_through_package_info unpackedInfo = passed_through_package_info_init();
14811483

14821484
trigproc_install_hooks();
14831485

@@ -1580,14 +1582,16 @@ archivefiles(const char *const *argv)
15801582

15811583
dpkg_selabel_load();
15821584

1583-
process_archive(argp[i]);
1585+
process_archive(argp[i], &unpackedInfo);
15841586
onerr_abort++;
15851587
m_output(stdout, _("<standard output>"));
15861588
m_output(stderr, _("<standard error>"));
15871589
onerr_abort--;
15881590

15891591
pop_error_context(ehflag_normaltidy);
15901592
}
1593+
serialize_the_info_about_triggerers_into_an_env_variable(&unpackedInfo);
1594+
passed_through_package_info_free(&unpackedInfo);
15911595

15921596
dpkg_selabel_close();
15931597

src/bencode.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This is free and unencumbered software released into the public domain.
3+
4+
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
5+
6+
* In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
7+
8+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9+
10+
* For more information, please refer to <https://unlicense.org/>
11+
*/
12+
13+
#include <stdio.h>
14+
15+
#include "bencode.h"
16+
17+
size_t measure_benc_pascal_str(struct pascal_str *s){
18+
return snprintf(NULL, 0, "%zu:", s->size) + s->size;
19+
}
20+
21+
void serialize_benc_pascal_str(char **serPtr, struct pascal_str *s){
22+
*serPtr += sprintf(*serPtr, "%zu:%s", s->size, s->ptr);
23+
}
24+
void serialize_benc_tag(char **serPtr, char tag){
25+
*serPtr += sprintf(*serPtr, "1:%c", tag);
26+
}
27+
28+
void serialize_benc_tag_value_pair(char **serPtr, char tag, struct pascal_str *s){
29+
if(s->ptr){
30+
serialize_benc_tag(serPtr, tag);
31+
serialize_benc_pascal_str(serPtr, s);
32+
}
33+
}
34+
35+
36+
size_t measure_benc_tag_value_pair(struct pascal_str *s){
37+
if(s->ptr){
38+
return measure_benc_pascal_str(s) + OUR_BENC_KEY_TAG_SIZE;
39+
} else {
40+
return 0;
41+
}
42+
}

src/bencode.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
/*
4+
* This is free and unencumbered software released into the public domain.
5+
6+
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
7+
8+
* In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
9+
10+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
12+
* For more information, please refer to <https://unlicense.org/>
13+
*/
14+
15+
#include "pascal_str.h"
16+
17+
enum {
18+
OUR_BENC_KEY_TAG_SIZE = 3,
19+
BENC_START_END = 2, // l<...>e
20+
};
21+
22+
size_t measure_benc_pascal_str(struct pascal_str *s);
23+
void serialize_benc_pascal_str(char **serPtr, struct pascal_str *s);
24+
void serialize_benc_tag(char **serPtr, char tag);
25+
void serialize_benc_tag_value_pair(char **serPtr, char tag, struct pascal_str *s);
26+
size_t measure_benc_tag_value_pair(struct pascal_str *s);

src/main.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <dpkg/pkg-list.h>
2727

2828
#include "force.h"
29+
#include "triggerer_info_passthrough.h"
2930

3031
/* These two are defined in <dpkg/fsys.h>. */
3132
struct fsys_namenode_list;
@@ -149,7 +150,7 @@ void ensure_package_clientdata(struct pkginfo *pkg);
149150
/* from archives.c */
150151

151152
int archivefiles(const char *const *argv);
152-
void process_archive(const char *filename);
153+
void process_archive(const char *filename, struct passed_through_package_info *unpackedInfo);
153154
bool wanttoinstall(struct pkginfo *pkg);
154155

155156
/* from update.c */

src/packages.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <dpkg/db-fsys.h>
4646

4747
#include "main.h"
48+
#include "triggerer_info_passthrough.h"
4849

4950
static struct pkginfo *progress_bytrigproc;
5051
static struct pkg_queue queue = PKG_QUEUE_INIT;
@@ -70,7 +71,7 @@ enqueue_package_mark_seen(struct pkginfo *pkg)
7071
}
7172

7273
static void
73-
enqueue_pending(void)
74+
enqueue_pending(struct passed_through_package_info *packagePassedThroughInfo)
7475
{
7576
struct pkg_hash_iter *iter;
7677
struct pkginfo *pkg;
@@ -108,20 +109,22 @@ enqueue_pending(void)
108109
default:
109110
internerr("unknown action '%d'", cipaction->arg_int);
110111
}
112+
passed_through_package_info_append(packagePassedThroughInfo, pkg);
111113
enqueue_package(pkg);
112114
}
113115
pkg_hash_iter_free(iter);
114116
}
115117

116118
static void
117-
enqueue_specified(const char *const *argv)
119+
enqueue_specified(const char *const *argv, struct passed_through_package_info *packagePassedThroughInfo)
118120
{
119121
const char *thisarg;
120122

121123
while ((thisarg = *argv++) != NULL) {
122124
struct pkginfo *pkg;
123125

124126
pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
127+
passed_through_package_info_append(packagePassedThroughInfo, pkg);
125128
if (pkg->status == PKG_STAT_NOTINSTALLED &&
126129
str_match_end(pkg->set->name, DEBEXT)) {
127130
badusage(_("you must specify packages by their own names, "
@@ -137,6 +140,7 @@ enqueue_specified(const char *const *argv)
137140
int
138141
packages(const char *const *argv)
139142
{
143+
struct passed_through_package_info unpackedInfo = passed_through_package_info_init();
140144
trigproc_install_hooks();
141145

142146
modstatdb_open(f_noact ? msdbrw_readonly :
@@ -151,14 +155,17 @@ packages(const char *const *argv)
151155
if (*argv)
152156
badusage(_("--%s --pending does not take any non-option arguments"),cipaction->olong);
153157

154-
enqueue_pending();
158+
enqueue_pending(&unpackedInfo);
155159
} else {
156160
if (!*argv)
157161
badusage(_("--%s needs at least one package name argument"), cipaction->olong);
158162

159-
enqueue_specified(argv);
163+
enqueue_specified(argv, &unpackedInfo);
160164
}
161165

166+
serialize_the_info_about_triggerers_into_an_env_variable(&unpackedInfo);
167+
passed_through_package_info_free(&unpackedInfo);
168+
162169
ensure_diversions();
163170

164171
process_queue();

src/pascal_str.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This is free and unencumbered software released into the public domain.
3+
4+
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
5+
6+
* In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
7+
8+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9+
10+
* For more information, please refer to <https://unlicense.org/>
11+
*/
12+
13+
#include <string.h>
14+
#include <stdlib.h>
15+
16+
#include "pascal_str.h"
17+
18+
struct pascal_str init_pascal_str(const char * s){
19+
struct pascal_str res;
20+
if(s){
21+
res.size = strlen(s);
22+
res.ptr = malloc(res.size + 1);
23+
strcpy(res.ptr, s);
24+
}else{
25+
res.size = 0;
26+
res.ptr = NULL;
27+
}
28+
return res;
29+
}
30+
31+
struct pascal_str init_nonowning_pascal_str(const char * s){
32+
struct pascal_str res;
33+
res.size = strlen(s);
34+
res.ptr = s;
35+
return res;
36+
}
37+
38+
void free_pascal_string(struct pascal_str *str){
39+
free(str->ptr);
40+
str->ptr = NULL;
41+
str->size = 0;
42+
}

src/pascal_str.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
/*
4+
* This is free and unencumbered software released into the public domain.
5+
6+
* Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
7+
8+
* In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
9+
10+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11+
12+
* For more information, please refer to <https://unlicense.org/>
13+
*/
14+
15+
struct pascal_str {
16+
size_t size;
17+
char * ptr;
18+
};
19+
20+
struct pascal_str init_pascal_str(const char * s);
21+
22+
struct pascal_str init_nonowning_pascal_str(const char * s);
23+
24+
void free_pascal_string(struct pascal_str *str);

src/triggerer_info_passthrough.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <stdlib.h>
2+
3+
#include "pascal_str.h"
4+
#include "bencode.h"
5+
6+
#include "triggerer_info_passthrough.h"
7+
#include <dpkg/ehandle.h>
8+
9+
10+
11+
struct passed_through_package_info passed_through_package_info_init(void){
12+
struct passed_through_package_info unpackedInfo;
13+
unpackedInfo.count = 0;
14+
unpackedInfo.records = NULL;
15+
return unpackedInfo;
16+
}
17+
18+
19+
void free_passed_through_package_info_record(struct passed_through_package_info_record *r){
20+
free_pascal_string(&r->name);
21+
free_pascal_string(&r->arch);
22+
free_pascal_string(&r->version);
23+
}
24+
25+
void passed_through_package_info_free(struct passed_through_package_info *unpackedInfo){
26+
for(unsigned short int i = 0; i< unpackedInfo->count; ++i){
27+
free_passed_through_package_info_record(&unpackedInfo->records[i]);
28+
}
29+
free(unpackedInfo->records);
30+
unpackedInfo->records = NULL;
31+
unpackedInfo->count = 0;
32+
}
33+
34+
void passed_through_package_info_emplace_internal(struct passed_through_package_info_record * rec, struct pkginfo *pkg, struct pascal_str (*pascal_str_ctor)(char *)){
35+
rec->name = pascal_str_ctor(pkg->set->name);
36+
rec->version = pascal_str_ctor(pkg->available.version.version);
37+
rec->arch = pascal_str_ctor(pkg->available.arch->name);
38+
rec->origin = pascal_str_ctor(pkg->available.origin);
39+
}
40+
41+
void passed_through_package_info_emplace(struct passed_through_package_info_record * rec, struct pkginfo *pkg){
42+
passed_through_package_info_emplace_internal(rec, pkg, init_pascal_str);
43+
}
44+
45+
void passed_through_package_info_nonowning_emplace(struct passed_through_package_info_record * rec, struct pkginfo *pkg){
46+
passed_through_package_info_emplace_internal(rec, pkg, init_nonowning_pascal_str);
47+
}
48+
49+
void passed_through_package_info_append(struct passed_through_package_info *unpackedInfo, struct pkginfo *pkg){
50+
if(!unpackedInfo)
51+
return;
52+
53+
struct passed_through_package_info_record * rec;
54+
unsigned int prevCountAndIndex = unpackedInfo->count;
55+
unpackedInfo->records = realloc(unpackedInfo->records, sizeof(struct passed_through_package_info_record) * (++unpackedInfo->count));
56+
if(!unpackedInfo->records){
57+
ohshite("Unable to reallocate in %s", __func__);
58+
}
59+
rec = &(unpackedInfo->records[prevCountAndIndex]);
60+
passed_through_package_info_emplace(rec, pkg);
61+
}
62+
63+
size_t precompute_the_bencode_serialized_size(struct passed_through_package_info *unpackedInfo){
64+
size_t totalSize = BENC_START_END;
65+
unsigned int i;
66+
for(i=0;i<unpackedInfo->count;++i){
67+
if(unpackedInfo->records[i].name.size){
68+
totalSize += measure_benc_pascal_str(&unpackedInfo->records[i].name);
69+
totalSize += BENC_START_END;
70+
totalSize += measure_benc_tag_value_pair(&unpackedInfo->records[i].arch);
71+
totalSize += measure_benc_tag_value_pair(&unpackedInfo->records[i].version);
72+
totalSize += measure_benc_tag_value_pair(&unpackedInfo->records[i].origin);
73+
}
74+
}
75+
totalSize ++; // null termination
76+
return totalSize;
77+
}
78+
79+
void serialize_the_info_about_triggerers_into_an_env_variable(struct passed_through_package_info *unpackedInfo){
80+
unsigned int i;
81+
size_t totalSize = precompute_the_bencode_serialized_size(unpackedInfo);
82+
char buf4str[totalSize];
83+
char * serPtr;
84+
85+
memset(buf4str, ' ', totalSize);
86+
buf4str[totalSize - 1] = 0;
87+
serPtr = &buf4str[0];
88+
*(serPtr++) = 'd';
89+
for(i=0;i<unpackedInfo->count;++i){
90+
serialize_benc_pascal_str(&serPtr, &unpackedInfo->records[i].name);
91+
*(serPtr++) = 'd';
92+
serialize_benc_tag_value_pair(&serPtr, 'A', &unpackedInfo->records[i].arch);
93+
serialize_benc_tag_value_pair(&serPtr, 'V', &unpackedInfo->records[i].version);
94+
serialize_benc_tag_value_pair(&serPtr, 'O', &unpackedInfo->records[i].origin);
95+
*(serPtr++) = 'e';
96+
}
97+
*(serPtr++) = 'e';
98+
if((unsigned long)((char*) serPtr - (char*) buf4str) > totalSize){
99+
ohshit("Buffer overflow %zu > %zu !", (char*) serPtr - (char*) buf4str, totalSize);
100+
}
101+
setenv("DPKG_TRIGGERER_PACKAGES_INFO", buf4str, 1);
102+
}
103+
104+
void serialize_the_info_about_pkg_into_an_env_variable(struct pkginfo *pkg){
105+
struct passed_through_package_info_record rec; // no need to free
106+
//passed_through_package_info_append(unpackedInfo, pkg);
107+
passed_through_package_info_emplace(&rec, pkg);
108+
struct passed_through_package_info i;
109+
i.records = &rec;
110+
i.count = 1;
111+
112+
passed_through_package_info_nonowning_emplace(&rec, pkg);
113+
serialize_the_info_about_triggerers_into_an_env_variable(&i);
114+
}

0 commit comments

Comments
 (0)