Use unique_ptr<DIR> to safely release resources.

Test: builds, boots
Bug: 66995913
Change-Id: Ib580501fc979b63295b180250581dc7527de76b2
This commit is contained in:
Jeff Sharkey 2018-02-24 18:09:21 -07:00
parent 8c26c46059
commit 5540b4406c

View file

@ -110,17 +110,16 @@ int Loop::destroyByDevice(const char *loopDevice) {
int Loop::destroyAll() {
ATRACE_NAME("Loop::destroyAll");
DIR* dir;
struct dirent* de;
std::string root = "/dev/block/";
if (!(dir = opendir(root.c_str()))) {
auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(root.c_str()), closedir);
if (!dirp) {
PLOG(ERROR) << "Failed to opendir";
return -1;
}
// Poke through all devices looking for loops
while ((de = readdir(dir))) {
struct dirent* de;
while ((de = readdir(dirp.get()))) {
auto test = std::string(de->d_name);
if (!android::base::StartsWith(test, "loop")) continue;
@ -151,7 +150,6 @@ int Loop::destroyAll() {
}
}
closedir(dir);
return 0;
}