Browse Source

<fixcrlf> will only overwrite files if their content is different from

the original.

Submitted by:	Attila Szegedi <szegedi@scriptum.hu>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268905 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
6231c77d24
2 changed files with 56 additions and 8 deletions
  1. +6
    -1
      build.xml
  2. +50
    -7
      src/main/org/apache/tools/ant/taskdefs/FixCRLF.java

+ 6
- 1
build.xml View File

@@ -630,13 +630,18 @@
<exclude name="org/apache/tools/ant/taskdefs/GUnzipTest.java" />
<exclude name="org/apache/tools/ant/taskdefs/GzipTest.java" />

<!-- only run this test if ANTLR is installed -->
<!-- only run these tests if their required libraries are installed -->
<exclude name="org/apache/tools/ant/taskdefs/optional/ANTLRTest.java"
unless="antlr.present" />
<exclude name="org/apache/tools/ant/util/regexp/JakartaRegexpMatcherTest.java"
unless="jakarta.regexp.present" />
<exclude name="org/apache/tools/ant/util/regexp/JakartaOroMatcherTest.java"
unless="jakarta.oro.present" />
<exclude name="${optional.package}/ide/VAJExportTest.java" unless="vaj.present" />
<!-- run when you have the environment setup to support them -->
<exclude name="org/apache/tools/ant/taskdefs/optional/net/FtpTest.java" />

</fileset>
</batchtest>



+ 50
- 7
src/main/org/apache/tools/ant/taskdefs/FixCRLF.java View File

@@ -238,9 +238,9 @@ public class FixCRLF extends MatchingTask {

// log options used
log("options:" +
" cr=" + (addcr==-1 ? "add" : addcr==0 ? "asis" : "remove") +
" tab=" + (addtab==-1 ? "add" : addtab==0 ? "asis" : "remove") +
" eof=" + (ctrlz==-1 ? "add" : ctrlz==0 ? "asis" : "remove") +
" cr=" + (addcr==1 ? "add" : addcr==0 ? "asis" : "remove") +
" tab=" + (addtab==1 ? "add" : addtab==0 ? "asis" : "remove") +
" eof=" + (ctrlz==1 ? "add" : ctrlz==0 ? "asis" : "remove") +
" tablength=" + tablength,
Project.MSG_VERBOSE);

@@ -363,11 +363,54 @@ public class FixCRLF extends MatchingTask {

// output the data
try {
// Determine whether it should be written,
// that is if it is different than the potentially already existing file
boolean write = false;
byte[] existingdata = indata;
File destFile = srcFile;
if (destDir != null) destFile = new File(destDir, files[i]);
FileOutputStream outStream = new FileOutputStream(destFile);
outStream.write(outdata,0,o);
outStream.close();
if (destDir != null) {
destFile = new File(destDir, files[i]);
if(destFile.isFile()) {
int len = (int)destFile.length();
if(len != o) {
write = true;
} else {
existingdata = new byte[len];
try {
FileInputStream in = new FileInputStream(destFile);
in.read(existingdata);
in.close();
} catch (IOException e) {
throw new BuildException(e);
}
}
} else {
write = true;
}
}

if(!write) {
if(existingdata.length != o) {
write = true;
} else {
for(int j = 0; j < o; ++j) {
if(existingdata[j] != outdata[j]) {
write = true;
break;
}
}
}
}

if(write) {
log(destFile + " is being written", Project.MSG_VERBOSE);
FileOutputStream outStream = new FileOutputStream(destFile);
outStream.write(outdata,0,o);
outStream.close();
} else {
log(destFile + " is not written, as the contents are identical",
Project.MSG_VERBOSE);
}
} catch (IOException e) {
throw new BuildException(e);
}


Loading…
Cancel
Save