summaryrefslogtreecommitdiff
path: root/usr/src/cmd/projadd/projdel.pl
blob: 3819aa4f635475493001fda6bbe6e3c96b1352fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/perl5/bin/perl -w
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"%Z%%M%	%I%	%E% SMI"
#

require 5.005;
use strict;
use locale;
use Errno;
use Fcntl;
use File::Basename;
use Getopt::Long qw(:config no_ignore_case bundling);
use POSIX qw(locale_h);
use Sun::Solaris::Utils qw(textdomain gettext);
use Sun::Solaris::Project qw(:ALL :PRIVATE);

#
# Print a usage message and exit.
#
sub usage
{
	my (@msg) = @_;
	my $prog = basename($0);
	print(STDERR "$prog: @msg\n") if (@msg);
	printf(STDERR gettext("Usage: %s [-f filename] project\n"), $prog);
	exit(2);
}

#
# Print a list of error messages and exit.
#
sub error
{
	my $exit = $_[0][0];
	my $prog = basename($0) . ': ';
	foreach my $err (@_) {
		my ($e, $fmt, @args) = @$err;
		printf(STDERR $prog . $fmt . "\n", @args);
	}
	exit($exit);
}

#
# Main routine of script.
#
# Set the message locale.
#
setlocale(LC_ALL, '');
textdomain(TEXT_DOMAIN);

# Process command options and do some initial command-line validity checking.
my $opt_f;

GetOptions("f=s" => \$opt_f) || usage();
usage(gettext('Invalid command-line arguments')) if (@ARGV != 1);
usage(gettext('No project name specified')) if (! defined($ARGV[0]));

my $pname = $ARGV[0];

my $projfile;
my $tmpprojf;

if (defined($opt_f)) {
	$projfile = $opt_f;
} else {
	$projfile = &PROJF_PATH;
}

# Fabricate an unique temporary filename.
$tmpprojf = $projfile . ".tmp.$$";

my $pfh;

# Read the project file.  sysopen() is used so we can control the file mode.
if (! sysopen($pfh, $projfile, O_RDONLY)) {
	error([10, gettext('Cannot open %s: %s'), $projfile, $!]);
}
my ($mode, $uid, $gid) = (stat($pfh))[2,4,5];

my $flags = {};
$flags->{'validate'} = 'false';
$flags->{'res'} = 'true';
$flags->{'dup'} = 'true';

my ($ret, $pf) = projf_read($pfh, $flags);
if ($ret != 0) {
	error(@$pf);
}
close($pfh);

# Search for the project & remove it.
my $del = 0;
my @newpf = grep { $_->{'name'} eq $pname ? $del++ && 0 : 1 } @$pf;
error([6, gettext('Project "%s" does not exist'), $pname])
    if ($del == 0);
error([6, gettext('Duplicate project name "%s"'), $pname])
    if ($del > 1);   # Should be impossible due to projf_validate() check.

# Write out the project file.
umask(0000);
sysopen($pfh, $tmpprojf, O_WRONLY | O_CREAT | O_EXCL, $mode) ||
    error([10, gettext('Cannot create %s: %s'), $tmpprojf, $!]);
projf_write($pfh, \@newpf);
close($pfh);
if (!chown($uid, $gid, $tmpprojf)) {
	unlink($tmpprojf);
	error([10, gettext('Cannot set ownership of %s: %s'),
	    $tmpprojf, $!]);
}
if (! rename($tmpprojf, $projfile)) {
	unlink($tmpprojf);
	error([10, gettext('cannot rename %s to %s: %s'),
	    $tmpprojf, $projfile, $!]);
}
exit(0);