Addresses -Wdeprecated-declarations warnings

changing all references of std::auto_ptr to std::unique_ptr and changing the implementation of get_directory_contents() to use readdir, which is now reentrant, instead of readdir_r.

Signed-off-by: Andrew Ayer <agwa@andrewayer.name>

  Note: old implementations or readdir might not be re-entrant, but that's OK
  because git-crypt is not multi-threaded.
This commit is contained in:
Adrian Cohea
2017-03-18 17:23:34 -06:00
committed by Andrew Ayer
parent edfa3dcb5f
commit d3bb5aba46
3 changed files with 16 additions and 30 deletions

View File

@@ -179,19 +179,6 @@ int util_rename (const char* from, const char* to)
return rename(from, to);
}
static size_t sizeof_dirent_for (DIR* p)
{
long name_max = fpathconf(dirfd(p), _PC_NAME_MAX);
if (name_max == -1) {
#ifdef NAME_MAX
name_max = NAME_MAX;
#else
name_max = 255;
#endif
}
return offsetof(struct dirent, d_name) + name_max + 1; // final +1 is for d_name's null terminator
}
std::vector<std::string> get_directory_contents (const char* path)
{
std::vector<std::string> contents;
@@ -201,19 +188,18 @@ std::vector<std::string> get_directory_contents (const char* path)
throw System_error("opendir", path, errno);
}
try {
std::vector<unsigned char> buffer(sizeof_dirent_for(dir));
struct dirent* dirent_buffer = reinterpret_cast<struct dirent*>(&buffer[0]);
struct dirent* ent = NULL;
int err = 0;
while ((err = readdir_r(dir, dirent_buffer, &ent)) == 0 && ent != NULL) {
if (std::strcmp(ent->d_name, ".") == 0 || std::strcmp(ent->d_name, "..") == 0) {
continue;
}
contents.push_back(ent->d_name);
}
if (err != 0) {
throw System_error("readdir_r", path, errno);
errno = 0;
while((ent = readdir(dir)) != NULL && errno == 0) {
if (std::strcmp(ent->d_name, ".") && std::strcmp(ent->d_name, ".."))
contents.push_back(ent->d_name);
}
if(errno)
throw System_error("readdir", path, errno);
} catch (...) {
closedir(dir);
throw;