summaryrefslogtreecommitdiffstats
path: root/src/lib/make_descr.c
blob: 1ba152039dd60d3341be9e33b8ea82d4c8215403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
    Copyright (C) 2010  ABRT team
    Copyright (C) 2010  RedHat Inc

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "abrtlib.h"

// caller is responsible for freeing **dsc
static void add_content(bool *was_multiline, char **dsc, const char *header, const char *content)
{
    struct strbuf *buf_description = strbuf_new();
    if (*was_multiline)
        strbuf_append_char(buf_description, '\n');

    while (content[0] == '\n')
        content++;

    if (strchr(content, '\n') == NULL)
    {
        if (skip_whitespace(content)[0] == '\0')
        {
            /* empty, dont report at all */
            *dsc = strbuf_free_nobuf(buf_description);
            return;
        }
        /* one string value, like OS release */
        strbuf_append_strf(buf_description, "%s: %s\n", header, content);
        *was_multiline = 0;
    }
    else
    {
        /* multi-string value, like backtrace */
        if (!*was_multiline && (buf_description->len != 0)) /* if wasn't yet separated */
            strbuf_append_char(buf_description, '\n');

        strbuf_append_strf(buf_description, "%s\n-----\n%s", header, content);
        if (content[strlen(content) - 1] != '\n')
            strbuf_append_char(buf_description, '\n');

        *was_multiline = 1;
    }

    *dsc = strbuf_free_nobuf(buf_description);
}

/* Items we don't want to include */
static const char *const blacklisted_items[] = {
    FILENAME_ANALYZER ,
    FILENAME_COREDUMP ,
    FILENAME_HOSTNAME ,
    FILENAME_DUPHASH  ,
    FILENAME_UUID     ,
    FILENAME_INFORMALL,
    CD_DUMPDIR        ,
    FILENAME_COUNT    ,
    FILENAME_MESSAGE  ,
    NULL
};

char* make_description_mailx(crash_data_t *crash_data)
{
    struct strbuf *buf_dsc = strbuf_new();
    struct strbuf *buf_additional_files = strbuf_new();
    struct strbuf *buf_duphash_file = strbuf_new();
    struct strbuf *buf_common_files = strbuf_new();

    GHashTableIter iter;
    char *name;
    struct crash_item *value;
    g_hash_table_iter_init(&iter, crash_data);
    while (g_hash_table_iter_next(&iter, (void**)&name, (void**)&value))
    {
        if (value->flags & CD_FLAG_TXT)
        {
            if ((strcmp(name, FILENAME_DUPHASH) != 0)
             && (strcmp(name, FILENAME_ARCHITECTURE) != 0)
             && (strcmp(name, FILENAME_KERNEL) != 0)
             && (strcmp(name, FILENAME_PACKAGE) != 0)
            ) {
                strbuf_append_strf(buf_additional_files, "%s\n-----\n%s\n\n", name, value->content);
            }
            else if (strcmp(name, FILENAME_DUPHASH) == 0)
                strbuf_append_strf(buf_duphash_file, "%s\n-----\n%s\n\n", name, value->content);
            else
                strbuf_append_strf(buf_common_files, "%s\n-----\n%s\n\n", name, value->content);
        }
    }

    char *common_files = strbuf_free_nobuf(buf_common_files);
    char *duphash_file = strbuf_free_nobuf(buf_duphash_file);
    char *additional_files = strbuf_free_nobuf(buf_additional_files);

    strbuf_append_strf(buf_dsc, "Duplicate check\n=====\n%s\n\n", duphash_file);
    strbuf_append_strf(buf_dsc, "Common information\n=====\n%s\n\n", common_files);
    strbuf_append_strf(buf_dsc, "Additional information\n=====\n%s\n", additional_files);

    free(common_files);
    free(duphash_file);
    free(additional_files);

    return strbuf_free_nobuf(buf_dsc);
}

