This source file includes following definitions.
- GetTopDirToCreate
- Initialize
- Delete
#include "chrome/installer/util/self_cleaning_temp_dir.h"
#include <windows.h>
#include "base/file_util.h"
#include "base/logging.h"
#include "chrome/installer/util/delete_after_reboot_helper.h"
namespace installer {
void SelfCleaningTempDir::GetTopDirToCreate(
const base::FilePath& temp_parent_dir,
base::FilePath* base_dir) {
DCHECK(base_dir);
if (base::PathExists(temp_parent_dir)) {
base_dir->clear();
} else {
base::FilePath parent_dir(temp_parent_dir);
do {
*base_dir = parent_dir;
parent_dir = parent_dir.DirName();
} while (parent_dir != *base_dir && !base::PathExists(parent_dir));
LOG_IF(WARNING, !base::DirectoryExists(parent_dir))
<< "A non-directory is at the base of the path leading to a desired "
"temp directory location: " << parent_dir.value();
}
}
SelfCleaningTempDir::SelfCleaningTempDir() {
}
SelfCleaningTempDir::~SelfCleaningTempDir() {
if (!path().empty() && !Delete())
LOG(WARNING) << "Failed to clean temp dir in dtor " << path().value();
}
bool SelfCleaningTempDir::Initialize(const base::FilePath& parent_dir,
const StringType& temp_name) {
DCHECK(parent_dir.IsAbsolute());
DCHECK(!temp_name.empty());
if (!path().empty()) {
LOG(DFATAL) << "Attempting to re-initialize a SelfSelfCleaningTempDir.";
return false;
}
base::FilePath temp_dir(parent_dir.Append(temp_name));
base::FilePath base_dir;
GetTopDirToCreate(parent_dir, &base_dir);
if (base::CreateDirectory(temp_dir)) {
base_dir_ = base_dir;
temp_dir_ = temp_dir;
return true;
}
return false;
}
bool SelfCleaningTempDir::Delete() {
if (path().empty()) {
LOG(DFATAL) << "Attempting to Delete an uninitialized SelfCleaningTempDir.";
return false;
}
base::FilePath next_dir(path().DirName());
bool schedule_deletes = false;
if (!base::DeleteFile(path(), true)) {
LOG(WARNING) << "Failed to delete temporary directory " << path().value()
<< ". Scheduling for deletion at reboot.";
schedule_deletes = true;
if (!ScheduleDirectoryForDeletion(path()))
return false;
}
if (!base_dir_.empty()) {
do {
if (!schedule_deletes && !RemoveDirectory(next_dir.value().c_str())) {
PLOG_IF(WARNING, GetLastError() != ERROR_DIR_NOT_EMPTY)
<< "Error removing directory " << next_dir.value().c_str();
schedule_deletes = true;
}
if (schedule_deletes) {
ScheduleFileSystemEntityForDeletion(next_dir);
}
if (next_dir == base_dir_)
break;
next_dir = next_dir.DirName();
} while (true);
}
base_dir_.clear();
temp_dir_.clear();
return true;
}
}