ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Bushman <...@traxel.com>
Subject Contrib: Attrib Task (chmod for Windows)
Date Thu, 17 Jul 2003 18:34:30 GMT
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.

    <chmod file="${test.jar}" perm="uga+w"/>
    <taskdef
        name="attrib"
        classname="org.apache.tools.ant.taskdefs.Attrib"
        classpathref="ant.path"/>
    <attrib file="${test.jar}" perm="-R"/>


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.
     * <br>+ Sets an attribute.
     * <br>- Clears an attribute.
     * <br>R Read-only file attribute.
     * <br>A Archive file attribute.
     * <br>S System file attribute.
     * <br>H Hidden file attribute.
     * <br>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


Mime
View raw message