Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an ant task that can copy without losing permissions

I'm aware that I can <exec executable="cp" failonerror="true">, however, I'd really rather have a task that I can call from any OS that can use all (or at least most) of the attributes to copy, but which doesn't suck away the permissions on unix.

I am wondering if there is already a solution out there, or will I have to write my own copy2.

As I was kind of afraid, there's nothing "off the shelf". We have this code, but it only handles directory to directory copy or file to file copy, has custom attributes, and doesn't do any of the other neat things copy does.

<!-- ==================================================================== -->
<!-- Copy files from A to B                                               -->
<!-- <copy> would do this job, if it weren't such a useless pile of fail  -->
<!-- and could manage to preserve execute bits on Linux                   -->
<!-- ==================================================================== -->
<macrodef name="internal-copydir">
    <attribute name="fromdir" default="NOT SET" />
    <attribute name="todir" default="NOT SET" />
    <sequential>
        <if>
            <os family="windows" />
            <then>
                <copy todir="@{todir}">
                    <fileset dir="@{fromdir}" />
                </copy>
            </then>
            <else>
                <exec executable="rsync" failonerror="true">
                    <arg value="-a" />
                    <arg value="@{fromdir}/" />
                    <arg value="@{todir}/" />
                </exec>
            </else>
        </if>
    </sequential>
</macrodef>

<!-- ==================================================================== -->
<!-- Copy file from A to B                                                -->
<!-- <copy> would do this job, if it weren't such a useless pile of fail  -->
<!-- and could manage to preserve execute bits on Linux                   -->
<!-- ==================================================================== -->
<macrodef name="internal-copyfile">
    <attribute name="file" default="NOT SET" />
    <attribute name="tofile" default="NOT SET" />
    <sequential>
        <if>
            <os family="windows" />
            <then>
                <copy file="@{file}" tofile="@{tofile}"/>
            </then>
            <else>
                <exec executable="cp" failonerror="true">
                    <arg value="@{file}" />
                    <arg value="@{tofile}" />
                </exec>
            </else>
        </if>
    </sequential>
</macrodef>

I wrote this one too.

<!-- ==================================================================== -->
<!-- Copy file to a directory                                             -->
<!-- <copy> would do this job, if it weren't such a useless pile of fail  -->
<!-- and could manage to preserve execute bits on Linux                   -->
<!-- ==================================================================== -->
<macrodef name="internal-copyfiletodir">
    <attribute name="file" default="NOT SET" />
    <attribute name="todir" default="NOT SET" />
    <sequential>
        <if>
            <os family="windows" />
            <then>
                <copy file="@{file}" todir="@{todir}"/>
            </then>
            <else>
                <exec executable="cp" failonerror="true">
                    <arg value="@{file}" />
                    <arg value="@{todir}/" />
                </exec>
            </else>
        </if>
    </sequential>
</macrodef>
like image 715
jgritty Avatar asked Apr 03 '12 19:04

jgritty


People also ask

How do I change the mode in Linux?

To change file and directory permissions, use the command chmod (change mode). The owner of a file can change the permissions for user ( u ), group ( g ), or others ( o ) by adding ( + ) or subtracting ( - ) the read, write, and execute permissions.

When would you use the option to assign permissions using the chmod command?

You must be superuser or the owner of a file or directory to change its permissions. You can use the chmod command to set permissions in either of two modes: Absolute Mode - Use numbers to represent file permissions (the method most commonly used to set permissions).


1 Answers

I'm not aware of one. As mentioned in the Ant documentation, this is because there was no mechanism to manipulate file permissions in the JRE. Ant Copy task

Java 7 brings much better support for this sort of thing, so perhaps in a future version of Ant this will be possible. This will likely take a very long time, as I think Ant is only moving to Java 5 for version 1.9.

I guess you might be able to simulate the behavior you are looking for by conditionally running OS specific commands based on the OS?

like image 97
groodt Avatar answered Oct 12 '22 01:10

groodt