1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2016 Planets Communications B.V.
6    Copyright (C) 2016-2018 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 
24 #ifndef BAREOS_CATS_BVFS_H_
25 #define BAREOS_CATS_BVFS_H_ 1
26 
27 /*
28  * This object can be use to browse the catalog
29  *
30  * Bvfs fs;
31  * fs.SetJobid(10);
32  * fs.update_cache();
33  * fs.ChDir("/");
34  * fs.ls_dirs();
35  * fs.ls_files();
36  */
37 
38 /* Helper for result handler */
39 typedef enum
40 {
41   BVFS_FILE_RECORD = 'F',
42   BVFS_DIR_RECORD = 'D',
43   BVFS_FILE_VERSION = 'V'
44 } bvfs_handler_type;
45 
46 typedef enum
47 {
48   BVFS_Type = 0, /**< Could be D, F, V */
49   BVFS_PathId = 1,
50 
51   BVFS_Name = 2,
52   BVFS_JobId = 3,
53 
54   BVFS_LStat = 4,  /**< Can be empty for missing directories */
55   BVFS_FileId = 5, /**< Can be empty for missing directories */
56 
57   /* Only if File Version record */
58   BVFS_Md5 = 6,
59   BVFS_VolName = 7,
60   BVFS_VolInchanger = 8
61 } bvfs_row_index;
62 
63 class Bvfs {
64  public:
65   Bvfs(JobControlRecord* j, BareosDb* mdb);
66   virtual ~Bvfs();
67 
68   void SetJobid(JobId_t id);
69   void SetJobids(char* ids);
70 
SetLimit(uint32_t max)71   void SetLimit(uint32_t max) { limit = max; }
72 
SetOffset(uint32_t nb)73   void SetOffset(uint32_t nb) { offset = nb; }
74 
SetPattern(char * p)75   void SetPattern(char* p)
76   {
77     uint32_t len = strlen(p);
78     pattern = CheckPoolMemorySize(pattern, len * 2 + 1);
79     db->EscapeString(jcr, pattern, p, len);
80   }
81 
82   /* Get the root point */
83   DBId_t get_root();
84 
85   /*
86    * It's much better to access Path though their PathId, it
87    * avoids mistakes with string encoding
88    */
ChDir(DBId_t pathid)89   void ChDir(DBId_t pathid)
90   {
91     ResetOffset();
92     pwd_id = pathid;
93   }
94 
95   /*
96    * Returns true if the directory exists
97    */
98   bool ChDir(const char* path);
99 
100   bool ls_files(); /* Returns true if we have more files to read */
101   bool ls_dirs();  /* Returns true if we have more dir to read */
102   void GetAllFileVersions(const char* path,
103                           const char* fname,
104                           const char* client);
105   void GetAllFileVersions(DBId_t pathid, const char* fname, const char* client);
106 
107   void update_cache();
108 
SetSeeAllVersions(bool val)109   void SetSeeAllVersions(bool val) { see_all_versions = val; }
110 
SetSeeCopies(bool val)111   void SetSeeCopies(bool val) { see_copies = val; }
112 
SetHandler(DB_RESULT_HANDLER * h,void * ctx)113   void SetHandler(DB_RESULT_HANDLER* h, void* ctx)
114   {
115     list_entries = h;
116     user_data = ctx;
117   }
118 
get_pwd()119   DBId_t get_pwd() { return pwd_id; }
120 
get_attr()121   Attributes* get_attr() { return attr; }
122 
get_jcr()123   JobControlRecord* get_jcr() { return jcr; }
124 
ResetOffset()125   void ResetOffset() { offset = 0; }
126 
next_offset()127   void next_offset() { offset += limit; }
128 
129   /* Clear all cache */
130   void clear_cache();
131 
132   /* Compute restore list */
133   bool compute_restore_list(char* fileid,
134                             char* dirid,
135                             char* hardlink,
136                             char* output_table);
137 
138   /* Drop previous restore list */
139   bool DropRestoreList(char* output_table);
140 
141   /* for internal use */
142   int _handlePath(void*, int, char**);
143 
144  private:
145   Bvfs(const Bvfs&);            /* prohibit pass by value */
146   Bvfs& operator=(const Bvfs&); /* prohibit class assignment */
147 
148   JobControlRecord* jcr;
149   BareosDb* db;
150   POOLMEM* jobids;
151   uint32_t limit;
152   uint32_t offset;
153   uint32_t nb_record; /* number of records of the last query */
154   POOLMEM* pattern;
155   DBId_t pwd_id;     /* Current pathid */
156   POOLMEM* prev_dir; /* ls_dirs query returns all versions, take the 1st one */
157   Attributes* attr;  /* Can be use by handler to call DecodeStat() */
158 
159   bool see_all_versions;
160   bool see_copies;
161 
162   DB_RESULT_HANDLER* list_entries;
163   void* user_data;
164 };
165 
166 #define BvfsIsDir(row) ((row)[BVFS_Type][0] == BVFS_DIR_RECORD)
167 #define BvfsIsFile(row) ((row)[BVFS_Type][0] == BVFS_FILE_RECORD)
168 #define BvfsIsVersion(row) ((row)[BVFS_Type][0] == BVFS_FILE_VERSION)
169 
170 char* bvfs_parent_dir(char* path);
171 
172 /*
173  * Return the basename of the with the trailing / (update the given string)
174  * TODO: see in the rest of bareos if we don't have this function already
175  */
176 char* bvfs_basename_dir(char* path);
177 
178 #endif /* BAREOS_CATS_BVFS_H_ */
179