Commit d134a09b authored by Roman Sorokin's avatar Roman Sorokin Committed by Commit Bot

AuthPolicyCredentialsManager: Use ImportantFileWriter

Switching from my own invented bike to a library function for
credentials and config files.

BUG=815158
TEST=manual

Change-Id: I32db516219a7db17d8b0dd7a3ed0f15c8430389f
Reviewed-on: https://chromium-review.googlesource.com/974108Reviewed-by: default avatarLutz Justen <ljusten@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Roman Sorokin <rsorokin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#547734}
parent f9d61d01
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include "chrome/browser/chromeos/authpolicy/auth_policy_credentials_manager.h" #include "chrome/browser/chromeos/authpolicy/auth_policy_credentials_manager.h"
#include "ash/public/cpp/vector_icons/vector_icons.h" #include "ash/public/cpp/vector_icons/vector_icons.h"
#include "base/files/file_util.h" #include "base/files/important_file_writer.h"
#include "base/location.h" #include "base/location.h"
#include "base/memory/singleton.h" #include "base/memory/singleton.h"
#include "base/path_service.h" #include "base/path_service.h"
...@@ -81,33 +81,14 @@ void SigninNotificationDelegate::ButtonClick(int button_index) { ...@@ -81,33 +81,14 @@ void SigninNotificationDelegate::ButtonClick(int button_index) {
} }
// Writes |blob| into file <UserPath>/kerberos/|file_name|. First writes into // Writes |blob| into file <UserPath>/kerberos/|file_name|. First writes into
// temporary file and then replace existing one. // temporary file and then replaces existing one.
void WriteFile(const std::string& file_name, const std::string& blob) { void WriteFile(const std::string& file_name, const std::string& blob) {
base::FilePath dir; base::FilePath dir;
PathService::Get(base::DIR_HOME, &dir); PathService::Get(base::DIR_HOME, &dir);
dir = dir.Append(kKrb5Directory); dir = dir.Append(kKrb5Directory);
base::File::Error error;
if (!base::CreateDirectoryAndGetError(dir, &error)) {
LOG(ERROR) << "Failed to create '" << dir.value()
<< "' directory: " << base::File::ErrorToString(error);
return;
}
base::FilePath temp_file;
if (!base::CreateTemporaryFileInDir(dir, &temp_file))
return;
if (base::WriteFile(temp_file, blob.data(), blob.size()) !=
static_cast<int>(blob.size())) {
LOG(ERROR) << "Failed to write file: " << temp_file.value();
return;
}
base::FilePath dest_file = dir.Append(file_name); base::FilePath dest_file = dir.Append(file_name);
if (!base::ReplaceFile(temp_file, dest_file, &error)) { if (!base::ImportantFileWriter::WriteFileAtomically(dest_file, blob)) {
LOG(ERROR) << "Failed to replace '" << dest_file.value() << "' with '" LOG(ERROR) << "Failed to write file " << dest_file.value();
<< temp_file.value()
<< "' :" << base::File::ErrorToString(error);
} }
} }
...@@ -250,12 +231,16 @@ void AuthPolicyCredentialsManager::OnGetUserKerberosFilesCallback( ...@@ -250,12 +231,16 @@ void AuthPolicyCredentialsManager::OnGetUserKerberosFilesCallback(
const authpolicy::KerberosFiles& kerberos_files) { const authpolicy::KerberosFiles& kerberos_files) {
if (kerberos_files.has_krb5cc()) { if (kerberos_files.has_krb5cc()) {
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, FROM_HERE,
{base::MayBlock(), base::TaskPriority::BACKGROUND,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
base::BindOnce(&WriteFile, kKrb5CCFile, kerberos_files.krb5cc())); base::BindOnce(&WriteFile, kKrb5CCFile, kerberos_files.krb5cc()));
} }
if (kerberos_files.has_krb5conf()) { if (kerberos_files.has_krb5conf()) {
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, FROM_HERE,
{base::MayBlock(), base::TaskPriority::BACKGROUND,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
base::BindOnce(&WriteFile, kKrb5ConfFile, kerberos_files.krb5conf())); base::BindOnce(&WriteFile, kKrb5ConfFile, kerberos_files.krb5conf()));
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment