OSDN Git Service

initial commit
[openbsd-octeon/openbsd-octeon.git] / src / usr.sbin / pkg_add / OpenBSD / SharedItems.pm
1 # ex:ts=8 sw=4:
2 # $OpenBSD: SharedItems.pm,v 1.24 2010/06/18 09:05:03 espie Exp $
3 #
4 # Copyright (c) 2004-2006 Marc Espie <espie@openbsd.org>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 use strict;
19 use warnings;
20
21 package OpenBSD::SharedItems;
22
23 use OpenBSD::Error;
24 use OpenBSD::PackageInfo;
25 use OpenBSD::PackingList;
26 use OpenBSD::Paths;
27
28 sub find_items_in_installed_packages
29 {
30         my $state = shift;
31         my $db = OpenBSD::SharedItemsRecorder->new;
32         $state->status->what("Read")->object("shared items");
33         $state->progress->for_list("Read shared items", [installed_packages()],
34             sub {
35                 my $e = shift;
36                 my $plist = OpenBSD::PackingList->from_installation($e,
37                     \&OpenBSD::PackingList::SharedItemsOnly) or next;
38                 return if !defined $plist;
39                 $plist->record_shared($db, $e);
40             });
41         return $db;
42 }
43
44 sub cleanup
45 {
46         my ($recorder, $state) = @_;
47
48         my $remaining = find_items_in_installed_packages($state);
49
50         $state->progress->clear;
51         $state->status->what("Clean");
52         $state->progress->set_header("Clean shared items");
53         my $h = $recorder->{dirs};
54         my $u = $recorder->{users};
55         my $g = $recorder->{groups};
56         my $total = 0;
57         $total += keys %$h if defined $h;
58         $total += keys %$u if defined $u;
59         $total += keys %$g if defined $g;
60         my $done = 0;
61
62         for my $d (sort {$b cmp $a} keys %$h) {
63                 $state->progress->show($done, $total);
64                 my $realname = $state->{destdir}.$d;
65                 if ($remaining->{dirs}->{$realname}) {
66                         for my $i (@{$h->{$d}}) {
67                                 $state->log->set_context($i->{pkgname});
68                                 $i->reload($state);
69                         }
70                 } else {
71                         for my $i (@{$h->{$d}}) {
72                                 $state->log->set_context($i->{pkgname});
73                                 $i->cleanup($state);
74                         }
75                         if (!rmdir $realname) {
76                                 $state->log("Error deleting directory #1: #2",
77                                     $realname, $!)
78                                         unless $state->{dirs_okay}->{$d};
79                         }
80                 }
81                 $done++;
82         }
83         while (my ($user, $pkgname) = each %$u) {
84                 $state->progress->show($done, $total);
85                 next if $remaining->{users}->{$user};
86                 if ($state->{extra}) {
87                         $state->system(OpenBSD::Paths->userdel, '--',
88                             $user);
89                 } else {
90                         $state->log->set_context($pkgname);
91                         $state->log("You should also run /usr/sbin/userdel #1", $user);
92                 }
93                 $done++;
94         }
95         while (my ($group, $pkgname) = each %$g) {
96                 $state->progress->show($done, $total);
97                 next if $remaining->{groups}->{$group};
98                 if ($state->{extra}) {
99                         $state->system(OpenBSD::Paths->groupdel, '--',
100                             $group);
101                 } else {
102                         $state->log->set_context($pkgname);
103                         $state->log("You should also run /usr/sbin/groupdel #1", $group);
104                 }
105                 $done++;
106         }
107         if ($state->verbose >= 2) {
108                 $state->progress->next;
109         } else {
110                 $state->progress->clear;
111         }
112 }
113
114 package OpenBSD::PackingElement;
115 sub cleanup
116 {
117 }
118
119 sub reload
120 {
121 }
122
123 package OpenBSD::PackingElement::Mandir;
124 sub cleanup
125 {
126         my ($self, $state) = @_;
127         my $fullname = $state->{destdir}.$self->fullname;
128         $state->log("You may wish to remove #1 from man.conf", $fullname);
129         for my $f (OpenBSD::Paths->man_cruft) {
130                 unlink("$fullname/$f");
131         }
132 }
133
134 package OpenBSD::PackingElement::Fontdir;
135 sub cleanup
136 {
137         my ($self, $state) = @_;
138         my $fullname = $state->{destdir}.$self->fullname;
139         $state->log("You may wish to remove #1 from your font path", $fullname);
140         for my $f (OpenBSD::Paths->font_cruft) {
141                 unlink("$fullname/$f");
142         }
143 }
144
145 package OpenBSD::PackingElement::Infodir;
146 sub cleanup
147 {
148         my ($self, $state) = @_;
149         my $fullname = $state->{destdir}.$self->fullname;
150         for my $f (OpenBSD::Paths->info_cruft) {
151                 unlink("$fullname/$f");
152         }
153 }
154
155 1;