| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2016 by Christian Dietrich * |
| 3 | * [email protected] * |
| 4 | * Copyright (C) 2017-2023 by Hans-Peter Deifel <[email protected]> * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * |
| 19 | * Boston, MA 02110-1301 USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | #include "cache.h" |
| 23 | #include "output.h" |
| 24 | |
| 25 | #include <fstream> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <dirent.h> |
| 29 | #include <unistd.h> |
| 30 | #include <iostream> |
| 31 | #include <cstdlib> |
| 32 | #include <unistd.h> |
| 33 | #include <pwd.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <cerrno> |
| 36 | #include <cstring> |
| 37 | |
| 38 | using namespace std; |
| 39 | |
| 40 | const char *CACHE_VERSION = "1"; |
| 41 | |
| 42 | static std::ostream& operator<<(std::ostream& out, const CachePage& page) { |
| 43 | out << page.label << '\0'; |
| 44 | out << page.text << '\0'; |
| 45 | return out; |
| 46 | } |
| 47 | |
| 48 | static std::istream& operator>>(std::istream& in, CachePage& page) { |
| 49 | std::getline(in, page.label, '\0'); |
| 50 | std::getline(in, page.text, '\0'); |
| 51 | return in; |
| 52 | } |
| 53 | |
| 54 | Cache::Cache(string const &cache_file) |
| 55 | : cache_file(cache_file), valid(false) { |
| 56 | // Open the cache file |
| 57 | ifstream fd(cache_file); |
| 58 | if (!fd) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | unsigned char indicator; |
| 63 | fd >> indicator; |
| 64 | if (indicator != 'C') { |
| 65 | return; |
| 66 | } |
| 67 | std::string version; |
| 68 | std::getline(fd, version, '\0'); |
| 69 | if (version != CACHE_VERSION) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | for (CachePage page; fd >> page; ) { |
| 74 | pages.push_back(page); |
| 75 | } |
| 76 | valid = true; |
| 77 | } |
| 78 | |
| 79 | void Cache::set_page(unsigned pagenum, const CachePage& page) { |
| 80 | pages.resize(max(pagenum, (unsigned)pages.size())); |
| 81 | pages[pagenum-1] = page; |
| 82 | } |
| 83 | |
| 84 | bool Cache::get_page(unsigned pagenum, CachePage& page) { |
| 85 | if (!valid) { |
| 86 | return false; |
| 87 | } |
| 88 | if (pagenum-1 < pages.size()) { |
| 89 | page = pages[pagenum-1]; |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | void Cache::dump() { |
| 96 | ofstream fd(cache_file); |
| 97 | if (!fd) { |
| 98 | return; |
| 99 | } |
| 100 | // The first byte of the cache file is an indicator byte, which can be |
| 101 | // written atomically. Initial it is \0, after all pages have been |
| 102 | // flushed, the indicator byte is written to 'C' (complete cache). |
| 103 | fd << '\0'; |
| 104 | fd << CACHE_VERSION << '\0'; |
| 105 | |
| 106 | for (const CachePage& page : pages) { |
| 107 | fd << page; |
| 108 | } |
| 109 | |
| 110 | fd.flush(); |
| 111 | fd.seekp(0, ios_base::beg); |
| 112 | fd << 'C'; |
| 113 | fd.close(); |
| 114 | } |
| 115 | |
| 116 | // I feel so bad... |
| 117 | static const char *cache_directory; |
| 118 | static int agesort(const struct dirent ** a, const struct dirent **b) { |
| 119 | std::string A = string(cache_directory) + "/" + (*a)->d_name; |
| 120 | std::string B = string(cache_directory) + "/" + (*b)->d_name; |
| 121 | |
| 122 | struct stat bufa, bufb; |
| 123 | if (stat(A.c_str(), &bufa) != 0) { |
| 124 | return 0; |
| 125 | } |
| 126 | if (stat(B.c_str(), &bufb) != 0) { |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | return bufb.st_mtime - bufa.st_mtime; |
| 131 | } |
| 132 | |
| 133 | static int agefilter(const struct dirent * a) { |
| 134 | if (a->d_name[0] == '.') { |
| 135 | return 0; |
| 136 | } |
| 137 | std::string A = string(cache_directory) + "/" + a->d_name; |
| 138 | struct stat bufa; |
| 139 | if (stat(A.c_str(), &bufa) != 0) { |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | // Filter all files that are younger than one day |
| 144 | if ((time(nullptr) - bufa.st_mtime) > 24 * 60 * 60) { |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | void limit_cachesize(const char *cache, int entries) { |
| 152 | struct dirent **namelist; |
| 153 | cache_directory = cache; |
| 154 | int n = scandir(cache, &namelist, agefilter, agesort); |
| 155 | |
| 156 | if (n < 0) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | while (entries--, n-- > 0) { |
| 161 | // Skip the first N cache entries |
| 162 | if (entries >= 0) { |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | string path(cache + string("/") + namelist[n]->d_name); |
| 167 | unlink(path.c_str()); |
| 168 | free(namelist[n]); |
| 169 | } |
| 170 | free(namelist); |
| 171 | } |
| 172 | |
| 173 | int find_cache_directory(std::string &dir) |
| 174 | { |
| 175 | const char *cache_base = getenv("XDG_CACHE_HOME"); |
| 176 | |
| 177 | dir = ""; |
| 178 | if (cache_base != nullptr && cache_base[0] != '\0') { |
| 179 | dir += cache_base; |
| 180 | } else { |
| 181 | char *home = getenv("HOME"); |
| 182 | if (home == nullptr) { |
| 183 | struct passwd *passwd; |
| 184 | passwd = getpwuid(getuid()); |
| 185 | if (passwd != nullptr) { |
| 186 | home = passwd->pw_dir; |
| 187 | } else { |
| 188 | return -1; |
| 189 | } |
| 190 | } |
| 191 | dir += home; |
| 192 | dir += "/.cache"; |
| 193 | } |
| 194 | // according to xdg spec, all directories should be created as 0700. |
| 195 | if (mkdir(dir.c_str(), 0700) != 0 && errno != EEXIST) { |
| 196 | char *msg = strerror(errno); |
| 197 | err() << "mkdir(" << dir << "): " << msg << endl; |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | dir += "/pdfgrep/"; |
| 202 | |
| 203 | if (mkdir(dir.c_str(), 0700) != 0 && errno != EEXIST) { |
| 204 | char *msg = strerror(errno); |
| 205 | err() << "mkdir(" << dir << "): " << msg << endl; |
| 206 | return -1; |
| 207 | } |
| 208 | return 0; |
| 209 | } |