2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file SignatureFinder.cpp
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Search for author-signature.xml and signatureN.xml files.
22 #include <vcore/SignatureFinder.h>
23 #include <dpl/log/log.h>
34 namespace ValidationCore {
38 const char *SIGNATURE_AUTHOR = "author-signature.xml";
39 const char *REGEXP_DISTRIBUTOR_SIGNATURE = "^(signature)([1-9][0-9]*)(\\.xml)";
41 struct dirent *readdir(DIR *dirp) {
43 auto ret = ::readdir(dirp);
45 LogWarning("Error read dir.");
49 } // anonymous namespace
51 class SignatureFinder::Impl {
53 Impl(const std::string &dir)
55 , m_signatureRegexp(REGEXP_DISTRIBUTOR_SIGNATURE)
60 Result find(SignatureFileInfoSet &set);
63 std::string getFullPath(const std::string &file);
66 pcrecpp::RE m_signatureRegexp;
69 std::string SignatureFinder::Impl::getFullPath(const std::string &file)
71 std::string fullPath = m_dir;
73 if (fullPath.back() != '/')
80 SignatureFinder::Result SignatureFinder::Impl::find(SignatureFileInfoSet &set)
82 std::unique_ptr<DIR, std::function<int(DIR *)>> dp(::opendir(m_dir.c_str()),
84 LogDebug("Opendir : " << m_dir);
86 LogError("Error opening directory : " << m_dir);
87 return ERROR_OPENING_DIR;
90 while (auto dirp = ValidationCore::readdir(dp.get())) {
91 /* number for author signature is -1 */
92 if (!strcmp(dirp->d_name, SIGNATURE_AUTHOR)) {
93 std::string fullPath = getFullPath(std::string(dirp->d_name));
94 LogDebug("Found author signature file full path : " << fullPath);
95 set.insert(SignatureFileInfo(fullPath, -1));
99 std::string sig, num, xml;
100 if (m_signatureRegexp.FullMatch(dirp->d_name, &sig, &num, &xml)) {
101 std::istringstream stream(num);
105 return ERROR_ISTREAM;
107 std::string fullPath = getFullPath(std::string(dirp->d_name));
108 LogDebug("Found signature file full path : " << fullPath);
109 set.insert(SignatureFileInfo(fullPath, number));
114 LogWarning("Signature file should exist more than 2.");
119 SignatureFinder::SignatureFinder(const std::string &dir)
120 : m_impl(new Impl(dir))
123 SignatureFinder::~SignatureFinder()
128 SignatureFinder::Result SignatureFinder::find(SignatureFileInfoSet &set)
130 return m_impl->find(set);
133 } // namespace ValidationCore