summaryrefslogtreecommitdiff
path: root/src/generic/apt/aptcache.h
blob: a7d716c9eff333bc153e8d07b7fc27e8d8e7a66b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// aptcache.h  -*-c++-*-
//
//  Copyright 1999-2005, 2007-2009, 2011 Daniel Burrows
//  Copyright 2015 Manuel A. Fernandez Montecelo
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.

#ifndef APTCACHE_H
#define APTCACHE_H

#include <config.h>

#include <cwidget/generic/util/bool_accumulate.h>

#include <apt-pkg/depcache.h>
#include <apt-pkg/pkgrecords.h>

#include <sigc++/signal.h>
#include <sigc++/trackable.h>

#include <iostream>
#include <map>
#include <set>
#include <vector>

/** \brief Replacements for the Apt cache and dependency cache file classes
 * 
 * 
 *  Replacements for the Apt cache and dependency cache file classes.  Those
 *  are lacking in a couple of crucial ways (eg: persistent state), so I
 *  have to come up with my own set of routines.. :(
 *  
 *  In order to track the appropriate package information, we must keep our
 *  own file (I think..apt doesn't have hooks for adding stuff..) containing
 *  that information, properly lock/unlock it, etc.
 * 
 * \file aptcache.h
 */

class undoable;
class undo_group;
class pkgProblemResolver;
class aptitude_universe;
template<typename PackageUniverse> class generic_solution;

class aptitudeDepCache:public pkgDepCache, public sigc::trackable
{
  typedef int user_tag_reference;

public:
  /** This is a general enum that's used for several purposes by the
   *  extended state.  Not every value is valid in every case.
   */
  enum changed_reason {manual, user_auto, libapt, from_resolver, unused};

  /** \brief An opaque type used to store references into the
   *  user-tags list.
   *
   *  We only store one copy of each tag string, to save space.
   */
  class user_tag
  {
    friend class aptitudeDepCache;
    user_tag_reference tag_num;
    explicit user_tag(const user_tag_reference &_tag_num) : tag_num(_tag_num)
    {
    }

  public:
    bool operator==(const user_tag &other) const
    {
      return tag_num == other.tag_num;
    }

    bool operator<(const user_tag &other) const
    {
      return tag_num < other.tag_num;
    }
  };

  /** This structure augments the basic depCache state structure to
   *  support special aptitude features.
   */
  struct aptitude_state
  {
    /** The original action stored on the package when starting. */
    pkgCache::State::PkgSelectedState original_selection_state;

    /** The current action stored on the package. */
    pkgCache::State::PkgSelectedState selection_state;

    /** Stores the version, if any, that the user explicitly selected.
     */
    std::string candver;

    /** Stores the version, if any, which the user has forbidden
     *  aptitude to choose as an upgrade target.  (handles a situation
     *  unstable users are familiar with, where package X version V is
     *  broken, so you want to hold at V-1 until V+1 is available..)
     *
     *  If this string is empty, no "forbid" qualifier is in place.
     */
    std::string forbidver;

    /** \brief Stores the tags attached to this package by the user. */
    std::set<user_tag> user_tags;

    /** If the package is going to be removed, this gives the reason
     *  for the removal.
     */
    changed_reason remove_reason:3;

    /** True if the package is a "new" package.  This is set on every
     *  package the first time it is seen, and remains set until
     *  forget_new_packages() is called.
     *
     *  If this field is missing in the state file, it defaults to \b
     *  false.
     */
    bool new_package:1;

    /** \brief True if an aptitude flag from the last run indicates
     *  that this package should be set to automatically installed.
     *
     *  This flag is used to migrate old settings and to flag queued
     *  installations as automatic.
     *
     *  \warning This value is stored here to get it from the code
     *  that interprets pkgstates to the code that sets up package
     *  states; user code should not assume that it is meaningful.
     */
    bool previously_auto_package:1;

