1# ex:ts=8 sw=4:
2# $OpenBSD: SharedItems.pm,v 1.20 2009/12/21 10:46:16 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	$state->progress->next;
117}
118
119package OpenBSD::PackingElement;
120sub cleanup
121{
122}
123
124sub reload
125{
126}
127
128package OpenBSD::PackingElement::Mandir;
129sub cleanup
130{
131	my ($self, $state) = @_;
132	my $fullname = $state->{destdir}.$self->fullname;
133	$state->log("You may wish to remove ", $fullname, " from man.conf\n");
134	for my $f (OpenBSD::Paths->man_cruft) {
135		unlink("$fullname/$f");
136	}
137}
138
139package OpenBSD::PackingElement::Fontdir;
140sub cleanup
141{
142	my ($self, $state) = @_;
143	my $fullname = $state->{destdir}.$self->fullname;
144	$state->log("You may wish to remove ", $fullname, " from your font path\n");
145	for my $f (OpenBSD::Paths->font_cruft) {
146		unlink("$fullname/$f");
147	}
148}
149
150package OpenBSD::PackingElement::Infodir;
151sub cleanup
152{
153	my ($self, $state) = @_;
154	my $fullname = $state->{destdir}.$self->fullname;
155	for my $f (OpenBSD::Paths->info_cruft) {
156		unlink("$fullname/$f");
157	}
158}
159
1601;
161