versioner: use unique_ptr to handle ownership of FTS*.

Bug: None
Test: python run_tests.py
Change-Id: I510063e9b57afda4f5492198cd40c15fc6380d2d
diff --git a/tools/versioner/src/Preprocessor.cpp b/tools/versioner/src/Preprocessor.cpp
index c863e6c..86bb225 100644
--- a/tools/versioner/src/Preprocessor.cpp
+++ b/tools/versioner/src/Preprocessor.cpp
@@ -439,8 +439,13 @@
 
   // Copy over the original headers before preprocessing.
   char* fts_paths[2] = { const_cast<char*>(src_dir.c_str()), nullptr };
-  FTS* fts = fts_open(fts_paths, FTS_LOGICAL, nullptr);
-  while (FTSENT* ent = fts_read(fts)) {
+  std::unique_ptr<FTS, decltype(&fts_close)> fts(fts_open(fts_paths, FTS_LOGICAL, nullptr),
+                                                 fts_close);
+  if (!fts) {
+    err(1, "failed to open directory %s", src_dir.c_str());
+  }
+
+  while (FTSENT* ent = fts_read(fts.get())) {
     llvm::StringRef path = ent->fts_path;
     if (!path.startswith(src_dir)) {
       err(1, "path '%s' doesn't start with source dir '%s'", ent->fts_path, src_dir.c_str());
@@ -461,7 +466,6 @@
            dst_path.c_str());
     }
   }
-  fts_close(fts);
 
   for (const auto& file_it : guards) {
     file_lines[file_it.first] = readFileLines(file_it.first);
diff --git a/tools/versioner/src/Utils.cpp b/tools/versioner/src/Utils.cpp
index 3806110..ef7facc 100644
--- a/tools/versioner/src/Utils.cpp
+++ b/tools/versioner/src/Utils.cpp
@@ -41,14 +41,14 @@
   std::vector<std::string> headers;
 
   char* dir_argv[2] = { const_cast<char*>(directory.c_str()), nullptr };
-  FTS* fts = fts_open(dir_argv, FTS_LOGICAL | FTS_NOCHDIR, nullptr);
+  std::unique_ptr<FTS, decltype(&fts_close)> fts(
+      fts_open(dir_argv, FTS_LOGICAL | FTS_NOCHDIR, nullptr), fts_close);
 
   if (!fts) {
     err(1, "failed to open directory '%s'", directory.c_str());
   }
 
-  FTSENT* ent;
-  while ((ent = fts_read(fts))) {
+  while (FTSENT* ent = fts_read(fts.get())) {
     if (ent->fts_info & (FTS_D | FTS_DP)) {
       continue;
     }
@@ -60,7 +60,6 @@
     headers.push_back(ent->fts_path);
   }
 
-  fts_close(fts);
   return headers;
 }
 
diff --git a/tools/versioner/src/VFS.cpp b/tools/versioner/src/VFS.cpp
index cd6d367..1aa7229 100644
--- a/tools/versioner/src/VFS.cpp
+++ b/tools/versioner/src/VFS.cpp
@@ -36,12 +36,14 @@
 
 static void addDirectoryToVFS(InMemoryFileSystem* vfs, const std::string& path) {
   char* paths[] = { const_cast<char*>(path.c_str()), nullptr };
-  FTS* fts = fts_open(paths, FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR, nullptr);
+  std::unique_ptr<FTS, decltype(&fts_close)> fts(
+      fts_open(paths, FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR, nullptr), fts_close);
+
   if (!fts) {
     err(1, "failed to open directory %s", path.c_str());
   }
 
-  while (FTSENT* ent = fts_read(fts)) {
+  while (FTSENT* ent = fts_read(fts.get())) {
     if ((ent->fts_info & FTS_F) == 0) {
       continue;
     }
@@ -61,8 +63,6 @@
       errx(1, "failed to add file '%s'", file_path);
     }
   }
-
-  fts_close(fts);
 }
 
 llvm::IntrusiveRefCntPtr<FileSystem> createCommonVFS(const std::string& header_dir,