KArchive

krcc.cpp
1 /* This file is part of the KDE libraries
2  SPDX-FileCopyrightText: 2014 David Faure <[email protected]>
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 
19 class Q_DECL_HIDDEN KRcc::KRccPrivate
20 {
21 public:
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  */
33 class KRccFileEntry : public KArchiveFile
34 {
35 public:
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 
63 private:
64  QString m_resourcePath;
65 };
66 
67 KRcc::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 
81 bool 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 
95 bool 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 
102 bool 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 
133 void 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
154  QResource::unregisterResource(fileName(), d->m_prefix);
155  // ignore errors
156  return true;
157 }
158 
159 void KRcc::virtual_hook(int id, void *data)
160 {
161  KArchive::virtual_hook(id, data);
162 }
bool registerResource(const QString &rccFileName, const QString &mapRoot)
bool addEntryV2(KArchiveEntry *)
Definition: karchive.cpp:917
KRcc(const QString &filename)
Creates an instance that operates on the given filename.
Definition: krcc.cpp:67
~KRcc() override
If the rcc file is still opened, then it will be closed automatically by the destructor.
Definition: krcc.cpp:73
int size() const const
bool doFinishWriting(qint64 size) override
Called after writing the data.
Definition: krcc.cpp:88
virtual QIODevice * createDevice() const
This method returns QIODevice (internal class: KLimitedIODevice) on top of the underlying QIODevice.
Definition: karchive.cpp:811
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
QString group() const
Group of the user who created the file.
Definition: karchive.cpp:720
virtual KArchiveDirectory * rootDir()
Retrieves or create the root directory.
Definition: karchive.cpp:509
virtual bool close()
Closes the archive.
Definition: karchive.cpp:214
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
A directory in an archive.
Base class for the archive-file's directory structure.
Definition: karchiveentry.h:34
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:628
QUuid createUuid()
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
QString toString() const const
generic class for reading/writing archives
Definition: karchive.h:39
bool isOpen() const
Checks whether the archive is open.
Definition: karchive.cpp:623
QIODevice::OpenMode mode() const
Returns the mode in which the archive was opened.
Definition: karchive.cpp:613
void addEntry(KArchiveEntry *)
Definition: karchive.cpp:912
KRcc is a class for reading dynamic binary resources created by Qt's rcc tool from a ....
Definition: krcc.h:19
const char * name(StandardAction id)
void setErrorString(const QString &errorStr)
Sets error description.
Definition: karchive.cpp:474
A file in an archive.
Definition: karchivefile.h:24
bool unregisterResource(const QString &rccFileName, const QString &mapRoot)
QString user() const
User who created the file.
Definition: karchive.cpp:715
virtual QByteArray data() const
Returns the data of the file.
Definition: karchive.cpp:795
bool closeArchive() override
Unregisters the .rcc resource from the QResource system.
Definition: krcc.cpp:151
int access(const QString &path, int mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:03:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.