shill: profile: Mark invalid profiles as corrupted and ignore

If we fail to open a profile due to a failure in loading
the keyfile store, move the broken file out of the way, since
we will never be able to load this file in later attempts.
In situations where this is the default profile, this will
allow us to start up successfully the next time.

BUG=chromium-os:33822
TEST=Unit tests + manual (copy default.profile from the bug
and ensure shill restarts itself and moves the corrupted
profile out of the way.)

Change-Id: Ia3b7716b36b52c559193f6f6e28c9b185a77c01e
Reviewed-on: https://gerrit.chromium.org/gerrit/31374
Commit-Ready: Paul Stewart <pstew@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/key_file_store.cc b/key_file_store.cc
index 26fe317..5117b66 100644
--- a/key_file_store.cc
+++ b/key_file_store.cc
@@ -18,6 +18,8 @@
 
 namespace shill {
 
+const char KeyFileStore::kCorruptSuffix[] = ".corrupted";
+
 KeyFileStore::KeyFileStore(GLib *glib)
     : glib_(glib),
       crypto_(glib),
@@ -104,6 +106,21 @@
   return success;
 }
 
+bool KeyFileStore::MarkAsCorrupted() {
+  LOG(INFO) << "In " << __func__ << " for " << path_.value();
+  if (path_.empty()) {
+    LOG(ERROR) << "Empty key file path.";
+    return false;
+  }
+  string corrupted_path = path_.value() + kCorruptSuffix;
+  int ret =  rename(path_.value().c_str(), corrupted_path.c_str());
+  if (ret != 0) {
+    PLOG(ERROR) << "File rename failed";
+    return false;
+  }
+  return true;
+}
+
 set<string> KeyFileStore::GetGroups() const {
   CHECK(key_file_);
   gsize length = 0;