Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 94d2f0e

Browse files
committed
Rename variables and refactor per comments from @matejak
Signed-off-by: Alexander Scheel <[email protected]>
1 parent 911fb41 commit 94d2f0e

File tree

2 files changed

+84
-73
lines changed

2 files changed

+84
-73
lines changed

include/ScanningSession.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@ class ScanningSession
291291
/// Our own tailoring that may or may not initially be loaded from a file
292292
mutable struct xccdf_tailoring* mTailoring;
293293

294-
/// Temporary copy of opened XCCDF file
295-
QTemporaryDir* mOpenDir;
296-
/// Path to temporary XCCDF file
297-
QString mOpenPath;
298-
/// Path to original XCCDF file
299-
QString mOriginalPath;
300-
/// Closure of original XCCDF file
301-
QSet<QString> mClosureOfFile;
294+
/// Temporary copy of opened DS or XCCDF file
295+
QTemporaryDir* mTempOpenDir;
296+
/// Path to temporary DS or XCCDF file
297+
QString mTempOpenPath;
298+
/// Path to original DS or XCCDF file
299+
QString mOriginalOpenPath;
300+
/// Closure of original DS or XCCDF file
301+
QSet<QString> mClosureOfOriginalFile;
302302

303303
/// Temporary file provides auto deletion and a valid temp file path
304304
QTemporaryFile mTailoringFile;
@@ -319,12 +319,14 @@ class ScanningSession
319319
QString mUserTailoringCID;
320320

321321
/// Gets the dependency closure of the specified file.
322-
void getDependencyClosureOfFile(const QString& filePath, QSet<QString>& targetSet) const;
322+
void updateDependencyClosureOfFile(const QString& filePath, QSet<QString>& targetSet) const;
323323

324324
/// Clones openFile(path) to a Temporary File
325325
void cloneToTemporaryFile(const QString& path);
326326
/// Closes mOpenFile if it is open
327327
void cleanTmpDir();
328+
/// Copies all files from the path into the temporary location
329+
void copyTempFiles(QString path, QString baseDirectory, const QFileInfo pathInfo);
328330
};
329331

330332
#endif

src/ScanningSession.cpp

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ ScanningSession::ScanningSession():
4747
mSession(0),
4848
mTailoring(0),
4949

50-
mOpenDir(NULL),
50+
mTempOpenDir(NULL),
51+
mTempOpenPath(""),
52+
mOriginalOpenPath(""),
5153

