1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 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 #include "backupservice.h"
21 
start(const char * spoolPath,Journal * j)22 void BackupService::start(const char *spoolPath, Journal *j)
23 {
24     POOLMEM *err_msg;
25     _journal = j;
26     _spoolPath = bstrdup(spoolPath);
27     FolderRecord *rec;
28     _journal->beginTransaction("r");
29 
30     for (;;) {
31        rec = _journal->readFolderRecord();
32 
33        if(rec == NULL) {
34            break;
35        }
36 
37        FolderWatcher *w = new FolderWatcher(this);
38        err_msg = w->watch(rec->path);
39 
40        if (err_msg != NULL) {
41           Dmsg2(0, "Error while trying to watch %s. %s", rec->path, err_msg);
42        } else {
43           _watchers[rec->path] = w;
44        }
45 
46        delete rec;
47     }
48 
49     _journal->endTransaction();
50 }
51 
watch(const char * folder)52 POOLMEM *BackupService::watch(const char *folder)
53 {
54     POOLMEM *err_msg;
55     char *fpath = bstrdup(folder);
56     CDP::winToUnixPath(fpath);
57     FolderWatcher *w = new FolderWatcher(this);
58     err_msg = w->watch(fpath);
59 
60     if (err_msg == NULL) {
61         FolderRecord rec;
62         rec.path = fpath;
63         _journal->writeFolderRecord(rec);
64         _watchers[rec.path] = w;
65     } else {
66         Dmsg2(0, "Error while trying to watch %s. %s", fpath, err_msg);
67         delete w;
68     }
69 
70     return err_msg;
71 }
72 
stopWatching(const char * folder)73 void BackupService::stopWatching(const char *folder)
74 {
75     _journal->removeFolderRecord(folder);
76     delete _watchers[folder];
77     _watchers[folder] = NULL;
78 }
79 
onChange(const char * fpath)80 void BackupService::onChange(const char *fpath)
81 {
82     char *tmp_fpath = bstrdup(fpath);
83     char *fname = basename(tmp_fpath);
84     FileRecord rec;
85     rec.name = bstrdup(fpath);
86     POOLMEM *spoolFilename = get_pool_memory(PM_FNAME);
87 
88     if (strcmp(fpath, _journal->_jPath) == 0) {
89         Dmsg0(0, "Change on Journal File ignored.\n");
90         goto bail_out;
91     }
92 
93     if (!rec.encode_attrs()) {
94         goto bail_out;
95     }
96 
97     if (rec.mtime - _last_mtime[fpath] < 5) {
98         Dmsg3(50, "File %s: current mtime: %d. Previous mtime: %d. Ignoring\n",
99               rec.name,
100               rec.mtime,
101               _last_mtime[fpath]);
102         goto bail_out;
103     }
104 
105     _last_mtime[fpath] = rec.mtime;
106     char str_mtime[50];
107     edit_int64(rec.mtime, str_mtime);
108     Mmsg(spoolFilename, "%s/%s_%s", _spoolPath, str_mtime, fname);
109 
110     if (copyfile(fpath, spoolFilename) == 0) {
111         Dmsg2(0, "Copied file %s into %s\n", fpath, spoolFilename);
112         rec.sname = bstrdup(spoolFilename);
113         _journal->writeFileRecord(rec);
114     } else {
115         Dmsg2(0, "Could not copy file %s into %s\n", fpath, spoolFilename);
116     }
117 
118 bail_out:
119     free(tmp_fpath);
120     free_and_null_pool_memory(spoolFilename);
121 }
122 
~BackupService()123 BackupService::~BackupService()
124 {
125     //Delete all Watchers
126     std::map<std::string, FolderWatcher *>::iterator it;
127     for(it = _watchers.begin(); it != _watchers.end(); it++) {
128         delete it->second;
129     }
130 }
131