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
|
//===========================================
// Lumina-DE source code
// Copyright (c) 2016, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#include "TarBackend.h"
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QDateTime>
#include <QCoreApplication>
Backend::Backend(QObject *parent) : QObject(parent){
//Setup the backend process
PROC.setProcessChannelMode(QProcess::MergedChannels);
PROC.setProgram("tar");
connect(&PROC, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(procFinished(int, QProcess::ExitStatus)) );
connect(&PROC, SIGNAL(readyReadStandardOutput()), this, SLOT(processData()) );
connect(&PROC, SIGNAL(started()), this, SIGNAL(ProcessStarting()) );
LIST = STARTING = false;
}
Backend::~Backend(){
}
//===============
// PUBLIC
//===============
void Backend::loadFile(QString path){
qDebug() << "void Backend::loadFile(QString path) has started";
qDebug() << "Loading Archive:" << path;
filepath = path;
tmpfilepath = filepath.section("/",0,-2)+"/"+".tmp_larchiver_"+filepath.section("/",-1);
flags.clear();
flags << "-f" << filepath; //add the actual archive path
if(QFile::exists(path)){ startList(); qDebug () << "BACKEND LOAD startList has started";}
else{ contents.clear(); emit ProcessFinished(true, ""); }
}
bool Backend::canModify(){
static QStringList validEXT;
if( validEXT.isEmpty() ){
validEXT << ".zip" << ".tar.gz" << ".tgz" << ".tar.xz" << ".txz" << ".tar.bz" << ".tbz" << ".tar.bz2" << ".tbz2" << ".tar" \
<< ".tar.lzma" << ".tlz" << ".cpio" << ".pax" << ".ar" << ".shar" << ".7z";
}
for(int i=0; i<validEXT.length(); i++){
if(filepath.endsWith(validEXT[i])){ return true; }
}
return false;
}
QString Backend::currentFile(){
return filepath;
}
bool Backend::isWorking(){
return (PROC.state() != QProcess::Running);
}
//Listing routines
QStringList Backend::heirarchy(){
return contents.keys();
}
double Backend::size(QString file){
if(!contents.contains(file)){ return -1; }
return contents.value(file)[1].toDouble();
}
double Backend::csize(QString file){
if(!contents.contains(file)){ return -1; }
return contents.value(file)[1].toDouble();
}
bool Backend::isDir(QString file){
if(!contents.contains(file)){ return false; }
return contents.value(file)[0].startsWith("d");
}
bool Backend::isLink(QString file){
if(!contents.contains(file)){ return false; }
return contents.value(file)[0].startsWith("l");
}
QString Backend::linkTo(QString file){
if(!contents.contains(file)){ return ""; }
return contents.value(file)[2];
}
//Modification routines
void Backend::startAdd(QStringList paths){
//NOTE: All the "paths" have to have the same parent directory
if(paths.contains(filepath)){ paths.removeAll(filepath); }
if(paths.isEmpty()){ return; }
QStringList args;
args << "-c" << "-a";
args << flags;
//Now setup the parent dir
QString parent = paths[0].section("/",0,-2);
for(int i=0; i<paths.length(); i++){
paths[i] = paths[i].section(parent,1,-1);
if(paths[i].startsWith("/")){ paths[i].remove(0,1); }
}
args << "-C" << parent;
args << paths;
if(QFile::exists(filepath)){ //append to existing
args.replaceInStrings(filepath, tmpfilepath);
args<< "@"+filepath;
}
STARTING=true;
PROC.start("tar", args);
}
void Backend::startRemove(QStringList paths){
if(paths.contains(filepath)){ paths.removeAll(filepath); }
if(contents.isEmpty() || paths.isEmpty() || !QFile::exists(filepath)){ return; } //invalid
QStringList args;
args << "-c" << "-a";
args << flags;
args.replaceInStrings(filepath, tmpfilepath);
//Add the include rules for all the files we want to keep (no exclude option in "tar")
for(int i=0; i<paths.length(); i++){
args << "--exclude" << paths[i];
}
args<< "@"+filepath;
STARTING=true;
PROC.start("tar", args);
}
void Backend::startExtract(QString path, bool overwrite, QString file){
startExtract(path, overwrite, QStringList() << file); //overload for multi-file function
}
void Backend::startExtract(QString path, bool overwrite, QStringList files){
QStringList args; //this is a new/empty list - no need for the check below (KPM)
//remove --ax arg if its still lingering so its not passed to external process
//for(int i=0; i<args.length(); i++){ if(args[i]=="--ax"){ args.removeAt(i);}}
args << "-x" << "--no-same-owner";
if(!overwrite){ args << "-k"; }
args << flags;
for(int i=0; i<files.length(); i++){
if(files[i].simplified().isEmpty()){ continue; }
args << "--include" << files[i] << "--strip-components" << QString::number(files[i].count("/"));
}
args << "-C" << path;
STARTING=true;
//qDebug() << "Starting command:" << "tar" << args;
PROC.start("tar", args);
}
void Backend::startViewFile(QString path){
QStringList args;
QString newfilename = QDateTime::currentDateTime().toString("yyyyMMddhhmmss")+"-"+path.section("/",-1);
args << "-x";
//args << flags <<"--include" << path <<"--strip-components" << QString::number(path.count("/")) << "-C" << QDir::tempPath();
args << flags <<"--include" << path <<"--to-stdout";
STARTING=true;
//qDebug() << "Starting command:" << "tar" << args;
emit ProcessStarting();
QProcess tmpProc;
tmpProc.setStandardOutputFile(newfilename);
tmpProc.start("tar",args);
while(!tmpProc.waitForFinished(500)){ QCoreApplication::processEvents(); }
emit ProcessFinished(tmpProc.exitCode()==0, "");
QProcess::startDetached("xdg-open", QStringList() << newfilename);
//PROC.start("tar", args);
}
//===============
// PUBLIC SLOTS
//===============
//===============
// PRIVATE
//===============
void Backend::parseLines(QStringList lines){
for(int i=0; i<lines.length(); i++){
QStringList info = lines[i].split(" ",QString::SkipEmptyParts);
//Format: [perms, ?, user, group, size, month, day, time, file]
if(info.startsWith("x ") && filepath.endsWith(".zip")){
//ZIP archives do not have all the extra information - just filenames
while(info.length()>2){ info[1]=info[1]+" "+info[2]; }
QString file = info[1];
QString perms = "";
if(file.endsWith("/")){ perms = "d"; file.chop(1); }
contents.insert(file, QStringList() << perms << "-1" <<""); //Save the [perms, size, linkto ]
}
else if(info.length()<9){ continue; } //invalid line
//TAR Archive parsing
while(info.length()>9){ info[8] = info[8]+" "+info[9]; info.removeAt(9); } //Filename has spaces in it
QString file = info[8];
if(file.endsWith("/")){ file.chop(1); }
QString linkto;
//See if this file has the "link to" or "->" notation
if(file.contains(" -> ")){ linkto = file.section(" -> ",1,-1); file = file.section(" -> ",0,0); }
else if(file.contains(" link to ")){
//Special case - alternate form of a link within a tar archive (not reflected in perms)
linkto = file.section(" link to ",1,-1);
file = file.section(" link to ",0,0);
if(info[0].startsWith("-")){ info[0].replace(0,1,"l"); }
}
contents.insert(file, QStringList() << info[0] << info[4] << linkto); //Save the [perms, size, linkto ]
}
}
void Backend::startList(){
contents.clear();
QStringList args;
args << "-tv";
LIST = STARTING=true;
//qDebug() << "Starting List:" << "tar "+args.join(" ")+" "+flags.join(" ");
PROC.start("tar", QStringList() << args << flags);
}
//===============
// PRIVATE SLOTS
//===============
void Backend::procFinished(int retcode, QProcess::ExitStatus){
static QString result;
//qDebug() << "Process Finished:" << PROC.arguments() << retcode;
processData();
LIST = STARTING = false;
if(PROC.arguments().contains("-tv")){
if(retcode!=0){ //could not read archive
contents.clear();
result = tr("Could not read archive");
}else if(result.isEmpty()){
result = tr("Archive Loaded");
emit FileLoaded();
}
emit ProcessFinished((retcode==0), result);
result.clear();
}else{
bool needupdate = true;
QStringList args = PROC.arguments();
if(args.contains("-x") && retcode==0){
needupdate=false;
if(args.count("--include")==1){
//Need to find the full path to the (single) extracted file
QString path = args.last() +"/"+ args[ args.indexOf("--include")+1].section("/",-1);
QFile::setPermissions(path, QFileDevice::ReadOwner);
QProcess::startDetached("xdg-open \""+path+"\"");
}else{
//Multi-file extract - open the dir instead
QString dir = args.last();
//Check to see if tar extracted into a new subdir it just created
if(QFile::exists(dir+"/"+filepath.section("/",-1).section(".",0,0) ) ){
dir = dir+"/"+filepath.section("/",-1).section(".",0,0);
}
QProcess::startDetached("xdg-open \""+ dir+"\""); //just extracted to a dir - open it now
}
}else if(args.contains("-c") && QFile::exists(tmpfilepath)){
if(retcode==0){
QFile::remove(filepath);
QFile::rename(tmpfilepath, filepath);
}else{
QFile::remove(tmpfilepath);
}
}
if(args.contains("-x")){ result = tr("Extraction Finished"); emit ExtractSuccessful(); }
//if(args.contains("-aa")){ result = tr("Archival Finished"); emit ArchivalSuccessful(); }
else if(args.contains("-c")){ result = tr("Modification Finished"); }
if(needupdate){ startList(); }
else{ emit ProcessFinished(retcode==0, result); result.clear(); }
}
}
void Backend::processData(){
//Read the process
static QString data;
QString read = data+PROC.readAllStandardOutput();
if(read.endsWith("\n")){ data.clear(); }
else{ data = read.section("\n",-1); read = read.section("\n",0,-2); }
QStringList lines = read.split("\n",QString::SkipEmptyParts);
QString info;
if(LIST){ parseLines(lines); }
else if(!lines.isEmpty()){ info = lines.last(); }
//qDebug() << lines;
emit ProgressUpdate(-1, info);
}
|