Browse Source

Move from readdir_r to readdir (#1955)

pull/1958/head
Keghani Kouzoujian 8 years ago
committed by GitHub
parent
commit
e95e140dd7
3 changed files with 17 additions and 13 deletions
  1. +0
    -2
      FAQ.md
  2. +2
    -1
      pending_code_changes.txt
  3. +15
    -10
      tools/cpp/src/cpp-build/generate_geocoding_data.cc

+ 0
- 2
FAQ.md View File

@ -490,8 +490,6 @@ group](https://groups.google.com/group/libphonenumber-discuss):
* PR [#1090](https://github.com/googlei18n/libphonenumber/pull/1090) /
[#824](https://github.com/googlei18n/libphonenumber/issues/824) to "Replace
POSIX directory operations by Boost Filesystem"
* [#1307](https://github.com/googlei18n/libphonenumber/issues/1307) to use
readdir instead of readdir_r
* [#1555](https://github.com/googlei18n/libphonenumber/issues/1555) to allow
Windows to build cpp library with pthreads for multi-threading


+ 2
- 1
pending_code_changes.txt View File

@ -1 +1,2 @@
Build changes:
- C++ geocoding build tools now use readdir, since readdir_r is deprecated.

+ 15
- 10
tools/cpp/src/cpp-build/generate_geocoding_data.cc View File

@ -17,6 +17,7 @@
#include "cpp-build/generate_geocoding_data.h"
#include <dirent.h>
#include <errno.h>
#include <locale>
#include <sys/stat.h>
#include <algorithm>
@ -102,20 +103,24 @@ bool ListDirectory(const string& path, vector<DirEntry>* entries) {
return false;
}
AutoCloser<DIR> dir_closer(&dir, closedir);
struct dirent entry, *dir_result;
struct dirent *entry;
struct stat entry_stat;
while (true) {
const int res = readdir_r(dir, &entry, &dir_result);
if (res) {
return false;
}
if (dir_result == NULL) {
return true;
// Set errno to 0 to be able to check if an error occurs during the
// readdir() call. NULL is the return value when the end of the directory
// stream is reached or when an error occurs, and the errno check is the
// only thing that helps us distinguish between the two cases. See
// documentation at
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html
errno = 0;
entry = readdir(dir);
if (entry == NULL) {
return errno == 0;
}
if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
const string entry_path = path + "/" + entry.d_name;
const string entry_path = path + "/" + entry->d_name;
if (stat(entry_path.c_str(), &entry_stat)) {
return false;
}
@ -125,7 +130,7 @@ bool ListDirectory(const string& path, vector<DirEntry>* entries) {
} else if (!S_ISREG(entry_stat.st_mode)) {
continue;
}
entries->push_back(DirEntry(entry.d_name, kind));
entries->push_back(DirEntry(entry->d_name, kind));
}
}


Loading…
Cancel
Save