Bug 21320 - Copy task ignores all but first file returned by FileNameMapper
Summary: Copy task ignores all but first file returned by FileNameMapper
Status: RESOLVED FIXED
Alias: None
Product: Ant
Classification: Unclassified
Component: Core (show other bugs)
Version: 1.5.3
Hardware: All All
: P3 normal (vote)
Target Milestone: ---
Assignee: Ant Notifications List
URL: n.a.
Keywords:
Depends on:
Blocks:
 
Reported: 2003-07-03 18:03 UTC by Evan Easton
Modified: 2008-02-22 12:18 UTC (History)
0 users



Attachments
Patch to Copy and Move to accomodate multiple mappings. (20.52 KB, patch)
2003-07-08 15:52 UTC, Evan Easton
Details | Diff
Ant build script to run some unit tests (11.03 KB, text/plain)
2003-07-15 13:44 UTC, Evan Easton
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Evan Easton 2003-07-03 18:03:59 UTC
The documentation for org.apache.tools.ant.util.FileNameMapper states "Used to 
find the name of the target file(s) corresponding to a source file."  Note the 
use of the word "file(s)."

Further the API:
java.lang.String[] mapFileName(java.lang.String sourceFileName) 
shows that you can map a single file name to many.

However, the copy task ignores all but the first mapped file name.  See line 
489 of 1.5.3-1 source for org.apache.tools.ant.taskdefs.Copy:
        for (int i = 0; i < toCopy.length; i++) {
            File src = new File(fromDir, toCopy[i]);
>>>         File dest = new File(toDir, mapper.mapFileName(toCopy[i])[0]);
            map.put(src.getAbsolutePath(), dest.getAbsolutePath());
        }

Copy and potentially other tasks that know about mappers should be fixed to 
fulfill the expectations set by the FileNameMapper javadoc and API declaration.
Comment 1 Antoine Levy-Lambert 2003-07-03 20:05:54 UTC
I could try to fix this problem, but I would be glad to see what other 
committers think about that first.
Comment 2 Evan Easton 2003-07-03 20:14:05 UTC
I'm going to patch the Copy task against 1.5.3-1 for use at my site since we 
need this capability anyhow.  Once I test it, I can submit a patch (against 
head for 1.6).  I don't know if the problem affects other classes, but I might 
beable to fix those too.

Let me know by Sunday if you'd rather I not do it...I've not followed the dev 
list and am unaware of attitudes of the ant team.
Comment 3 Antoine Levy-Lambert 2003-07-03 21:17:27 UTC
It is a good idea that you prepare patches against HEAD.
Since these patches will induce a change of behavior of the tasks, it might be 
good to add a new flag to the copy task called for instance ignoredoublemappings 
and set it to true by default.
This way in the "normal" case only the first mapping will be used, leaving the 
behavior of the task intact. 
If you do <copy ignoredoublemappings="false"/> then <copy/> will use all 
mappings.
Comment 4 Stefan Bodewig 2003-07-04 06:50:41 UTC
Any change will likely need to propagate to Move.java as well (which is a subclass
of Copy).  And to Sync (new in Ant 1.6), which uses a subclass of Copy internally.

We'd also need to make some strong notes in the javadocs as well as in WHATSNEW.
Comment 5 Evan Easton 2003-07-07 23:45:36 UTC
Does a move of a single file to multiple files even make sense?  

Options for applying this fix to :
(a) implement a move of a single file to N mapped files as N-1 copies and 1 
move  ?
(b) warn that only first mapping is used if ignoremultiplemappings="false" and 
the mapper returns >1 mapped path?
(c) error if same conditions as (b)
(d) error if Copy's ignoremultiplemappings is used on Move (i.e. break LSP)
(e) nothing - silently ignore multiple mapped files

What do others think should be done for Move?
Comment 6 Stefan Bodewig 2003-07-08 07:30:04 UTC
better keep dev@ant in CC or nobody is going to notice when you make progress or
ask questions ;-)

To your question - if copy to multiple files makes sense, so does move, IMHO.  I'd
lean to your option (a).

Comment 7 peter reilly 2003-07-08 07:39:14 UTC
One thing to be aware of is that the dependency
code in copy is strongly bound to
a single source file -> target file mapping.
Comment 8 Evan Easton 2003-07-08 15:52:10 UTC
Created attachment 7164 [details]
Patch to Copy and Move to accomodate multiple mappings.
Comment 9 Evan Easton 2003-07-08 15:53:59 UTC
Submitted Patch - Please review and commit if acceptable.

I changed the behavior of Move slightly to check for self-move of empty 
directories.  As of Move's 1.35 version, this check was not done and a self-
move would delete the directory.

I did not touch Sync$MyCopy as it currently asserts the use of an Identity 
mapper.  Certainly rules for doing mapping in a Sync task could be devised, but 
they'd be pretty hairy since syncing is two way and I'm not sure how you'd deal 
with N-to-1 syncs in the reversal of a mapping.  I'll let the core team decide 
to do something about this if they feel it's necessary...I don't consider Sync 
broken in terms of mapping.
Comment 10 Evan Easton 2003-07-15 00:35:01 UTC
I was just curious if I submitted my proposed patch correctly (per directions 
at http://www.apache.org/dev/contributors.html) and wanted to make sure it 
didn't fall through the cracks.  Not urgent...just checking.
Comment 11 peter reilly 2003-07-15 08:15:22 UTC
The patch is fine.

It would be nice to have some unit tests.
Comment 12 Evan Easton 2003-07-15 13:44:15 UTC
Created attachment 7302 [details]
Ant build script to run some unit tests
Comment 13 peter reilly 2003-07-24 13:24:13 UTC
Commited the patch with some slight modifications (change
name of attribute from igore to enable, and some check
style changes). Thanks.