From: sangwan.kwon Date: Wed, 21 Dec 2016 12:39:27 +0000 (+0900) Subject: Replace deprecated readdir_r with readdir X-Git-Tag: accepted/tizen/common/20161227.192121~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bdab0d2801de50ec365ec2a59296336e8fe9feaf;p=platform%2Fcore%2Fsecurity%2Fcert-svc.git Replace deprecated readdir_r with readdir Change-Id: I271f4a500a25d6b7325a5c7087bf25486ed4a0c9 Signed-off-by: sangwan.kwon --- diff --git a/src/vcore/CertificateCollection.cpp b/src/vcore/CertificateCollection.cpp index dde4750..7fd370b 100644 --- a/src/vcore/CertificateCollection.cpp +++ b/src/vcore/CertificateCollection.cpp @@ -43,9 +43,9 @@ #include "vcore/CertificateCollection.h" -namespace { +namespace ValidationCore { -using namespace ValidationCore; +namespace { inline std::string toBinaryString(int data) { @@ -69,53 +69,48 @@ bool isHashMatchedFile(const std::string &path, const std::string &hash) return isHashMatchedName(name, hash); } +struct dirent *readdir(DIR *dirp) { + errno = 0; + auto ret = ::readdir(dirp); + if (errno != 0) + LogWarning("Error read dir."); + return ret; +} + CertificatePtr searchCert(const std::string &dir, const CertificatePtr &certPtr, bool withHash) { try { std::string hash = certPtr->getNameHash(Certificate::FIELD_ISSUER); - std::unique_ptr> dp(::opendir(dir.c_str()), ::closedir); + std::unique_ptr> dp(::opendir(dir.c_str()), + ::closedir); - if (dp.get() == NULL) { + if (dp == nullptr) { LogError("Failed open dir[" << dir << "]"); return CertificatePtr(); } - size_t len = offsetof(struct dirent, d_name) + pathconf(dir.c_str(), _PC_NAME_MAX) + 1; - std::unique_ptr> - pEntry(static_cast(::malloc(len)), ::free); - struct dirent *dirp = NULL; - int ret = 0; - - while ((ret = readdir_r(dp.get(), pEntry.get(), &dirp)) == 0 && dirp) { + while (auto dirp = ValidationCore::readdir(dp.get())) { if (dirp->d_type == DT_DIR) continue; - std::string candidatePath(dir); - candidatePath += "/"; - candidatePath += dirp->d_name; - + auto fullPath = dir + "/" + dirp->d_name; if (withHash) { if (!isHashMatchedName(dirp->d_name, hash)) continue; } else { - if (!isHashMatchedFile(candidatePath, hash)) + if (!isHashMatchedFile(fullPath, hash)) continue; } - LogDebug("Found hash matched file! : " << candidatePath); - CertificatePtr candidate = Certificate::createFromFile(candidatePath); - - if (candidate->getOneLine().compare(certPtr->getOneLine(Certificate::FIELD_ISSUER)) != 0) + LogDebug("Found hash matched file! : " << fullPath); + auto candidate = Certificate::createFromFile(fullPath); + if (candidate->getOneLine().compare( + certPtr->getOneLine(Certificate::FIELD_ISSUER)) != 0) continue; return candidate; } - if (ret != 0) { - LogError("readdir_r error. ret[" << ret << "]"); - return CertificatePtr(); - } - LogWarning("cert not found by hash[" << hash << "]"); return CertificatePtr(); } catch (const Certificate::Exception::Base &e) { @@ -146,9 +141,7 @@ CertificatePtr getIssuerCertFromStore(const CertificatePtr &certPtr) return searchCert(TZ_SYS_CA_CERTS, certPtr, true); } -} // namespace - -namespace ValidationCore { +} // anonymous namespace CertificateCollection::CertificateCollection() : m_collectionStatus(COLLECTION_UNSORTED) diff --git a/src/vcore/ReferenceValidator.cpp b/src/vcore/ReferenceValidator.cpp index fc2f4f6..d3a218f 100644 --- a/src/vcore/ReferenceValidator.cpp +++ b/src/vcore/ReferenceValidator.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -39,18 +38,23 @@ #define PATH_MAX 4096 #endif +namespace ValidationCore { + namespace { -const char *SPECIAL_SYMBOL_CURRENT_DIR = "."; -const char *SPECIAL_SYMBOL_UPPER_DIR = ".."; -const char *SPECIAL_SYMBOL_AUTHOR_SIGNATURE_FILE = "author-signature.xml"; +const char *AUTHOR_SIGNATURE = "author-signature.xml"; const char *REGEXP_DISTRIBUTOR_SIGNATURE = "^signature[1-9][0-9]*\\.xml"; - const char MARK_ENCODED_CHAR = '%'; -} // namespace anonymous +struct dirent *readdir(DIR *dirp) { + errno = 0; + auto ret = ::readdir(dirp); + if (errno != 0) + LogWarning("Error read dir."); + return ret; +} -namespace ValidationCore { +} // anonymous namespace class ReferenceValidator::Impl { public: @@ -162,111 +166,72 @@ ReferenceValidator::Result ReferenceValidator::Impl::dfsCheckDirectories( const std::string &directory, bool isAuthorSignature) { - int ret; - DIR *dirp; - struct dirent entry; - struct dirent *result; std::string currentDir = m_dirpath; + if (!directory.empty()) + currentDir += ("/" + directory); - if (!directory.empty()) { - currentDir += "/"; - currentDir += directory; - } - - if ((dirp = opendir(currentDir.c_str())) == NULL) { + std::unique_ptr> dp(::opendir(currentDir.c_str()), + ::closedir); + if (dp == nullptr) { LogError("Error opening directory : " << currentDir); return ERROR_OPENING_DIR; } - for (ret = readdir_r(dirp, &entry, &result); - ret == 0 && result != NULL; - ret = readdir_r(dirp, &entry, &result)) { - if (!strcmp(result->d_name, SPECIAL_SYMBOL_CURRENT_DIR)) { + while (auto dirp = ValidationCore::readdir(dp.get())) { + if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, "..")) continue; - } - - if (!strcmp(result->d_name, SPECIAL_SYMBOL_UPPER_DIR)) { - continue; - } - if (result->d_type == DT_UNKNOWN) { + if (dirp->d_type == DT_UNKNOWN) { // try to stat inode when readdir is not returning known type - std::string path = currentDir + "/" + result->d_name; + std::string path = currentDir + "/" + dirp->d_name; struct stat s; - - if (lstat(path.c_str(), &s) != 0) { - closedir(dirp); + if (lstat(path.c_str(), &s) != 0) return ERROR_LSTAT; - } - if (S_ISREG(s.st_mode)) { - result->d_type = DT_REG; - } else if (S_ISDIR(s.st_mode)) { - result->d_type = DT_DIR; - } + if (S_ISREG(s.st_mode)) + dirp->d_type = DT_REG; + else if (S_ISDIR(s.st_mode)) + dirp->d_type = DT_DIR; } - if (currentDir == m_dirpath && result->d_type == DT_REG && - !strcmp(result->d_name, SPECIAL_SYMBOL_AUTHOR_SIGNATURE_FILE) && - isAuthorSignature) { - continue; - } + if (currentDir == m_dirpath && dirp->d_type == DT_REG) + if ((!strcmp(dirp->d_name, AUTHOR_SIGNATURE) && isAuthorSignature) || + isDistributorSignature(dirp->d_name)) + continue; - if (currentDir == m_dirpath && result->d_type == DT_REG && - isDistributorSignature(result->d_name)) { - continue; - } - - if (result->d_type == DT_DIR) { - LogDebug("Open directory : " << (directory + result->d_name)); - std::string tmp_directory = directory + result->d_name + "/"; + if (dirp->d_type == DT_DIR) { + LogDebug("Open directory : " << (directory + dirp->d_name)); + std::string tmp_directory = directory + dirp->d_name + "/"; Result result = dfsCheckDirectories(referenceSet, tmp_directory, isAuthorSignature); - - if (result != NO_ERROR) { - closedir(dirp); + if (result != NO_ERROR) return result; - } - } else if (result->d_type == DT_REG) { - if (referenceSet.end() == - referenceSet.find(directory + result->d_name)) { - LogDebug("Found file : " << (directory + result->d_name)); - LogError("Unknown ERROR_REFERENCE_NOT_FOUND."); - closedir(dirp); + } else if (dirp->d_type == DT_REG) { + if (referenceSet.end() == referenceSet.find(directory + dirp->d_name)) { + LogError("Cannot find : " << (directory + dirp->d_name)); return ERROR_REFERENCE_NOT_FOUND; } - } else if (result->d_type == DT_LNK) { - std::string linkPath(directory + result->d_name); - - if (referenceSet.end() == - referenceSet.find(linkPath)) { - LogDebug("Found file : " << (directory + result->d_name)); - LogError("Unknown ERROR_REFERENCE_NOT_FOUND."); - closedir(dirp); + } else if (dirp->d_type == DT_LNK) { + std::string linkPath(directory + dirp->d_name); + if (referenceSet.end() == referenceSet.find(linkPath)) { + LogError("Cannot find : " << (directory + dirp->d_name)); return ERROR_REFERENCE_NOT_FOUND; } - Result ret = checkOutbound(linkPath, m_dirpath); - - if (ret != NO_ERROR) { - LogError("Link file point wrong path"); - closedir(dirp); - return ret; + Result result = checkOutbound(linkPath, m_dirpath); + if (result != NO_ERROR) { + LogError("Link file point wrong path. : " << linkPath); + return result; } } else { LogError("Unknown file type."); - closedir(dirp); return ERROR_UNSUPPORTED_FILE_TYPE; } } - if (ret != 0) { - closedir(dirp); - return ERROR_READING_DIR; - } - closedir(dirp); + return NO_ERROR; } diff --git a/src/vcore/SignatureFinder.cpp b/src/vcore/SignatureFinder.cpp index 48d3529..689cb74 100644 --- a/src/vcore/SignatureFinder.cpp +++ b/src/vcore/SignatureFinder.cpp @@ -26,17 +26,27 @@ #include #include #include +#include #include + +namespace ValidationCore { + namespace { +const char *SIGNATURE_AUTHOR = "author-signature.xml"; +const char *REGEXP_DISTRIBUTOR_SIGNATURE = "^(signature)([1-9][0-9]*)(\\.xml)"; + +struct dirent *readdir(DIR *dirp) { + errno = 0; + auto ret = ::readdir(dirp); + if (errno != 0) + LogWarning("Error read dir."); + return ret; } -namespace ValidationCore { -static const char *SIGNATURE_AUTHOR = "author-signature.xml"; -static const char *REGEXP_DISTRIBUTOR_SIGNATURE = - "^(signature)([1-9][0-9]*)(\\.xml)"; +} // anonymous namespace class SignatureFinder::Impl { public: @@ -69,54 +79,40 @@ std::string SignatureFinder::Impl::getFullPath(const std::string &file) SignatureFinder::Result SignatureFinder::Impl::find(SignatureFileInfoSet &set) { - int ret; - DIR *dirp; - struct dirent entry; - struct dirent *result; - - if ((dirp = opendir(m_dir.c_str())) == NULL) { - LogError("Error opening directory: " << m_dir); + std::unique_ptr> dp(::opendir(m_dir.c_str()), + ::closedir); + LogDebug("Opendir : " << m_dir); + if (dp == nullptr) { + LogError("Error opening directory : " << m_dir); return ERROR_OPENING_DIR; } - for (ret = readdir_r(dirp, &entry, &result); - ret == 0 && result != NULL; - ret = readdir_r(dirp, &entry, &result)) { + while (auto dirp = ValidationCore::readdir(dp.get())) { /* number for author signature is -1 */ - if (!strcmp(result->d_name, SIGNATURE_AUTHOR)) { - std::string fullPath = getFullPath(std::string(result->d_name)); + if (!strcmp(dirp->d_name, SIGNATURE_AUTHOR)) { + std::string fullPath = getFullPath(std::string(dirp->d_name)); LogDebug("Found author signature file full path : " << fullPath); set.insert(SignatureFileInfo(fullPath, -1)); continue; } - std::string sig; - std::string num; - std::string xml; /* just for cutting out .xml */ - - if (m_signatureRegexp.FullMatch(result->d_name, &sig, &num, &xml)) { + std::string sig, num, xml; + if (m_signatureRegexp.FullMatch(dirp->d_name, &sig, &num, &xml)) { std::istringstream stream(num); int number; stream >> number; - - if (stream.fail()) { - closedir(dirp); + if (stream.fail()) return ERROR_ISTREAM; - } - std::string fullPath = getFullPath(std::string(result->d_name)); + std::string fullPath = getFullPath(std::string(dirp->d_name)); LogDebug("Found signature file full path : " << fullPath); set.insert(SignatureFileInfo(fullPath, number)); } } - if (ret != 0) { - LogError("Error in readdir"); - closedir(dirp); - return ERROR_READING_DIR; - } + if (set.size() < 2) + LogWarning("Signature file should exist more than 2."); - closedir(dirp); return NO_ERROR; }