whyclwt
2022-05-18 ab72c6e114d7c4e83564194367ba623ca23bf09d
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
#include "filemanage.h"
#include <QApplication>
#include <QTimer>
#include <QFile>
#include <QIODevice>
#include <QDirIterator>
#include "Common/app_define.h"
#include "unistd.h"
#include <sys/vfs.h>
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
FileManage::~FileManage()
{
    Run_CP_EN = false;
    Run_RM_EN = false;
    wait();
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
bool FileManage::removeDir(const QString &fromDir)
{
    QDir dir(fromDir);
    if(!dir.exists())
        return true;
 
    dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
    QFileInfoList fileList = dir.entryInfoList();
    foreach (QFileInfo fi, fileList)
    {
        if (fi.isFile())
            fi.dir().remove(fi.fileName());
        else
            removeDir(fi.absoluteFilePath());
    }
 
    return dir.rmdir(dir.absolutePath());
}
//----------------------------------------------------------------------------------------------
bool FileManage::removeFile(const QString &fromDir, const QString &fromFile)
{
    QDir sourceDir(fromDir);
    if(!sourceDir.remove(fromFile))
        return false;
    return true;
}
 
void FileManage::removeFile(QString &fname)
{
    char cmd[1024];
 
    sprintf(cmd, "rm  \"%s\"",fname.toLocal8Bit().data());
    //qDebug(cmd);
    system(cmd);
    system("sync");
}
 
//----------------------------------------------------------------------------------------------
QStringList FileManage::getDirs()
{
    QStringList list;
    QDir sourceDir(dataDir);
    sourceDir.setSorting(QDir::Time);
    QFileInfoList fileInfoList = sourceDir.entryInfoList();
    foreach (QFileInfo fileInfo, fileInfoList)
    {
        if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
            continue;
        if(fileInfo.isDir())
        {
            list.append(fileInfo.fileName());
        }
    }
    return list;
}
//----------------------------------------------------------------------------------------------
QStringList FileManage::getFiles(const QString &fromDir)
{
    QStringList list;
    QDir sourceDir(fromDir);
    sourceDir.setSorting(QDir::Time);
    QFileInfoList fileInfoList = sourceDir.entryInfoList();
    foreach (QFileInfo fileInfo, fileInfoList)
    {
        if(fileInfo.isFile() && fileInfo.fileName().endsWith(testFileType))
        {
            list.append(fileInfo.fileName());
        }
    }
 
    return list;
}
//----------------------------------------------------------------------------------------------
int FileManage::GetFileCount(const QString &fromDir)
{
    int seedcount = 0;
    QDir sourceDir(fromDir);
    QFileInfoList fileInfoList = sourceDir.entryInfoList();
    foreach(QFileInfo fileInfo, fileInfoList)
    {
        if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
            continue;
 
        if(fileInfo.isDir()) // 当为目录时,递归的进行copy //
        {
            seedcount += GetFileCount(fileInfo.filePath());
        }
        else
        {
            seedcount += 1;
        }
    }
 
    return seedcount;
}
//----------------------------------------------------------------------------------------------
int FileManage::GetFileCountFromDirs(const QStringList &fromDir)
{
    int seedcount = 0;
    for(int n=0; n<fromDir.count(); n++)
    {
        seedcount += GetFileCount(fromDir.at(n));
    }
 
    return seedcount;
}
//----------------------------------------------------------------------------------------------
void FileManage::copyFile(const QString &fromDir, const QString &toDir)
{
    QDir targetDir(toDir);
    char cmd[1024];
    QString path_from,path_to;
    targetDir.mkpath(targetDir.absolutePath());
    //path_from = "\"" + fromDir + "/" + "\"";
    path_from = "\"" + fromDir + "\"";
    path_to = "\"" + toDir + "/" + "\"";
    //sprintf(cmd, "rm -r %s",
    //        path_to.toLocal8Bit().data());
    //system(cmd);
    sprintf(cmd, "cp -r %s %s",
             path_from.toLocal8Bit().data(),
            path_to.toLocal8Bit().data());
    system(cmd);
    system("sync");
    //progressValue(100);
    //progress = 100;
    //emit progressValue(progress);
}
//----------------------------------------------------------------------------------------------
bool FileManage::copyDirectoryFiles(const QStringList &fromDir, const QString &toDir,
                                    bool coverFileIfExist)
{
    Run_CP_EN = true;
    Run_RM_EN = false;
 
    from_Dir = fromDir;
    to_Dir = toDir;
 
    (void)coverFileIfExist;
 
    this->start();
 
    return true;
}
 
bool FileManage::deleteDirectoryFiles(const QStringList &fromDir)
{
    Run_RM_EN = true;
    Run_CP_EN = false;
 
    from_Dir = fromDir;
 
    this->start();
 
    return true;
}
//----------------------------------------------------------------------------------------------
bool FileManage::clearDirectoryFiles(const QString &fromDir, const QStringList &fileList)
{
    for(int n=0; n<fileList.count(); n++)
    {
        removeFile(fromDir, fileList.at(n));
    }
    return true;
}
 
int FileManage::FindLocalFileFromPath(const QString &strFilePath, const QString filename)
{
    int filecnt = 0;
 
    if (strFilePath.isEmpty() || filename.isEmpty())
    {
        return filecnt;
    }
 
    //qDebug(strFilePath.toLocal8Bit());
    //qDebug(filename.toLocal8Bit());
 
    QDir dir;
    QStringList filters;
 
    filters << filename;//过滤条件,可以添加多个选项,可以匹配文件后缀等。我这里只是找指定文件
    dir.setPath(strFilePath);
    dir.setNameFilters(filters);//添加过滤器
    //QDirIterator 此类可以很容易的返回指定目录的所有文件及文件夹,可以再递归遍历,也可以自动查找指定的文件
    QDirIterator iter(dir,QDirIterator::Subdirectories);
    while (iter.hasNext())
    {
        iter.next();
        QFileInfo info=iter.fileInfo();
        if (info.isFile())
        {
            filecnt++;
        }
    }
 
    return filecnt;
}
 
bool FileManage::CheckDiskState(const QString &disk)
{
    struct statfs st;
    statfs(disk.toLocal8Bit().data(), &st);
    if(19780 == st.f_type)
    {
        return true;
    }
 
    return false;
}
 
//----------------------------------------------------------------------------------------------
void FileManage::run()
{
    //Run_EN = true;
 
    int p_v = 0;
    QString fn;
 
    while(true == Run_CP_EN)
    {
        //qDebug("Run_CP_EN is true");
 
        for(int n=0; n<from_Dir.count(); n++)
        {
            if(false == CheckDiskState(usbDir))
            {
                //p_v = from_Dir.count()+1;
 
                qDebug("usb not exists.");
 
                emit progressValue(65535);
 
                break;
            }
 
            copyFile(from_Dir.at(n), to_Dir);
 
            fn = from_Dir.at(n);
            fn = fn.remove(0,testDataDir.length());
            fn = to_Dir+fn;
            //qDebug(fn.toLocal8Bit());
 
            p_v = n+1;
 
            if(!(QFile(fn).exists()))
            {
                //p_v = from_Dir.count()+1;
 
                qDebug("file not exists.");
 
                emit progressValue(65535);
 
                break;
            }
            else
            {
                //qDebug("file exists.");
                emit progressValue(p_v);
            }
        }
 
        Run_CP_EN = false;
    }
 
    while(true == Run_RM_EN)
    {
        //qDebug("Run_RM_EN is true");
 
        for(int n=0; n<from_Dir.count(); n++)
        {
            QString fn = from_Dir.at(n);
 
            removeFile(fn);
 
            p_v = n+1;
 
            emit progressValue(p_v);
        }
 
        Run_RM_EN = false;
    }
}
//----------------------------------------------------------------------------------------------