1*7c478bd9Sstevel@tonic-gate#!/usr/perl5/bin/perl -w 2*7c478bd9Sstevel@tonic-gate# 3*7c478bd9Sstevel@tonic-gate# CDDL HEADER START 4*7c478bd9Sstevel@tonic-gate# 5*7c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the 6*7c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only 7*7c478bd9Sstevel@tonic-gate# (the "License"). You may not use this file except in compliance 8*7c478bd9Sstevel@tonic-gate# with the License. 9*7c478bd9Sstevel@tonic-gate# 10*7c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11*7c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing. 12*7c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions 13*7c478bd9Sstevel@tonic-gate# and limitations under the License. 14*7c478bd9Sstevel@tonic-gate# 15*7c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each 16*7c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17*7c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the 18*7c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying 19*7c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner] 20*7c478bd9Sstevel@tonic-gate# 21*7c478bd9Sstevel@tonic-gate# CDDL HEADER END 22*7c478bd9Sstevel@tonic-gate# 23*7c478bd9Sstevel@tonic-gate# 24*7c478bd9Sstevel@tonic-gate# Copyright 2004 Sun Microsystems, Inc. All rights reserved. 25*7c478bd9Sstevel@tonic-gate# Use is subject to license terms. 26*7c478bd9Sstevel@tonic-gate# 27*7c478bd9Sstevel@tonic-gate#ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate# 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gaterequire 5.005; 31*7c478bd9Sstevel@tonic-gateuse strict; 32*7c478bd9Sstevel@tonic-gateuse locale; 33*7c478bd9Sstevel@tonic-gateuse Errno; 34*7c478bd9Sstevel@tonic-gateuse Fcntl; 35*7c478bd9Sstevel@tonic-gateuse File::Basename; 36*7c478bd9Sstevel@tonic-gateuse Getopt::Long qw(:config no_ignore_case bundling); 37*7c478bd9Sstevel@tonic-gateuse POSIX qw(locale_h); 38*7c478bd9Sstevel@tonic-gateuse Sun::Solaris::Utils qw(textdomain gettext); 39*7c478bd9Sstevel@tonic-gateuse Sun::Solaris::Project qw(:ALL :PRIVATE); 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate# 42*7c478bd9Sstevel@tonic-gate# Print a usage message and exit. 43*7c478bd9Sstevel@tonic-gate# 44*7c478bd9Sstevel@tonic-gatesub usage 45*7c478bd9Sstevel@tonic-gate{ 46*7c478bd9Sstevel@tonic-gate my (@msg) = @_; 47*7c478bd9Sstevel@tonic-gate my $prog = basename($0); 48*7c478bd9Sstevel@tonic-gate print(STDERR "$prog: @msg\n") if (@msg); 49*7c478bd9Sstevel@tonic-gate printf(STDERR gettext("Usage: %s [-f filename] project\n"), $prog); 50*7c478bd9Sstevel@tonic-gate exit(2); 51*7c478bd9Sstevel@tonic-gate} 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate# 54*7c478bd9Sstevel@tonic-gate# Print a list of error messages and exit. 55*7c478bd9Sstevel@tonic-gate# 56*7c478bd9Sstevel@tonic-gatesub error 57*7c478bd9Sstevel@tonic-gate{ 58*7c478bd9Sstevel@tonic-gate my $exit = $_[0][0]; 59*7c478bd9Sstevel@tonic-gate my $prog = basename($0) . ': '; 60*7c478bd9Sstevel@tonic-gate foreach my $err (@_) { 61*7c478bd9Sstevel@tonic-gate my ($e, $fmt, @args) = @$err; 62*7c478bd9Sstevel@tonic-gate printf(STDERR $prog . $fmt . "\n", @args); 63*7c478bd9Sstevel@tonic-gate } 64*7c478bd9Sstevel@tonic-gate exit($exit); 65*7c478bd9Sstevel@tonic-gate} 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate# 68*7c478bd9Sstevel@tonic-gate# Main routine of script. 69*7c478bd9Sstevel@tonic-gate# 70*7c478bd9Sstevel@tonic-gate# Set the message locale. 71*7c478bd9Sstevel@tonic-gate# 72*7c478bd9Sstevel@tonic-gatesetlocale(LC_ALL, ''); 73*7c478bd9Sstevel@tonic-gatetextdomain(TEXT_DOMAIN); 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate# Process command options and do some initial command-line validity checking. 76*7c478bd9Sstevel@tonic-gatemy $opt_f; 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gateGetOptions("f=s" => \$opt_f) || usage(); 79*7c478bd9Sstevel@tonic-gateusage(gettext('Invalid command-line arguments')) if (@ARGV != 1); 80*7c478bd9Sstevel@tonic-gateusage(gettext('No project name specified')) if (! defined($ARGV[0])); 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gatemy $pname = $ARGV[0]; 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gatemy $projfile; 85*7c478bd9Sstevel@tonic-gatemy $tmpprojf; 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gateif (defined($opt_f)) { 88*7c478bd9Sstevel@tonic-gate $projfile = $opt_f; 89*7c478bd9Sstevel@tonic-gate} else { 90*7c478bd9Sstevel@tonic-gate $projfile = &PROJF_PATH; 91*7c478bd9Sstevel@tonic-gate} 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate# Fabricate an unique temporary filename. 94*7c478bd9Sstevel@tonic-gate$tmpprojf = $projfile . ".tmp.$$"; 95*7c478bd9Sstevel@tonic-gate 96*7c478bd9Sstevel@tonic-gatemy $pfh; 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate# Read the project file. sysopen() is used so we can control the file mode. 99*7c478bd9Sstevel@tonic-gateif (! sysopen($pfh, $projfile, O_RDONLY)) { 100*7c478bd9Sstevel@tonic-gate error([10, gettext('Cannot open %s: %s'), $projfile, $!]); 101*7c478bd9Sstevel@tonic-gate} 102*7c478bd9Sstevel@tonic-gatemy ($mode, $uid, $gid) = (stat($pfh))[2,4,5]; 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gatemy $flags = {}; 105*7c478bd9Sstevel@tonic-gate$flags->{'validate'} = 'false'; 106*7c478bd9Sstevel@tonic-gate$flags->{'res'} = 'true'; 107*7c478bd9Sstevel@tonic-gate$flags->{'dup'} = 'true'; 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gatemy ($ret, $pf) = projf_read($pfh, $flags); 110*7c478bd9Sstevel@tonic-gateif ($ret != 0) { 111*7c478bd9Sstevel@tonic-gate error(@$pf); 112*7c478bd9Sstevel@tonic-gate} 113*7c478bd9Sstevel@tonic-gateclose($pfh); 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate# Search for the project & remove it. 116*7c478bd9Sstevel@tonic-gatemy $del = 0; 117*7c478bd9Sstevel@tonic-gatemy @newpf = grep { $_->{'name'} eq $pname ? $del++ && 0 : 1 } @$pf; 118*7c478bd9Sstevel@tonic-gateerror([6, gettext('Project "%s" does not exist'), $pname]) 119*7c478bd9Sstevel@tonic-gate if ($del == 0); 120*7c478bd9Sstevel@tonic-gateerror([6, gettext('Duplicate project name "%s"'), $pname]) 121*7c478bd9Sstevel@tonic-gate if ($del > 1); # Should be impossible due to projf_validate() check. 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate# Write out the project file. 124*7c478bd9Sstevel@tonic-gateumask(0000); 125*7c478bd9Sstevel@tonic-gatesysopen($pfh, $tmpprojf, O_WRONLY | O_CREAT | O_EXCL, $mode) || 126*7c478bd9Sstevel@tonic-gate error([10, gettext('Cannot create %s: %s'), $tmpprojf, $!]); 127*7c478bd9Sstevel@tonic-gateprojf_write($pfh, \@newpf); 128*7c478bd9Sstevel@tonic-gateclose($pfh); 129*7c478bd9Sstevel@tonic-gateif (!chown($uid, $gid, $tmpprojf)) { 130*7c478bd9Sstevel@tonic-gate unlink($tmpprojf); 131*7c478bd9Sstevel@tonic-gate error([10, gettext('Cannot set ownership of %s: %s'), 132*7c478bd9Sstevel@tonic-gate $tmpprojf, $!]); 133*7c478bd9Sstevel@tonic-gate} 134*7c478bd9Sstevel@tonic-gateif (! rename($tmpprojf, $projfile)) { 135*7c478bd9Sstevel@tonic-gate unlink($tmpprojf); 136*7c478bd9Sstevel@tonic-gate error([10, gettext('cannot rename %s to %s: %s'), 137*7c478bd9Sstevel@tonic-gate $tmpprojf, $projfile, $!]); 138*7c478bd9Sstevel@tonic-gate} 139*7c478bd9Sstevel@tonic-gateexit(0); 140