Bug 37795 - [PATCH] Making file sizes optional in DifferentSelector
Summary: [PATCH] Making file sizes optional in DifferentSelector
Alias: None
Product: Ant
Classification: Unclassified
Component: Core tasks (show other bugs)
Version: 1.6.5
Hardware: All All
: P2 enhancement (vote)
Target Milestone: ---
Assignee: Ant Notifications List
Keywords: PatchAvailable
Depends on:
Reported: 2005-12-05 19:06 UTC by Georg Boecherer
Modified: 2008-02-22 12:18 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Georg Boecherer 2005-12-05 19:06:40 UTC

the Selector org.apache.tools.ant.types.selectors.DifferentSelector compares to
directory trees by file times (last change), file sizes (number of bytes), file
contents (byte compare) and of course existenz. Only file times and file
contents were optional so far via the attributes ignorefiletimes="true/false"
and ignorecontents="true/false". I added the attribute
ignorefilesizes="true/false" to make the file size criteria optional. Setting
all attributes equal "true" you can now only compare by existenz, which in my
case was very usefull to synchronize a folder under source control with a
temporary folder containing build outputs.

below the output of 

diff -u DifferentSelector.java.orig DifferentSelector.java > DifferentSelector.diffs

for my patch.

Georg Boecherer

BEGIN DifferentSelector.diffs

--- DifferentSelector.java.orig	2005-12-05 19:48:58.000000000 +0100
+++ DifferentSelector.java	2005-12-05 19:48:39.000000000 +0100
@@ -50,10 +50,17 @@
     private FileUtils fileUtils = FileUtils.newFileUtils();
+	private boolean ignoreFileSizes = false;
     private boolean ignoreFileTimes = true;
     private boolean ignoreContents = false;
+	/**
+     * This flag tells the selector to ignore file sizes in the comparison
+     * @param ignoreFileSizes if true ignore file sizes
+     */
+    public void setIgnoreFileSizes(boolean ignoreFileSizes) {
+        this.ignoreFileSizes = ignoreFileSizes;
+    }
      * This flag tells the selector to ignore file times in the comparison
      * @param ignoreFileTimes if true ignore file times
@@ -82,11 +89,13 @@
             return true;
-        if (srcfile.length() != destfile.length()) {
-            // different size =>different files
-            return true;
+		if (!ignoreFileSizes) {
+	        if (srcfile.length() != destfile.length()) {
+	            // different size => different files
+	            return true;
+			}
         if (!ignoreFileTimes) {
             //same date if dest timestamp is within granularity of the srcfile
             boolean sameDate;
@@ -110,5 +119,4 @@
             return false;
\ No newline at end of file

END DifferentSelector.diffs
Comment 1 Matt Benson 2005-12-05 19:58:54 UTC
uh, in what way does the <present> selector not cover the use case of selecting
files by existence only?
Comment 2 Georg Boecherer 2005-12-05 21:12:32 UTC

you can try it but it won't pass the following test case:

you use 
<different targetdir="..." ignorecontents="true" ignorefiletimes="true" />
and the two files you test for difference are of different size. so <different/>
will report them as different, although they both exist.

but with my patch and
<different targetdir="..." ignorecontents="true" ignorefiletimes="true"
it won't...

Comment 3 Matt Benson 2005-12-05 21:18:12 UTC
Did you think that, by "<present> selector", I meant the <different> selector in
its present form?  I meant the entirely different <present> selector, which
selects files that do or do not exist elsewhere.  You should be able to use:
<present targetdir="..." present="srconly">
to select only files that do not exist in the target directory.
Comment 4 Georg Boecherer 2005-12-05 21:29:04 UTC

thank you, you are absolutely right (I interpreted "<present>" as "present
<different>". I looked in the sources, my Ant installation has the <present>
selector, but why couldn't I find it when navigating the Ant documentation?

So I could have solved my problem with an other selector. I think though that
the <different> selector should provide the filesize as optional.

Comment 5 Steve Loughran 2005-12-06 11:55:01 UTC
I'm really confused. 

the filesize check encodes the rule "if two files are different sizes, then they
are different files". 

You seem to be looking only for files that are present/absent. For that, use the
<present> selector. 
Comment 6 Georg Boecherer 2005-12-06 12:28:20 UTC

you are right, but what about:

the file contents check encodes the rule "if two files are different in at least
one byte, then they are different files", so why is it optional and filesize is not?

perhaps we can reduce my suggestion to this question, for my purpose I'm already
happy with the <present> selector.

Comment 7 Matt Benson 2005-12-06 15:27:22 UTC
Okay, then the answer to your question is because the capability provided by
<different>'s ignorefiletimes and ignorecontents attributes was not available by
any other means.  Obviously ignorefilesizes="true" would imply
ignorecontents="true" as contents would never match while sizes did not.  Even
assuming both of these to be true, your comparison is now based on time alone,
which case is covered by the <depend> selector in much the same way as the case
of all three--ignore time, size, and contents--is covered by the <present> selector.

Comment 8 Steve Loughran 2005-12-06 22:35:40 UTC
The main reason for allowing you to ignore the contents in a difference test is
because the performance of that is awful on files that actually match. 

Both the source and dest files need parsing and byte-for-byte comparision, which
is slow. Over a network, it is extra slow. 

so by saying ignore file sizes you are cheating, you are saying 'ignore the file
times' -which you ought to do over a network, and 'ignore the file contents'.
you are therefore defining different to be 'two files of different sizes are
different; if they are the same size, we assume they are the same'

once you ignore file sizes you define different as "two files are different only
if one exists". Which is what the <present> selector already did at the time of

We dont need an extra feature that another selector does, because it only adds
complexity, bugreps and test cases. 

At the same time, I dont want you to feel disappointed that we have rejected
your work. There was nothing wrong with the code, and you submitted a patch
exactly as we like it. It was just we had a different solution to the problem
somewhere else. Please dont be dissuaded from submitting more bugreps or
patches, and hassling us to get the patches committed :)