Tizen 2.1 base
[framework/web/wrt-commons.git] / modules / 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 <dirent.h>
23 #include <errno.h>
24 #include <istream>
25
26 #include <dpl/log/log.h>
27
28 #include "SignatureFinder.h"
29
30 namespace ValidationCore {
31 static const char *SIGNATURE_AUTHOR = "author-signature.xml";
32 static const char *REGEXP_DISTRIBUTOR_SIGNATURE =
33     "^(signature)([1-9][0-9]*)(\\.xml)";
34
35 SignatureFinder::SignatureFinder(const std::string& dir) :
36     m_dir(dir),
37     m_signatureRegexp(REGEXP_DISTRIBUTOR_SIGNATURE)
38 {
39 }
40
41 SignatureFinder::Result SignatureFinder::find(SignatureFileInfoSet &set)
42 {
43     DIR *dp;
44     struct dirent *dirp;
45
46     /*
47      * find a dir
48      */
49     if ((dp = opendir(m_dir.c_str())) == NULL) {
50         LogError("Error opening directory:" << m_dir);
51         return ERROR_OPENING_DIR;
52     }
53
54     for (errno = 0; (dirp = readdir(dp)) != NULL; errno = 0) {
55         /**
56          * check if it's author signature
57          */
58         if (!strcmp(dirp->d_name, SIGNATURE_AUTHOR)) {
59             set.insert(SignatureFileInfo(std::string(dirp->d_name), -1));
60             continue;
61         }
62
63         std::string sig, num, xml;
64         if (m_signatureRegexp.FullMatch(dirp->d_name, &sig, &num, &xml)) {
65             std::istringstream stream(num);
66             int number;
67             stream >> number;
68
69             if (stream.fail()) {
70                 closedir(dp);
71                 return ERROR_ISTREAM;
72             }
73
74             set.insert(SignatureFileInfo(std::string(dirp->d_name), number));
75         }
76     }
77
78     if (errno != 0) {
79         LogError("Error in readdir");
80         closedir(dp);
81         return ERROR_READING_DIR;
82     }
83
84     closedir(dp);
85     return NO_ERROR;
86 }
87 } // namespace ValidationCore