From c3e740dc21933d852a23a5723ada27c24054a71a Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Fri, 11 Mar 2022 08:19:18 +0200 Subject: [PATCH] ark: do not use static buffer in KArchiveInterface::permissionsString() make it protected so that it is reusable and implement permissions for LibArchiveInterface via it while at it Signed-off-by: Ivailo Monev --- ark/kerfuffle/archiveinterface.cpp | 57 ++++++++++++++++++ ark/kerfuffle/archiveinterface.h | 2 + ark/plugins/karchiveplugin/karchiveplugin.cpp | 59 +------------------ ark/plugins/karchiveplugin/karchiveplugin.h | 2 - ark/plugins/libarchive/libarchivehandler.cpp | 4 +- 5 files changed, 63 insertions(+), 61 deletions(-) diff --git a/ark/kerfuffle/archiveinterface.cpp b/ark/kerfuffle/archiveinterface.cpp index bc97603f..540d1121 100644 --- a/ark/kerfuffle/archiveinterface.cpp +++ b/ark/kerfuffle/archiveinterface.cpp @@ -88,6 +88,63 @@ bool ReadOnlyArchiveInterface::doResume() return false; } +// Borrowed and adapted from KFileItemPrivate::parsePermissions. +QString ReadOnlyArchiveInterface::permissionsString(mode_t perm) +{ + 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); +} + ReadWriteArchiveInterface::ReadWriteArchiveInterface(QObject *parent, const QVariantList & args) : ReadOnlyArchiveInterface(parent, args) { diff --git a/ark/kerfuffle/archiveinterface.h b/ark/kerfuffle/archiveinterface.h index d878caa3..51e25e6b 100644 --- a/ark/kerfuffle/archiveinterface.h +++ b/ark/kerfuffle/archiveinterface.h @@ -114,6 +114,8 @@ protected: */ void setWaitForFinishedSignal(bool value); + static QString permissionsString(mode_t perm); + private: QString m_filename; QString m_password; diff --git a/ark/plugins/karchiveplugin/karchiveplugin.cpp b/ark/plugins/karchiveplugin/karchiveplugin.cpp index 9bd3178c..d909650c 100644 --- a/ark/plugins/karchiveplugin/karchiveplugin.cpp +++ b/ark/plugins/karchiveplugin/karchiveplugin.cpp @@ -204,7 +204,7 @@ void KArchiveInterface::createEntryFor(const KArchiveEntry *aentry, const QStrin e[ FileName ] = fileName; e[ InternalID ] = e[ FileName ]; - e[ Permissions ] = permissionsString(aentry->permissions()); + e[ Permissions ] = ReadWriteArchiveInterface::permissionsString(aentry->permissions()); e[ Owner ] = aentry->user(); e[ Group ] = aentry->group(); e[ IsDirectory ] = aentry->isDirectory(); @@ -273,61 +273,4 @@ bool KArchiveInterface::deleteFiles(const QList & 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) diff --git a/ark/plugins/karchiveplugin/karchiveplugin.h b/ark/plugins/karchiveplugin/karchiveplugin.h index db7a6f51..76565117 100644 --- a/ark/plugins/karchiveplugin/karchiveplugin.h +++ b/ark/plugins/karchiveplugin/karchiveplugin.h @@ -56,8 +56,6 @@ private: void createEntryFor(const KArchiveEntry *aentry, const QString& prefix); - QString permissionsString(mode_t perm); - void getAllEntries(const KArchiveDirectory *dir, const QString &prefix, QList< QVariant > &list); int handleFileExistsMessage(const QString &dir, const QString &fileName); diff --git a/ark/plugins/libarchive/libarchivehandler.cpp b/ark/plugins/libarchive/libarchivehandler.cpp index d1e474d2..adf3cb95 100644 --- a/ark/plugins/libarchive/libarchivehandler.cpp +++ b/ark/plugins/libarchive/libarchivehandler.cpp @@ -739,7 +739,9 @@ void LibArchiveInterface::emitEntryFromArchiveEntry(struct archive_entry *aentry } e[Size] = (qlonglong)archive_entry_size(aentry); - e[IsDirectory] = S_ISDIR(archive_entry_mode(aentry)); + const mode_t amode = archive_entry_mode(aentry); + e[IsDirectory] = S_ISDIR(amode); + e[Permissions] = ReadWriteArchiveInterface::permissionsString(amode); #if ARCHIVE_VERSION_NUMBER >= 3002000 e[IsPasswordProtected] = archive_entry_is_encrypted(aentry); #endif