CARDS 2.4.94
Package manager for the NuTyX GNU/Linux distribution
pkgdbh.h
1 //
2 // pkgdbh.h
3 //
4 // Copyright (c) 2000-2005 Per Liden
5 // Copyright (c) 2006-2013 by CRUX team (http://crux.nu)
6 // Copyright (c) 2013-2019 by NuTyX team (http://nutyx.org)
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // USA.
22 //
23 
28 #ifndef PKGDBH_H
29 #define PKGDBH_H
30 
31 #include "archive_utils.h"
32 #include "file_utils.h"
33 #include "process.h"
34 
35 #include <stdexcept>
36 #include <csignal>
37 #include <algorithm>
38 
39 #include <regex.h>
40 #include <ext/stdio_filebuf.h>
41 #include <pwd.h>
42 #include <grp.h>
43 
44 #define PKG_DB_DIR "var/lib/pkg/DB/"
45 #define PKG_FILES "/files"
46 #define PKG_META "META"
47 #define PKG_RECEPT "Pkgfile"
48 #define PKG_README "README"
49 #define PKG_PRE_INSTALL ".PRE"
50 #define PKG_POST_INSTALL ".POST"
51 
52 #define PKG_REJECTED "var/lib/pkg/rejected"
53 #define PKGADD_CONF "var/lib/pkg/pkgadd.conf"
54 #define PKGADD_CONF_MAXLINE 1024
55 
56 #define LDCONFIG "sbin/ldconfig"
57 #define LDCONFIG_CONF "etc/ld.so.conf"
58 #define LDCONFIG_CONF_ARGS "-r "
59 #define SHELL "bin/sh"
60 
61 // /usr/bin/install-info --info-dir="/usr/share/info" /usr/share/info/<file>.info"
62 #define INSTALL_INFO "usr/bin/install-info"
63 #define INSTALL_INFO_ARGS "--info-dir=usr/share/info "
64 
65 // /usr/bin/gtk-update-icon-cache -f -t /usr/share/icons/hicolor
66 #define UPDATE_ICON "usr/bin/gtk-update-icon-cache"
67 #define UPDATE_ICON_ARGS "-f -t "
68 
69 // /usr/bin/glib-compile-schemas /usr/share/glib-2/schemas
70 #define COMPILE_SCHEMAS "usr/bin/glib-compile-schemas"
71 #define COMPILE_SCHEMAS_ARGS ""
72 
73 // /usr/bin/update-desktop-database -q /usr/share/applications
74 #define UPDATE_DESKTOP_DB "usr/bin/update-desktop-database"
75 #define UPDATE_DESKTOP_DB_ARGS "-q "
76 
77 // /usr/bin/update-mime-database usr/share/mime
78 #define UPDATE_MIME_DB "usr/bin/update-mime-database"
79 #define UPDATE_MIME_DB_ARGS "-n "
80 
81 // /usr/bin/gdk-pixbuf-query-loaders --update-cache
82 #define GDK_PIXBUF_QUERY_LOADER "usr/bin/gdk-pixbuf-query-loaders"
83 #define GDK_PIXBUF_QUERY_LOADER_ARGS "--update-cache"
84 
85 // /usr/bin/gio-querymodules /usr/lib/gio/modules
86 #define GIO_QUERYMODULES "usr/bin/gio-querymodules"
87 #define GIO_QUERYMODULES_ARGS "usr/lib/gio/modules"
88 
89 // /usr/bin/gtk-query-immodules-3.0 --update-cache
90 #define QUERY_IMMODULES_3 "usr/bin/gtk-query-immodules-3.0"
91 #define QUERY_IMMODULES_3_ARGS "--update-cache"
92 
93 // /usr/bin/gtk-query-immodules-2.0 --update-cache
94 #define QUERY_IMMODULES_2 "usr/bin/gtk-query-immodules-2.0"
95 #define QUERY_IMMODULES_2_ARGS "--update-cache"
96 
97 // /usr/bin/mkfontdir /usr/share/fonts/<dir>/
98 #define MKFONTDIR "usr/bin/mkfontdir"
99 #define MKFONTDIR_ARGS ""
100 
101 // /usr/bin/mkfontscale /usr/share/fonts/<dir>/
102 #define MKFONTSCALE "usr/bin/mkfontscale"
103 #define MKFONTSCALE_ARGS ""
104 
105 // /usr/bin/fc-cache /usr/share/fonts/<dir>/
106 #define FC_CACHE "usr/bin/fc-cache"
107 #define FC_CACHE_ARGS ""
108 
109 enum action
110 {
111 PKG_DOWNLOAD_START,
112 PKG_DOWNLOAD_RUN,
113 PKG_DOWNLOAD_END,
114 DB_OPEN_START,
115 DB_OPEN_RUN,
116 DB_OPEN_END,
117 PKG_PREINSTALL_START,
118 PKG_PREINSTALL_END,
119 PKG_INSTALL_START,
120 PKG_INSTALL_END,
121 PKG_INSTALL_RUN,
122 PKG_POSTINSTALL_START,
123 PKG_POSTINSTALL_END,
124 PKG_MOVE_META_START,
125 PKG_MOVE_META_END,
126 DB_ADD_PKG_START,
127 DB_ADD_PKG_END,
128 LDCONFIG_START,
129 LDCONFIG_END,
130 RM_PKG_FILES_START,
131 RM_PKG_FILES_RUN,
132 RM_PKG_FILES_END
133 };
134 
135 struct pkginfo_t {
136  std::string base;
137  std::string group;
138  std::string collection;
139  std::string description;
140  std::string signature;
141  time_t build; // date of build
142  std::string version;
143  int release;
144  std::string url;
145  std::string contributors;
146  std::string packager;
147  std::string maintainer;
148  time_t install; // date of last installation
149  std::string arch;
150  int size;
151  bool dependency; // true it's a dependency: automaticaly install
152  std::set< std::pair<std::string,time_t> > dependencies;
153  std::set<std::string> alias;
154  std::set<std::string> files;
155 };
156 typedef std::map<std::string, pkginfo_t> packages_t;
157 typedef std::map<std::string, std::string> alias_t;
158 
159 enum rule_event_t {
160  LDCONF,
161  UPGRADE,
162  INSTALL,
163  INFO,
164  ICONS,
165  FONTS,
166  SCHEMAS,
167  DESKTOP_DB,
168  MIME_DB,
169  QUERY_PIXBUF,
170  GIO_QUERY,
171  QUERY_IMOD3,
172  QUERY_IMOD2
173 };
174 
175 struct rule_t {
176  rule_event_t event;
177  std::string pattern;
178  bool action;
179 };
180 
181 class Pkgdbh {
182 public:
183 
184  explicit Pkgdbh(const std::string& name);
185  virtual ~Pkgdbh();
186 
187  /* Following methods can be redefined in derivated class */
188  virtual void parseArguments(int argc, char** argv);
189  virtual void run(int argc, char** argv) {};
190  virtual void run() {};
191 
192  virtual void printHelp() const {};
193 
194  virtual void progressInfo();
195  virtual void treatErrors(const std::string& s) const;
196 
197 
198  void print_version() const;
199  int getNumberOfPackages();
200  std::set<std::string> getListOfPackageName();
201 
202  bool checkPackageNameExist(const std::string& name) const;
203  bool checkDependency(const std::string& name);
204  void setDependency();
205  void resetDependency();
206 
207  unsigned int getFilesNumber();
208  unsigned int getInstalledFilesNumber();
209  std::set<std::string> getFilesList();
210 
211 protected:
212  // Database
213 
214  std::set<std::string> getFilesOfPackage(const std::string& packageName);
215  int getListOfPackageNames(const std::string& path);
216  std::pair<std::string, pkginfo_t> getInfosPackage(const std::string& packageName);
217  void buildSimpleDatabase();
219 
220  void buildCompleteDatabase(const bool& silent);
221  void buildDatabase(const bool& progress,
222  const bool& simple,
223  const bool& all,
224  const bool& files,
225  const std::string& packageName);
226 
227 
228  void addPackageFilesRefsToDB(const std::string& name,
229  const pkginfo_t& info);
230 
231  bool checkPackageNameUptodate(const std::pair<std::string,
232  pkginfo_t>& archiveName);
233  bool checkPackageNameBuildDateSame(const std::pair<std::string,
234  time_t>& dependencieNameBuild);
235 
236  /*
237  * Remove the physical files after followings some rules
238  */
239  void removePackageFiles(const std::string& name);
240  void removePackageFiles(const std::string& name,
241  const std::set<std::string>& keep_list);
242 
243  /*
244  * Remove meta data about the removed package
245  */
246  void removePackageFilesRefsFromDB(const std::string& name);
247  void removePackageFilesRefsFromDB(std::set<std::string> files,
248  const std::set<std::string>& keep_list);
249  std::set<std::string> getConflictsFilesList(const std::string& name,
250  const pkginfo_t& info);
251 
252  // Tar.gz
253  std::pair<std::string, pkginfo_t> openArchivePackage(const std::string& filename);
254  std::set< std::pair<std::string, time_t> > getPackageDependencies(const std::string& filename);
255  void extractAndRunPREfromPackage(const std::string& filename);
256  void installArchivePackage(const std::string& filename,
257  const std::set<std::string>& keep_list,
258  const std::set<std::string>& non_install_files);
259 
260  /*
261  * The folder holding the meta datas is going to be create here
262  */
263  void moveMetaFilesPackage(const std::string& name, pkginfo_t& info);
264 
265 
266  void readRulesFile();
267  void getInstallRulesList(const std::vector<rule_t>& rules,
268  rule_event_t event, std::vector<rule_t>& found) const;
269  bool checkRuleAppliesToFile(const rule_t& rule,
270  const std::string& file);
271 
272  void getFootprintPackage(std::string& filename);
273 
274  std::string m_packageArchiveName;
275  std::string m_packageName;
276  std::string m_packageArchiveVersion;
277  std::string m_packageArchiveRelease;
278  std::string m_packageArchiveCollection;
279  std::string m_packageVersion;
280  std::string m_packageRelease;
281  std::string m_packageCollection;
282  std::string m_utilName;
283  std::string m_root;
284  std::string m_build;
285  std::vector<rule_t> m_actionRules;
286  std::set< std::pair<std::string, int> > m_postInstallList;
287  alias_t m_listOfAlias;
288 
289  packages_t m_listOfInstPackages;
290  packages_t m_listOfDepotPackages;
291 
292  std::set<std::pair<std::string,std::set<std::string> > > m_listOfInstalledPackagesWithDeps;
293 
294  action m_actualAction;
295  error m_actualError;
296 
297 private:
298 
299  void runLastPostInstall();
300 
301  std::set<std::string> m_runtimeLibrariesList;
302  std::set<std::string> m_filesList;
303  std::set<std::string> m_packageNamesList;
304  unsigned int m_filesNumber;
305  unsigned int m_installedFilesNumber;
306 
307  bool m_dependency;
308 
309  bool m_DB_Empty;
310  bool m_miniDB_Empty;
311 };
312 
313 class Db_lock {
314 public:
315  Db_lock(const std::string& m_root, bool exclusive);
316  ~Db_lock();
317 
318 private:
319  DIR* m_dir;
320  struct sigaction m_sa;
321 };
322 
323 // Utility functions
324 void assertArgument(char** argv, int argc, int index);
325 void rotatingCursor();
326 #endif /* PKGDBH_H */
327 // vim:set ts=2 :
Definition: libcards.h:1456
Definition: libcards.h:1496
void buildSimpleDependenciesDatabase()
Definition: pkgdbh.cxx:464
void buildSimpleDatabase()
Definition: pkgdbh.cxx:494
void buildCompleteDatabase(const bool &silent)
Definition: pkgdbh.cxx:560
void buildDatabase(const bool &progress, const bool &simple, const bool &all, const bool &files, const std::string &packageName)
Definition: pkgdbh.cxx:355
Definition: libcards.h:1634
Definition: libcards.h:1502