    /** If the package is in state Install and is upgradable, this
     *  field determines whether or not it should be upgraded.  This
     *  field is not updated as the package is modified; it is used
     *  when the package states are initially loaded, then ignored.
     *
     *  This solves the problem of "part of my upgrade failed and
     *  aptitude forgot I wanted to upgrade anything!".  I am
     *  concerned about its behavior if the package cache is updated
     *  between upgrade runs, and I have considered indicating which
     *  version each package should be upgraded to...but I haven't yet.
     *
     *  If this field is missing in the state file, it defaults to \b
     *  false.
     */
    bool upgrade:1;

    /** If \b true, the package in question will be reinstalled.
     *
     *  This member is not saved to the state file.
     */
    bool reinstall:1;

    /** If \b true, the package in question is tagged for an action.
     *
     *  This member is not saved to the state file.
     */
    bool tagged:1;

    /** If \b true, the package in question is flagged for future reference. */
    bool flagged:1;
  };

  /** \brief Represents a group of aptitude actions. */
  class action_group
  {
    /** The parent group.  This is a member and not a parent class so
     *  that I can force its cleanup routines to run before mine.
     */
    pkgDepCache::ActionGroup *parent_group;

    aptitudeDepCache &cache;

    undo_group *group;

    action_group(const action_group &other);
  public:
    /** \brief Create a new action group.
     *
     *  \param cache  The package cache on which to act.
     *  \param group  The undo group to add changes to, or NULL to not remember
     *                changes.
     */
    action_group(aptitudeDepCache &cache, undo_group *group = NULL);

    ~action_group();
  };

  /** This flag is \b true iff the persistent state has changed (ie, we
   *  need to save the cache).
   */
  bool dirty;

  /** This flag is \b true if the cache is in 'read-only' mode.
   */
  bool read_only;

  // Some internal classes for undo information
  class apt_undoer;
  class forget_undoer;
  friend class apt_undoer;
  friend class forget_undoer;

  class candver_undoer;
  friend class candver_undoer;

  /** This class "remembers" the current cache state, and is used to
   *  perform post facto analysis of the decisions libapt makes.
   *  (thus enabling the "undo" function)
   */
  class apt_state_snapshot
  {
    StateCache *PkgState;
    unsigned char *DepState;
    aptitude_state *AptitudeState;
    signed long long iUsrSize;
    unsigned long long iDownloadSize;
    unsigned long iInstCount;
    unsigned long iDelCount;
    unsigned long iKeepCount;
    unsigned long iBrokenCount;
    unsigned long iBadCount;

  private:
    apt_state_snapshot():PkgState(NULL), DepState(NULL), AptitudeState(NULL) {}

  public:
    ~apt_state_snapshot()
    {
      delete[] PkgState;
      delete[] DepState;
      delete[] AptitudeState;
    }

    friend class aptitudeDepCache;
  };

  const std::string &deref_user_tag(const user_tag &tag) const
  {
    return user_tags[tag.tag_num];
  }
private:
  void parse_user_tags(std::set<user_tag> &tags,
		       const char *&start, const char *end,
		       const std::string &package_name);

  aptitude_state *package_states;
  // To speed the program up and save memory, I only store one copy of
  // each distinct tag, and keep a reference to this list.  The list
  // is not managed especially intelligently: if you repeatedly add
  // and remove never-before-seen tags to a package, it will grow
  // without bound.  I don't consider this a problem, because users
  // are unlikely to add "very many" (say, more than a few hundred)
  // tags in a single session.  If it does become a problem, tags can
  // be reference-counted, at the expense of maintaining an explicit
  // free list.
  std::vector<std::string> user_tags;
  // Stores the reference corresponding to each string.
  std::map<std::string, user_tag_reference> user_tags_index;
  // Read a set of user tags from the given string region
  // and write the tags into the index and into the given
  // set of tags.
  void parse_usertags(std::set<user_tag_reference> &tags,
		      const char *&start, const char *end);

  int lock;
  // The lock on the extra-info file.
  int group_level;
  // The current 'group level' -- how many times start_action_group has been
  // called without a matching end_action_group.

  /** The number of "new" packages. */
  int new_package_count;

  apt_state_snapshot backup_state;
  // Stores what the cache was like just before an action was performed

