Brian Silverman | 70325d6 | 2015-09-20 17:00:43 -0400 | [diff] [blame] | 1 | // Copyright (c) 2006, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | // --- |
| 31 | |
| 32 | #include <config.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <sys/stat.h> // for stat() |
| 35 | #include <time.h> // for time_t |
| 36 | #ifdef HAVE_UNISTD_H |
| 37 | # include <unistd.h> |
| 38 | #endif |
| 39 | #include <algorithm> // for binary_search |
| 40 | #include HASH_SET_H // that's NameListType |
| 41 | #include <string> |
| 42 | #include <vector> // that's MissingListType, SyntaxListType |
| 43 | #include <ctemplate/template_namelist.h> |
| 44 | #include <ctemplate/template_pathops.h> |
| 45 | #include <ctemplate/template.h> // for Strip, GetTemplate(), etc. |
| 46 | #include <assert.h> |
| 47 | #include <iostream> // for cerr |
| 48 | #include "base/fileutil.h" |
| 49 | |
| 50 | using std::max; |
| 51 | using std::pair; |
| 52 | using std::string; |
| 53 | using std::vector; |
| 54 | |
| 55 | #define LOG(level) std::cerr << #level << ": " |
| 56 | |
| 57 | namespace ctemplate { |
| 58 | |
| 59 | TemplateNamelist::NameListType *TemplateNamelist::namelist_ = NULL; |
| 60 | TemplateNamelist::MissingListType *TemplateNamelist::missing_list_ = NULL; |
| 61 | TemplateNamelist::SyntaxListType *TemplateNamelist::bad_syntax_list_ = NULL; |
| 62 | |
| 63 | // Make sure there is a namelist_ and then insert the name onto it |
| 64 | const char* TemplateNamelist::RegisterTemplate(const char* name) { |
| 65 | if (!namelist_) { |
| 66 | namelist_ = new NameListType; |
| 67 | } |
| 68 | pair<NameListType::iterator, bool> insert_result = namelist_->insert(name); |
| 69 | // return a pointer to the entry corresponding to name; |
| 70 | return insert_result.first->c_str(); |
| 71 | } |
| 72 | |
| 73 | // GetList |
| 74 | // Make sure there is a namelist_ and return a reference to it. |
| 75 | const TemplateNamelist::NameListType& TemplateNamelist::GetList() { |
| 76 | if ( !namelist_ ) { |
| 77 | namelist_ = new NameListType; |
| 78 | } |
| 79 | return *namelist_; |
| 80 | } |
| 81 | |
| 82 | // GetMissingList |
| 83 | // On the first invocation, it creates a new missing list and sets |
| 84 | // refresh to true. |
| 85 | // If refresh is true, whether from being passed to the function |
| 86 | // or being set when the list is created the first time, it iterates |
| 87 | // through the complete list of registered template files |
| 88 | // and adds to the list any that are missing |
| 89 | // On subsequent calls, if refresh is false it merely returns the |
| 90 | // list created in the prior call that refreshed the list. |
| 91 | // Returns a sorted list of missing templates. |
| 92 | const TemplateNamelist::MissingListType& TemplateNamelist::GetMissingList( |
| 93 | bool refresh) { |
| 94 | if (!missing_list_) { |
| 95 | missing_list_ = new MissingListType; |
| 96 | refresh = true; // always refresh the first time |
| 97 | } |
| 98 | |
| 99 | if (refresh) { |
| 100 | const NameListType& the_list = TemplateNamelist::GetList(); |
| 101 | missing_list_->clear(); |
| 102 | |
| 103 | for (NameListType::const_iterator iter = the_list.begin(); |
| 104 | iter != the_list.end(); |
| 105 | ++iter) { |
| 106 | const string path = Template::FindTemplateFilename(*iter); |
| 107 | if (path.empty() || !File::Readable(path.c_str())) { |
| 108 | missing_list_->push_back(*iter); |
| 109 | LOG(ERROR) << "Template file missing: " << *iter |
| 110 | << " at path: " << (path.empty() ? "(empty path)" : path) |
| 111 | << "\n"; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | sort(missing_list_->begin(), missing_list_->end()); |
| 117 | return *missing_list_; |
| 118 | } |
| 119 | |
| 120 | // GetBadSyntaxList |
| 121 | // On the first invocation, it creates a new "bad syntax" list and |
| 122 | // sets refresh to true. |
| 123 | // If refresh is true, whether from being passed to the function |
| 124 | // or being set when the list is created the first time, it |
| 125 | // iterates through the complete list of registered template files |
| 126 | // and adds to the list any that cannot be loaded. In the process, it |
| 127 | // calls GetMissingList, refreshing it. It does not include any |
| 128 | // files in the bad syntax list which are in the missing list. |
| 129 | // On subsequent calls, if refresh is false it merely returns the |
| 130 | // list created in the prior call that refreshed the list. |
| 131 | const TemplateNamelist::SyntaxListType& TemplateNamelist::GetBadSyntaxList( |
| 132 | bool refresh, Strip strip) { |
| 133 | if (!bad_syntax_list_) { |
| 134 | bad_syntax_list_ = new SyntaxListType; |
| 135 | refresh = true; // always refresh the first time |
| 136 | } |
| 137 | |
| 138 | if (refresh) { |
| 139 | const NameListType& the_list = TemplateNamelist::GetList(); |
| 140 | |
| 141 | bad_syntax_list_->clear(); |
| 142 | |
| 143 | const MissingListType& missing_list = GetMissingList(true); |
| 144 | for (NameListType::const_iterator iter = the_list.begin(); |
| 145 | iter != the_list.end(); |
| 146 | ++iter) { |
| 147 | Template *tpl = Template::GetTemplate((*iter), strip); |
| 148 | if (!tpl) { |
| 149 | if (!binary_search(missing_list.begin(), missing_list.end(), *iter)) { |
| 150 | // If it's not in the missing list, then we're here because |
| 151 | // it caused an error during parsing |
| 152 | bad_syntax_list_->push_back(*iter); |
| 153 | LOG(ERROR) << "Error loading template: " << (*iter) << "\n"; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | return *bad_syntax_list_; |
| 159 | } |
| 160 | |
| 161 | // Look at all the existing template files, and get their lastmod time via stat() |
| 162 | time_t TemplateNamelist::GetLastmodTime() { |
| 163 | time_t retval = -1; |
| 164 | |
| 165 | const NameListType& the_list = TemplateNamelist::GetList(); |
| 166 | for (NameListType::const_iterator iter = the_list.begin(); |
| 167 | iter != the_list.end(); |
| 168 | ++iter) { |
| 169 | // Only prepend root_dir if *iter isn't an absolute path: |
| 170 | const string path = Template::FindTemplateFilename(*iter); |
| 171 | struct stat statbuf; |
| 172 | if (path.empty() || stat(path.c_str(), &statbuf) != 0) |
| 173 | continue; // ignore files we can't find |
| 174 | retval = max(retval, statbuf.st_mtime); |
| 175 | } |
| 176 | return retval; |
| 177 | } |
| 178 | |
| 179 | // AllDoExist |
| 180 | bool TemplateNamelist::AllDoExist() { |
| 181 | // AllDoExist always refreshes the list, hence the "true" |
| 182 | const MissingListType& missing_list = TemplateNamelist::GetMissingList(true); |
| 183 | return missing_list.empty(); |
| 184 | } |
| 185 | |
| 186 | // IsAllSyntaxOkay |
| 187 | bool TemplateNamelist::IsAllSyntaxOkay(Strip strip) { |
| 188 | // IsAllSyntaxOkay always refreshes the list, hence the "true" |
| 189 | const SyntaxListType& bad_syntax_list = |
| 190 | TemplateNamelist::GetBadSyntaxList(true, strip); |
| 191 | return bad_syntax_list.empty(); |
| 192 | } |
| 193 | |
| 194 | } |