5254
mSkipValid(false),
5355
mSessionDirty(false),
@@ -80,17 +82,63 @@ struct xccdf_session* ScanningSession::getXCCDFSession() const
8082
void ScanningSession::cleanTmpDir()
8183
{
8284
/*
83-
* For use in cloneToTemporaryFile(...); closes mOpenDir
85+
* For use in cloneToTemporaryFile(...); closes mTempOpenDir
8486
* if it is open and removes backing source. Ignores errors.
8587
*
8688
* A new Temporary Directory will be created afterwards,
8789
* so the worst case is leaving temporary directory (+files),
8890
* but this is cleaned on reboot or manually by the user.
8991
*/
90-
if (mOpenDir != NULL) {
91-
mOpenDir->remove();
92-
delete mOpenDir;
93-
mOpenDir = NULL;
92+
if (mTempOpenDir != NULL) {
93+
mTempOpenDir->remove();
94+
delete mTempOpenDir;
95+
mTempOpenDir = NULL;
96+
}
97+
}
98+
99+
void ScanningSession::copyTempFiles(QString path, QString baseDirectory,
100+
const QFileInfo pathInfo)
101+
{
102+
// File is valid; extract the new location and copy dependencies into
103+
// their correct places.
104+
QString tmpPath = mTempOpenDir->path() + QDir::separator();
105+
106+
// set mTempOpenPath to the new temporary location and copy it.
107+
mTempOpenPath = tmpPath + pathInfo.fileName();
108+
QFile::copy(path, mTempOpenPath);
109+
110+
QDir tmpDir(tmpPath);
111+
for (const QString& cur : mClosureOfOriginalFile)
112+
{
113+
const QFileInfo curInfo(cur);
114+
const QDir curDir = curInfo.absoluteDir();
115+
116+
if (curDir.absolutePath() != baseDirectory)
117+
{
118+
// Create subdirectories and copy into the new directory
119+
//
120+
// Since curPath is an absolute path, which has an eventual
121+
// parent of baseDirectory, the left n = baseDirectory.length()+1
122+
// characters are baseDirectory plus a trailing slash; the
123+
// remaining characters to the right of this (curPath.length()-n)
124+
// is the recursive directory structure of cur (e.g., all the
125+
// subdirs it resides in). We can then plant this on tmpPath
126+
// and use mkpath to recreate that directory structure. We ignore
127+
// errors from mkpath because we might have previously created
128+
// this path in a previous loop iteration.
129+
const QString curPath = curDir.absolutePath();
130+
const int pathLen = curPath.length() - baseDirectory.length() - 1;
131+
const QString subDirs = curPath.right(pathLen);
132+
const QString newPath = tmpPath + subDirs + QDir::separator() + curInfo.fileName();
133+
134+
tmpDir.mkpath(tmpPath + subDirs);
135+
QFile::copy(cur, newPath);
136+
}
137+
else
138+
{
139+
// Simply copy the file since it resides in baseDirectory
140+
QFile::copy(cur, tmpPath + curInfo.fileName());
141+
}
94142
}
95143
}
96144

@@ -104,7 +152,7 @@ void ScanningSession::cloneToTemporaryFile(const QString& path)
104152
* 2) We need to create a new temporary file:
105153
* Then we can copy data into it from path.
106154
*
107-
* By not closing and removing mOpenDir in closeFile()
155+
* By not closing and removing mTempOpenDir in closeFile()
108156
* until openFile() is called, we prevent unintentional reloading
109157
* of the real path. Further, calling fileOpen(...) with the same
110158
* path results in reloading the file.
@@ -113,12 +161,12 @@ void ScanningSession::cloneToTemporaryFile(const QString& path)
113161
// Clean the temporary directory if it is open already, then create
114162
// a new one.
115163
cleanTmpDir();
116-
mOpenDir = new QTemporaryDir();
164+
mTempOpenDir = new QTemporaryDir();
117165

118166
// Recalling is unlikely to succeed, so throw a fatal exception
119-
if (!mOpenDir->isValid())
167+
if (!mTempOpenDir->isValid())
120168
{
121-
throw std::runtime_error(mOpenDir->errorString().toUtf8().constData());
169+
throw std::runtime_error(mTempOpenDir->errorString().toUtf8().constData());
122170
}
123171

124172
// XCCDF files can have relative includes; get a complete closure set
@@ -128,14 +176,14 @@ void ScanningSession::cloneToTemporaryFile(const QString& path)
128176
//
129177
// Files which violate this constraint are rare; thus it should be safe
130178
// to refuse to open them.
131-
mClosureOfFile.clear();
132-
getDependencyClosureOfFile(path, mClosureOfFile);
179+
mClosureOfOriginalFile.clear();
180+
updateDependencyClosureOfFile(path, mClosureOfOriginalFile);
133181

134182
const QFileInfo pathInfo(path);
135183
QString baseDirectory = pathInfo.absolutePath();
136-
for (const QString& cur : mClosureOfFile)
184+
for (const QString& cur : mClosureOfOriginalFile)
137185
{
138-
// Since getDependencyClosureOfFile(...) returns cleaned absolute
186+
// Since updateDependencyClosureOfFile(...) returns cleaned, absolute
139187
// paths, we can check if the path starts with the baseDirectory
140188
if (!cur.startsWith(baseDirectory))
141189
{
@@ -145,63 +193,24 @@ void ScanningSession::cloneToTemporaryFile(const QString& path)
145193
}
146194
}
147195

