@@ -47,7 +47,9 @@ ScanningSession::ScanningSession():
47
47
mSession(0 ),
48
48
mTailoring(0 ),
49
49
50
- mOpenDir(NULL ),
50
+ mTempOpenDir(NULL ),
51
+ mTempOpenPath(" " ),
52
+ mOriginalOpenPath(" " ),
51
53
52
54
mSkipValid(false ),
53
55
mSessionDirty(false ),
@@ -80,17 +82,63 @@ struct xccdf_session* ScanningSession::getXCCDFSession() const
80
82
void ScanningSession::cleanTmpDir ()
81
83
{
82
84
/*
83
- * For use in cloneToTemporaryFile(...); closes mOpenDir
85
+ * For use in cloneToTemporaryFile(...); closes mTempOpenDir
84
86
* if it is open and removes backing source. Ignores errors.
85
87
*
86
88
* A new Temporary Directory will be created afterwards,
87
89
* so the worst case is leaving temporary directory (+files),
88
90
* but this is cleaned on reboot or manually by the user.
89
91
*/
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
+ }
94
142
}
95
143
}
96
144
@@ -104,7 +152,7 @@ void ScanningSession::cloneToTemporaryFile(const QString& path)
104
152
* 2) We need to create a new temporary file:
105
153
* Then we can copy data into it from path.
106
154
*
107
- * By not closing and removing mOpenDir in closeFile()
155
+ * By not closing and removing mTempOpenDir in closeFile()
108
156
* until openFile() is called, we prevent unintentional reloading
109
157
* of the real path. Further, calling fileOpen(...) with the same
110
158
* path results in reloading the file.
@@ -113,12 +161,12 @@ void ScanningSession::cloneToTemporaryFile(const QString& path)
113
161
// Clean the temporary directory if it is open already, then create
114
162
// a new one.
115
163
cleanTmpDir ();
116
- mOpenDir = new QTemporaryDir ();
164
+ mTempOpenDir = new QTemporaryDir ();
117
165
118
166
// Recalling is unlikely to succeed, so throw a fatal exception
119
- if (!mOpenDir ->isValid ())
167
+ if (!mTempOpenDir ->isValid ())
120
168
{
121
- throw std::runtime_error (mOpenDir ->errorString ().toUtf8 ().constData ());
169
+ throw std::runtime_error (mTempOpenDir ->errorString ().toUtf8 ().constData ());
122
170
}
123
171
124
172
// XCCDF files can have relative includes; get a complete closure set
@@ -128,14 +176,14 @@ void ScanningSession::cloneToTemporaryFile(const QString& path)
128
176
//
129
177
// Files which violate this constraint are rare; thus it should be safe
130
178
// to refuse to open them.
131
- mClosureOfFile .clear ();
132
- getDependencyClosureOfFile (path, mClosureOfFile );
179
+ mClosureOfOriginalFile .clear ();
180
+ updateDependencyClosureOfFile (path, mClosureOfOriginalFile );
133
181
134
182
const QFileInfo pathInfo (path);
135
183
QString baseDirectory = pathInfo.absolutePath ();
136
- for (const QString& cur : mClosureOfFile )
184
+ for (const QString& cur : mClosureOfOriginalFile )
137
185
{
138
- // Since getDependencyClosureOfFile (...) returns cleaned absolute
186
+ // Since updateDependencyClosureOfFile (...) returns cleaned, absolute
139
187
// paths, we can check if the path starts with the baseDirectory
140
188
if (!cur.startsWith (baseDirectory))
141
189
{
@@ -145,63 +193,24 @@ void ScanningSession::cloneToTemporaryFile(const QString& path)
145
193
}
146
194
}
147
195
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);
189
198
190
- mOriginalPath = path;
199
+ mOriginalOpenPath = path;
191
200
}
192
201
193
202
void ScanningSession::openFile (const QString& path, bool reload)
194
203
{
195
204
if (mSession )
196
205
closeFile ();
197
206
198
- if (reload || mOriginalPath != path)
207
+ if (reload || mOriginalOpenPath != path)
199
208
{
200
209
cloneToTemporaryFile (path);
201
210
}
202
211
203
- const QString tmpPath = mOpenPath ;
204
- const QFileInfo pathInfo (mOpenPath );
212
+ const QString tmpPath = mTempOpenPath ;
213
+ const QFileInfo pathInfo (mTempOpenPath );
205
214
206
215
// We have to make sure that we *ALWAYS* open the session by absolute
207
216
// path. oscap local won't be run from the same directory from where
@@ -250,15 +259,15 @@ QString ScanningSession::getOriginalFilePath() const
250
259
if (!fileOpened ())
251
260
return QString (" " );
252
261
253
- return mOriginalPath ;
262
+ return mOriginalOpenPath ;
254
263
}
255
264
256
265
QSet<QString> ScanningSession::getOriginalClosure () const
257
266
{
258
- return mClosureOfFile ;
267
+ return mClosureOfOriginalFile ;
259
268
}
260
269
261
- void ScanningSession::getDependencyClosureOfFile (const QString& filePath, QSet<QString>& targetSet) const
270
+ void ScanningSession::updateDependencyClosureOfFile (const QString& filePath, QSet<QString>& targetSet) const
262
271
{
263
272
QFileInfo fileInfo (filePath);
264
273
targetSet.insert (fileInfo.absoluteFilePath ()); // insert current file
@@ -299,7 +308,7 @@ void ScanningSession::getDependencyClosureOfFile(const QString& filePath, QSet<Q
299
308
const QAbstractXmlNodeModel* model = itemIdx.model ();
300
309
const QString relativeFileName = model->stringValue (itemIdx);
301
310
const QString absUncleanPath = parentDir.absoluteFilePath (relativeFileName);
302
- getDependencyClosureOfFile (QDir::cleanPath (absUncleanPath), targetSet);
311
+ updateDependencyClosureOfFile (QDir::cleanPath (absUncleanPath), targetSet);
303
312
item = result.next ();
304
313
}
305
314
@@ -323,7 +332,7 @@ void ScanningSession::getDependencyClosureOfFile(const QString& filePath, QSet<Q
323
332
QSet<QString> ScanningSession::getOpenedFilesClosure () const
324
333
{
325
334
QSet<QString> ret;
326
- ScanningSession::getDependencyClosureOfFile (getOpenedFilePath (), ret);
335
+ ScanningSession::updateDependencyClosureOfFile (getOpenedFilePath (), ret);
327
336
return ret;
328
337
}
329
338
0 commit comments