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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
/*
Copyright © 2015-2016 Gluzskiy Alexandr (sss)
This file is part of Unknown Download Manager (UDM).
UDM is free software: 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 2 of the License, or
(at your option) any later version.
UDM 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 UDM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "main.h"
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/bind.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
std::string replace_home_var(const std::string &path)
{
std::string home = getenv("HOME"), tmp_path = path;
for(std::string::size_type p1 = tmp_path.find("~"); p1 != std::string::npos; p1 = tmp_path.find("~", p1))
tmp_path.replace(p1, 1, home);
return tmp_path;
}
void storage_impl::on_modules_loaded()
{
//just checking if we working fine via core
//test set
/* {
std::string d = "some string\ndfjoghdfyigehygerityertoweuyrtghyuewitgweyuitgweiurtgeiutrerytiewgytyewuirtgewrgyutygergytiu";
std::vector<char> data;
for(auto i = d.begin(), end = d.end(); i != end; ++i)
{
data.push_back(*i);
}
api->metadata_set(this, "test_setting", data);
} */
//test get
/* {
std::vector<char> data;
api->metadata_get(this, "test_setting", data);
std::string s;
for(auto i = data.begin(), end = data.end(); i != end; ++i)
{
s.push_back(*i);
}
std::cout<<"we have read:\n"<<s<<std::endl;
} */
//test remove
/* {
api->metadata_remove(this, "test_setting");
} */
}
void storage_impl::load(core_api *a)
{
api = a;
info.name = "flat_files_metadata";
info.description = "this module provide metadata storage in flat files";
info.version = "0.0.0.1draft";
info.default_settings["data_path"].value = "~/.local/share/udm/metadata";
info.on_modules_loaded = boost::bind(&storage_impl::on_modules_loaded, this); //optional definition of function which is called after all modules loaded
//std::cout<<"flat_files metadata module succesfully loaded\n"; //working fine
}
const module_info_base &storage_impl::get_module_info()
{
return info;
}
void storage_impl::set_module_settings(const std::map<std::string, setting_s> &settings)
{
this->settings = settings;
parsed_data_path = replace_home_var(this->settings["data_path"].value);
}
bool storage_impl::set(const std::string &module_name, const std::string &setting_name, const std::vector<char> &data)
{
//print data
/* std::cout<<"printing data in metadata_flat_files set api:\n";
for(auto i = data.begin(), end = data.end(); i != end; ++i)
{
std::cout<<*i;
}
std::cout<<std::endl; */
std::string out_file_path = parsed_data_path;
out_file_path += "/";
out_file_path += module_name;
if(!boost::filesystem::exists(out_file_path))
{
//TODO: validation
boost::filesystem::create_directories(out_file_path);
}
else if(!boost::filesystem::is_directory(out_file_path))
{
//TODO: handle error
return false;
}
out_file_path += "/";
out_file_path += setting_name;
std::ofstream os(out_file_path, std::ios::binary);
boost::archive::binary_oarchive ar(os);
ar<<data;
return true;
}
bool storage_impl::get(const std::string &module_name, const std::string &setting_name, std::vector<char> &data)
{
std::string in_file_path = parsed_data_path;
in_file_path += "/";
in_file_path += module_name;
in_file_path += "/";
in_file_path += setting_name;
if(!boost::filesystem::exists(in_file_path) || !boost::filesystem::is_regular(in_file_path))
{
//data does not exists
return false;
}
std::ifstream is(in_file_path, std::ios::binary);
boost::archive::binary_iarchive ar(is);
ar>>data;
return true;
}
bool storage_impl::remove(const std::string &module_name, const std::string &setting_name)
{
//TODO
std::string file_path = parsed_data_path;
file_path += "/";
file_path += module_name;
file_path += "/";
file_path += setting_name;
if(!boost::filesystem::exists(file_path) || !boost::filesystem::is_regular(file_path))
{
return false;
}
boost::filesystem::remove(file_path);
return true;
}
std::list<std::string> storage_impl::setting_list(const std::string &module_name)
{
std::list<std::string> l;
std::string settings_dir = parsed_data_path;
settings_dir += "/";
settings_dir += module_name;
if(boost::filesystem::exists(settings_dir) && boost::filesystem::is_directory(settings_dir))
{
for(auto i = boost::filesystem::directory_iterator(settings_dir), end = boost::filesystem::directory_iterator(); i != end; ++i)
{
l.push_back(i->path().string().substr(settings_dir.length() + 1));
}
}
return l;
}
storage_impl::storage_impl()
{
}
extern "C" module_metadata_storage* udm_metadata_module_load()
{
return new storage_impl;
}
|