KArchive

krcc.cpp
1/* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "krcc.h"
8#include "karchive_p.h"
9#include "loggingcategory.h"
10
11#include <QDateTime>
12#include <QDebug>
13#include <QDir>
14#include <QFile>
15#include <QFileInfo>
16#include <QResource>
17#include <QUuid>
18
19class Q_DECL_HIDDEN KRcc::KRccPrivate
20{
21public:
22 KRccPrivate()
23 {
24 }
25 void createEntries(const QDir &dir, KArchiveDirectory *parentDir, KRcc *q);
26
27 QString m_prefix; // '/' + uuid
28};
29
30/**
31 * A KRccFileEntry represents a file in a rcc archive.
32 */
33class KRccFileEntry : public KArchiveFile
34{
35public:
36 KRccFileEntry(KArchive *archive,
37 const QString &name,
38 int access,
39 const QDateTime &date,
40 const QString &user,
41 const QString &group,
42 qint64 size,
43 const QString &resourcePath)
44 : KArchiveFile(archive, name, access, date, user, group, QString(), 0, size)
45 , m_resourcePath(resourcePath)
46 {
47 }
48
49 QByteArray data() const override
50 {
51 QFile f(m_resourcePath);
52 if (f.open(QIODevice::ReadOnly)) {
53 return f.readAll();
54 }
55 qCWarning(KArchiveLog) << "Couldn't open" << m_resourcePath;
56 return QByteArray();
57 }
58 QIODevice *createDevice() const override
59 {
60 return new QFile(m_resourcePath);
61 }
62
63private:
64 QString m_resourcePath;
65};
66
67KRcc::KRcc(const QString &filename)
68 : KArchive(filename)
69 , d(new KRccPrivate)
70{
71}
72
74{
75 if (isOpen()) {
76 close();
77 }
78 delete d;
79}
80
81bool KRcc::doPrepareWriting(const QString &, const QString &, const QString &, qint64, mode_t, const QDateTime &, const QDateTime &, const QDateTime &)
82{
83 setErrorString(tr("Cannot write to RCC file"));
84 qCWarning(KArchiveLog) << "doPrepareWriting not implemented for KRcc";
85 return false;
86}
87
89{
90 setErrorString(tr("Cannot write to RCC file"));
91 qCWarning(KArchiveLog) << "doFinishWriting not implemented for KRcc";
92 return false;
93}
94
95bool KRcc::doWriteDir(const QString &, const QString &, const QString &, mode_t, const QDateTime &, const QDateTime &, const QDateTime &)
96{
97 setErrorString(tr("Cannot write to RCC file"));
98 qCWarning(KArchiveLog) << "doWriteDir not implemented for KRcc";
99 return false;
100}
101
102bool KRcc::doWriteSymLink(const QString &, const QString &, const QString &, const QString &, mode_t, const QDateTime &, const QDateTime &, const QDateTime &)
103{
104 setErrorString(tr("Cannot write to RCC file"));
105 qCWarning(KArchiveLog) << "doWriteSymLink not implemented for KRcc";
106 return false;
107}
108
110{
111 // Open archive
112
113 if (mode == QIODevice::WriteOnly) {
114 return true;
115 }
117 setErrorString(tr("Unsupported mode %1").arg(mode));
118 return false;
119 }
120
121 QUuid uuid = QUuid::createUuid();
122 d->m_prefix = QLatin1Char('/') + uuid.toString();
123 if (!QResource::registerResource(fileName(), d->m_prefix)) {
124 setErrorString(tr("Failed to register resource %1 under prefix %2").arg(fileName(), d->m_prefix));
125 return false;
126 }
127
128 QDir dir(QLatin1Char(':') + d->m_prefix);
129 d->createEntries(dir, rootDir(), this);
130 return true;
131}
132
133void KRcc::KRccPrivate::createEntries(const QDir &dir, KArchiveDirectory *parentDir, KRcc *q)
134{
135 for (const QString &fileName : dir.entryList()) {
136 const QString entryPath = dir.path() + QLatin1Char('/') + fileName;
137 const QFileInfo info(entryPath);
138 if (info.isFile()) {
139 KArchiveEntry *entry = new KRccFileEntry(q, fileName, 0444, info.lastModified(), parentDir->user(), parentDir->group(), info.size(), entryPath);
140 parentDir->addEntry(entry);
141 } else {
142 KArchiveDirectory *entry =
143 new KArchiveDirectory(q, fileName, 0555, info.lastModified(), parentDir->user(), parentDir->group(), /*symlink*/ QString());
144 if (parentDir->addEntryV2(entry)) {
145 createEntries(QDir(entryPath), entry, q);
146 }
147 }
148 }
149}
150
152{
153 // Close the archive
155 // ignore errors
156 return true;
157}
158
159void KRcc::virtual_hook(int id, void *data)
160{
161 KArchive::virtual_hook(id, data);
162}
Represents a directory entry in a KArchive.
void addEntry(KArchiveEntry *)
Definition karchive.cpp:922
bool addEntryV2(KArchiveEntry *)
Definition karchive.cpp:927
A base class for entries in an KArchive.
QString user() const
User who created the file.
Definition karchive.cpp:725
QDateTime date() const
Creation date of the file.
Definition karchive.cpp:710
QString group() const
Group of the user who created the file.
Definition karchive.cpp:730
QString name() const
Name of the file without path.
Definition karchive.cpp:715
Represents a file entry in a KArchive.
qint64 size() const
Size of the data.
Definition karchive.cpp:795
KArchive is a base class for reading and writing archives.
Definition karchive.h:41
virtual bool close()
Closes the archive.
Definition karchive.cpp:214
virtual KArchiveDirectory * rootDir()
Retrieves or create the root directory.
Definition karchive.cpp:519
QIODevice::OpenMode mode() const
Returns the mode in which the archive was opened.
Definition karchive.cpp:623
QString fileName() const
The name of the archive file, as passed to the constructor that takes a fileName, or an empty string ...
Definition karchive.cpp:638
bool isOpen() const
Checks whether the archive is open.
Definition karchive.cpp:633
void setErrorString(const QString &errorStr)
Sets error description.
Definition karchive.cpp:484
KRcc is a class for reading dynamic binary resources created by Qt's rcc tool from a ....
Definition krcc.h:20
bool doWriteDir(const QString &name, const QString &user, const QString &group, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime) override
Write a directory to the archive.
Definition krcc.cpp:95
bool doFinishWriting(qint64 size) override
Called after writing the data.
Definition krcc.cpp:88
KRcc(const QString &filename)
Creates an instance that operates on the given filename.
Definition krcc.cpp:67
bool closeArchive() override
Unregisters the .rcc resource from the QResource system.
Definition krcc.cpp:151
bool doWriteSymLink(const QString &name, const QString &target, const QString &user, const QString &group, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime) override
Writes a symbolic link to the archive.
Definition krcc.cpp:102
bool doPrepareWriting(const QString &name, const QString &user, const QString &group, qint64 size, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime) override
This virtual method must be implemented by subclasses.
Definition krcc.cpp:81
bool openArchive(QIODevice::OpenMode mode) override
Registers the .rcc resource in the QResource system under a unique identifier, then lists that,...
Definition krcc.cpp:109
~KRcc() override
If the rcc file is still opened, then it will be closed automatically by the destructor.
Definition krcc.cpp:73
bool registerResource(const QString &rccFileName, const QString &mapRoot)
bool unregisterResource(const QString &rccFileName, const QString &mapRoot)
qsizetype size() const const
QUuid createUuid()
QString toString(StringFormat mode) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.