File: tree.h

package info (click to toggle)
xstow 1.0.2-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,064 kB
  • ctags: 1,073
  • sloc: cpp: 7,264; sh: 1,216; makefile: 196; awk: 26
file content (173 lines) | stat: -rw-r--r-- 4,048 bytes parent folder | download
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
/*
 * $Log: tree.h,v $
 * Revision 1.2  2005/07/04 21:59:42  martin
 * added logging to all files
 *
 */
#ifndef tree_h
#define tree_h

#include "cppdir.h"
#include "ref.h"
#include "local_config.h"
#include <vector>
#include <iostream>

class Node
{
 public:

    struct EnumType
    {
	enum ETYPE
	{
	    FIRST__,
	    FILE,
	    DIR,
	    UNKNOWN,
	    LAST__
	};
    };
  
    typedef EnumRange<EnumType> TYPE;

  typedef std::vector< Ref<Node> > vec_node;

 protected:

  TYPE         type;
  CppDir::File file;

  Ref<Node> parent;
  Ref<vec_node> childs;

  bool path_is_hashed;
  mutable std::string full_path;

 public:

  Node( CppDir::File file, Ref<Node> parent = Ref<Node>() );

  bool scan( bool recursion = true );

  Ref<Node> getParent() const { return parent; }
  Ref<vec_node> getChilds() const { return childs; }

  bool hasParent() const { return parent.valid(); }
  bool hasChilds() const { return childs.valid(); }

  TYPE getType() const { return type; }
  bool isLink() const { return file.is_link(); }
  bool isDeadLink() const { return file.is_link() && !file; }

  std::string getFullPathName() const;
  std::string getName() const { return file.get_name(); }

  static std::string calcFullPathName( const Node &node );

  CppDir::File getFile() const { return file; }

  Ref<Node> getRoot();
};

class Tree
{
 public:

  struct Link
  {
    std::string origin;
    std::string target;
    std::string exec;

      struct EnumAction {
	  enum ETYPE
	  {
	      // don't change order; required for sorting
	      FIRST__  = 0,
	      REMOVE   = 1,
	      MKDIR    = 2,
	      SET_LINK = 3,
	      COPY     = 4,
	      COPY_DIR = 5,
	      EXEC     = 6,
	      LAST__
	  };
      };

      typedef EnumRange<EnumAction> ACTION;

      ACTION action;

    Link( const std::string &origin, const std::string &target, const std::string &exec = "" )
      : origin( origin ), target( target ), exec( exec ), action( ACTION::SET_LINK )
    {}
    Link() : action( ACTION::SET_LINK ) {}
  };

  typedef std::vector<Link> vec_link;

 private:

  Ref<Node> base;
  Ref<Node> target;
  Ref<Node> working;
  Ref<vec_link> links;

 public:

  Tree( const std::string &base, 
	const std::string &target,
	const std::string &working );

  bool scan();

  bool check()
    {
      links->clear();
      return check( target, base, links );
    }

  Ref<Node> getTree() const { return base; }

  bool exec_list();

 private:
  bool check( Ref<Node> target, Ref<Node> base, Ref<vec_link> links );
  bool checkDirs( Ref<Node> a, Ref<Node> b, std::string wpath, Ref<vec_link> links );

  void unify_links( Ref<vec_link> &links );
  
  bool compare_keep_dir( const std::string & a, const std::string &b );
  bool keep_dir( const std::string & a );
};

std::ostream& operator<<( std::ostream &out, Ref<Node> node );
std::ostream& operator<<( std::ostream& out, Ref<Tree::vec_link> links );
std::ostream& operator<<( std::ostream& out, const Tree::Link &link );

bool operator==( const Tree::Link &a, const Tree::Link &b );
bool operator<( const Tree::Link &a, const Tree::Link &b );

void make_relative_links( Ref<Tree::vec_link> links );

bool match_ignore( const std::string &file );
bool match_copy( const std::string &file );
bool match_ignore_file_in_dir( const std::string working_dir, const std::string &file );
bool match_copy_file_in_dir( const std::string working_dir, const std::string &file );
bool match_auto_traverse( const std::string &file );
bool match_stow_dirs( const std::string &file );
bool match_auto_stow_dirs( const std::string &file );
bool match_protect_dirs( const std::string &file );
bool match_auto_protect_dirs( const std::string &file );
bool match_protect_targets( const std::string &file );

#ifdef CAN_USE_INI
bool match_exec( const std::string &file, std::string &exec_string );
std::string command( std::string exec, const std::string &source, const std::string &target );
#endif

bool file_copy( std::string origin, std::string target );
bool dir_copy( std::string origin, std::string target );

#endif