Browse Source

[3555] CSVFile::validateHeader() is now virtual

Tomek Mrugalski 10 years ago
parent
commit
db49f4a652

+ 15 - 0
src/lib/dhcpsrv/csv_lease_file6.cc

@@ -205,7 +205,22 @@ CSVLeaseFile6::readHWAddr(const CSVRow& row) {
 
         return (HWAddrPtr());
     }
+}
 
+bool
+CSVLeaseFile6::validateHeader(const isc::util::CSVRow& header) {
+
+    if (!CSVFile::validateHeader(header)) {
+
+        // One possible validation failure is that we're reading Kea 0.9
+        // lease file that didn't have hwaddr column. Let's add it and
+        // try to revalidate.
+        isc::util::CSVRow copy = header;
+        copy.append("hwaddr");
+        return CSVFile::validateHeader(copy);
+    } else {
+        return (true);
+    }
 
 }
 

+ 12 - 0
src/lib/dhcpsrv/csv_lease_file6.h

@@ -77,6 +77,18 @@ public:
     /// ticket http://kea.isc.org/ticket/2405 is implemented.
     bool next(Lease6Ptr& lease);
 
+protected:
+    /// @brief This function validates the header of the Lease6 CSV file.
+    ///
+    /// It works similar to @c CSVFile::validateHeader, but if the validation
+    /// fails, it attempts to add hwaddr column and retry validation.
+    /// That's useful when attmepting to read CSV file generated in 0.9
+    /// (did not have hwaddr field) in 0.9.1 or later code.
+    ///
+    /// @param header A row holding a header.
+    /// @return true if header matches the columns; false otherwise.
+    virtual bool validateHeader(const isc::util::CSVRow& header);
+
 private:
 
     /// @brief Initializes columns of the CSV file holding leases.

+ 2 - 9
src/lib/util/csv_file.cc

@@ -289,15 +289,8 @@ CSVFile::open() {
             // Check the header against the columns specified for the CSV file.
             if (!validateHeader(header)) {
 
-                // One possible validation failure is that we're reading Kea 0.9
-                // lease file that didn't have hwaddr column. Let's add it and
-                // try to revalidate.
-                header.append("hwaddr");
-
-                if (!validateHeader(header)) {
-                    isc_throw(CSVFileError, "invalid header '" << header
-                              << "' in CSV file '" << filename_ << "'");
-                }
+                isc_throw(CSVFileError, "invalid header '" << header
+                          << "' in CSV file '" << filename_ << "'");
             }
 
             // Everything is good, so if we haven't added any columns yet,

+ 7 - 3
src/lib/util/csv_file.h

@@ -444,7 +444,7 @@ protected:
     /// @return true if the column is valid; false otherwise.
     virtual bool validate(const CSVRow& row);
 
-private:
+protected:
 
     /// @brief This function validates the header of the CSV file.
     ///
@@ -452,12 +452,16 @@ private:
     /// compare that they exactly match (including order) the header read
     /// from the file.
     ///
-    /// This function is called internally by @CSVFile::open.
+    /// This function is called internally by @CSVFile::open. Derived classes
+    /// may add extra validation steps.
+    ///
+    /// @todo There should be a support for optional columns (see ticket #3626).
     ///
     /// @param header A row holding a header.
     /// @return true if header matches the columns; false otherwise.
-    bool validateHeader(const CSVRow& header);
+    virtual bool validateHeader(const CSVRow& header);
 
+private:
     /// @brief Sanity check if stream is open.
     ///
     /// Checks if the file stream is open so as IO operations can be performed