VirtualBox

source: vbox/trunk/src/VBox/Main/include/UnattendedScript.h@ 72353

Last change on this file since 72353 was 68162, checked in by vboxsync, 7 years ago

Main: export Unattended* source files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1/* $Id: UnattendedScript.h 68162 2017-07-28 15:28:33Z vboxsync $ */
2/** @file
3 * Implementeation of algorithms which read/parse/save scripts for unattended installation.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ____H_UNATTENDEDSCRIPT
19#define ____H_UNATTENDEDSCRIPT
20
21#include "VirtualBoxBase.h"
22#include <iprt/cpp/utils.h>
23
24using namespace xml;
25
26class Unattended;
27
28
29/**
30 * Base class for all the script readers/editors.
31 *
32 * @todo get rid of this bugger
33 */
34class AbstractScript : public RTCNonCopyable
35{
36protected:
37 /** For setting errors.
38 * Yeah, class isn't entirely abstract now. */
39 VirtualBoxBase *mpSetError;
40
41private: /* no default constructors for children. */
42 AbstractScript() {}
43
44public:
45 AbstractScript(VirtualBoxBase *pSetError) : mpSetError(pSetError) {}
46 virtual ~AbstractScript() {}
47
48 /**
49 * Read a script from a file
50 */
51 virtual HRESULT read(const Utf8Str &rStrFilename) = 0;
52
53 /**
54 * Read a script from a VFS file handle.
55 */
56 virtual HRESULT readFromHandle(RTVFSFILE hVfsFile, const char *pszFilename) = 0;
57
58 /**
59 * Parse the script
60 */
61 virtual HRESULT parse() = 0;
62
63 /**
64 * Save a script to a string.
65 *
66 * This is used by save() and later others to deloy the script.
67 */
68 virtual HRESULT saveToString(Utf8Str &rStrDst) = 0;
69
70 /**
71 * Save a script to a file.
72 * @param rStrPath Where to save the script. This normally points to a
73 * file, but in a number of child use cases it's
74 * actually giving a directory to put the script in
75 * using the default deloyment filename. One day we
76 * might make the caller do this path joining.
77 * @param fOverwrite Whether to overwrite the file or not.
78 */
79 virtual HRESULT save(const Utf8Str &rStrPath, bool fOverwrite) = 0;
80
81 /**
82 * Path where an actual script with user's data is located
83 */
84 virtual const Utf8Str &getActualScriptPath() const = 0;
85};
86
87/**
88 * Base class for text based script readers/editors.
89 *
90 * This deals with reading the file into a string data member, writing it back
91 * out to a file, and remember the filenames.
92 */
93class BaseTextScript : public AbstractScript
94{
95protected:
96 const char * const mpszDefaultTemplateFilename; /**< The default template filename. Can be empty. */
97 const char * const mpszDefaultFilename; /**< Filename to use when someone calls save() with a directory path. Can be NULL. */
98 RTCString mStrScriptFullContent; /**< Raw text file content. Produced by read() and typically only used by parse(). */
99 Utf8Str mStrOriginalPath; /**< Path where an original script is located (set by read()). */
100 Utf8Str mStrSavedPath; /**< Path where an saved script with user's data is located (set by save()). */
101
102public:
103 BaseTextScript(VirtualBoxBase *pSetError, const char *pszDefaultTemplateFilename, const char *pszDefaultFilename)
104 : AbstractScript(pSetError)
105 , mpszDefaultTemplateFilename(pszDefaultTemplateFilename)
106 , mpszDefaultFilename(pszDefaultFilename)
107 { }
108 virtual ~BaseTextScript() {}
109
110 HRESULT read(const Utf8Str &rStrFilename);
111 HRESULT readFromHandle(RTVFSFILE hVfsFile, const char *pszFilename);
112 HRESULT save(const Utf8Str &rStrFilename, bool fOverwrite);
113
114 /**
115 * Gets the default filename for this class of scripts (empty if none).
116 *
117 * @note Just the filename, no path.
118 */
119 const char *getDefaultFilename() const
120 {
121 return mpszDefaultFilename;
122 }
123
124 /**
125 * Gets the default template filename for this class of scripts (empty if none).
126 *
127 * @note Just the filename, no path.
128 */
129 const char *getDefaultTemplateFilename() const
130 {
131 return mpszDefaultTemplateFilename;
132 }
133
134 /**
135 * Path to the file we last saved the script as.
136 */
137 const Utf8Str &getActualScriptPath() const
138 {
139 return mStrSavedPath;
140 }
141
142 /**
143 * Path where an original script is located
144 */
145 const Utf8Str &getOriginalScriptPath() const
146 {
147 return mStrOriginalPath;
148 }
149};
150
151
152/**
153 * Generic unattended text script template editor.
154 *
155 * This just perform variable replacements, no other editing possible.
156 *
157 * Everything happens during saveToString, parse is a noop.
158 */
159class UnattendedScriptTemplate : public BaseTextScript
160{
161protected:
162 /** Where to get the replacement strings from. */
163 Unattended *mpUnattended;
164
165public:
166 UnattendedScriptTemplate(Unattended *pUnattended, const char *pszDefaultTemplateFilename, const char *pszDefaultFilename);
167 virtual ~UnattendedScriptTemplate() {}
168
169 HRESULT parse() { return S_OK; }
170 HRESULT saveToString(Utf8Str &rStrDst);
171
172protected:
173 /**
174 * Gets the replacement value for the given placeholder.
175 *
176 * @returns COM status code.
177 * @param pachPlaceholder The placholder string. Not zero terminated.
178 * @param cchPlaceholder The length of the placeholder.
179 * @param fOutputting Indicates whether we actually need the correct value
180 * or is just syntax checking excluded template parts.
181 * @param rValue Where to return the value.
182 */
183 HRESULT getReplacement(const char *pachPlaceholder, size_t cchPlaceholder, bool fOutputting, RTCString &rValue);
184
185 /**
186 * Overridable worker for getReplacement.
187 *
188 * @returns COM status code.
189 * @param pachPlaceholder The placholder string. Not zero terminated.
190 * @param cchPlaceholder The length of the placeholder.
191 * @param cchFullPlaceholder The full placeholder length, including suffixes
192 * indicating how it should be escaped (for error
193 * messages).
194 * @param fOutputting Indicates whether we actually need the correct
195 * value or is just syntax checking excluded
196 * template parts. Intended for voiding triggering
197 * sanity checks regarding which replacements
198 * should be used and not (e.g. no guest additions
199 * path when installing GAs aren't enabled).
200 * @param rValue Where to return the value.
201 * @throws std::bad_alloc
202 */
203 virtual HRESULT getUnescapedReplacement(const char *pachPlaceholder, size_t cchPlaceholder,
204 size_t cchFullPlaceholder, bool fOutputting, RTCString &rValue);
205
206
207 /**
208 * Get the result of a conditional.
209 *
210 * @returns COM status code.
211 * @param pachPlaceholder The placholder string. Not zero terminated.
212 * @param cchPlaceholder The length of the placeholder.
213 * @param pfOutputting Where to return the result of the conditional.
214 * This holds the current outputting state on input
215 * in case someone want to sanity check anything.
216 */
217 virtual HRESULT getConditional(const char *pachPlaceholder, size_t cchPlaceholder, bool *pfOutputting);
218};
219
220
221/**
222 * Generic line based text script editor.
223 *
224 * This is used for editing isolinux configuratin files among other things.
225 */
226class GeneralTextScript : public BaseTextScript
227{
228protected:
229 RTCList<RTCString> mScriptContentByLines; /**< Content index by line. This contains the edited version. */
230 bool mfDataParsed; /**< Indicates whether the script has been parse() yet. */
231
232public:
233 GeneralTextScript(VirtualBoxBase *pSetError, const char *pszDefaultTemplateFilename = NULL, const char *pszDefaultFilename = NULL)
234 : BaseTextScript(pSetError, pszDefaultTemplateFilename, pszDefaultFilename), mfDataParsed(false)
235 {}
236 virtual ~GeneralTextScript() {}
237
238 HRESULT parse();
239 HRESULT saveToString(Utf8Str &rStrDst);
240
241 //////////////////New functions//////////////////////////////
242
243 bool isDataParsed() const
244 {
245 return mfDataParsed;
246 }
247
248 /**
249 * Returns the actual size of script in lines
250 */
251 size_t getLineNumbersOfScript() const
252 {
253 return mScriptContentByLines.size();
254 }
255
256 /**
257 * Gets a read-only reference to the given line, returning Utf8Str::Empty if
258 * idxLine is out of range.
259 *
260 * @returns Line string reference or Utf8Str::Empty.
261 * @param idxLine The line number.
262 *
263 * @todo RTCList doesn't allow this method to be const.
264 */
265 RTCString const &getContentOfLine(size_t idxLine);
266
267 /**
268 * Set new content of line
269 */
270 HRESULT setContentOfLine(size_t idxLine, const Utf8Str &newContent);
271
272 /**
273 * Find a substring in the script
274 * Returns a list with the found lines
275 * @throws std::bad_alloc
276 */
277 std::vector<size_t> findTemplate(const Utf8Str &rStrNeedle, RTCString::CaseSensitivity enmCase = RTCString::CaseSensitive);
278
279 /**
280 * In line @a idxLine replace the first occurence of @a rStrNeedle with
281 * @a rStrRelacement.
282 */
283 HRESULT findAndReplace(size_t idxLine, const Utf8Str &rStrNeedle, const Utf8Str &rStrReplacement);
284
285 /**
286 * Append a string into the end of the given line.
287 */
288 HRESULT appendToLine(size_t idxLine, const Utf8Str &rStrToAppend);
289
290 /**
291 * Prepend a string in the beginning of the given line.
292 */
293 HRESULT prependToLine(size_t idxLine, const Utf8Str &rStrToPrepend);
294
295 //////////////////New functions//////////////////////////////
296};
297
298
299#if 0 /* convert when we fix SUSE */
300/**
301 * SUSE unattended XML file editor.
302 */
303class UnattendedSUSEXMLScript : public UnattendedXMLScript
304{
305public:
306 UnattendedSUSEXMLScript(VirtualBoxBase *pSetError, const char *pszDefaultFilename = "autoinst.xml")
307 : UnattendedXMLScript(pSetError, pszDefaultFilename) {}
308 ~UnattendedSUSEXMLScript() {}
309
310 HRESULT parse();
311
312protected:
313 HRESULT setFieldInElement(xml::ElementNode *pElement, const DataId enmDataId, const Utf8Str &rStrValue);
314
315private:
316 //////////////////New functions//////////////////////////////
317 /** @throws std::bad_alloc */
318 HRESULT LoopThruSections(const xml::ElementNode *pelmRoot);
319 /** @throws std::bad_alloc */
320 HRESULT HandleUserAccountsSection(const xml::ElementNode *pelmSection);
321 /** @throws std::bad_alloc */
322 Utf8Str createProbableValue(const DataId enmDataId, const xml::ElementNode *pCurElem);
323 /** @throws std::bad_alloc */
324 Utf8Str createProbableUserHomeDir(const xml::ElementNode *pCurElem);
325 //////////////////New functions//////////////////////////////
326};
327#endif
328
329
330#endif // !____H_UNATTENDEDSCRIPT
331
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette