VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestCtrlImplFile.cpp@ 39895

Last change on this file since 39895 was 39850, checked in by vboxsync, 13 years ago

Main/GuestCtrlImplFile: Fixed return value (typo).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
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
35STDMETHODIMP 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
62HRESULT 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
97HRESULT 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#if 0
147 /* Dump the parsed stream contents to Log(). */
148 stdOut[0].Dump();
149#endif
150 const char *pszFsType = stdOut[0].GetString("ftype");
151 if (!pszFsType) /* Was an object found at all? */
152 rc = VERR_FILE_NOT_FOUND;
153 if ( RT_SUCCESS(rc)
154 && strcmp(pszFsType, "-")) /* Regular file? */
155 {
156 rc = VERR_FILE_NOT_FOUND;
157 /* This is not critical for Main, so don't set hr --
158 * we will take care of rc then. */
159 }
160 if ( RT_SUCCESS(rc)
161 && aObjInfo) /* Do we want object details? */
162 {
163 rc = executeStreamQueryFsObjInfo(aFile, stdOut[0],
164 aObjInfo, enmAddAttribs);
165 }
166 }
167 else
168 rc = VERR_NO_DATA;
169
170 if (pRC)
171 *pRC = rc;
172 }
173 }
174 catch (std::bad_alloc &)
175 {
176 hr = E_OUTOFMEMORY;
177 }
178
179 return hr;
180}
181#endif /* VBOX_WITH_GUEST_CONTROL */
182
183STDMETHODIMP Guest::FileQuerySize(IN_BSTR aFile, IN_BSTR aUsername, IN_BSTR aPassword, LONG64 *aSize)
184{
185#ifndef VBOX_WITH_GUEST_CONTROL
186 ReturnComNotImplemented();
187#else /* VBOX_WITH_GUEST_CONTROL */
188 using namespace guestControl;
189
190 CheckComArgStrNotEmptyOrNull(aFile);
191
192 /* Do not allow anonymous executions (with system rights). */
193 if (RT_UNLIKELY((aUsername) == NULL || *(aUsername) == '\0'))
194 return setError(E_INVALIDARG, tr("No user name specified"));
195
196 AutoCaller autoCaller(this);
197 if (FAILED(autoCaller.rc())) return autoCaller.rc();
198
199 return fileQuerySizeInternal(aFile,
200 aUsername, aPassword, aSize);
201#endif
202}
203
204#ifdef VBOX_WITH_GUEST_CONTROL
205HRESULT Guest::fileQuerySizeInternal(IN_BSTR aFile, IN_BSTR aUsername, IN_BSTR aPassword, LONG64 *aSize)
206{
207 using namespace guestControl;
208
209 CheckComArgStrNotEmptyOrNull(aFile);
210
211 int rc;
212 RTFSOBJINFO objInfo;
213 HRESULT hr = fileQueryInfoInternal(aFile,
214 aUsername, aPassword,
215 &objInfo, RTFSOBJATTRADD_NOTHING, &rc);
216 if (SUCCEEDED(hr))
217 {
218 switch (rc)
219 {
220 case VINF_SUCCESS:
221 *aSize = objInfo.cbObject;
222 break;
223
224 case VERR_FILE_NOT_FOUND:
225 hr = setError(VBOX_E_IPRT_ERROR,
226 Guest::tr("File not found"));
227 break;
228
229 default:
230 hr = setError(VBOX_E_IPRT_ERROR,
231 Guest::tr("Unable to query file size (%Rrc)"), rc);
232 break;
233 }
234 }
235
236 return hr;
237}
238#endif /* VBOX_WITH_GUEST_CONTROL */
239
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