aboutsummaryrefslogtreecommitdiffstats
path: root/libopkg/opkg_remove.c
blob: 5254388227903b8b6df996f5b9dcbd8762704e86 (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
/* vi: set expandtab sw=4 sts=4: */
/* opkg_remove.c - the opkg package management system

   Carl D. Worth

   Copyright (C) 2001 University of Southern California

   SPDX-License-Identifier: GPL-2.0-or-later

   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, 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.
*/

#include "config.h"

#include <libgen.h>
#include <stdio.h>
#include <glob.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>

#include "opkg_message.h"
#include "opkg_remove.h"
#include "opkg_cmd.h"
#include "file_util.h"
#include "sprintf_alloc.h"
#include "xfuncs.h"

void remove_data_files_and_list(pkg_t * pkg)
{
    str_list_t installed_dirs, installed_dirs_symlinks;
    str_list_elt_t *iter;
    file_list_t *installed_files;
    file_list_elt_t *fiter;
    char *file_name;
    conffile_t *conffile;
    int removed_a_dir, removed_a_dir_symlink;
    pkg_t *owner;
    int rootdirlen = 0;
    int r;

    installed_files = pkg_get_installed_files(pkg);
    if (installed_files == NULL) {
        opkg_msg(ERROR,
                 "Failed to determine installed "
                 "files for %s. None removed.\n", pkg->name);
        return;
    }

    str_list_init(&installed_dirs);
    str_list_init(&installed_dirs_symlinks);

    /* don't include trailing slash */
    if (opkg_config->offline_root)
        rootdirlen = strlen(opkg_config->offline_root);

    for (fiter = file_list_first(installed_files); fiter;
            fiter = file_list_next(installed_files, fiter)) {
        file_info_t *file_info = (file_info_t *)fiter->data;
        file_name = file_info->path;

        owner = file_hash_get_file_owner(file_name);
        if (owner != pkg)
            /* File may have been claimed by another package. */
            continue;

        if (file_is_dir(file_name)) {
            /*
               Ensure that the directory is marked for deletion only if
               no other installed package is using that directory.
             */
            if (dir_hash_get_ref_count(file_name) == 1)
            {
                str_list_append(&installed_dirs, file_name);
                dir_hash_remove(file_name);
            }
            else
            {
                /* directory should get a new owner in the file hash*/
                file_hash_remove(file_name);
            }
            continue;
        } else if (file_is_symlink(file_name)) {
            char *link_target;
            struct stat target_stat;

            link_target = realpath(file_name, NULL);
            if (link_target) {
                if ((xlstat(link_target, &target_stat) == 0) && S_ISDIR(target_stat.st_mode)) {
                    /*
                     * Ensure that the symlink is marked for deletion
                     * only if no other installed package uses that symlink.
                     */
                    if (dir_hash_get_ref_count(file_name) == 1)
                    {
                        str_list_append(&installed_dirs_symlinks, file_name);
                        dir_hash_remove(file_name);
                    }
                    else
                    {
                        /* directory should get a new owner in the file hash*/
                        file_hash_remove(file_name);
                    }

                    free(link_target);
                    continue;
                }
                free(link_target);
            }
        }

        conffile = pkg_get_conffile(pkg, file_name + rootdirlen);
        if (conffile) {
            if (conffile_has_been_modified(conffile)) {
                opkg_msg(NOTICE, "Not deleting modified conffile %s.\n",
                         file_name);
                continue;
            }
        }

        if (!opkg_config->noaction) {
            opkg_msg(INFO, "Deleting %s.\n", file_name);
            unlink(file_name);
        } else
            opkg_msg(INFO, "Not deleting %s. (noaction)\n", file_name);

        file_hash_remove(file_name);
    }

    /* Remove empty directories */
    if (!opkg_config->noaction) {
        do {
            removed_a_dir = 0;
            for (iter = str_list_first(&installed_dirs); iter;
                    iter = str_list_next(&installed_dirs, iter)) {
                file_name = (char *)iter->data;

                r = rmdir(file_name);
                if (r == 0) {
                    opkg_msg(INFO, "Deleting %s.\n", file_name);
                    removed_a_dir = 1;
                    str_list_remove(&installed_dirs, &iter);
                }
            }
        } while (removed_a_dir);
        do {
            removed_a_dir_symlink = 0;
            for (iter = str_list_first(&installed_dirs_symlinks); iter;
                    iter = str_list_next(&installed_dirs_symlinks, iter)) {
                file_name = (char *)iter->data;

                r = unlink(file_name);
                if (r == 0) {
                    opkg_msg(INFO, "Deleting %s.\n", file_name);
                    removed_a_dir_symlink = 1;
                    str_list_remove(&installed_dirs_symlinks, &iter);
                }
            }
        } while (removed_a_dir_symlink);
    }

    pkg_free_installed_files(pkg);
    pkg_remove_installed_files_list(pkg);

    /* Don't print warning for dirs that are provided by other packages */
    for (iter = str_list_first(&installed_dirs); iter;
            iter = str_list_next(&installed_dirs, iter)) {
        file_name = (char *)iter->data;

        owner = file_hash_get_file_owner(file_name);
        if (owner) {
            free(iter->data);
            iter->data = NULL;
            str_list_remove(&installed_dirs, &iter);
        }
    }

    /* cleanup */
    while (!void_list_empty(&installed_dirs)) {
        iter = str_list_pop(&installed_dirs);
        free(iter->data);
        free(iter);
    }
    while (!void_list_empty(&installed_dirs_symlinks)) {
        iter = str_list_pop(&installed_dirs_symlinks);
        free(iter->data);
        free(iter);
    }
    str_list_deinit(&installed_dirs);
    str_list_deinit(&installed_dirs_symlinks);
}

void remove_maintainer_scripts(pkg_t * pkg)
{
    unsigned int i;
    int err;
    char *globpattern, *filename, *lastdot;
    glob_t globbuf;

    if (opkg_config->noaction)
        return;

    sprintf_alloc(&globpattern, "%s/%s.*", pkg->dest->info_dir, pkg->name);

    err = glob(globpattern, 0, NULL, &globbuf);
    free(globpattern);
    if (err)
        return;

    for (i = 0; i < globbuf.gl_pathc; i++) {
        filename = xstrdup(basename(globbuf.gl_pathv[i]));
        lastdot = strrchr(filename, '.');
        *lastdot = '\0';
        // Only delete files that match the package name (the glob may match files
        // with similar names)
        if (!strcmp(filename, pkg->name)) {
            opkg_msg(INFO, "Deleting %s.\n", globbuf.gl_pathv[i]);
            unlink(globbuf.gl_pathv[i]);
        }
        free(filename);
    }
    globfree(&globbuf);
}

int opkg_remove_pkg(pkg_t * pkg)
{
    int err;
    int r;

    if (opkg_config->download_only)
	return 0;
    /*
     * If called from an upgrade and not from a normal remove,
     * ignore the essential flag.
     */
    if (pkg->essential) {
        if (opkg_config->force_removal_of_essential_packages) {
            opkg_msg(NOTICE,
                     "Removing essential package %s under your coercion.\n"
                     "\tIf your system breaks, you get to keep both pieces\n",
                     pkg->name);
        } else {
            opkg_msg(NOTICE,
                     "Refusing to remove essential package %s.\n"
                     "\tRemoving an essential package may lead to an unusable system, but if\n"
                     "\tyou enjoy that kind of pain, you can force opkg to proceed against\n"
                     "\tits will with the option: --force-removal-of-essential-packages\n",
                     pkg->name);
            return -1;
        }
    }

    if (pkg->parent == NULL)
        return 0;

    opkg_msg(NOTICE, "Removing %s (%s) from %s...\n", pkg->name,
             pkg->version, pkg->dest->name);
    pkg->state_flag |= SF_FILELIST_CHANGED;

    pkg->state_want = SW_DEINSTALL;
    opkg_state_changed++;

    r = pkg_run_script(pkg, "prerm", "remove");
    if (r != 0) {
        if (!opkg_config->force_remove) {
            opkg_msg(ERROR,
                     "not removing package \"%s\", " "prerm script failed\n",
                     pkg->name);
            opkg_msg(NOTICE,
                     "You can force removal of packages with failed "
                     "prerm scripts with the option: \n" "\t--force-remove\n");
            return -1;
        }
    }

    /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
     * maintains an empty filelist rather than deleting it. That seems
     * like a big pain, and I don't see that that should make a big
     * difference, but for anyone who wants tighter compatibility,
     * feel free to fix this. */
    remove_data_files_and_list(pkg);

    err = pkg_run_script(pkg, "postrm", "remove");

    remove_maintainer_scripts(pkg);
    pkg->state_status = SS_NOT_INSTALLED;

    pkg->parent->state_status = SS_NOT_INSTALLED;

    return err;
}