  pkgRecords *records;

  /** Call whenever the cache state is modified; discards the
   *  state of the active resolver.
   *
   *  \param undo the undo group with which this discarding should be
   *  associated.
   */
  void discard_resolver(undo_group *undo);

  /** Call whenever a new resolver should be instantiated. */
  void create_resolver();

  undoable *state_restorer(PkgIterator pkg, StateCache &state, aptitude_state &ext_state);
  // Returns an 'undoable' object which will restore the given package to the
  // given state via {Mark,Set}* routines

  void duplicate_cache(apt_state_snapshot *target);
  // This makes the **ASSUMPTION** that if the target's tables aren't
  // NULL, they're properly sized..

  void cleanup_after_change(undo_group *undo,
			    std::set<pkgCache::PkgIterator> *changed_packages,
			    bool alter_stickies=true);
  // Finds anything that magically changed and creates an undo item for it..
  // If alter_stickies is false, sticky states will be left alone.  (hack :( )

  void MarkFromDselect(const PkgIterator &Pkg);
  // Marks the package based on its current status and its dselect state,
  // adjusting its selected state as appropriate.

  // The following methods just perform the core part of the named
  // action, without creating a new action group or running
  // mark&sweep.

  /**
   * Use this instead of pkgDepCache::MarkInstall; it ensures that the
   * package's auto flag is set properly.
   */
  void internal_mark_install(const PkgIterator &Pkg, bool AutoInst, bool ReInstall);

  /** Internally marking packages for deletion -- main entry point
   */
  void internal_mark_delete(const PkgIterator &Pkg, bool Purge, bool unused_delete);
  /** Internally marking packages for deletion -- recursive
   *
   * When following reverse dependencies to see if they are automatically
   * installed and unused (so they can be pro-actively marked for deletion as
   * well), the function calls itself recursively.  In this case, it uses this
   * version with an extra parameter to detect when packages were already
   * visited, to avoid infinite loops in the case of circular dependencies (bug
   * #801430).
   */
  void internal_mark_delete(const PkgIterator &Pkg, bool Purge, bool unused_delete,
			    std::vector<unsigned int>& unused_already_visited);

  void internal_mark_keep(const PkgIterator &Pkg, bool Automatic, bool SetHold);

  /** Handle changing package states to take into account the garbage
   *  collector's output.  Uses the core pkgDepCache methods.
   */
  void sweep();
  void begin_action_group();
  void end_action_group(undo_group *undo);
public:
  /** Create a new depcache from the given cache and policy.  By
   *  default, the depcache is readonly if and only if it is not
   *  locked.
   */
  aptitudeDepCache(pkgCache *cache, Policy *Plcy=0);

  bool Init(OpProgress *Prog, bool WithLock,
	    bool do_initselections, const char * status_fname=NULL);

  bool is_locked() {return lock!=-1;}

  bool is_dirty() {return dirty;}

  pkgRecords &get_records() { return *records; }

  // If do_initselections is "false", the "sticky states" will not be used
  // to initialize packages.  (important for the command-line mode)
  bool build_selection_list(OpProgress &Prog, bool WithLock,
			    bool do_initselections,
			    const char * status_fname=NULL);

  void forget_new(undoable **undoer);
  // Clears all information about which packages are 'new'.  Overwrites undoer
  // if it is not NULL

  /** Sets the "new" flag for the given package; use this instead of
   *  directly manipulating the cache so the count of new packages is
   *  updated. (gross?)
   */
  void set_new_flag(const pkgCache::PkgIterator &pkg, bool is_new);

  /** Gets the number of new packages. */
  int get_new_package_count() const {return new_package_count;}

  inline aptitude_state &get_ext_state(const PkgIterator &Pkg)
  {return package_states[Pkg->ID];}

  bool save_selection_list(OpProgress &prog, const char *status_fname=NULL);
  // If the list isn't locked (or an fd isn't provided), is a NOP.

  void mark_install(const PkgIterator &Pkg, bool AutoInst, bool ReInstall, undo_group *undo);
  void mark_delete(const PkgIterator &Pkg, bool Purge, bool unused_delete, undo_group *undo);

