VirtualBox

source: vbox/trunk/src/VBox/Main/include/GuestSessionImplTasks.h@ 97359

Last change on this file since 97359 was 97344, checked in by vboxsync, 2 years ago

Guest Control/Main: Fixes for copying to/from guests with mixed OSes. bugref:10286

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1/* $Id: GuestSessionImplTasks.h 97344 2022-10-31 10:17:50Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Guest session tasks header.
4 */
5
6/*
7 * Copyright (C) 2018-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef MAIN_INCLUDED_GuestSessionImplTasks_h
29#define MAIN_INCLUDED_GuestSessionImplTasks_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include "GuestSessionWrap.h"
35#include "EventImpl.h"
36
37#include "GuestCtrlImplPrivate.h"
38#include "GuestSessionImpl.h"
39#include "ThreadTask.h"
40
41#include <iprt/vfs.h>
42
43#include <vector>
44
45class Guest;
46class GuestSessionTask;
47class GuestSessionTaskInternalStart;
48
49
50/**
51 * Structure for keeping a file system source specification,
52 * along with options.
53 */
54struct GuestSessionFsSourceSpec
55{
56 GuestSessionFsSourceSpec()
57 : enmType(FsObjType_Unknown)
58 , enmPathStyle(PathStyle_Unknown)
59 , fDryRun(false) { RT_ZERO(Type); }
60
61 /** The (absolute) path to the source to use. */
62 Utf8Str strSource;
63 /** Filter to use. Currently not implemented and thus ignored. */
64 Utf8Str strFilter;
65 /** The object type of this source. */
66 FsObjType_T enmType;
67 /** The path style to use. */
68 PathStyle_T enmPathStyle;
69 /** Whether to do a dry run (e.g. not really touching anything) or not. */
70 bool fDryRun;
71 /** Union to keep type-specific data. Must be a POD type (zero'ing). */
72 union
73 {
74 /** Directory-specific data. */
75 struct
76 {
77 /** Directory copy flags. */
78 DirectoryCopyFlag_T fCopyFlags;
79 } Dir;
80 /** File-specific data. */
81 struct
82 {
83 /** File copy flags. */
84 FileCopyFlag_T fCopyFlags;
85 /** Source file offset to start copying from. */
86 size_t offStart;
87 /** Host file handle to use for reading from / writing to.
88 * Optional and can be NULL if not used. */
89 PRTFILE phFile;
90 /** Source size (in bytes) to copy. */
91 uint64_t cbSize;
92 } File;
93 } Type;
94};
95
96/** A set of GuestSessionFsSourceSpec sources. */
97typedef std::vector<GuestSessionFsSourceSpec> GuestSessionFsSourceSet;
98
99/**
100 * Structure for keeping a file system entry.
101 */
102struct FsEntry
103{
104 /** The entrie's file mode. */
105 RTFMODE fMode;
106 /** The entrie's path, relative to the list's root path. */
107 Utf8Str strPath;
108};
109
110/** A vector of FsEntry entries. */
111typedef std::vector<FsEntry *> FsEntries;
112
113/**
114 * Class for storing and handling file system entries, neeed for doing
115 * internal file / directory operations to / from the guest.
116 */
117class FsList
118{
119public:
120
121 FsList(const GuestSessionTask &Task);
122 virtual ~FsList();
123
124public:
125
126 int Init(const Utf8Str &strSrcRootAbs, const Utf8Str &strDstRootAbs, const GuestSessionFsSourceSpec &SourceSpec);
127 void Destroy(void);
128
129 int AddEntryFromGuest(const Utf8Str &strFile, const GuestFsObjData &fsObjData);
130 int AddDirFromGuest(const Utf8Str &strPath, const Utf8Str &strSubDir = "");
131
132 int AddEntryFromHost(const Utf8Str &strFile, PCRTFSOBJINFO pcObjInfo);
133 int AddDirFromHost(const Utf8Str &strPath, const Utf8Str &strSubDir, char *pszPathReal, size_t cbPathReal, PRTDIRENTRYEX pDirEntry);
134
135public:
136
137 /** The guest session task object this list is working on. */
138 const GuestSessionTask &mTask;
139 /** File system filter / options to use for this task. */
140 GuestSessionFsSourceSpec mSourceSpec;
141 /** The source' root path. Always in the source's path style!
142 *
143 * For a single file list this is the full (absolute) path to a file,
144 * for a directory list this is the source root directory. */
145 Utf8Str mSrcRootAbs;
146 /** The destinations's root path. Always in the destination's path style!
147 *
148 * For a single file list this is the full (absolute) path to a file,
149 * for a directory list this is the destination root directory. */
150 Utf8Str mDstRootAbs;
151 /** Total size (in bytes) of all list entries together. */
152 uint64_t mcbTotalSize;
153 /** List of file system entries this list contains. */
154 FsEntries mVecEntries;
155};
156
157/** A set of FsList lists. */
158typedef std::vector<FsList *> FsLists;
159
160/**
161 * Abstract base class for a lenghtly per-session operation which
162 * runs in a Main worker thread.
163 */
164class GuestSessionTask
165 : public ThreadTask
166{
167public:
168 DECLARE_TRANSLATE_METHODS(GuestSessionTask)
169
170 GuestSessionTask(GuestSession *pSession);
171
172 virtual ~GuestSessionTask(void);
173
174public:
175
176 /**
177 * Function which implements the actual task to perform.
178 *
179 * @returns VBox status code.
180 */
181 virtual int Run(void) = 0;
182
183 void handler()
184 {
185 int vrc = Run();
186 NOREF(vrc);
187 /** @todo
188 *
189 * r=bird: what was your idea WRT to Run status code and async tasks?
190 *
191 */
192 }
193
194 // unused: int RunAsync(const Utf8Str &strDesc, ComObjPtr<Progress> &pProgress);
195
196 virtual HRESULT Init(const Utf8Str &strTaskDesc)
197 {
198 setTaskDesc(strTaskDesc);
199 int rc = createAndSetProgressObject(); /* Single operation by default. */
200 if (RT_FAILURE(rc))
201 return E_FAIL;
202
203 return S_OK;
204 }
205
206 /** Returns the task's progress object. */
207 const ComObjPtr<Progress>& GetProgressObject(void) const { return mProgress; }
208
209 /** Returns the task's guest session object. */
210 const ComObjPtr<GuestSession>& GetSession(void) const { return mSession; }
211
212protected:
213
214 /** @name Directory handling primitives.
215 * @{ */
216 int directoryCreateOnGuest(const com::Utf8Str &strPath,
217 DirectoryCreateFlag_T enmDirectoryCreateFlags, uint32_t fMode,
218 bool fFollowSymlinks, bool fCanExist);
219 int directoryCreateOnHost(const com::Utf8Str &strPath, uint32_t fCreate, uint32_t fMode, bool fCanExist);
220 /** @} */
221
222 /** @name File handling primitives.
223 * @{ */
224 int fileCopyFromGuestInner(const Utf8Str &strSrcFile, ComObjPtr<GuestFile> &srcFile,
225 const Utf8Str &strDstFile, PRTFILE phDstFile,
226 FileCopyFlag_T fFileCopyFlags, uint64_t offCopy, uint64_t cbSize);
227 int fileCopyFromGuest(const Utf8Str &strSource, const Utf8Str &strDest, FileCopyFlag_T fFileCopyFlags);
228 int fileCopyToGuestInner(const Utf8Str &strSrcFile, RTVFSFILE hSrcFile,
229 const Utf8Str &strDstFile, ComObjPtr<GuestFile> &dstFile,
230 FileCopyFlag_T fFileCopyFlags, uint64_t offCopy, uint64_t cbSize);
231
232 int fileCopyToGuest(const Utf8Str &strSource, const Utf8Str &strDest, FileCopyFlag_T fFileCopyFlags);
233 /** @} */
234
235 /** @name Guest property handling primitives.
236 * @{ */
237 int getGuestProperty(const ComObjPtr<Guest> &pGuest, const Utf8Str &strPath, Utf8Str &strValue);
238 /** @} */
239
240 int setProgress(ULONG uPercent);
241 int setProgressSuccess(void);
242 HRESULT setProgressErrorMsg(HRESULT hr, const Utf8Str &strMsg);
243 HRESULT setProgressErrorMsg(HRESULT hr, const Utf8Str &strMsg, const GuestErrorInfo &guestErrorInfo);
244
245 inline void setTaskDesc(const Utf8Str &strTaskDesc) throw()
246 {
247 mDesc = strTaskDesc;
248 }
249
250 int createAndSetProgressObject(ULONG cOperations = 1);
251
252protected:
253
254 Utf8Str mDesc;
255 /** The guest session object this task is working on. */
256 ComObjPtr<GuestSession> mSession;
257 /** Progress object for getting updated when running
258 * asynchronously. Optional. */
259 ComObjPtr<Progress> mProgress;
260 /** The guest's path style as char representation (depending on the guest OS type set). */
261 Utf8Str mstrGuestPathStyle;
262};
263
264/**
265 * Task for opening a guest session.
266 */
267class GuestSessionTaskOpen : public GuestSessionTask
268{
269public:
270
271 GuestSessionTaskOpen(GuestSession *pSession,
272 uint32_t uFlags,
273 uint32_t uTimeoutMS);
274 virtual ~GuestSessionTaskOpen(void);
275 int Run(void);
276
277protected:
278
279 /** Session creation flags. */
280 uint32_t mFlags;
281 /** Session creation timeout (in ms). */
282 uint32_t mTimeoutMS;
283};
284
285class GuestSessionCopyTask : public GuestSessionTask
286{
287public:
288 DECLARE_TRANSLATE_METHODS(GuestSessionCopyTask)
289
290 GuestSessionCopyTask(GuestSession *pSession);
291 virtual ~GuestSessionCopyTask();
292
293protected:
294
295 /** Source set. */
296 GuestSessionFsSourceSet mSources;
297 /** Destination to copy to. */
298 Utf8Str mDest;
299 /** Vector of file system lists to handle.
300 * This either can be from the guest or the host side. */
301 FsLists mVecLists;
302};
303
304/**
305 * Guest session task for copying files / directories from guest to the host.
306 */
307class GuestSessionTaskCopyFrom : public GuestSessionCopyTask
308{
309public:
310 DECLARE_TRANSLATE_METHODS(GuestSessionTaskCopyFrom)
311
312 GuestSessionTaskCopyFrom(GuestSession *pSession, GuestSessionFsSourceSet const &vecSrc, const Utf8Str &strDest);
313 virtual ~GuestSessionTaskCopyFrom(void);
314
315 HRESULT Init(const Utf8Str &strTaskDesc);
316 int Run(void);
317};
318
319/**
320 * Task for copying directories from host to the guest.
321 */
322class GuestSessionTaskCopyTo : public GuestSessionCopyTask
323{
324public:
325 DECLARE_TRANSLATE_METHODS(GuestSessionTaskCopyTo)
326
327 GuestSessionTaskCopyTo(GuestSession *pSession, GuestSessionFsSourceSet const &vecSrc, const Utf8Str &strDest);
328 virtual ~GuestSessionTaskCopyTo(void);
329
330 HRESULT Init(const Utf8Str &strTaskDesc);
331 int Run(void);
332};
333
334/**
335 * Guest session task for automatically updating the Guest Additions on the guest.
336 */
337class GuestSessionTaskUpdateAdditions : public GuestSessionTask
338{
339public:
340 DECLARE_TRANSLATE_METHODS(GuestSessionTaskUpdateAdditions)
341
342 GuestSessionTaskUpdateAdditions(GuestSession *pSession, const Utf8Str &strSource,
343 const ProcessArguments &aArguments, uint32_t fFlags);
344 virtual ~GuestSessionTaskUpdateAdditions(void);
345 int Run(void);
346
347protected:
348
349 /**
350 * Suported OS types for automatic updating.
351 */
352 enum eOSType
353 {
354 eOSType_Unknown = 0,
355 eOSType_Windows = 1,
356 eOSType_Linux = 2,
357 eOSType_Solaris = 3
358 };
359
360 /**
361 * Structure representing a file to
362 * get off the .ISO, copied to the guest.
363 */
364 struct ISOFile
365 {
366 ISOFile(const Utf8Str &aSource,
367 const Utf8Str &aDest,
368 uint32_t aFlags = 0)
369 : strSource(aSource),
370 strDest(aDest),
371 fFlags(aFlags) { }
372
373 ISOFile(const Utf8Str &aSource,
374 const Utf8Str &aDest,
375 uint32_t aFlags,
376 const GuestProcessStartupInfo &aStartupInfo)
377 : strSource(aSource),
378 strDest(aDest),
379 fFlags(aFlags),
380 mProcInfo(aStartupInfo)
381 {
382 mProcInfo.mExecutable = strDest;
383 if (mProcInfo.mName.isEmpty())
384 mProcInfo.mName = strDest;
385 }
386
387 /** Source file on .ISO. */
388 Utf8Str strSource;
389 /** Destination file on the guest. */
390 Utf8Str strDest;
391 /** ISO file flags (see ISOFILE_FLAG_ defines). */
392 uint32_t fFlags;
393 /** Optional arguments if this file needs to be
394 * executed. */
395 GuestProcessStartupInfo mProcInfo;
396 };
397
398 int addProcessArguments(ProcessArguments &aArgumentsDest, const ProcessArguments &aArgumentsSource);
399 int copyFileToGuest(GuestSession *pSession, RTVFS hVfsIso, Utf8Str const &strFileSource, const Utf8Str &strFileDest, bool fOptional);
400 int runFileOnGuest(GuestSession *pSession, GuestProcessStartupInfo &procInfo);
401
402 /** Files to handle. */
403 std::vector<ISOFile> mFiles;
404 /** The (optionally) specified Guest Additions .ISO on the host
405 * which will be used for the updating process. */
406 Utf8Str mSource;
407 /** (Optional) installer command line arguments. */
408 ProcessArguments mArguments;
409 /** Update flags. */
410 uint32_t mFlags;
411};
412#endif /* !MAIN_INCLUDED_GuestSessionImplTasks_h */
Note: See TracBrowser for help on using the repository browser.

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