-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainLoop.cpp
154 lines (131 loc) · 4.55 KB
/
MainLoop.cpp
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
/*
Copyright 2015-2016 Laurent Claessens
contact : [email protected]
This is part of 'lora': you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
//*/
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp> // starts_with
#include "MainLoop.h"
#include "Configuration.h"
#include "DirectoryConverter.h"
#include "tasks.h"
// MAIN LOOP ----
MainLoop::MainLoop(Configuration* c_ptr) :
config_ptr(c_ptr)
{
config_ptr->create_purge_directories();
assert( config_ptr->are_all_paths_ok() );
}
void MainLoop::run()
{
assert( is_directory(getStartingPath()) );
loopOverDirectory(getStartingPath());
}
void MainLoop::loopOverDirectory(path directory)
{
assert(is_directory(directory));
directory_iterator end_itr;
for( directory_iterator itr(directory); itr!=end_itr;++itr )
{
path pathname=itr->path();
if (is_symlink(pathname))
{
config_ptr->writeLog("I do not deal with symlink : "+ pathname.string());
}
else if (is_directory( pathname )) { DealWithDirectory(pathname); }
else if (is_regular_file(pathname)) { DealWithFile(pathname); }
else
{
config_ptr->writeLog("In the directory "+directory.string());
config_ptr->writeLog("The stuff +"+pathname.string()+" seems strange.");
config_ptr->writeLog("I'm not going to enter inside.");
}
}
}
// MAIN BACKUP LOOP ----
MainBackupLoop::MainBackupLoop(Configuration* config_ptr) :
MainLoop(config_ptr) { }
const path MainBackupLoop::getStartingPath() const
{
return config_ptr->getStartingBackupPath();
}
void MainBackupLoop::run()
{
std::cout<<"Launching the backup loop."<<std::endl;
this->MainLoop::run();
std::cout<<"Backup loop ended."<<std::endl;
}
bool MainBackupLoop::is_excluded(path dirname) { return config_ptr->is_excluded(dirname); }
void MainBackupLoop::DealWithDirectory(path rep_path)
{
if (!is_excluded(rep_path))
{
path bak_rep=config_ptr->home_to_backup(rep_path);
if (!is_directory(bak_rep))
{
Utilities(config_ptr).create_directory_tree(bak_rep);
}
loopOverDirectory(rep_path);
}
}
void MainBackupLoop::DealWithFile(path file_path)
{
assert( is_regular_file(file_path) );
const path bak_path=config_ptr->home_to_backup(file_path);
const path purge_modified_path=config_ptr->home_to_modified_purge(file_path);
if (config_ptr->do_we_backup(file_path,bak_path))
{
// This assert checks that we will not write in the home directory.
assert( !boost::algorithm::starts_with(bak_path,config_ptr->getHomePath() ) );
pathTriple triple;
triple.orig=file_path;
triple.bak=bak_path;
triple.purge=purge_modified_path;
FileCopyTask* ftask= new FileCopyTask(triple,config_ptr);
config_ptr->add_task(ftask);
}
}
// MAIN PURGE LOOP ----
MainPurgeLoop::MainPurgeLoop(Configuration* config_ptr) : MainLoop(config_ptr),starting_path(config_ptr->getPurgePath()) {}
void MainPurgeLoop::run()
{
std::cout<<"Launching the purge loop."<<std::endl;
this->MainLoop::run();
std::cout<<"Purge loop ended."<<std::endl;
FinalTask* etask= new FinalTask(config_ptr);
config_ptr->add_task(etask);
}
const path MainPurgeLoop::getStartingPath() const
{
return config_ptr->getBackupPath();
}
void MainPurgeLoop::DealWithFile(const path pathname)
{
if(!is_regular_file(config_ptr->backup_to_home(pathname) ))
{
FileMoveTask* mtask= new FileMoveTask(pathname, config_ptr->backup_to_removed_purge(pathname),config_ptr );
config_ptr->add_task(mtask);
}
}
void MainPurgeLoop::DealWithDirectory(const path backup_path)
{
if (!is_directory( config_ptr->backup_to_home(backup_path) ))
{
DirectoryMoveTask* dtask= new DirectoryMoveTask(backup_path, config_ptr->backup_to_removed_purge(backup_path),config_ptr );
config_ptr->add_task(dtask);
}
else
{
loopOverDirectory(backup_path);
}
}