  /** \brief Keep the given package at its current version.
   *
   *  \param Pkg The package to keep.
   *
   *  \param Automatic If \b true, then this is an automatically
   *  triggered action.
   *
   *  \param SetHold If \b true, then a sticky hold will be set on
   *  this package.
   *
   *  \param undo The undo group into which actions should be placed.
   */
  void mark_keep(const PkgIterator &Pkg, bool Automatic, bool SetHold, undo_group *undo);

  void set_candidate_version(const VerIterator &TargetVer, undo_group *undo);
  // These just wrap the equivalent depCache functions for the UI's benefit;
  // they mark what the user wants done with each package.
  //  If an undo group is passed in, items are added to it for each of the
  // actions taken.

  /** Forbids an upgrade to the given version.  If the package is currently
   *  marked for upgrade to that version, it is kept back.
   */
  void forbid_upgrade(const pkgCache::PkgIterator &pkg,
		      std::string verstr, undo_group *undo);

  /** Marks all upgradable and non-held packages for upgrade.
   *
   *  \param with_autoinst if \b true, the dependencies of packages
   *  begin upgraded will automatically be installed.
   *
   *  \param ignore_removed if \b false, all upgradable packages that
   *  are not held back will be upgraded; otherwise, packages that are
   *  going to be removed will be ignored.
   *
   *  \param undo an undo group with which the actions taken by this
   *  routine will be registered, or \b NULL.
   */
  void mark_all_upgradable(bool with_autoinst, bool ignore_removed,
			   undo_group *undo);

  /** \brief Retrieve the set of packages that mark_all_upgradable
   *  would attempt to upgrade.
   *
   *  \param ignore_removed if \b false, all upgradable packages that
   *  are not held back will be upgraded; otherwise, packages that are
   *  going to be removed will be ignored.
   *
   *  \param upgradable A location in which to place the set of
   *  currently upgradable packages.
   */
  void get_upgradable(bool ignore_removed, std::set<pkgCache::PkgIterator> &upgradable);

  void mark_single_install(const PkgIterator &pkg, undo_group *undo);
  // Marks this package to be install, and all other packages to be kept.
  //  The "keep" on the other packages, however, is NOT sticky and will NOT
  // be saved in the extended state file! (this is a bit of a hack..)
  // (this could be fairly easily emulated; it's a convenience routine)

  void mark_auto_installed(const PkgIterator &pkg,
			   bool set_auto,
			   undo_group *undo);

  /** \brief Attach a tag to a package.
   *
   *  The tag will be added to the user_tags member of the package's
   *  extended state.
   */
  void attach_user_tag(const PkgIterator &pkg, const std::string &tag,
		       undo_group *undo);

  /** \brief Remove a tag from a package.
   *
   *  The tag will be removed from the user_tags member of the
   *  package's extended state.  If it isn't already present, nothing
   *  will happen.
   */
  void detach_user_tag(const PkgIterator &pkg, const std::string &tag,
		       undo_group *undo);

  // Marks the given package as having been autoinstalled (so it will be
  // removed automatically) or having been installed manually.

  /** Retrieve the read-only flag. */
  bool get_read_only() const { return read_only; }

  /** Set the read-only flag.  If the cache is read-only, then any
   *  attempt to modify a package's state will instead call the
   *  read_only_alert() signal, which can either allow the
   *  modification to continue or cancel it.
   */
  void set_read_only(bool new_read_only);

  /** Apply the given solution as a resolver result; any actions
   *  that it requests will be marked as having been performed to
   *  fulfill dependencies.
   *
   *  \param solution   the solution that is to be applied.  A local
   *                    copy will be made (thus incrementing the
   *                    reference count on the underlying
   *                    representation) so this routine is safe if one
   *                    of the signals it emits destroys the solution
   *                    object.
   *
   *  \param undo       the undo group to which any undo actions
   *                    generated by applying the solution should
   *                    be added.
   */
  void apply_solution(const generic_solution<aptitude_universe> &solution,
		      undo_group *undo);

