1# ex:ts=8 sw=4:
2# $OpenBSD: SharedItems.pm,v 1.22 2010/05/10 09:17:55 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
18use strict;
19use warnings;
20
21package OpenBSD::SharedItems;
22
23use OpenBSD::Error;
24use OpenBSD::PackageInfo;
25use OpenBSD::PackingList;
26use OpenBSD::Paths;
27
28sub find_items_in_installed_packages
29{
30	my $state = shift;
31	my $db = OpenBSD::SharedItemsRecorder->new;
32	my @list = installed_packages();
33	my $total = @list;
34	$state->status->what("Read")->object("shared items");
35	$state->progress->set_header("Read shared items");
36	my $done = 0;
37	for my $e (@list) {
38		$state->progress->show($done, $total);
39		my $plist = OpenBSD::PackingList->from_installation($e,
40		    \&OpenBSD::PackingList::SharedItemsOnly) or next;
41		next if !defined $plist;
42		$plist->record_shared($db, $e);
43		$done++;
44	}
45	return $db;
46}
47
48sub cleanup
49{
50	my ($recorder, $state) = @_;
51
52	my $remaining = find_items_in_installed_packages($state);
53
54	$state->progress->clear;
55	$state->status->what("Clean");
56	$state->progress->set_header("Clean shared items");
57	my $h = $recorder->{dirs};
58	my $u = $recorder->{users};
59	my $g = $recorder->{groups};
60	my $total = 0;
61	$total += keys %$h if defined $h;
62	$total += keys %$u if defined $u;
63	$total += keys %$g if defined $g;
64	my $done = 0;
65
66	if (defined $h) {
67		for my $d (sort {$b cmp $a} keys %$h) {
68			$state->progress->show($done, $total);
69			my $realname = $state->{destdir}.$d;
70			if ($remaining->{dirs}->{$realname}) {
71				for my $i (@{$h->{$d}}) {
72					$state->log->set_context($i->{pkgname});
73					$i->reload($state);
74				}
75			} else {
76				for my $i (@{$h->{$d}}) {
77					$state->log->set_context($i->{pkgname});
78					$i->cleanup($state);
79				}
80				if (!rmdir $realname) {
81					$state->log("Error deleting directory $realname: $!\n")
82					    unless $state->{dirs_okay}->{$d};
83				}
84			}
85			$done++;
86		}
87	}
88	if (defined $u) {
89		while (my ($user, $pkgname) = each %$u) {
90			$state->progress->show($done, $total);
91			next if $remaining->{users}->{$user};
92			if ($state->{extra}) {
93				$state->system(OpenBSD::Paths->userdel, '--',
94				    $user);
95			} else {
96				$state->log->set_context($pkgname);
97				$state->log("You should also run /usr/sbin/userdel $user\n");
98			}
99			$done++;
100		}
101	}
102	if (defined $g) {
103		while (my ($group, $pkgname) = each %$g) {
104			$state->progress->show($done, $total);
105			next if $remaining->{groups}->{$group};
106			if ($state->{extra}) {
107				$state->system(OpenBSD::Paths->groupdel, '--',
108				    $group);
109			} else {
110				$state->log->set_context($pkgname);
111				$state->log("You should also run /usr/sbin/groupdel $group\n");
112			}
113			$done++;
114		}
115	}
116	if ($state->verbose >= 2) {
117		$state->progress->next;
118	} else {
119		$state->progress->clear;
120	}
121}
122
123package OpenBSD::PackingElement;
124sub cleanup
125{
126}
127
128sub reload
129{
130}
131
132package OpenBSD::PackingElement::Mandir;
133sub cleanup
134{
135	my ($self, $state) = @_;
136	my $fullname = $state->{destdir}.$self->fullname;
137	$state->log("You may wish to remove ", $fullname, " from man.conf\n");
138	for my $f (OpenBSD::Paths->man_cruft) {
139		unlink("$fullname/$f");
140	}
141}
142
143package OpenBSD::PackingElement::Fontdir;
144sub cleanup
145{
146	my ($self, $state) = @_;
147	my $fullname = $state->{destdir}.$self->fullname;
148	$state->log("You may wish to remove ", $fullname, " from your font path\n");
149	for my $f (OpenBSD::Paths->font_cruft) {
150		unlink("$fullname/$f");
151	}
152}
153
154package OpenBSD::PackingElement::Infodir;
155sub cleanup
156{
157	my ($self, $state) = @_;
158	my $fullname = $state->{destdir}.$self->fullname;
159	for my $f (OpenBSD::Paths->info_cruft) {
160		unlink("$fullname/$f");
161	}
162}
163
1641;
165