Line data Source code
1 : /*-
2 : * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
3 : * All rights reserved.
4 : *
5 : * Redistribution and use in source and binary forms, with or without
6 : * modification, are permitted provided that the following conditions
7 : * are met:
8 : * 1. Redistributions of source code must retain the above copyright
9 : * notice, this list of conditions and the following disclaimer
10 : * in this position and unchanged.
11 : * 2. Redistributions in binary form must reproduce the above copyright
12 : * notice, this list of conditions and the following disclaimer in the
13 : * documentation and/or other materials provided with the distribution.
14 : *
15 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 : * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 : */
26 :
27 : #include <sys/stat.h>
28 :
29 : #include <assert.h>
30 : #include <dirent.h>
31 : #include "pkg.h"
32 : #include "private/pkg.h"
33 : #include "private/event.h"
34 :
35 : static void
36 0 : rm_rf(const char *path)
37 : {
38 : DIR *d;
39 : struct dirent *e;
40 : struct stat st;
41 : char filepath[MAXPATHLEN];
42 :
43 0 : if ((d = opendir(path)) == NULL) {
44 0 : pkg_emit_errno("opendir", path);
45 0 : return;
46 : }
47 :
48 0 : while ((e = readdir(d)) != NULL) {
49 0 : if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
50 0 : continue;
51 0 : snprintf(filepath, sizeof(filepath), "%s/%s", path, e->d_name);
52 0 : if (lstat(filepath, &st) != 0) {
53 0 : pkg_emit_errno("lstat", filepath);
54 0 : continue;
55 : }
56 0 : if (S_ISDIR(st.st_mode))
57 0 : rm_rf(filepath);
58 0 : remove(filepath);
59 : }
60 0 : closedir(d);
61 : }
62 :
63 : void
64 4 : pkg_cache_full_clean(void)
65 : {
66 : const char *cachedir;
67 :
68 4 : if (!pkg_object_bool(pkg_config_get("AUTOCLEAN")))
69 4 : return;
70 :
71 0 : pkg_debug(1, "Cleaning up cachedir");
72 :
73 0 : cachedir = pkg_object_string(pkg_config_get("PKG_CACHEDIR"));
74 :
75 0 : return (rm_rf(cachedir));
76 : }
|