  /** \return \b true if automatic aptitude upgrades should ignore this
   *  package.
   */
  bool is_held(const PkgIterator &pkg);

  bool all_upgrade(bool with_autoinst, undo_group *undo);
  // Wrapper for pkgAllUpgrade (the engine of "apt-get upgrade")

  bool try_fix_broken(undo_group *undo);
  // Attempts to fix any broken packages, dumping the changes the problem
  // fixer creates into the given undo group.

  bool try_fix_broken(pkgProblemResolver &fixer, undo_group *undo);
  // Just runs the resolver given and catches automatic changes.
  // (this lets callers customize the information given to the resolver)

  const apt_state_snapshot *snapshot_apt_state();
  // Returns the current state of the *APT* cache (no information about
  // Aptitude states is included); this is meant to be used to implement undo,
  // detection of "wtf happened??" after the problem resolver runs, etc.
  void restore_apt_state(const apt_state_snapshot *snapshot);
  // Restores the *APT* cache to the given state.

  /** This signal is emitted *before* any package's install state is
   *  changed.  It may be emitted more than once per state change; if
   *  no states actually change, it might not be emitted at all.
   */
  sigc::signal0<void> pre_package_state_changed;

  /** This signal is emitted when any package's install state is
   *  changed.
   */
  sigc::signal0<void> package_state_changed;

  /** \brief This signal is emitted when any changes to a package's state
   *  that might need to trigger a redraw of that package take place.
   */
  sigc::signal1<void, const std::set<pkgCache::PkgIterator> *> package_states_changed;

  // Emitted when a package's categorization is potentially changed.
  // (in particular, when package "new" states are forgotten)
  sigc::signal0<void> package_category_changed;

  /** This signal is emitted when the user attempts to modify the
   *  cache while it is in read-only mode.  If any callback returns \b
   *  false, the modification is cancelled.
   */
  sigc::signal0<bool, cwidget::util::accumulate_and> read_only_permission;

  /** This signal is emitted when a read-only operation fails.  It is
   *  emitted exactly once for every action group.
   */
  sigc::signal0<void> read_only_fail;

  /** \name GC control methods */
  // @{

  InRootSetFunc *GetRootSetFunc();
  bool MarkFollowsRecommends();
  bool MarkFollowsSuggests();

  // @}

  bool IsInstallOk(const pkgCache::PkgIterator &pkg,
		   bool AutoInst,
		   unsigned long Depth,
		   bool FromUser);

  bool IsDeleteOk(const pkgCache::PkgIterator &pkg,
		  bool Purge,
		  unsigned long Depth,
		  bool FromUser);

  virtual ~aptitudeDepCache();
};

class pkgPolicy;

class aptitudeCacheFile
// Hack around problems in libapt.  Most of the code associated with this
// class was copied directly from libapt and reindented..
{
  MMap *Map;

  pkgCache *Cache;
  aptitudeDepCache *DCache;

  bool have_system_lock;
  // hm, used to make it look like the old stuff?
public:

  pkgPolicy *Policy;

  // We look pretty much exactly like a pointer to a dep cache
  inline operator pkgCache &() {return *Cache;};
  inline operator pkgCache *() {return Cache;};
  inline operator aptitudeDepCache &() {return *DCache;};
  inline operator aptitudeDepCache *() {return DCache;};
  inline aptitudeDepCache *operator ->() {return DCache;};
  inline aptitudeDepCache &operator *() {return *DCache;};
  inline aptitudeDepCache::StateCache &operator [](pkgCache::PkgIterator const &I) {return (*DCache)[I];};
  inline unsigned char &operator [](pkgCache::DepIterator const &I) {return (*DCache)[I];};

  bool Open(OpProgress &Progress, bool do_initselections, bool WithLock=true,
	    const char * status_fname=NULL);
  bool is_locked() {return have_system_lock;} // EWW (also not quite right)

  void ReleaseLock();
  bool GainLock();

  aptitudeCacheFile();
  ~aptitudeCacheFile();
};

#endif