Refactor log system
[platform/core/security/cert-svc.git] / vcore / src / vcore / SignatureFinder.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        SignatureFinder.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Search for author-signature.xml and signatureN.xml files.
21  */
22 #include <vcore/SignatureFinder.h>
23 #include <dpl/log/log.h>
24
25 #include <dirent.h>
26 #include <errno.h>
27 #include <istream>
28 #include <sstream>
29
30 #include <pcrecpp.h>
31
32
33 namespace ValidationCore {
34 static const char *SIGNATURE_AUTHOR = "author-signature.xml";
35 static const char *REGEXP_DISTRIBUTOR_SIGNATURE =
36     "^(signature)([1-9][0-9]*)(\\.xml)";
37
38 class SignatureFinder::Impl {
39 public:
40     Impl(const std::string& dir)
41       : m_dir(dir)
42       , m_signatureRegexp(REGEXP_DISTRIBUTOR_SIGNATURE)
43     {}
44
45     virtual ~Impl(){}
46
47     Result find(SignatureFileInfoSet &set);
48
49 private:
50     std::string m_dir;
51     pcrecpp::RE m_signatureRegexp;
52 };
53
54 SignatureFinder::Result SignatureFinder::Impl::find(SignatureFileInfoSet &set)
55 {
56     DIR *dp;
57     struct dirent *dirp;
58
59     /*
60      * find a dir
61      */
62     if ((dp = opendir(m_dir.c_str())) == NULL) {
63         LogError("Error opening directory: " << m_dir);
64         return ERROR_OPENING_DIR;
65     }
66
67     for (errno = 0; (dirp = readdir(dp)) != NULL; errno = 0) {
68         /**
69          * check if it's author signature
70          */
71         if (!strcmp(dirp->d_name, SIGNATURE_AUTHOR)) {
72             set.insert(SignatureFileInfo(std::string(dirp->d_name), -1));
73             continue;
74         }
75
76         std::string sig, num, xml;
77         if (m_signatureRegexp.FullMatch(dirp->d_name, &sig, &num, &xml)) {
78             std::istringstream stream(num);
79             int number;
80             stream >> number;
81
82             if (stream.fail()) {
83                 closedir(dp);
84                 return ERROR_ISTREAM;
85             }
86
87             set.insert(SignatureFileInfo(std::string(dirp->d_name), number));
88         }
89     }
90
91     if (errno != 0) {
92         LogError("Error in readdir");
93         closedir(dp);
94         return ERROR_READING_DIR;
95     }
96
97     closedir(dp);
98     return NO_ERROR;
99 }
100
101 SignatureFinder::SignatureFinder(const std::string& dir)
102   : m_impl(new Impl(dir))
103 {}
104
105 SignatureFinder::~SignatureFinder()
106 {
107     delete m_impl;
108 }
109
110 SignatureFinder::Result SignatureFinder::find(SignatureFileInfoSet &set) {
111     return m_impl->find(set);
112 }
113
114 } // namespace ValidationCore