]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/bvfs.h
a9bcf5f2fa628b30a499d3ea0ed09b642215ce3f
[bacula/bacula] / bacula / src / cats / bvfs.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19
20 #ifndef __BVFS_H_
21 #define __BVFS_H_ 1
22
23
24 /*
25  * This object can be use to browse the catalog
26  *
27  * Bvfs fs;
28  * fs.set_jobid(10);
29  * fs.update_cache();
30  * fs.ch_dir("/");
31  * fs.ls_dirs();
32  * fs.ls_files();
33  */
34
35 /* Helper for result handler */
36 typedef enum {
37    BVFS_FILE_RECORD  = 'F',
38    BVFS_DIR_RECORD   = 'D',
39    BVFS_FILE_VERSION = 'V',
40    BVFS_VOLUME_LIST  = 'L',
41    BVFS_DELTA_RECORD = 'd',
42 } bvfs_handler_type;
43
44 typedef enum {
45    BVFS_Type    = 0,            /* Could be D, F, V, L */
46    BVFS_PathId  = 1,
47    BVFS_FilenameId = 2,
48
49    BVFS_Name    = 3,            /* Can be empty for File version */
50    BVFS_JobId   = 4,
51
52    BVFS_LStat   = 5,            /* Can be empty for missing directories */
53    BVFS_FileId  = 6,            /* Can be empty for missing directories */
54
55    /* Only if Path record */
56    BVFS_FileIndex = 7,
57
58    /* Only if File Version record */
59    BVFS_Md5     = 7,
60    BVFS_VolName = 8,
61    BVFS_VolInchanger = 9,
62
63    /* Only if Delta record */
64    BVFS_DeltaSeq = 6,
65    BVFS_JobTDate = 7
66 } bvfs_row_index;
67
68 class Bvfs {
69
70 public:
71    Bvfs(JCR *j, BDB *mdb);
72    virtual ~Bvfs();
73
74    void set_compute_delta(bool val) {
75       compute_delta = val;
76    };
77
78    /* Return the number of jobids after the filter */
79    int set_jobid(JobId_t id);
80    int set_jobids(char *ids);
81
82    char *get_jobids() {
83       return jobids;
84    }
85
86    void set_limit(uint32_t max) {
87       limit = max;
88    }
89
90    void set_offset(uint32_t nb) {
91       offset = nb;
92    }
93
94    void set_pattern(char *p) {
95       uint32_t len = strlen(p);
96       pattern = check_pool_memory_size(pattern, len*2+1);
97       db->bdb_escape_string(jcr, pattern, p, len);
98    }
99
100    void set_filename(char *p) {
101       uint32_t len = strlen(p);
102       filename = check_pool_memory_size(filename, len*2+1);
103       db->bdb_escape_string(jcr, filename, p, len);
104    }
105
106    /* Get the root point */
107    DBId_t get_root();
108
109    /* It's much better to access Path though their PathId, it
110     * avoids mistakes with string encoding
111     */
112    bool ch_dir(DBId_t pathid);
113
114    /*
115     * Returns true if the directory exists
116     */
117    bool ch_dir(const char *path);
118
119    bool ls_files();             /* Returns true if we have more files to read */
120    bool ls_dirs();              /* Returns true if we have more dir to read */
121    void ls_special_dirs();      /* get . and .. */
122    void get_all_file_versions(DBId_t pathid, FileId_t fnid, const char *client);
123
124    void update_cache();
125
126    /* bfileview */
127    void fv_update_cache();
128
129    void set_see_all_versions(bool val) {
130       see_all_versions = val;
131    }
132
133    void set_see_copies(bool val) {
134       see_copies = val;
135    }
136
137    DBId_t get_dir_filenameid();
138
139    int filter_jobid();         /* Call after set_username, returns the number of jobids */
140
141    void set_username(char *user) {
142       if (user) {
143          username = bstrdup(user);
144       }
145    };
146
147    char *escape_list(alist *list);
148
149    bool copy_acl(alist *list) {
150       if (!list ||
151           (list->size() > 0 &&
152            (strcasecmp((char *)list->get(0), "*all*") == 0)))
153       {
154          return false;
155       }
156       return true;
157    };
158
159    /* Keep a pointer to various ACLs */
160    void set_job_acl(alist *lst) {
161       job_acl = copy_acl(lst)?lst:NULL;
162       use_acl = true;
163    };
164    void set_fileset_acl(alist *lst) {
165       fileset_acl = copy_acl(lst)?lst:NULL;
166       use_acl = true;
167    };
168    void set_client_acl(alist *lst) {
169       client_acl = copy_acl(lst)?lst:NULL;
170       use_acl = true;
171    };
172    void set_pool_acl(alist *lst) {
173       pool_acl = copy_acl(lst)?lst:NULL;
174       use_acl = true;
175    };
176    void set_handler(DB_RESULT_HANDLER *h, void *ctx) {
177       list_entries = h;
178       user_data = ctx;
179    };
180
181    DBId_t get_pwd() {
182       return pwd_id;
183    };
184
185    ATTR *get_attr() {
186       return attr;
187    }
188
189    JCR *get_jcr() {
190       return jcr;
191    }
192
193    void reset_offset() {
194       offset=0;
195    }
196
197    void next_offset() {
198       offset+=limit;
199    }
200
201    /* Clear all cache */
202    void clear_cache();
203
204    /* Compute restore list */
205    bool compute_restore_list(char *fileid, char *dirid, char *hardlink,
206                              char *output_table);
207
208    /* Drop previous restore list */
209    bool drop_restore_list(char *output_table);
210
211    /* for internal use */
212    int _handle_path(void *, int, char **);
213
214    /* Handle Delta parts if any */
215    void insert_missing_delta(char *output_table, int64_t *res);
216
217    /* Get a list of volumes */
218    void get_volumes(FileId_t fileid);
219
220    /* Get Delta parts of a file */
221    bool get_delta(FileId_t fileid);
222
223    /* Check if the parent directories are accessible */
224    bool check_path_access(DBId_t pathid);
225
226    /* Check if the full path is authorized by the current set of ACLs */
227    bool check_full_path_access(int nb, sellist *sel, db_list_ctx *toexcl);
228
229    alist *dir_acl;
230
231    int  check_dirs;             /* When it's 1, we check the against directory_acl */
232    bool can_access(struct stat *st);
233    bool can_access_dir(const char *path);
234
235 private:
236    Bvfs(const Bvfs &);               /* prohibit pass by value */
237    Bvfs & operator = (const Bvfs &); /* prohibit class assignment */
238
239    JCR *jcr;
240    BDB *db;
241    POOLMEM *jobids;
242    char *username;              /* Used with Bweb */
243
244    POOLMEM *prev_dir; /* ls_dirs query returns all versions, take the 1st one */
245    POOLMEM *pattern;
246    POOLMEM *filename;
247
248    POOLMEM *tmp;
249    POOLMEM *escaped_list;
250
251    /* Pointer to Console ACL */
252    alist *job_acl;
253    alist *client_acl;
254    alist *fileset_acl;
255    alist *pool_acl;
256    char  *last_dir_acl;
257
258    ATTR *attr;        /* Can be use by handler to call decode_stat() */
259
260    uint32_t limit;
261    uint32_t offset;
262    uint32_t nb_record;          /* number of records of the last query */
263    DBId_t pwd_id;               /* Current pathid */
264    DBId_t dir_filenameid;       /* special FilenameId where Name='' */
265
266    bool see_all_versions;
267    bool see_copies;
268    bool compute_delta;
269
270    db_list_ctx fileid_to_delete; /* used also by check_path_access */
271    bool need_to_check_permissions();
272    bool use_acl;
273
274    /* bfileview */
275    void fv_get_big_files(int64_t pathid, int64_t min_size, int32_t limit);
276    void fv_update_size_and_count(int64_t pathid, int64_t size, int64_t count);
277    void fv_compute_size_and_count(int64_t pathid, int64_t *size, int64_t *count);
278    void fv_get_current_size_and_count(int64_t pathid, int64_t *size, int64_t *count);
279    void fv_get_size_and_count(int64_t pathid, int64_t *size, int64_t *count);
280
281    DB_RESULT_HANDLER *list_entries;
282    void *user_data;
283 };
284
285 #define bvfs_is_dir(row) ((row)[BVFS_Type][0] == BVFS_DIR_RECORD)
286 #define bvfs_is_file(row) ((row)[BVFS_Type][0] == BVFS_FILE_RECORD)
287 #define bvfs_is_version(row) ((row)[BVFS_Type][0] == BVFS_FILE_VERSION)
288 #define bvfs_is_volume_list(row) ((row)[BVFS_Type][0] == BVFS_VOLUME_LIST)
289 #define bvfs_is_delta_list(row) ((row)[BVFS_Type][0] == BVFS_DELTA_RECORD)
290
291 void bvfs_update_fv_cache(JCR *jcr, BDB *mdb, char *jobids);
292 int bvfs_update_path_hierarchy_cache(JCR *jcr, BDB *mdb, char *jobids);
293 void bvfs_update_cache(JCR *jcr, BDB *mdb);
294 char *bvfs_parent_dir(char *path);
295
296 /* Return the basename of the with the trailing /  (update the given string)
297  * TODO: see in the rest of bacula if we don't have
298  * this function already
299  */
300 char *bvfs_basename_dir(char *path);
301
302 #endif /* __BVFS_H_ */