148-
// File is valid; extract the new location and copy dependencies into
149-
// their correct places.
150-
QString tmpPath = mOpenDir->path() + QDir::separator();
151-
152-
// set mOpenPath to the new temporary location and copy it.
153-
mOpenPath = tmpPath + pathInfo.fileName();
154-
QFile::copy(path, mOpenPath);
155-
156-
QDir tmpDir(tmpPath);
157-
for (const QString& cur : mClosureOfFile)
158-
{
159-
const QFileInfo curInfo(cur);
160-
const QDir curDir = curInfo.absoluteDir();
161-
162-
if (curDir.absolutePath() != baseDirectory)
163-
{
164-
// Create subdirectories and copy into the new directory
165-
//
166-
// Since curPath is an absolute path, which has an eventual
167-
// parent of baseDirectory, the left n = baseDirectory.length()+1
168-
// characters are baseDirectory plus a trailing slash; the
169-
// remaining characters to the right of this (curPath.length()-n)
170-
// is the recursive directory structure of cur (e.g., all the
171-
// subdirs it resides in). We can then plant this on tmpPath
172-
// and use mkpath to recreate that directory structure. We ignore
173-
// errors from mkpath because we might have previously created
174-
// this path in a previous loop iteration.
175-
const QString curPath = curDir.absolutePath();
176-
const int pathLen = curPath.length() - baseDirectory.length() - 1;
177-
const QString subDirs = curPath.right(pathLen);
178-
const QString newPath = tmpPath + subDirs + QDir::separator() + curInfo.fileName();
179-
180-
tmpDir.mkpath(tmpPath + subDirs);
181-
QFile::copy(cur, newPath);
182-
}
183-
else
184-
{
185-
// Simply copy the file since it resides in baseDirectory
186-
QFile::copy(cur, tmpPath + curInfo.fileName());
187-
}
188-
}
196+
// Copy all files into the new temporary directory since the structure is valid.
197+
copyTempFiles(path, baseDirectory, pathInfo);
189198

190-
mOriginalPath = path;
199+
mOriginalOpenPath = path;
191200
}
192201

193202
void ScanningSession::openFile(const QString& path, bool reload)
194203
{
195204
if (mSession)
196205
closeFile();
197206

198-
if (reload || mOriginalPath != path)
207+
if (reload || mOriginalOpenPath != path)
199208
{
200209
cloneToTemporaryFile(path);
201210
}
202211

203-
const QString tmpPath = mOpenPath;
204-
const QFileInfo pathInfo(mOpenPath);
212+
const QString tmpPath = mTempOpenPath;
213+
const QFileInfo pathInfo(mTempOpenPath);
205214

206215
// We have to make sure that we *ALWAYS* open the session by absolute
207216
// path. oscap local won't be run from the same directory from where
@@ -250,15 +259,15 @@ QString ScanningSession::getOriginalFilePath() const
250259
if (!fileOpened())
251260
return QString("");
252261

253-
return mOriginalPath;
262+
return mOriginalOpenPath;
254263
}
255264

256265
QSet<QString> ScanningSession::getOriginalClosure() const
257266
{
258-
return mClosureOfFile;
267+
return mClosureOfOriginalFile;
259268
}
260269

261-
void ScanningSession::getDependencyClosureOfFile(const QString& filePath, QSet<QString>& targetSet) const
270+
void ScanningSession::updateDependencyClosureOfFile(const QString& filePath, QSet<QString>& targetSet) const
262271
{
263272
QFileInfo fileInfo(filePath);
264273
targetSet.insert(fileInfo.absoluteFilePath()); // insert current file
@@ -299,7 +308,7 @@ void ScanningSession::getDependencyClosureOfFile(const QString& filePath, QSet<Q
299308
const QAbstractXmlNodeModel* model = itemIdx.model();
300309
const QString relativeFileName = model->stringValue(itemIdx);
301310
const QString absUncleanPath = parentDir.absoluteFilePath(relativeFileName);
302-
getDependencyClosureOfFile(QDir::cleanPath(absUncleanPath), targetSet);
311+
updateDependencyClosureOfFile(QDir::cleanPath(absUncleanPath), targetSet);
303312
item = result.next();
304313
}
305314

@@ -323,7 +332,7 @@ void ScanningSession::getDependencyClosureOfFile(const QString& filePath, QSet<Q
323332
QSet<QString> ScanningSession::getOpenedFilesClosure() const
324333
{
325334
QSet<QString> ret;
326-
ScanningSession::getDependencyClosureOfFile(getOpenedFilePath(), ret);
335+
ScanningSession::updateDependencyClosureOfFile(getOpenedFilePath(), ret);
327336
return ret;
328337
}
329338

0 commit comments

Comments
 (0)