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 | #endif
|
---|
51 | }
|
---|
52 |
|
---|
53 | #ifdef VBOX_WITH_GUEST_CONTROL
|
---|
54 | HRESULT Guest::fileExistsInternal(IN_BSTR aFile, IN_BSTR aUsername, IN_BSTR aPassword, BOOL *aExists)
|
---|
55 | {
|
---|
56 | using namespace guestControl;
|
---|
57 |
|
---|
58 | CheckComArgStrNotEmptyOrNull(aFile);
|
---|
59 |
|
---|
60 | AutoCaller autoCaller(this);
|
---|
61 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
62 |
|
---|
63 | RTFSOBJINFO objInfo;
|
---|
64 | int rc;
|
---|
65 | HRESULT hr = fileQueryInfoInternal(aFile,
|
---|
66 | aUsername, aPassword,
|
---|
67 | &objInfo, RTFSOBJATTRADD_NOTHING, &rc);
|
---|
68 | if (SUCCEEDED(hr))
|
---|
69 | {
|
---|
70 | switch (rc)
|
---|
71 | {
|
---|
72 | case VINF_SUCCESS:
|
---|
73 | *aExists = TRUE;
|
---|
74 | break;
|
---|
75 |
|
---|
76 | case VERR_FILE_NOT_FOUND:
|
---|
77 | *aExists = FALSE;
|
---|
78 | break;
|
---|
79 |
|
---|
80 | case VERR_NOT_FOUND:
|
---|
81 | rc = setError(VBOX_E_IPRT_ERROR,
|
---|
82 | Guest::tr("Unable to query file existence"));
|
---|
83 | break;
|
---|
84 |
|
---|
85 | default:
|
---|
86 | AssertReleaseMsgFailed(("fileExistsInternal: Unknown return value (%Rrc)\n", rc));
|
---|
87 | break;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return hr;
|
---|
91 | }
|
---|
92 |
|
---|
93 | HRESULT Guest::fileQueryInfoInternal(IN_BSTR aFile,
|
---|
94 | IN_BSTR aUsername, IN_BSTR aPassword,
|
---|
95 | PRTFSOBJINFO aObjInfo, RTFSOBJATTRADD enmAddAttribs,
|
---|
96 | int *pRC)
|
---|
97 | {
|
---|
98 | using namespace guestControl;
|
---|
99 |
|
---|
100 | /* aUsername is optional. */
|
---|
101 | /* aPassword is optional. */
|
---|
102 | /* aObjInfo is optional. */
|
---|
103 |
|
---|
104 | HRESULT hr;
|
---|
105 | try
|
---|
106 | {
|
---|
107 | Utf8Str Utf8File(aFile);
|
---|
108 | Utf8Str Utf8Username(aUsername);
|
---|
109 | Utf8Str Utf8Password(aPassword);
|
---|
110 |
|
---|
111 | com::SafeArray<IN_BSTR> args;
|
---|
112 | com::SafeArray<IN_BSTR> env;
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Prepare tool command line.
|
---|
116 | */
|
---|
117 |
|
---|
118 | /* We need to get output which is machine-readable in form
|
---|
119 | * of "key=value\0..key=value\0\0". */
|
---|
120 | args.push_back(Bstr("--machinereadable").raw());
|
---|
121 |
|
---|
122 | /* Only the actual file name to chekc is needed for now. */
|
---|
123 | args.push_back(Bstr(Utf8File).raw());
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Execute guest process.
|
---|
127 | */
|
---|
128 | ULONG uPID;
|
---|
129 | hr = executeAndWaitForTool(Bstr(VBOXSERVICE_TOOL_STAT).raw(), Bstr("Querying file information").raw(),
|
---|
130 | ComSafeArrayAsInParam(args),
|
---|
131 | ComSafeArrayAsInParam(env),
|
---|
132 | aUsername, aPassword,
|
---|
133 | NULL /* Progress */, &uPID);
|
---|
134 | if (SUCCEEDED(hr))
|
---|
135 | {
|
---|
136 | GuestCtrlStreamObjects streamObjs;
|
---|
137 | hr = executeStreamParse(uPID, streamObjs);
|
---|
138 | if (SUCCEEDED(hr))
|
---|
139 | {
|
---|
140 | int rc = VINF_SUCCESS;
|
---|
141 |
|
---|
142 | GuestProcessStreamBlock *pBlock = streamObjs[0];
|
---|
143 | AssertPtr(pBlock);
|
---|
144 | const char *pszFsType = pBlock->GetString("ftype");
|
---|
145 | if (!pszFsType) /* Attribute missing? */
|
---|
146 | rc = VERR_NOT_FOUND;
|
---|
147 | if ( RT_SUCCESS(rc)
|
---|
148 | && strcmp(pszFsType, "-")) /* Regular file? */
|
---|
149 | {
|
---|
150 | rc = VERR_FILE_NOT_FOUND;
|
---|
151 | }
|
---|
152 | if ( RT_SUCCESS(rc)
|
---|
153 | && aObjInfo) /* Do we want object details? */
|
---|
154 | {
|
---|
155 | hr = executeStreamQueryFsObjInfo(aFile, pBlock,
|
---|
156 | aObjInfo, enmAddAttribs);
|
---|
157 | }
|
---|
158 |
|
---|
159 | executeStreamFree(streamObjs);
|
---|
160 |
|
---|
161 | if (pRC)
|
---|
162 | *pRC = rc;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | catch (std::bad_alloc &)
|
---|
167 | {
|
---|
168 | hr = E_OUTOFMEMORY;
|
---|
169 | }
|
---|
170 |
|
---|
171 | return hr;
|
---|
172 | }
|
---|
173 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
174 |
|
---|
175 | STDMETHODIMP Guest::FileQuerySize(IN_BSTR aFile, IN_BSTR aUsername, IN_BSTR aPassword, LONG64 *aSize)
|
---|
176 | {
|
---|
177 | #ifndef VBOX_WITH_GUEST_CONTROL
|
---|
178 | ReturnComNotImplemented();
|
---|
179 | #else /* VBOX_WITH_GUEST_CONTROL */
|
---|
180 | using namespace guestControl;
|
---|
181 |
|
---|
182 | CheckComArgStrNotEmptyOrNull(aFile);
|
---|
183 |
|
---|
184 | /* Do not allow anonymous executions (with system rights). */
|
---|
185 | if (RT_UNLIKELY((aUsername) == NULL || *(aUsername) == '\0'))
|
---|
186 | return setError(E_INVALIDARG, tr("No user name specified"));
|
---|
187 |
|
---|
188 | AutoCaller autoCaller(this);
|
---|
189 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
190 |
|
---|
191 | return fileQuerySizeInternal(aFile,
|
---|
192 | aUsername, aPassword, aSize);
|
---|
193 | #endif
|
---|
194 | }
|
---|
195 |
|
---|
196 | #ifdef VBOX_WITH_GUEST_CONTROL
|
---|
197 | HRESULT Guest::fileQuerySizeInternal(IN_BSTR aFile, IN_BSTR aUsername, IN_BSTR aPassword, LONG64 *aSize)
|
---|
198 | {
|
---|
199 | using namespace guestControl;
|
---|
200 |
|
---|
201 | CheckComArgStrNotEmptyOrNull(aFile);
|
---|
202 |
|
---|
203 | int rc;
|
---|
204 | RTFSOBJINFO objInfo;
|
---|
205 | HRESULT hr = fileQueryInfoInternal(aFile,
|
---|
206 | aUsername, aPassword,
|
---|
207 | &objInfo, RTFSOBJATTRADD_NOTHING, &rc);
|
---|
208 | if (SUCCEEDED(hr))
|
---|
209 | {
|
---|
210 | switch (rc)
|
---|
211 | {
|
---|
212 | case VINF_SUCCESS:
|
---|
213 | *aSize = objInfo.cbObject;
|
---|
214 | break;
|
---|
215 |
|
---|
216 | case VERR_FILE_NOT_FOUND:
|
---|
217 | rc = setError(VBOX_E_IPRT_ERROR,
|
---|
218 | Guest::tr("File not found"));
|
---|
219 | break;
|
---|
220 |
|
---|
221 | case VERR_NOT_FOUND:
|
---|
222 | rc = setError(VBOX_E_IPRT_ERROR,
|
---|
223 | Guest::tr("Unable to query file size"));
|
---|
224 | break;
|
---|
225 |
|
---|
226 | default:
|
---|
227 | AssertReleaseMsgFailed(("fileExistsInternal: Unknown return value (%Rrc)\n", rc));
|
---|
228 | break;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | return rc;
|
---|
232 | }
|
---|
233 | #endif /* VBOX_WITH_GUEST_CONTROL */
|
---|
234 |
|
---|