Return-Path: Delivered-To: apmail-ant-dev-archive@ant.apache.org Received: (qmail 66227 invoked by uid 500); 21 Jul 2003 05:42:03 -0000 Mailing-List: contact dev-help@ant.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Ant Developers List" Reply-To: "Ant Developers List" Delivered-To: mailing list dev@ant.apache.org Received: (qmail 26092 invoked from network); 17 Jul 2003 18:34:25 -0000 Date: Thu, 17 Jul 2003 11:34:30 -0700 (MST) From: Robert Bushman To: dev@ant.apache.org Subject: Contrib: Attrib Task (chmod for Windows) Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N I was looking for a Windows equiv of the chmod task and couldn't find it. Attrib is the equivalent command, and the company I'm with is cross platform. We use a locking repository and check in the jars (resulting in script failure on jar update), so I wrote an Attrib task (a blatant rip-off of chmod). It's below, and is all yours if you want it. Could definitely be improved by using a common superclass with chmod, and would be nice to also have a MakeWritable task (or some such) that used both of these as servants. Does not support the post-file args that attrib offers, but could fairly easily. package org.apache.tools.ant.taskdefs; import java.io.File; import java.io.IOException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.condition.Os; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.PatternSet; /** * Attrib equivalent for Windows-like environments. * * @author Robert Bushman bob@traxel.com * * @ant.task category="filesystem" */ public class Attrib extends ExecuteOn { private FileSet defaultSet = new FileSet(); private boolean defaultSetDefined = false; private boolean havePerm = false; /** * Attrib task for setting file and directory attributes. */ public Attrib() { super.setExecutable( "attrib" ); super.setParallel( true ); super.setSkipEmptyFilesets( true ); } /** * @see org.apache.tools.ant.ProjectComponent#setProject */ public void setProject( Project project ) { super.setProject( project ); defaultSet.setProject( project ); } /** * The file or single directory of which the permissions must be changed. * @param src */ public void setFile( File src ) { FileSet fs = new FileSet(); fs.setDir( new File( src.getParent() ) ); fs.createInclude().setName( src.getName() ); addFileset( fs ); } /** * The directory which holds the files whose permissions must be changed. */ public void setDir( File src ) { defaultSet.setDir( src ); } /** * The new permissions. *
+ Sets an attribute. *
- Clears an attribute. *
R Read-only file attribute. *
A Archive file attribute. *
S System file attribute. *
H Hidden file attribute. *
EG: "-R" (remove read only) * @param perm */ public void setPerm( String perm ) { createArg().setValue( perm ); havePerm = true; } /** * Add a name entry on the include list. */ public PatternSet.NameEntry createInclude() { defaultSetDefined = true; return defaultSet.createInclude(); } /** * Add a name entry on the exclude list. */ public PatternSet.NameEntry createExclude() { defaultSetDefined = true; return defaultSet.createExclude(); } /** * Add a set of patterns. */ public PatternSet createPatternSet() { defaultSetDefined = true; return defaultSet.createPatternSet(); } /** * Sets the set of include pattersn. Patterns may be separated by a comma * or a space. * * @param includes the string containing the include patterns */ public void setIncludes( String includes ) { defaultSetDefined = true; defaultSet.setIncludes( includes ); } /** * Sets the set of exclude patterns. Patterns may be separated by a comma * or a space. * * @param excludes the string containing the exclude patterns */ public void setExcludes( String excludes ) { defaultSetDefined = true; defaultSet.setExcludes( excludes ); } /** * Sets whether default exclusions should be used or not. * * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions * should be used, "false"|"off"|"no" when they * shouldn't be used. */ public void setDefaultexcludes(boolean useDefaultExcludes) { defaultSetDefined = true; defaultSet.setDefaultexcludes(useDefaultExcludes); } protected void checkConfiguration() { if (!havePerm) { throw new BuildException("Required attribute perm not set in chmod", location); } if (defaultSetDefined && defaultSet.getDir(project) != null) { addFileset(defaultSet); } super.checkConfiguration(); } public void execute() throws BuildException { if( defaultSetDefined || defaultSet.getDir( project ) == null ) { try { super.execute(); } finally { if( defaultSetDefined && defaultSet.getDir( project ) != null ) { filesets.removeElement( defaultSet ); } } } else if( isValidOs() ) { // we are attribbing the given directory Execute execute = prepareExec(); Commandline cloned = (Commandline) cmdl.clone(); cloned.createArgument().setValue( defaultSet.getDir( project ) .getPath() ); try { execute.setCommandline( cloned.getCommandline() ); runExecute( execute ); } catch( IOException e ) { throw new BuildException( "Execute failed: " + e, e, location ); } finally { // close the output file if required logFlush(); } } } /** * @ant.attribute ignore="true" */ public void setExecutable( String e ) { throw new BuildException ( taskType + " doesn\'t support the executable attribute", location ); } /** * @ant.attribute ignore="true" */ public void setCommand( Commandline cmdl ) { throw new BuildException ( taskType + " doesn\'t support the command attribute.", location ); } /** * @ant.attribute ignore="true" */ public void setSkipEmptyFilesets(boolean skip) { throw new BuildException(taskType + " doesn\'t support the skipemptyfileset attribute", location); } protected boolean isValidOs() { return Os.isFamily("windows") && super.isValidOs(); } } ---------------------------------------------------------------------- Fill yer hands you sonofabitch. - Rooster Cogburn ---------------------------------------------------------------------- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org For additional commands, e-mail: dev-help@ant.apache.org