CARDS 2.4.121
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 - 2020 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 group;
137  std::string collection;
138  std::string description;
139  std::string signature;
140  time_t build; // date of build
141  std::string version;
142  int release;
143  std::string url;
144  std::string contributors;
145  std::string packager;
146  std::string maintainer;
147  time_t install; // date of last installation
148  std::string arch;
149  int size;
150  bool dependency; // true it's a dependency: automaticaly install
151  std::set< std::pair<std::string,time_t> > dependencies;
152  std::set<std::string> alias;
153  std::set<std::string> set;
154  std::set<std::string> categories;
155  std::set<std::string> files;
156 };
157 typedef std::map<std::string, pkginfo_t> packages_t;
158 typedef std::map<std::string, std::string> alias_t;
159 
160 enum rule_event_t {
161  LDCONF,
162  UPGRADE,
163  INSTALL,
164  INFO,
165  ICONS,
166  FONTS,
167  SCHEMAS,
168  DESKTOP_DB,
169  MIME_DB,
170  QUERY_PIXBUF,
171  GIO_QUERY,
172  QUERY_IMOD3,
173  QUERY_IMOD2
174 };
175 
176 struct rule_t {
177  rule_event_t event;
178  std::string pattern;
179  bool action;
180 };
181 
182 class Pkgdbh {
183 public:
184 
185  explicit Pkgdbh(const std::string& name);
186  virtual ~Pkgdbh();
187 
188  /* Following methods can be redefined in derivated class */
189  virtual void parseArguments(int argc, char** argv);
190  virtual void run(int argc, char** argv) {};
191  virtual void run() {};
192 
193  virtual void printHelp() const {};
194 
195  virtual void progressInfo();
196  virtual void treatErrors(const std::string& s) const;
197 
198 
199  void print_version() const;
200  int getNumberOfPackages();
201  std::set<std::string> getListOfPackageName();
202 
203  bool checkPackageNameExist(const std::string& name) const;
204  bool checkDependency(const std::string& name);
205  void setDependency();
206  void resetDependency();
207 
208  unsigned int getFilesNumber();
209  unsigned int getInstalledFilesNumber();
210  std::set<std::string> getFilesList();
211 
212 protected:
213  // Database
214 
215  std::set<std::string> getFilesOfPackage(const std::string& packageName);
216  int getListOfPackageNames(const std::string& path);
217  std::pair<std::string, pkginfo_t> getInfosPackage(const std::string& packageName);
218  void buildSimpleDatabase();
220 
221  void buildCompleteDatabase(const bool& silent);
222  void buildDatabase(const bool& progress,
223  const bool& simple,
224  const bool& all,
225  const bool& files,
226  const std::string& packageName);
227 
228 
229  void addPackageFilesRefsToDB(const std::string& name,
230  const pkginfo_t& info);
231 
232  bool checkPackageNameUptodate(const std::pair<std::string,
233  pkginfo_t>& archiveName);
234  bool checkPackageNameBuildDateSame(const std::pair<std::string,
235  time_t>& dependencieNameBuild);
236 
237  /*
238  * Remove the physical files after followings some rules
239  */
240  void removePackageFiles(const std::string& name);
241  void removePackageFiles(const std::string& name,
242  const std::set<std::string>& keep_list);
243 
244  /*
245  * Remove meta data about the removed package
246  */
247  void removePackageFilesRefsFromDB(const std::string& name);
248  void removePackageFilesRefsFromDB(std::set<std::string> files,
249  const std::set<std::string>& keep_list);
250  std::set<std::string> getConflictsFilesList(const std::string& name,
251  const pkginfo_t& info);
252 
253  // Tar.gz
254  std::pair<std::string, pkginfo_t> openArchivePackage(const std::string& filename);
255  std::set< std::pair<std::string, time_t> > getPackageDependencies(const std::string& filename);
256  void extractAndRunPREfromPackage(const std::string& filename);
257  void installArchivePackage(const std::string& filename,
258  const std::set<std::string>& keep_list,
259  const std::set<std::string>& non_install_files);
260 
261  /*
262  * The folder holding the meta datas is going to be create here
263  */
264  void moveMetaFilesPackage(const std::string& name, pkginfo_t& info);
265 
266 
267  void readRulesFile();
268  void getInstallRulesList(const std::vector<rule_t>& rules,
269  rule_event_t event, std::vector<rule_t>& found) const;
270  bool checkRuleAppliesToFile(const rule_t& rule,
271  const std::string& file);
272 
273  void getFootprintPackage(std::string& filename);
274 
275  std::string m_packageArchiveName;
276  std::string m_packageName;
277  std::string m_packageArchiveVersion;
278  std::string m_packageArchiveRelease;
279  std::string m_packageArchiveCollection;
280  std::string m_packageVersion;
281  std::string m_packageRelease;
282  std::string m_packageCollection;
283  std::string m_utilName;
284  std::string m_root;
285  std::string m_build;
286  std::vector<rule_t> m_actionRules;
287  std::set< std::pair<std::string, int> > m_postInstallList;
288  alias_t m_listOfAlias;
289 
290  packages_t m_listOfInstPackages;
291  packages_t m_listOfDepotPackages;
292 
293  std::set<std::pair<std::string,std::set<std::string> > > m_listOfInstalledPackagesWithDeps;
294 
295  action m_actualAction;
296  error m_actualError;
297 
298 private:
299 
300  void runLastPostInstall();
301 
302  std::set<std::string> m_runtimeLibrariesList;
303  std::set<std::string> m_filesList;
304  std::set<std::string> m_packageNamesList;
305  unsigned int m_filesNumber;
306  unsigned int m_installedFilesNumber;
307 
308  bool m_dependency;
309 
310  bool m_DB_Empty;
311  bool m_miniDB_Empty;
312 };
313 
314 class Db_lock {
315 public:
316  Db_lock(const std::string& m_root, bool exclusive);
317  ~Db_lock();
318 
319 private:
320  DIR* m_dir;
321  struct sigaction m_sa;
322 };
323 
324 // Utility functions
325 void assertArgument(char** argv, int argc, int index);
326 void rotatingCursor();
327 #endif /* PKGDBH_H */
328 // vim:set ts=2 :
pkginfo_t
Definition: libcards.h:1476
Pkgdbh::buildSimpleDependenciesDatabase
void buildSimpleDependenciesDatabase()
Definition: pkgdbh.cxx:475
Db_lock
Definition: libcards.h:1655
Pkgdbh::buildSimpleDatabase
void buildSimpleDatabase()
Definition: pkgdbh.cxx:506
Pkgdbh::buildDatabase
void buildDatabase(const bool &progress, const bool &simple, const bool &all, const bool &files, const std::string &packageName)
Definition: pkgdbh.cxx:361
rule_t
Definition: libcards.h:1517
Pkgdbh::buildCompleteDatabase
void buildCompleteDatabase(const bool &silent)
Definition: pkgdbh.cxx:572
Pkgdbh
Definition: libcards.h:1523