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
|
/*
* 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 <KZip>
#include <KTar>
#include <KMimeType>
#include <KDebug>
#include <KLocale>
#include <QDir>
#include <QFileInfo>
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("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)) {
error(i18nc("@info", "Could not open the archive <filename>%1</filename> for reading", filename()));
return false;
} else {
return browseArchive(archive());
}
}
bool KArchiveInterface::copyFiles(const QList<QVariant> &files, const QString &destinationDirectory, ExtractionOptions options)
{
const bool preservePaths = options.value(QLatin1String("PreservePaths")).toBool();
if (!archive()->isOpen() && !archive()->open(QIODevice::ReadOnly)) {
error(i18nc("@info", "Could not open the archive <filename>%1</filename> for reading", filename()));
return false;
}
foreach(const QVariant &file, files) {
QString realDestination = destinationDirectory;
const KArchiveEntry *archiveEntry = archive()->directory()->entry(file.toString());
if (!archiveEntry) {
error(i18nc("@info", "File <filename>%1</filename> not found in the archive" , file.toString()));
return false;
}
// TODO: handle errors, copyTo fails silently
if (preservePaths) {
QFileInfo fi(file.toString());
QDir dest(destinationDirectory);
QString filepath = archiveEntry->isDirectory() ? fi.filePath() : fi.path();
dest.mkpath(filepath);
realDestination = dest.absolutePath() + '/' + filepath;
}
if (archiveEntry->isDirectory()) {
kDebug() << "Calling copyTo(" << realDestination << ") for " << archiveEntry->name();
static_cast<const KArchiveDirectory*>(archiveEntry)->copyTo(realDestination);
} else {
static_cast<const KArchiveFile*>(archiveEntry)->copyTo(realDestination);
}
}
return true;
}
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 + '/') + entryName;
processDir(static_cast<const KArchiveDirectory*>(entry), newPrefix);
}
}
return true;
}
void KArchiveInterface::createEntryFor(const KArchiveEntry *aentry, const QString& prefix)
{
ArchiveEntry e;
e[ FileName ] = prefix.isEmpty() ? aentry->name() : prefix + '/' + aentry->name();
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;
}
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)) {
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, "");
processDir((KArchiveDirectory*) archive()->directory()->entry(fi.fileName()), fi.fileName());
} else {
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, "");
} else {
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)
|