1 | /* $Id: */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Guest Control - Guest file handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011 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 | #include "GuestImpl.h"
|
---|
19 | #include "GuestCtrlImplPrivate.h"
|
---|
20 |
|
---|
21 | #include "Global.h"
|
---|
22 | #include "ConsoleImpl.h"
|
---|
23 | #include "ProgressImpl.h"
|
---|
24 | #include "VMMDev.h"
|
---|
25 |
|
---|
26 | #include "AutoCaller.h"
|
---|
27 | #include "Logging.h"
|
---|
28 |
|
---|
29 | #include <VBox/VMMDev.h>
|
---|
30 | #ifdef VBOX_WITH_GUEST_CONTROL
|
---|
31 | # include <VBox/com/array.h>
|
---|
32 | # include <VBox/com/ErrorInfo.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | STDMETHODIMP Guest::FileExists(IN_BSTR aFile, IN_BSTR aUsername, IN_BSTR aPassword, BOOL *aExists)
|
---|
36 | {
|
---|
37 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
38 | ReturnComNotImplemented();
|
---|
39 | #else /* VBOX_WITH_GUEST_CONTROL */
|
---|
40 | using namespace guestControl;
|
---|
41 |
|
---|
42 | CheckComArgStrNotEmptyOrNull(aFile);
|
---|
43 |
|
---|
44 | /* Do not allow anonymous executions (with system rights). */
|
---|
45 | if (RT_UNLIKELY((aUsername) == NULL || *(aUsername) == '\0'))
|
---|
46 | return setError(E_INVALIDARG, tr("No user name specified"));
|
---|
47 |
|
---|
48 | return fileExistsInternal(aFile,
|
---|
49 | aUsername, aPassword, aExists,
|
---|
50 | NULL /* rc */);
|
---|
51 | #endif
|
---|
52 | }
|
---|
53 |
|
---|
54 | #ifdef VBOX_WITH_GUEST_CONTROL
|
---|
55 | HRESULT Guest::fileExistsInternal(IN_BSTR aFile, IN_BSTR aUsername, IN_BSTR aPassword, BOOL *aExists, int *pRC)
|
---|
56 | {
|
---|
57 | using namespace guestControl;
|
---|
58 |
|
---|
59 | CheckComArgStrNotEmptyOrNull(aFile);
|
---|
60 |
|
---|
61 | AutoCaller autoCaller(this);
|
---|
62 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
63 |
|
---|
64 | HRESULT rc = S_OK;
|
---|
65 | try
|
---|
66 | {
|
---|
67 | Utf8Str Utf8File(aFile);
|
---|
68 | Utf8Str Utf8Username(aUsername);
|
---|
69 | Utf8Str Utf8Password(aPassword);
|
---|
70 |
|
---|
71 | com::SafeArray<IN_BSTR> args;
|
---|
72 | com::SafeArray<IN_BSTR> env;
|
---|
73 |
|
---|
74 | /*
|
---|
75 | * Prepare tool command line.
|
---|
76 | */
|
---|
77 |
|
---|
78 | /* We need to get output which is machine-readable in form
|
---|
79 | * of "key=value\0..key=value\0\0". */
|
---|
80 | args.push_back(Bstr("--machinereadable").raw());
|
---|
81 |
|
---|
82 | /* Only the actual file name to chekc is needed for now. */
|
---|
83 | args.push_back(Bstr(Utf8File).raw());
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Execute guest process.
|
---|
87 | */
|
---|
88 | rc = executeAndWaitForTool(Bstr(VBOXSERVICE_TOOL_STAT).raw(), Bstr("Checking for file existence").raw(),
|
---|
89 | ComSafeArrayAsInParam(args),
|
---|
90 | ComSafeArrayAsInParam(env),
|
---|
91 | aUsername, aPassword,
|
---|
92 | NULL /* Progress */, NULL /* PID */);
|
---|
93 |
|
---|
94 | /* If the call succeeded the file exists, otherwise it does not. */
|
---|
95 | *aExists = SUCCEEDED(rc) ? TRUE : FALSE;
|
---|
96 | }
|
---|
97 | catch (std::bad_alloc &)
|
---|
98 | {
|
---|
99 | rc = E_OUTOFMEMORY;
|
---|
100 | }
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
104 |
|
---|
105 | STDMETHODIMP Guest::FileQuerySize(IN_BSTR aFile, IN_BSTR aUsername, IN_BSTR aPassword, LONG64 *aSize)
|
---|
106 | {
|
---|
107 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
108 | ReturnComNotImplemented();
|
---|
109 | #else /* VBOX_WITH_GUEST_CONTROL */
|
---|
110 | using namespace guestControl;
|
---|
111 |
|
---|
112 | CheckComArgStrNotEmptyOrNull(aFile);
|
---|
113 |
|
---|
114 | /* Do not allow anonymous executions (with system rights). */
|
---|
115 | if (RT_UNLIKELY((aUsername) == NULL || *(aUsername) == '\0'))
|
---|
116 | return setError(E_INVALIDARG, tr("No user name specified"));
|
---|
117 |
|
---|
118 | return fileQuerySizeInternal(aFile,
|
---|
119 | aUsername, aPassword, aSize,
|
---|
120 | NULL /* rc */);
|
---|
121 | #endif
|
---|
122 | }
|
---|
123 |
|
---|
124 | #ifdef VBOX_WITH_GUEST_CONTROL
|
---|
125 | HRESULT Guest::fileQuerySizeInternal(IN_BSTR aFile, IN_BSTR aUsername, IN_BSTR aPassword, LONG64 *aSize, int *pRC)
|
---|
126 | {
|
---|
127 | using namespace guestControl;
|
---|
128 |
|
---|
129 | CheckComArgStrNotEmptyOrNull(aFile);
|
---|
130 |
|
---|
131 | AutoCaller autoCaller(this);
|
---|
132 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
133 |
|
---|
134 | HRESULT rc = S_OK;
|
---|
135 | try
|
---|
136 | {
|
---|
137 | Utf8Str Utf8File(aFile);
|
---|
138 | Utf8Str Utf8Username(aUsername);
|
---|
139 | Utf8Str Utf8Password(aPassword);
|
---|
140 |
|
---|
141 | com::SafeArray<IN_BSTR> args;
|
---|
142 | com::SafeArray<IN_BSTR> env;
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * Prepare tool command line.
|
---|
146 | */
|
---|
147 |
|
---|
148 | /* We need to get output which is machine-readable in form
|
---|
149 | * of "key=value\0..key=value\0\0". */
|
---|
150 | args.push_back(Bstr("--machinereadable").raw());
|
---|
151 |
|
---|
152 | /* Only the actual file name to chekc is needed for now. */
|
---|
153 | args.push_back(Bstr(Utf8File).raw());
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Execute guest process.
|
---|
157 | */
|
---|
158 | ComPtr<IProgress> progressFileSize;
|
---|
159 | ULONG uPID;
|
---|
160 |
|
---|
161 | rc = executeAndWaitForTool(Bstr(VBOXSERVICE_TOOL_STAT).raw(), Bstr("Querying file size").raw(),
|
---|
162 | ComSafeArrayAsInParam(args),
|
---|
163 | ComSafeArrayAsInParam(env),
|
---|
164 | aUsername, aPassword,
|
---|
165 | progressFileSize.asOutParam(), &uPID);
|
---|
166 | if (SUCCEEDED(rc))
|
---|
167 | {
|
---|
168 | GuestCtrlStreamObjects streamObjs;
|
---|
169 | rc = executeCollectOutput(uPID, streamObjs);
|
---|
170 | if (SUCCEEDED(rc))
|
---|
171 | {
|
---|
172 | /** @todo */
|
---|
173 | #if 0
|
---|
174 | int64_t iVal;
|
---|
175 | vrc = guestStream.GetInt64Ex("st_size", &iVal);
|
---|
176 | if (RT_SUCCESS(vrc))
|
---|
177 | *aSize = iVal;
|
---|
178 | else
|
---|
179 | rc = setError(VBOX_E_IPRT_ERROR,
|
---|
180 | tr("Query file size: Unable to retrieve file size for file \"%s\" (%Rrc)"),
|
---|
181 | Utf8File.c_str(), vrc);
|
---|
182 | #endif
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 | catch (std::bad_alloc &)
|
---|
187 | {
|
---|
188 | rc = E_OUTOFMEMORY;
|
---|
189 | }
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
193 |
|
---|