VirtualBox

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

Last change on this file since 74526 was 73826, checked in by vboxsync, 6 years ago

Main/UnattendedScript: clean up includes, it never needed MachineImpl.h

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