Repository navigation
Expand file tree
/
Copy pathutil.cpp
More file actions
279 lines (249 loc) · 6.61 KB
/
Copy pathutil.cpp
File metadata and controls
279 lines (249 loc) · 6.61 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "util.h"
#include <QSettings>
#include <stdio.h>
#include <QRegExp>
#include <QDebug>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include "const_define.h"
#include <sys/stat.h>
#include <map>
#include <QDateTime>
#include <fcntl.h>
#include <const_define.h>
#include <QDir>
#include <sys/sendfile.h>
typedef std::multimap<time_t, std::string> result_set_t;
Util::Util()
{
}
void Util::list_ports()
{
vector<serial::PortInfo> devices_found = serial::list_ports();
vector<serial::PortInfo>::iterator iter = devices_found.begin();
while( iter != devices_found.end() )
{
serial::PortInfo device = *iter++;
printf( "(%s, %s, %s)\n", device.port.c_str(), device.description.c_str(),
device.hardware_id.c_str() );
}
}
QStringList Util::getLocalFileList()
{
QDir dir(PrePath);
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Time);
QStringList filelist;
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
filelist.push_back(fileInfo.fileName());
}
return filelist;
}
QMap<QString,QString> Util::parseFileList(QString val)
{
QMap<QString,QString> res;
Json::Reader reader;
Json::Value value;
if (reader.parse(val.toStdString(), value, false))
{
std::string aa = value["return_code"].asString();
if(aa.compare("success") == 0)
{
Json::Value array = value["data"];
for(int i=0;i<array.size();i++)
{
Json::Value content = array[i];
res.insert(QString::fromStdString(content["name"].asString()),QString::fromStdString(content["path"].asString()));\
}
}
}
return res;
}
QString Util::readFile(QString path)
{
QFile f(path);
if(!f.open(QIODevice::ReadOnly | QIODevice::Text))
{
return NULL;
}
QTextStream input(&f);
return input.readAll();
}
void Util::fileSync(const char *file)
{
int fd = open( file, O_RDWR);
if(fd != -1)
{
syncfs(fd);
close(fd);
}
}
int Util::get_file_size(FILE *file)
{
int size = 0;
fseek(file, 0L, SEEK_END);
size = ftell(file);
fseek(file, 0L, SEEK_SET);
return size;
}
void Util::cpyFile(const char *src, const char *dst)
{
int read_fd;
int write_fd;
struct stat stat_buf;
off_t offset = 0;
/* Open the input file. */
read_fd = open (src, O_RDONLY);
/* Stat the input file to obtain its size. */
fstat (read_fd, &stat_buf);
/* Open the output file for writing, with the same permissions as the
source file. */
write_fd = open (dst, O_WRONLY | O_CREAT, stat_buf.st_mode);
/* Blast the bytes from one file to the other. */
sendfile (write_fd, read_fd, &offset, stat_buf.st_size);
/* Close up. */
close (read_fd);
close (write_fd);
chmod(dst,S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH|S_IXOTH);
}
QString Util::checkUDiskPath()
{
DIR *dir;
struct dirent *ptr;
dir = opendir(UDISK_PATH_PREFIX); ///open the dir
QString pattern_a("\\.");
QString pattern_b("\\.\\.");
QRegExp rx_a(pattern_a);
QRegExp rx_b(pattern_b);
while((ptr = readdir(dir)) != NULL) ///read the list of this dir
{
if(ptr->d_type == 4)
{
{
if(!rx_a.exactMatch(ptr->d_name))
{
if(!rx_b.exactMatch(ptr->d_name))
{
closedir(dir);
return QString(ptr->d_name);
}
}
}
}
}
closedir(dir);
return NULL;
}
QString Util::checkUpdatePath(QString udiskPath)
{
DIR *dir;
struct dirent *ptr;
dir = opendir(udiskPath.toStdString().c_str()); ///open the dir
if(dir==NULL){
return NULL;
}
QString pattern("PowerSupplyController");
QRegExp rx(pattern);
while((ptr = readdir(dir)) != NULL) ///read the list of this dir
{
//if(ptr->d_type == 4)
{
qDebug()<<ptr->d_name;
if(rx.exactMatch(ptr->d_name))
{
closedir(dir);
return QString(ptr->d_name);
}
}
}
closedir(dir);
return NULL;
}
QString Util::checkFirmWareUpdatePath(QString udiskPath)
{
DIR *dir;
struct dirent *ptr;
dir = opendir(udiskPath.toStdString().c_str()); ///open the dir
if(dir==NULL){
return NULL;
}
QString pattern("Robos0.bin");
QRegExp rx(pattern);
while((ptr = readdir(dir)) != NULL) ///read the list of this dir
{
//if(ptr->d_type == 4)
{
qDebug()<<ptr->d_name;
if(rx.exactMatch(ptr->d_name))
{
closedir(dir);
return QString(ptr->d_name);
}
}
}
closedir(dir);
return NULL;
}
void Util::deleteUnpluedUdiskPath()
{
DIR *dir;
struct dirent *ptr;
dir = opendir(UDISK_PATH_PREFIX);
QString path_pre = QString(UDISK_PATH_PREFIX);
QString pattern_a("\\.");
QString pattern_b("\\.\\.");
QRegExp rx_a(pattern_a);
QRegExp rx_b(pattern_b);
while((ptr = readdir(dir)) != NULL) ///read the list of this dir
{
if(ptr->d_type == 4)
{
if(!rx_a.exactMatch(ptr->d_name))
{
if(!rx_b.exactMatch(ptr->d_name))
{
QString path = QString(path_pre+ptr->d_name);
struct stat buf;
int res = stat(path.toStdString().c_str(), &buf);
if(res == 0)
{
if(buf.st_uid != 1000)
{
QString cmd("sudo rm -rf "+path_pre+"\""+ptr->d_name+"\"");
system(cmd.toStdString().c_str());
}
}
}
}
}
}
closedir(dir);
}
QString Util::checkFirmWareUpdatePath_(QString udiskPath)
{
DIR *dir;
struct dirent *ptr;
dir = opendir(udiskPath.toStdString().c_str()); ///open the dir
if(dir==NULL){
return NULL;
}
QString pattern("Robos1.bin");
QRegExp rx(pattern);
while((ptr = readdir(dir)) != NULL) ///read the list of this dir
{
//if(ptr->d_type == 4)
{
qDebug()<<ptr->d_name;
if(rx.exactMatch(ptr->d_name))
{
closedir(dir);
return QString(ptr->d_name);
}
}
}
closedir(dir);
return NULL;
}