LibPkg
unpack.h
Go to the documentation of this file.
1 // This file is part of LibPkg.
2 //
3 // Copyright 2003-2020 Graham Shaw
4 // Copyright 2013-2020 Alan Buckley
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 
18 #ifndef LIBPKG_UNPACK
19 #define LIBPKG_UNPACK
20 
21 #include <string>
22 #include <set>
23 #include <tr1/functional>
24 #include "string.h"
25 
26 #include "libpkg/auto_dir.h"
27 #include "libpkg/thread.h"
28 #include "libpkg/log.h"
29 
30 namespace pkg {
31 
32 using std::string;
33 
34 class pkgbase;
35 class zipfile;
36 class log;
37 class triggers;
38 class trigger_run;
39 class trigger;
40 
43  bool operator() (const std::string& a, const std::string& b) const {
44  return stricmp(a.c_str(), b.c_str()) < 0;
45  }
46 };
47 
49 class unpack:
50  public thread
51 {
52 public:
54  typedef unsigned long long size_type;
55 
57  static const size_type npos=static_cast<size_type>(-1);
58 
61  {
127  state_fail
128  };
129 private:
131  pkgbase& _pb;
132 
134  state_type _state;
135 
138  auto_dir _ad;
139 
141  zipfile* _zf;
142 
144  string _pkgname;
145 
147  size_type _files_done;
148 
150  size_type _files_total;
151 
153  size_type _bytes_done;
154 
156  size_type _bytes_total;
157 
159  size_type _files_total_unpack;
160 
162  size_type _files_total_remove;
163 
165  size_type _bytes_total_unpack;
166 
169  string _message;
170 
173  std::set<string> _packages_to_unpack;
174 
177  std::set<string> _packages_pre_unpacked;
178 
181  std::set<string> _packages_being_unpacked;
182 
185  std::set<string> _packages_unpacked;
186 
189  std::set<string> _packages_to_remove;
190 
193  std::set<string> _packages_being_removed;
194 
197  std::set<string> _packages_removed;
198 
201  std::set<string> _files_to_unpack;
202 
205  std::set<string> _files_being_unpacked;
206 
209  std::set<string> _files_unpacked;
210 
213  std::set<string> _empty_dirs_to_create;
214 
217  std::set<string> _parent_dirs;
218 
221  std::set<string> _files_to_replace_by_dirs;
222 
224  std::set<string> _files_replaced_by_dirs;
225 
228  std::set<string, case_insensitive_cmp> _files_to_remove;
229 
232  std::set<string, case_insensitive_cmp> _dirs_to_remove;
233 
236  std::set<string> _files_being_removed;
237 
240  std::set<string> _files_removed;
241 
244  std::set<string> _empty_dirs_to_check;
246  std::set<string> _dirs_removed;
248  std::set<string> _dirs_created;
249 
251  std::set<string> _packages_cannot_process;
252 
255  std::set<string, case_insensitive_cmp> _files_that_conflict;
256 
259  std::set<string> _existing_module_packages;
260 
262  triggers *_triggers;
263 
265  trigger_run *_trigger_run;
266 
268  trigger *_trigger;
269 
271  pkg::log *_log;
272 
274  std::tr1::function<void(LogCode code, const std::string &item, const std::string &what)> _warning;
275 
277  bool _state_text_changed;
279  std::string _state_text;
280 
282  std::string _exception_item;
283 
284 public:
289  unpack(pkgbase& pb,const std::set<string>& packages);
290 
292  virtual ~unpack();
293 
298  { return _state; }
299 
303  size_type files_done() const
304  { return _files_done; }
305 
309  size_type files_total() const
310  { return _files_total; }
311 
315  size_type bytes_done() const
316  { return _bytes_done; }
317 
321  size_type bytes_total() const
322  { return _bytes_total; }
323 
329  string message() const
330  { return _message; }
331 
336  {
337  if (_state_text_changed) {_state_text_changed=false; return true;}
338  else return false;
339  }
340 
342  const std::string &state_text() const
343  { return _state_text; }
344 
351  const std::set<string>& packages_cannot_process() const
352  { return _packages_cannot_process; }
353 
360  const std::set<string, case_insensitive_cmp>& files_that_conflict() const
361  { return _files_that_conflict; }
362 
364  void use_trigger_run(trigger_run *tr);
365 
367  void log_to(pkg::log *use_log);
368 
370  void warning_func(std::tr1::function<void(LogCode code, const std::string &item, const std::string &what)> &f) {_warning = f;}
371 
378  triggers *detach_triggers();
379 
380 protected:
381  void poll();
382 private:
388  void _poll();
389 
391  void pre_unpack_check_files();
393  void pre_unpack_check_empty_dirs();
395  void pre_unpack_check_parent_dirs();
397  void pre_unpack_select_package();
399  void pre_remove_select_package();
401  void unpack_select_package();
402 
404  void state(state_type new_state);
405 
407  void state_text(const std::string &text);
408 
415  void read_manifest(std::set<string>& mf,const string& pkgname);
416 
427  void build_manifest(std::set<string>& mf,zipfile& zf,size_type* usize=0);
428 
435  void prepare_manifest(std::set<string>& mf,const string& pkgname);
436 
442  void activate_manifest(const string& pkgname);
443 
449  void remove_manifest(const string& pkgname);
450 
454  void add_pre_install_trigger(const string &pkgname, bool has_unwind);
455 
459  void add_post_install_trigger(const string &pkgname);
460 
464  void add_pre_remove_trigger(const string &pkgname);
469  void set_post_install_unwind(const string &pkgname);
470 
476  void add_post_remove_trigger(const string &pkgname, std::set<std::string> &mf);
477 
485  void unpack_file(const string& src_pathname,const string& dst_pathname);
486 
494  void replace_file(const string& dst_pathname,bool overwrite);
495 
501  void remove_file(const string& dst_pathname);
502 
507  void remove_backup(const string& dst_pathname);
512  void unwind_remove_file(const string& dst_pathname);
513 
522  void unwind_replace_file(const string& dst_pathname,bool overwrite);
523 
528  void unwind_unpack_file(const string& dst_pathname);
529 
541  bool already_installed(const control& ctrl, const std::set<string> &mf);
542 
545  void update_existing_modules();
546 
548  void unwind_existing_modules();
549 
555  void get_trigger_versions(const std::string &pkgname, std::string &old_version, std::string &new_version);
556 
557  class cannot_process;
558  class file_conflict;
559  class file_info_not_found;
560  class riscos_info_not_found;
561 };
562 
563 }; /* namespace pkg */
564 
565 #endif
The state in which old versions of files are backed up and replaced with new versions.
Definition: unpack.h:85
size_type files_total() const
Get total number of files to process.
Definition: unpack.h:309
The state in which post remove triggers are run to unwind the actions of the pre install triggers...
Definition: unpack.h:111
A class to manage the package triggers executed during the commiting of the packages.
Definition: triggers.h:38
The state in which state_pre_remove is being backed out.
Definition: unpack.h:122
An exception class for reporting that a file information record could not be found.
Definition: unpack.cc:168
const std::set< string > & packages_cannot_process() const
Get the set of packages that cannot be processed.
Definition: unpack.h:351
The state in which state_pre_unpack is being backed out.
Definition: unpack.h:124
The state in which state_unpack is being backed out.
Definition: unpack.h:113
The state in which post install triggers are run to unwind the actions of the pre remove and/or pre i...
Definition: unpack.h:118
The namespace used to hold the package management library.
Definition: auto_dir.cc:23
const std::set< string, case_insensitive_cmp > & files_that_conflict() const
Get the set of destination pathnames that conflict with files already on the system.
Definition: unpack.h:360
bool operator()(const std::string &a, const std::string &b) const
Definition: unpack.h:43
An exception class for reporting that one or more files conflict with those already on the system...
Definition: unpack.cc:158
The state in which backups are deleted, and the states of packages to be removed are changed to statu...
Definition: unpack.h:92
size_type bytes_total() const
Get total number of bytes to process.
Definition: unpack.h:321
A class for unpacking and removing sets of packages.
Definition: unpack.h:49
size_type files_done() const
Get number of files processed.
Definition: unpack.h:303
The state in which files are unpacked from their zip archives and moved to temporary locations...
Definition: unpack.h:80
state_type
An enumeration for describing the state of the unpack operation.
Definition: unpack.h:60
A class for automatically creating and deleting directories.
Definition: auto_dir.h:31
The state in which the states of packages to be unpacked are changed to status::state_unpacked.
Definition: unpack.h:99
class to represent a trigger action from a script
Definition: trigger.h:33
bool clear_state_text_changed()
Check is state text has changed and clear the changed flag.
Definition: unpack.h:335
string message() const
Get error message.
Definition: unpack.h:329
The state in which empty diwas created are removed.
Definition: unpack.h:104
The state in which pre install triggers are run.
Definition: unpack.h:82
A class to represent the content of a RiscPkg control file.
Definition: control.h:35
The state in which files are moved that will overwritten by auto create directories.
Definition: unpack.h:77
A mixin class to represent a cooperative thread.
Definition: thread.h:23
Class to log actions that occur in LibPkg.
Definition: log.h:226
Interface to execute a trigger.
Definition: trigger.h:77
size_type bytes_done() const
Get number of bytes processed.
Definition: unpack.h:315
A class for representing the collection of package database tables.
Definition: pkgbase.h:35
An exception class for reporting that one or more packages cannot be processed.
Definition: unpack.cc:148
The state in which state_replace is being backed out.
Definition: unpack.h:108
The state in which any empty directories that need creating are created.
Definition: unpack.h:96
unsigned long long size_type
A type for representing byte counts.
Definition: unpack.h:54
The state in which the pre remove triggers are run.
Definition: unpack.h:74
Comparison that does not take into account the case of the string.
Definition: unpack.h:42
const char * dst_pathname
Definition: path_table.cc:32
const std::string & state_text() const
Return the current state text.
Definition: unpack.h:342
The state in which post remove trigger copies are removed.
Definition: unpack.h:120
The state in which all operations have been successfully completed.
Definition: unpack.h:102
The state in which post remove triggers are copied so they are preserved to be run after the packages...
Definition: unpack.h:72
The state in which old versions of files that do not have replacements are backed up then removed...
Definition: unpack.h:88
The state in which files replaced by directories are restored.
Definition: unpack.h:115
state_type state() const
Get current state of the unpack operation.
Definition: unpack.h:297
const char * src_pathname
Definition: path_table.cc:31
The state in which the states of packages to be removed are changed to status::state_half_unpacked.
Definition: unpack.h:69
An interface class to represent a zip file.
Definition: zipfile.h:31
The state in which state_remove is being backed out.
Definition: unpack.h:106
LogCode
An enumeration of all the items that can be logged.
Definition: log.h:30
The state in which the states of packages to be unpacked are changed to status::state_half_unpacked.
Definition: unpack.h:65
void warning_func(std::tr1::function< void(LogCode code, const std::string &item, const std::string &what)> &f)
Set the function to log and capture warnings.
Definition: unpack.h:370

Reference Manual LibPkg Version 0.9.0 (4 Sep 2020)