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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*
* ark -- archiver for the KDE project
*
* Copyright (C) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
*
* This program 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.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "karchiveplugin.h"
#include "kerfuffle/queries.h"
#include <KZip>
#include <KTar>
#include <KMimeType>
#include <KDebug>
#include <KLocale>
#include <QDir>
#include <QFileInfo>
#include <QSet>
KArchiveInterface::KArchiveInterface(QObject *parent, const QVariantList &args)
: ReadWriteArchiveInterface(parent, args), m_archive(0)
{
kDebug();
}
KArchiveInterface::~KArchiveInterface()
{
delete m_archive;
m_archive = 0;
}
KArchive *KArchiveInterface::archive()
{
if (m_archive == 0) {
KMimeType::Ptr mimeType = KMimeType::findByPath(filename());
if (mimeType->is(QLatin1String("application/zip"))) {
m_archive = new KZip(filename());
} else {
m_archive = new KTar(filename());
}
}
return m_archive;
}
bool KArchiveInterface::list()
{
kDebug();
if (!archive()->isOpen() && !archive()->open(QIODevice::ReadOnly)) {
emit error(i18nc("@info", "Could not open the archive <filename>%1</filename> for reading", filename()));
return false;
} else {
return browseArchive(archive());
}
}
void KArchiveInterface::getAllEntries(const KArchiveDirectory *dir, const QString &prefix, QList< QVariant > &list)
{
foreach(const QString &entryName, dir->entries()) {
const KArchiveEntry *entry = dir->entry(entryName);
if (entry->isDirectory()) {
QString newPrefix = (prefix.isEmpty() ? prefix : prefix + QLatin1Char('/')) + entryName;
getAllEntries(static_cast<const KArchiveDirectory*>(entry), newPrefix, list);
}
else {
list.append(prefix + QLatin1Char('/') + entryName);
}
}
}
bool KArchiveInterface::copyFiles(const QList<QVariant> &files, const QString &destinationDirectory, ExtractionOptions options)
{
const bool preservePaths = options.value(QLatin1String("PreservePaths")).toBool();
const KArchiveDirectory *dir = archive()->directory();
if (!archive()->isOpen() && !archive()->open(QIODevice::ReadOnly)) {
emit error(i18nc("@info", "Could not open the archive <filename>%1</filename> for reading", filename()));
return false;
}
QList<QVariant> extrFiles = files;
if (extrFiles.isEmpty()) { // All files should be extracted
getAllEntries(dir, QString(), extrFiles);
}
bool overwriteAllSelected = false;
bool autoSkipSelected = false;
QSet<QString> dirCache;
foreach(const QVariant &file, extrFiles) {
QString realDestination = destinationDirectory;
const KArchiveEntry *archiveEntry = dir->entry(file.toString());
if (!archiveEntry) {
emit error(i18nc("@info", "File <filename>%1</filename> not found in the archive" , file.toString()));
return false;
}
if (preservePaths) {
QFileInfo fi(file.toString());
QDir dest(destinationDirectory);
QString filepath = archiveEntry->isDirectory() ? fi.filePath() : fi.path();
if (!dirCache.contains(filepath)) {
if (!dest.mkpath(filepath)) {
emit error(i18nc("@info", "Error creating directory <filename>%1</filename>", filepath));
return false;
}
dirCache << filepath;
}
realDestination = dest.absolutePath() + QLatin1Char('/') + filepath;
}
// TODO: handle errors, copyTo fails silently
if (!archiveEntry->isDirectory()) { // We don't need to do anything about directories
if (QFile::exists(realDestination + QLatin1Char('/') + archiveEntry->name()) && !overwriteAllSelected) {
if (autoSkipSelected) {
continue;
}
int response = handleFileExistsMessage(realDestination, archiveEntry->name());
if (response == OverwriteCancel) {
break;
}
if (response == OverwriteYes || response == OverwriteAll) {
static_cast<const KArchiveFile*>(archiveEntry)->copyTo(realDestination);
if (response == OverwriteAll) {
overwriteAllSelected = true;
}
}
if (response == OverwriteAutoSkip) {
autoSkipSelected = true;
}
}
else {
static_cast<const KArchiveFile*>(archiveEntry)->copyTo(realDestination);
}
}
}
return true;
}
int KArchiveInterface::handleFileExistsMessage(const QString &dir, const QString &fileName)
{
Kerfuffle::OverwriteQuery query(dir + QLatin1Char('/') + fileName);
query.setNoRenameMode(true);
emit userQuery(&query);
query.waitForResponse();
if (query.responseOverwrite()) {
return OverwriteYes;
} else if (query.responseSkip()) {
return OverwriteSkip;
} else if (query.responseOverwriteAll()) {
return OverwriteAll;
} else if (query.responseAutoSkip()) {
return OverwriteAutoSkip;
}
return OverwriteCancel;
}
bool KArchiveInterface::browseArchive(KArchive *archive)
{
return processDir(archive->directory());
}
bool KArchiveInterface::processDir(const KArchiveDirectory *dir, const QString & prefix)
{
foreach(const QString& entryName, dir->entries()) {
const KArchiveEntry *entry = dir->entry(entryName);
createEntryFor(entry, prefix);
if (entry->isDirectory()) {
QString newPrefix = (prefix.isEmpty() ? prefix : prefix + QLatin1Char('/')) + entryName;
processDir(static_cast<const KArchiveDirectory*>(entry), newPrefix);
}
}
return true;
}
void KArchiveInterface::createEntryFor(const KArchiveEntry *aentry, const QString& prefix)
{
ArchiveEntry e;
QString fileName = prefix.isEmpty() ? aentry->name() : prefix + QLatin1Char('/') + aentry->name();
if (aentry->isDirectory() && !fileName.endsWith(QLatin1Char('/')))
fileName += QLatin1Char('/');
e[ FileName ] = fileName;
e[ InternalID ] = e[ FileName ];
e[ Permissions ] = permissionsString(aentry->permissions());
e[ Owner ] = aentry->user();
e[ Group ] = aentry->group();
e[ IsDirectory ] = aentry->isDirectory();
e[ Timestamp ] = aentry->datetime();
if (!aentry->symLinkTarget().isEmpty()) {
e[ Link ] = aentry->symLinkTarget();
}
if (aentry->isFile()) {
e[ Size ] = static_cast<const KArchiveFile*>(aentry)->size();
}
else {
e[ Size ] = 0;
}
emit entry(e);
}
bool KArchiveInterface::addFiles(const QStringList &files, const Kerfuffle::CompressionOptions &options)
{
Q_UNUSED(options)
kDebug() << "Starting...";
// delete m_archive;
// m_archive = 0;
if (archive()->isOpen()) {
archive()->close();
}
if (!archive()->open(QIODevice::ReadWrite)) {
emit error(i18nc("@info", "Could not open the archive <filename>%1</filename> for writing.", filename()));
return false;
}
kDebug() << "Archive opened for writing...";
kDebug() << "Will add " << files.count() << " files";
foreach(const QString &path, files) {
kDebug() << "Adding " << path;
QFileInfo fi(path);
Q_ASSERT(fi.exists());
if (fi.isDir()) {
if (archive()->addLocalDirectory(path, fi.fileName())) {
const KArchiveEntry *entry = archive()->directory()->entry(fi.fileName());
createEntryFor(entry, QString());
processDir((KArchiveDirectory*) archive()->directory()->entry(fi.fileName()), fi.fileName());
} else {
emit error(i18nc("@info", "Could not add the directory <filename>%1</filename> to the archive", path));
return false;
}
} else {
if (archive()->addLocalFile(path, fi.fileName())) {
const KArchiveEntry *entry = archive()->directory()->entry(fi.fileName());
createEntryFor(entry, QString());
} else {
emit error(i18nc("@info", "Could not add the file <filename>%1</filename> to the archive.", path));
return false;
}
}
}
kDebug() << "Closing the archive";
archive()->close();
kDebug() << "Done";
return true;
}
bool KArchiveInterface::deleteFiles(const QList<QVariant> & files)
{
Q_UNUSED(files)
return false;
}
// Borrowed and adapted from KFileItemPrivate::parsePermissions.
QString KArchiveInterface::permissionsString(mode_t perm)
{
static char buffer[ 12 ];
char uxbit,gxbit,oxbit;
if ( (perm & (S_IXUSR|S_ISUID)) == (S_IXUSR|S_ISUID) )
uxbit = 's';
else if ( (perm & (S_IXUSR|S_ISUID)) == S_ISUID )
uxbit = 'S';
else if ( (perm & (S_IXUSR|S_ISUID)) == S_IXUSR )
uxbit = 'x';
else
uxbit = '-';
if ( (perm & (S_IXGRP|S_ISGID)) == (S_IXGRP|S_ISGID) )
gxbit = 's';
else if ( (perm & (S_IXGRP|S_ISGID)) == S_ISGID )
gxbit = 'S';
else if ( (perm & (S_IXGRP|S_ISGID)) == S_IXGRP )
gxbit = 'x';
else
gxbit = '-';
if ( (perm & (S_IXOTH|S_ISVTX)) == (S_IXOTH|S_ISVTX) )
oxbit = 't';
else if ( (perm & (S_IXOTH|S_ISVTX)) == S_ISVTX )
oxbit = 'T';
else if ( (perm & (S_IXOTH|S_ISVTX)) == S_IXOTH )
oxbit = 'x';
else
oxbit = '-';
// Include the type in the first char like kde3 did; people are more used to seeing it,
// even though it's not really part of the permissions per se.
if (S_ISDIR(perm))
buffer[0] = 'd';
else if (S_ISLNK(perm))
buffer[0] = 'l';
else
buffer[0] = '-';
buffer[1] = ((( perm & S_IRUSR ) == S_IRUSR ) ? 'r' : '-' );
buffer[2] = ((( perm & S_IWUSR ) == S_IWUSR ) ? 'w' : '-' );
buffer[3] = uxbit;
buffer[4] = ((( perm & S_IRGRP ) == S_IRGRP ) ? 'r' : '-' );
buffer[5] = ((( perm & S_IWGRP ) == S_IWGRP ) ? 'w' : '-' );
buffer[6] = gxbit;
buffer[7] = ((( perm & S_IROTH ) == S_IROTH ) ? 'r' : '-' );
buffer[8] = ((( perm & S_IWOTH ) == S_IWOTH ) ? 'w' : '-' );
buffer[9] = oxbit;
buffer[10] = 0;
return QString::fromLatin1(buffer);
}
KERFUFFLE_EXPORT_PLUGIN(KArchiveInterface)
|