From 46c784186c390a0f44491f04280a8d8fa94c4d3e Mon Sep 17 00:00:00 2001 From: Tomasz Marciniak Date: Mon, 3 Jul 2017 10:50:37 +0200 Subject: [PATCH] [Filesystem, Archive] Remove deprecated function [Feature] readdir_r is deprecated. Now readdir is used [Verification] Code compiles. There is no warning while compiling. TCT pass rate for filesystem and archive modules is 100%. Change-Id: Idf32278fb1b53c3d49d6b7d75c23e35b4b2e403f Signed-off-by: Tomasz Marciniak --- src/archive/filesystem_node.cc | 35 ++++++++++++++++------------------- src/filesystem/filesystem_manager.cc | 32 ++++++++++++++++---------------- src/filesystem/filesystem_stat.cc | 11 +++++------ 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/archive/filesystem_node.cc b/src/archive/filesystem_node.cc index 60a14ef..ef6683e 100644 --- a/src/archive/filesystem_node.cc +++ b/src/archive/filesystem_node.cc @@ -215,22 +215,21 @@ PlatformResult Node::getChildNames(Node::NameList* out_name_list) const return LogAndCreateResult(ErrorCode::IO_ERR, "Node has been deleted from platform."); } - int err = 0; - struct dirent entry = {0}; - struct dirent* result = nullptr; + errno = 0; + struct dirent* entry = nullptr; NameList name_list; - while ((0 == (err = readdir_r(dir, &entry, &result))) && result) { - if (!strcmp(entry.d_name, ".") || !strncmp(entry.d_name, "..", 2)) { + while (nullptr != (entry = readdir(dir))) { + if (!strcmp(entry->d_name, ".") || !strncmp(entry->d_name, "..", 2)) { continue; } - name_list.push_back(entry.d_name); + name_list.push_back(entry->d_name); } if (closedir(dir) != 0) { return LogAndCreateResult(ErrorCode::IO_ERR, "Could not close platform node."); } - if (0 != err) { + if (0 != errno) { return LogAndCreateResult(ErrorCode::IO_ERR, "Error while reading directory."); } @@ -258,17 +257,16 @@ PlatformResult Node::getChildNodes(NodeList* out_node_list) const return LogAndCreateResult(ErrorCode::IO_ERR, "Node has been deleted from platform."); } - int err = 0; - struct dirent entry = {0}; - struct dirent* result = nullptr; + errno = 0; + struct dirent* entry = nullptr; NodeList node_list; - while ((0 == (err = readdir_r(dir, &entry, &result))) && result) { - if (!strcmp(entry.d_name, ".") || !strncmp(entry.d_name, "..", 2)) { + while (nullptr != (entry = readdir(dir))) { + if (!strcmp(entry->d_name, ".") || !strncmp(entry->d_name, "..", 2)) { continue; } NodePtr node; - Node::resolve(*m_path + entry.d_name, &node); + Node::resolve(*m_path + entry->d_name, &node); node->setPermissions(getPermissions()); // inherit access rights node_list.push_back(node); } @@ -278,7 +276,7 @@ PlatformResult Node::getChildNodes(NodeList* out_node_list) const return LogAndCreateResult(ErrorCode::IO_ERR, "Could not close platform node."); } - if (0 != err) { + if (0 != errno) { SLoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); return LogAndCreateResult(ErrorCode::IO_ERR, "Error while reading directory."); } @@ -568,14 +566,13 @@ PlatformResult Node::removeAsDirectory(const PathPtr& path, bool recursive) SLoggerE("File %s", path->getFullPath().c_str()); return LogAndCreateResult(ErrorCode::IO_ERR, "Node does not exist or access denied."); } - struct dirent entry = {0}; - struct dirent* result = nullptr; + struct dirent* entry = nullptr; PlatformResult platform_result(ErrorCode::NO_ERROR); - while ((0 == (readdir_r(dir, &entry, &result))) && result) { - if (!strcmp(entry.d_name, ".") || !strncmp(entry.d_name, "..", 2)) { + while (nullptr != (entry = readdir(dir))) { + if (!strcmp(entry->d_name, ".") || !strncmp(entry->d_name, "..", 2)) { continue; } - PathPtr subPath = *path + entry.d_name; + PathPtr subPath = *path + entry->d_name; struct stat info; memset(&info, 0, sizeof(struct stat)); if (lstat(subPath->getFullPath().c_str(), &info) == 0) { diff --git a/src/filesystem/filesystem_manager.cc b/src/filesystem/filesystem_manager.cc index 43f7462..8531f4c 100644 --- a/src/filesystem/filesystem_manager.cc +++ b/src/filesystem/filesystem_manager.cc @@ -106,20 +106,20 @@ FilesystemError copyDirectory(const std::string& originPath, SCOPE_EXIT { (void)closedir(dp); }; - struct dirent entry; - struct dirent* result = nullptr; - while (0 == (status = readdir_r(dp, &entry, &result)) && result != nullptr) { - if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0) + errno = 0; + struct dirent* entry = nullptr; + while (nullptr != (entry = readdir(dp))) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; std::string oldLocation = originPath + std::string("/") - + std::string(result->d_name); + + std::string(entry->d_name); std::string newLocation = destPath + std::string("/") - + std::string(result->d_name); + + std::string(entry->d_name); FilesystemError fstatus = FilesystemError::None; - if (result->d_type == DT_DIR) { + if (entry->d_type == DT_DIR) { fstatus = copyDirectory(oldLocation, newLocation); - } else if (result->d_type == DT_REG) { + } else if (entry->d_type == DT_REG) { fstatus = copyFile(oldLocation, newLocation); } if (fstatus != FilesystemError::None) { @@ -127,7 +127,8 @@ FilesystemError copyDirectory(const std::string& originPath, return fstatus; } } - if (status != 0) { + + if (0 != errno) { LoggerE("error occured"); return FilesystemError::Other; } @@ -339,18 +340,17 @@ void FilesystemManager::ReadDir( std::vector < std::string > fileList; DIR* dp = nullptr; - struct dirent entry; - struct dirent* result = nullptr; - int status = 0; + struct dirent* entry = nullptr; dp = opendir(path.c_str()); if (dp != NULL) { - while ((status = readdir_r(dp, &entry, &result)) == 0 && result != nullptr) { - if (strcmp(result->d_name, ".") != 0 && strcmp(result->d_name, "..") != 0) - fileList.push_back(path + "/" + std::string(result->d_name)); + errno = 0; + while (nullptr != (entry = readdir(dp))) { + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) + fileList.push_back(path + "/" + std::string(entry->d_name)); } (void) closedir(dp); - if (status == 0) { + if (0 == errno) { success_cb(fileList); } else { LoggerE("error occured"); diff --git a/src/filesystem/filesystem_stat.cc b/src/filesystem/filesystem_stat.cc index 014a28d..40667cf 100755 --- a/src/filesystem/filesystem_stat.cc +++ b/src/filesystem/filesystem_stat.cc @@ -117,19 +117,18 @@ FilesystemStat FilesystemStat::getStat(const std::string& path) { (void) closedir(dir); }; - struct dirent entry; - struct dirent *result = nullptr; - int status; + errno = 0; + struct dirent* entry = nullptr; - while ( (0 == (status = readdir_r(dir, &entry, &result) ) && result != nullptr) ) { - std::string name = result->d_name; + while (nullptr != (entry = readdir(dir))) { + std::string name = entry->d_name; if (name == "." || name == "..") { continue; } _result.nlink++; } - if (status != 0) { + if (0 != errno) { LoggerE("Cannot count files in directory: %s", GetErrorString(errno).c_str()); return _result; } -- 2.7.4