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