rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cataloger.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #include <iostream>
4 #include <string>
5 #include <vector>
6 
7 #include "asserts.h"
8 #include "error.h"
9 #include "estring.h"
10 #include "fs.h"
11 #include "rconfig.h"
12 #include "timer.h"
13 #include "logger.h"
14 #include "vaulter.h"
15 
16 #include "cataloger.h"
17 
18 //-----------------------------------------------------------------------------
19 
20 /** C'tor */
22 {
23  if (this != &cataloger)
24  throw(INTERNAL_ERROR(0,"Attempt to allocate multiple catalog managers"));
25 
26  clear();
27 }
28 
29 /** Reset the catalog manager */
31 {
32  m_initialized = false;
33 }
34 
35 /** Initialize the catalog manager */
37 {
38  m_initialized = true;
39 }
40 
41 /** Return the initialized status */
42 const bool catalog_manager::initialized(void) const
43 {
44  return(m_initialized);
45 }
46 
47 /** Erase the contents of the catalog */
49 {
50  subdirectory subdir;
51  subdirectory::const_iterator sdi;
52  std::string path;
53  estring lstr;
54 
55  if (!initialized())
56  throw(INTERNAL_ERROR(0,"Catalog manager not initialized"));
57 
58  if (config.link_catalog_dir().size() == 0)
59  return;
60  subdir.path(config.link_catalog_dir());
61  if (subdir.size() == 0)
62  return;
63  lstr = "Cataloger - Erasing old link catalog...\n";
64  logger.write(lstr);
65  for (sdi = subdir.begin(); sdi != subdir.end(); ++sdi) {
67  TRY_nomem(path += "/");
68  TRY_nomem(path += *sdi);
69  TRY_nomem(path = reform_path(path));
70  rm_recursive(path);
71  }
72  lstr = "Cataloger - Finished erasing link catalog\n";
73  logger.write(lstr);
74 }
75 
76 /** Create or update the catalog */
78 {
79  estring cpath, vpath;
80  subdirectory subdir;
81  subdirectory::const_iterator si;
82  configuration_manager::vaults_type::const_iterator vi;
83  estring lstr;
84 
85  if (!initialized())
86  throw(INTERNAL_ERROR(0,"Catalog manager not initialized"));
87 
88  if (config.link_catalog_dir().size() == 0)
89  return;
90 
91  erase();
92  lstr = "Cataloger - Building link catalog...\n";
93  logger.write(lstr);
94  for (
95  vi = config.vaults().begin();
96  vi != config.vaults().end();
97  ++vi)
98  {
99  subdir.path(*vi);
100  if (subdir.size() == 0)
101  continue;
102  for (si = subdir.begin(); si != subdir.end(); ++si) {
103  if (!is_timestamp(*si))
104  continue;
105 
106  vpath = *vi;
107  vpath += "/";
108  vpath += *si;
109  vpath = reform_path(vpath);
110 
111  cpath = config.link_catalog_dir();
112  cpath += "/";
113  cpath += *si;
114  cpath = reform_path(cpath);
115 
116  lstr = "Linking: ";
117  lstr += vpath;
118  lstr += "\n";
119  logger.write(lstr);
120  lstr = " To: ";
121  lstr += cpath;
122  lstr += "\n";
123  logger.write(lstr);
124 
125  TRY_log(mk_relative_symlink(vpath, cpath),"Could not link");
126  }
127  }
128  lstr = "Cataloger - Finished building link catalog\n";
129  logger.write(lstr);
130 }
131 
132 //-----------------------------------------------------------------------------
133 
134 /** The global catalog manager */
136 
std::string reform_path(const std::string &a_path)
Reformat a path to remove double slashes.
Definition: fs.cc:205
log_manager logger
The global log manager.
Definition: logger.cc:138
catalog_manager()
C'tor.
Definition: cataloger.cc:21
Create and maintain a directory of links to all archives on all vaults.
Definition: cataloger.h:11
An extended string class.
Definition: estring.h:52
void catalog(void)
Create or update the catalog.
Definition: cataloger.cc:77
void clear(void)
Reset the catalog manager.
Definition: cataloger.cc:30
#define TRY_log(code, es)
Definition: error.h:177
void write(const std::string &a_str, const uint16 a_indention=0, const configuration_manager::logging_type a_logging_level=configuration_manager::logging_manager, const pid_t a_pid=pid())
Write a string to the log file.
Definition: logger.cc:96
const vaults_type & vaults(void) const
Return the vaults.
Definition: rconfig.cc:1604
#define TRY_nomem(code)
Definition: error.h:144
const bool initialized(void) const
Return the initialized status.
Definition: cataloger.cc:42
catalog_manager cataloger
The global catalog manager.
Definition: cataloger.cc:135
const type & path(const std::string a_path, const std::string a_filter="*")
Return a vector of strings of a list of files in a subdirectory.
Definition: fs.cc:1361
void mk_relative_symlink(const std::string a_from, const std::string a_to)
Given a from and to path, create a relative symbolic link.
Definition: fs.cc:770
void rm_recursive(const std::string a_path)
Recursively delete the contents of a directory.
Definition: fs.cc:1414
#define INTERNAL_ERROR(e, s)
Definition: error.h:123
configuration_manager config
The global configuration manager instance.
Definition: rconfig.cc:3364
const std::string & link_catalog_dir(void) const
Return the link-catalog-dir path.
Definition: rconfig.cc:1514
bool is_timestamp(const std::string &a_s)
Return true if the string is a valid timestamp.
Definition: tstamp.cc:502
void init(void)
Initialize the catalog manager.
Definition: cataloger.cc:36
bool m_initialized
Definition: cataloger.h:24
Retrieve a list of files in a subdirectory that match a given wildcard filename.
Definition: fs.h:273
void erase(void)
Erase the contents of the catalog.
Definition: cataloger.cc:48