char* make_description_bz(crash_data_t *crash_data)
{
    struct strbuf *buf_dsc = strbuf_new();
    struct strbuf *buf_long_dsc = strbuf_new();

    GHashTableIter iter;
    char *name;
    struct crash_item *value;
    g_hash_table_iter_init(&iter, crash_data);
    while (g_hash_table_iter_next(&iter, (void**)&name, (void**)&value))
    {
        struct stat statbuf;
        unsigned flags = value->flags;
        const char *content = value->content;
        if (flags & CD_FLAG_TXT)
        {
            /* Skip items we are not interested in */
            const char *const *bl = blacklisted_items;
            while (*bl)
            {
                if (strcmp(name, *bl) == 0)
                    break;
                bl++;
            }
            if (*bl)
                continue; /* blacklisted */
            if (strcmp(content, "1.\n2.\n3.\n") == 0)
                continue; /* user did not change default "How to reproduce" */

            if (strlen(content) <= CD_TEXT_ATT_SIZE)
            {
                /* Add small (less than few kb) text items inline */
                bool was_multiline = 0;
                char *tmp = NULL;
                add_content(&was_multiline,
                        &tmp,
			/* "reproduce: blah" looks ugly, fixing: */
                        (strcmp(name, FILENAME_REPRODUCE) == 0) ? "How to reproduce" : name,
                        content
                );

                if (was_multiline)
                {
                    /* Not one-liner */
                    if (buf_long_dsc->len != 0)
                        strbuf_append_char(buf_long_dsc, '\n');

                    strbuf_append_str(buf_long_dsc, tmp);
                }
                else
                    strbuf_append_str(buf_dsc, tmp);

                free(tmp);
            }
            else
            {
                statbuf.st_size = strlen(content);
                goto add_attachment_info;
            }
        }
        if (flags & CD_FLAG_BIN)
        {
            /* In many cases, it is useful to know how big binary files are
             * (for example, helps with diagnosing bug upload problems)
             */
            if (stat(content, &statbuf) != 0)
                statbuf.st_size = (off_t) -1;

 add_attachment_info: ;
            char *descr;
            if (statbuf.st_size >= 0)
                descr = xasprintf("%s, %llu bytes", name, (long long)statbuf.st_size);
            else
                descr = xstrdup(name);
            bool was_multiline = 0;
            char *tmp = NULL;
            add_content(&was_multiline, &tmp, "Attached file", descr);
            free(descr);
            strbuf_append_str(buf_dsc, tmp);
            free(tmp);
        }
    }

    /* One-liners go first, then multi-line items */
    if (buf_dsc->len != 0 && buf_long_dsc->len != 0)
        strbuf_append_char(buf_dsc, '\n');


    char *long_dsc = strbuf_free_nobuf(buf_long_dsc);
    strbuf_append_str(buf_dsc, long_dsc);
    free(long_dsc);

    return strbuf_free_nobuf(buf_dsc);
}

char* make_description_logger(crash_data_t *crash_data)
{
    struct strbuf *buf_dsc = strbuf_new();
    struct strbuf *buf_long_dsc = strbuf_new();

    GHashTableIter iter;
    char *name;
    struct crash_item *value;
    g_hash_table_iter_init(&iter, crash_data);
    while (g_hash_table_iter_next(&iter, (void**)&name, (void**)&value))
    {
        const char *content = value->content;
        if (value->flags & (CD_FLAG_TXT|CD_FLAG_BIN))
        {
            /* Skip items we are not interested in */
            const char *const *bl = blacklisted_items;
            while (*bl)
            {
                if (name == *bl)
                    break;
                bl++;
            }
            if (*bl)
                continue; /* blacklisted */
            if (strcmp(content, "1.\n2.\n3.\n") == 0)
                continue; /* user did not change default "How to reproduce" */

            bool was_multiline = 0;
            char *tmp = NULL;
            add_content(&was_multiline, &tmp, name, content);

            if (was_multiline)
            {
                if (buf_long_dsc->len != 0)
                    strbuf_append_char(buf_long_dsc, '\n');

                strbuf_append_str(buf_long_dsc, tmp);
            }
            else
                strbuf_append_str(buf_dsc, tmp);
        }
    }

    if (buf_dsc->len != 0 && buf_long_dsc->len != 0)
        strbuf_append_char(buf_dsc, '\n');

    char *long_dsc = strbuf_free_nobuf(buf_long_dsc);
    strbuf_append_str(buf_dsc, long_dsc);
    free(long_dsc);

    return strbuf_free_nobuf(buf_dsc);
}

char* make_description_reproduce_comment(crash_data_t *crash_data)
{
    char *repro = NULL;
    char *comment = NULL;
    struct crash_item *value;

    value = get_crash_data_item_or_NULL(crash_data, FILENAME_REPRODUCE);
    if (value)
    {
        if (value->content[0]
         && strcmp(value->content, "1.\n2.\n3.\n") != 0
        ) {
            repro = xasprintf("\n\nHow to reproduce\n-----\n%s", value->content);
        }
    }

    value = get_crash_data_item_or_NULL(crash_data, FILENAME_COMMENT);
    if (value)
    {
        if (value->content[0])
            comment = xasprintf("\n\nComment\n-----\n%s", value->content);
    }

    if (!repro && !comment)
        return NULL;

    struct strbuf *buf_dsc = strbuf_new();

    if (repro)
        strbuf_append_str(buf_dsc, repro);

    if (comment)
        strbuf_append_str(buf_dsc, comment);

    free(repro);
    free(comment);

    return strbuf_free_nobuf(buf_dsc);
}