tmf: Bug 478578: Add weak binary validation to text trace types
authorBernd Hufmann <Bernd.Hufmann@ericsson.com>
Fri, 25 Sep 2015 19:55:43 +0000 (15:55 -0400)
committerBernd Hufmann <bernd.hufmann@ericsson.com>
Tue, 29 Sep 2015 16:35:17 +0000 (12:35 -0400)
Change-Id: I0e693ff8f03907b7b3fc4c2f2dafcf547c2db698
Signed-off-by: Bernd Hufmann <Bernd.Hufmann@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/56936
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Hudson CI
btf/org.eclipse.tracecompass.btf.core/src/org/eclipse/tracecompass/btf/core/trace/BtfTrace.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/parsers/custom/CustomTxtTrace.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/parsers/custom/CustomXmlTrace.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceUtils.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/text/TextTrace.java

index bffcbe1059f55c31b2acf33100915e836d5a4ebe..1dc22f6f4e24bec4f8d620ef0364170c9f13f4c4 100644 (file)
@@ -43,6 +43,7 @@ import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceProperties;
 import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
@@ -227,6 +228,14 @@ public class BtfTrace extends TmfTrace implements ITmfPersistentlyIndexable, ITm
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Not a file. It's a directory: " + path); //$NON-NLS-1$
         }
         int confidence = 0;
+        try {
+            if (!TmfTraceUtils.isText(file)) {
+                return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
+            }
+        } catch (IOException e) {
+            Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+        }
         try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
             int lineCount = 0;
             int matches = 0;
index 3c85eeb37a1dbb3a2828fc80382270ea2cc093cb..cf57aa41ff10bc1f18a8f8f04a3fa25ef8fddc9c 100644 (file)
@@ -39,6 +39,7 @@ import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
@@ -428,10 +429,19 @@ public class CustomTxtTrace extends TmfTrace implements ITmfPersistentlyIndexabl
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
         }
         int confidence = 0;
+        try {
+            if (!TmfTraceUtils.isText(file)) {
+                return new TraceValidationStatus(confidence, Activator.PLUGIN_ID);
+            }
+        } catch (IOException e) {
+            Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+        }
         try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
             int lineCount = 0;
             double matches = 0.0;
             String line = rafile.getNextLine();
+
             while ((line != null) && (lineCount++ < MAX_LINES)) {
                 for (InputLine inputLine : fDefinition.inputs) {
                     Matcher matcher = inputLine.getPattern().matcher(line);
index 7da703416d679683728630ae23faff89b906327c..bf3aa668220985c041fd830aa8e8778376f2c8ca 100644 (file)
@@ -38,6 +38,7 @@ import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
@@ -546,6 +547,14 @@ public class CustomXmlTrace extends TmfTrace implements ITmfPersistentlyIndexabl
         if (!file.exists() || !file.isFile() || !file.canRead()) {
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CustomTrace_FileNotFound + ": " + path); //$NON-NLS-1$
         }
+        try {
+            if (!TmfTraceUtils.isText(file)) {
+                return new TraceValidationStatus(0, Activator.PLUGIN_ID);
+            }
+        } catch (IOException e) {
+            Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+        }
         try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
             int lineCount = 0;
             long rawPos = 0;
index 3f810d96034eeb84e010028c2c3bf780fa9a8fc5..fc8baad479886061013b6444027c3bede3800181 100644 (file)
 
 package org.eclipse.tracecompass.tmf.core.trace;
 
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -29,6 +33,8 @@ import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
 @NonNullByDefault
 public final class TmfTraceUtils {
 
+    private static final int MAX_NB_BINARY_BYTES = 2048;
+
     private TmfTraceUtils() {
     }
 
@@ -105,4 +111,32 @@ public final class TmfTraceUtils {
         }
         return null;
     }
+
+    /**
+     * Checks for text file.
+     *
+     * Note that it checks for binary value 0 in the first MAX_NB_BINARY_BYTES
+     * bytes to determine if the file is text.
+     *
+     * @param file
+     *            the file to check. Caller has to make sure that file exists.
+     * @return true if it is binary else false
+     * @throws IOException
+     *             if IOException occurs
+     * @since 2.0
+     */
+    public static boolean isText(File file) throws IOException {
+        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
+            int count = 0;
+            int val = bufferedInputStream.read();
+            while ((count < MAX_NB_BINARY_BYTES) && (val >= 0)) {
+                if (val == 0) {
+                    return false;
+                }
+                count++;
+                val = bufferedInputStream.read();
+            }
+        }
+        return true;
+    }
 }
index 0fe75752d7a7a6dc5f0432ab98245f8a41457287..1e41e06688bdd2046a61725b852d39a2468db6a4 100644 (file)
@@ -33,6 +33,7 @@ import org.eclipse.tracecompass.tmf.core.io.BufferedRandomAccessFile;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
@@ -87,6 +88,14 @@ public abstract class TextTrace<T extends TextTraceEvent> extends TmfTrace imple
         if (!file.isFile()) {
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Not a file. It's a directory: " + path); //$NON-NLS-1$
         }
+        try {
+            if (!TmfTraceUtils.isText(file)) {
+                return new TraceValidationStatus(0, Activator.PLUGIN_ID);
+            }
+        } catch (IOException e) {
+            Activator.logError("Error validating file: " + path, e); //$NON-NLS-1$
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "IOException validating file: " + path, e); //$NON-NLS-1$
+        }
         int confidence = 0;
         try (BufferedRandomAccessFile rafile = new BufferedRandomAccessFile(path, "r")) { //$NON-NLS-1$
             int lineCount = 0;
This page took 0.027527 seconds and 5 git commands to generate.