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