makefiles: Add makedep pragmas for registration idl files.
[wine.git] / dlls / qmgr / file.c
blobccba81b0004086fc3ff270f76f92664cab1d3444
1 /*
2 * Queue Manager (BITS) File
4 * Copyright 2007, 2008 Google (Roy Shea, Dan Hipschman)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
30 #include "urlmon.h"
31 #include "wininet.h"
33 #include "qmgr.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
38 static inline BackgroundCopyFileImpl *impl_from_IBackgroundCopyFile(IBackgroundCopyFile *iface)
40 return CONTAINING_RECORD(iface, BackgroundCopyFileImpl, IBackgroundCopyFile_iface);
43 static HRESULT WINAPI BITS_IBackgroundCopyFile_QueryInterface(
44 IBackgroundCopyFile* iface,
45 REFIID riid,
46 void **obj)
48 BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
50 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
52 if (IsEqualGUID(riid, &IID_IUnknown)
53 || IsEqualGUID(riid, &IID_IBackgroundCopyFile))
55 *obj = iface;
56 IBackgroundCopyFile_AddRef(iface);
57 return S_OK;
60 *obj = NULL;
61 return E_NOINTERFACE;
64 static ULONG WINAPI BITS_IBackgroundCopyFile_AddRef(IBackgroundCopyFile* iface)
66 BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
67 ULONG ref = InterlockedIncrement(&This->ref);
68 TRACE("(%p)->(%d)\n", This, ref);
69 return ref;
72 static ULONG WINAPI BITS_IBackgroundCopyFile_Release(
73 IBackgroundCopyFile* iface)
75 BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
76 ULONG ref = InterlockedDecrement(&This->ref);
78 TRACE("(%p)->(%d)\n", This, ref);
80 if (ref == 0)
82 IBackgroundCopyJob2_Release(&This->owner->IBackgroundCopyJob2_iface);
83 HeapFree(GetProcessHeap(), 0, This->info.LocalName);
84 HeapFree(GetProcessHeap(), 0, This->info.RemoteName);
85 HeapFree(GetProcessHeap(), 0, This);
88 return ref;
91 /* Get the remote name of a background copy file */
92 static HRESULT WINAPI BITS_IBackgroundCopyFile_GetRemoteName(
93 IBackgroundCopyFile* iface,
94 LPWSTR *pVal)
96 BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
97 int n = (lstrlenW(This->info.RemoteName) + 1) * sizeof(WCHAR);
99 *pVal = CoTaskMemAlloc(n);
100 if (!*pVal)
101 return E_OUTOFMEMORY;
103 memcpy(*pVal, This->info.RemoteName, n);
104 return S_OK;
107 static HRESULT WINAPI BITS_IBackgroundCopyFile_GetLocalName(
108 IBackgroundCopyFile* iface,
109 LPWSTR *pVal)
111 BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
112 int n = (lstrlenW(This->info.LocalName) + 1) * sizeof(WCHAR);
114 *pVal = CoTaskMemAlloc(n);
115 if (!*pVal)
116 return E_OUTOFMEMORY;
118 memcpy(*pVal, This->info.LocalName, n);
119 return S_OK;
122 static HRESULT WINAPI BITS_IBackgroundCopyFile_GetProgress(
123 IBackgroundCopyFile* iface,
124 BG_FILE_PROGRESS *pVal)
126 BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
128 EnterCriticalSection(&This->owner->cs);
129 pVal->BytesTotal = This->fileProgress.BytesTotal;
130 pVal->BytesTransferred = This->fileProgress.BytesTransferred;
131 pVal->Completed = This->fileProgress.Completed;
132 LeaveCriticalSection(&This->owner->cs);
134 return S_OK;
137 static const IBackgroundCopyFileVtbl BITS_IBackgroundCopyFile_Vtbl =
139 BITS_IBackgroundCopyFile_QueryInterface,
140 BITS_IBackgroundCopyFile_AddRef,
141 BITS_IBackgroundCopyFile_Release,
142 BITS_IBackgroundCopyFile_GetRemoteName,
143 BITS_IBackgroundCopyFile_GetLocalName,
144 BITS_IBackgroundCopyFile_GetProgress
147 HRESULT BackgroundCopyFileConstructor(BackgroundCopyJobImpl *owner,
148 LPCWSTR remoteName, LPCWSTR localName,
149 BackgroundCopyFileImpl **file)
151 BackgroundCopyFileImpl *This;
152 int n;
154 TRACE("(%s, %s, %p)\n", debugstr_w(remoteName), debugstr_w(localName), file);
156 This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
157 if (!This)
158 return E_OUTOFMEMORY;
160 n = (lstrlenW(remoteName) + 1) * sizeof(WCHAR);
161 This->info.RemoteName = HeapAlloc(GetProcessHeap(), 0, n);
162 if (!This->info.RemoteName)
164 HeapFree(GetProcessHeap(), 0, This);
165 return E_OUTOFMEMORY;
167 memcpy(This->info.RemoteName, remoteName, n);
169 n = (lstrlenW(localName) + 1) * sizeof(WCHAR);
170 This->info.LocalName = HeapAlloc(GetProcessHeap(), 0, n);
171 if (!This->info.LocalName)
173 HeapFree(GetProcessHeap(), 0, This->info.RemoteName);
174 HeapFree(GetProcessHeap(), 0, This);
175 return E_OUTOFMEMORY;
177 memcpy(This->info.LocalName, localName, n);
179 This->IBackgroundCopyFile_iface.lpVtbl = &BITS_IBackgroundCopyFile_Vtbl;
180 This->ref = 1;
182 This->fileProgress.BytesTotal = BG_SIZE_UNKNOWN;
183 This->fileProgress.BytesTransferred = 0;
184 This->fileProgress.Completed = FALSE;
185 This->owner = owner;
186 IBackgroundCopyJob2_AddRef(&owner->IBackgroundCopyJob2_iface);
188 *file = This;
189 return S_OK;
192 static DWORD CALLBACK copyProgressCallback(LARGE_INTEGER totalSize,
193 LARGE_INTEGER totalTransferred,
194 LARGE_INTEGER streamSize,
195 LARGE_INTEGER streamTransferred,
196 DWORD streamNum,
197 DWORD reason,
198 HANDLE srcFile,
199 HANDLE dstFile,
200 LPVOID obj)
202 BackgroundCopyFileImpl *file = obj;
203 BackgroundCopyJobImpl *job = file->owner;
204 ULONG64 diff;
206 EnterCriticalSection(&job->cs);
207 diff = (file->fileProgress.BytesTotal == BG_SIZE_UNKNOWN
208 ? totalTransferred.QuadPart
209 : totalTransferred.QuadPart - file->fileProgress.BytesTransferred);
210 file->fileProgress.BytesTotal = totalSize.QuadPart;
211 file->fileProgress.BytesTransferred = totalTransferred.QuadPart;
212 job->jobProgress.BytesTransferred += diff;
213 LeaveCriticalSection(&job->cs);
215 return (job->state == BG_JOB_STATE_TRANSFERRING
216 ? PROGRESS_CONTINUE
217 : PROGRESS_CANCEL);
220 typedef struct
222 IBindStatusCallback IBindStatusCallback_iface;
223 BackgroundCopyFileImpl *file;
224 LONG ref;
225 } DLBindStatusCallback;
227 static inline DLBindStatusCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
229 return CONTAINING_RECORD(iface, DLBindStatusCallback, IBindStatusCallback_iface);
232 static ULONG WINAPI DLBindStatusCallback_AddRef(IBindStatusCallback *iface)
234 DLBindStatusCallback *This = impl_from_IBindStatusCallback(iface);
235 return InterlockedIncrement(&This->ref);
238 static ULONG WINAPI DLBindStatusCallback_Release(IBindStatusCallback *iface)
240 DLBindStatusCallback *This = impl_from_IBindStatusCallback(iface);
241 ULONG ref = InterlockedDecrement(&This->ref);
243 if (ref == 0)
245 IBackgroundCopyFile_Release(&This->file->IBackgroundCopyFile_iface);
246 HeapFree(GetProcessHeap(), 0, This);
249 return ref;
252 static HRESULT WINAPI DLBindStatusCallback_QueryInterface(
253 IBindStatusCallback *iface,
254 REFIID riid,
255 void **ppvObject)
257 DLBindStatusCallback *This = impl_from_IBindStatusCallback(iface);
259 if (IsEqualGUID(riid, &IID_IUnknown)
260 || IsEqualGUID(riid, &IID_IBindStatusCallback))
262 *ppvObject = &This->IBindStatusCallback_iface;
263 DLBindStatusCallback_AddRef(iface);
264 return S_OK;
267 *ppvObject = NULL;
268 return E_NOINTERFACE;
271 static HRESULT WINAPI DLBindStatusCallback_GetBindInfo(
272 IBindStatusCallback *iface,
273 DWORD *grfBINDF,
274 BINDINFO *pbindinfo)
276 return E_NOTIMPL;
279 static HRESULT WINAPI DLBindStatusCallback_GetPriority(
280 IBindStatusCallback *iface,
281 LONG *pnPriority)
283 return E_NOTIMPL;
286 static HRESULT WINAPI DLBindStatusCallback_OnDataAvailable(
287 IBindStatusCallback *iface,
288 DWORD grfBSCF,
289 DWORD dwSize,
290 FORMATETC *pformatetc,
291 STGMEDIUM *pstgmed)
293 return E_NOTIMPL;
296 static HRESULT WINAPI DLBindStatusCallback_OnLowResource(
297 IBindStatusCallback *iface,
298 DWORD reserved)
300 return E_NOTIMPL;
303 static HRESULT WINAPI DLBindStatusCallback_OnObjectAvailable(
304 IBindStatusCallback *iface,
305 REFIID riid,
306 IUnknown *punk)
308 return E_NOTIMPL;
311 static HRESULT WINAPI DLBindStatusCallback_OnProgress(
312 IBindStatusCallback *iface,
313 ULONG progress,
314 ULONG progressMax,
315 ULONG statusCode,
316 LPCWSTR statusText)
318 DLBindStatusCallback *This = impl_from_IBindStatusCallback(iface);
319 BackgroundCopyFileImpl *file = This->file;
320 BackgroundCopyJobImpl *job = file->owner;
321 ULONG64 diff;
323 EnterCriticalSection(&job->cs);
324 diff = (file->fileProgress.BytesTotal == BG_SIZE_UNKNOWN
325 ? progress
326 : progress - file->fileProgress.BytesTransferred);
327 file->fileProgress.BytesTotal = progressMax ? progressMax : BG_SIZE_UNKNOWN;
328 file->fileProgress.BytesTransferred = progress;
329 job->jobProgress.BytesTransferred += diff;
330 LeaveCriticalSection(&job->cs);
332 return S_OK;
335 static HRESULT WINAPI DLBindStatusCallback_OnStartBinding(
336 IBindStatusCallback *iface,
337 DWORD dwReserved,
338 IBinding *pib)
340 return E_NOTIMPL;
343 static HRESULT WINAPI DLBindStatusCallback_OnStopBinding(
344 IBindStatusCallback *iface,
345 HRESULT hresult,
346 LPCWSTR szError)
348 return E_NOTIMPL;
351 static const IBindStatusCallbackVtbl DLBindStatusCallback_Vtbl =
353 DLBindStatusCallback_QueryInterface,
354 DLBindStatusCallback_AddRef,
355 DLBindStatusCallback_Release,
356 DLBindStatusCallback_OnStartBinding,
357 DLBindStatusCallback_GetPriority,
358 DLBindStatusCallback_OnLowResource,
359 DLBindStatusCallback_OnProgress,
360 DLBindStatusCallback_OnStopBinding,
361 DLBindStatusCallback_GetBindInfo,
362 DLBindStatusCallback_OnDataAvailable,
363 DLBindStatusCallback_OnObjectAvailable
366 static DLBindStatusCallback *DLBindStatusCallbackConstructor(
367 BackgroundCopyFileImpl *file)
369 DLBindStatusCallback *This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
370 if (!This)
371 return NULL;
373 This->IBindStatusCallback_iface.lpVtbl = &DLBindStatusCallback_Vtbl;
374 IBackgroundCopyFile_AddRef(&file->IBackgroundCopyFile_iface);
375 This->file = file;
376 This->ref = 1;
377 return This;
380 BOOL processFile(BackgroundCopyFileImpl *file, BackgroundCopyJobImpl *job)
382 static const WCHAR prefix[] = {'B','I','T', 0};
383 DLBindStatusCallback *callbackObj;
384 WCHAR tmpDir[MAX_PATH];
385 WCHAR tmpName[MAX_PATH];
386 HRESULT hr;
388 if (!GetTempPathW(MAX_PATH, tmpDir))
390 ERR("Couldn't create temp file name: %d\n", GetLastError());
391 /* Guessing on what state this should give us */
392 transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR);
393 return FALSE;
396 if (!GetTempFileNameW(tmpDir, prefix, 0, tmpName))
398 ERR("Couldn't create temp file: %d\n", GetLastError());
399 /* Guessing on what state this should give us */
400 transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR);
401 return FALSE;
404 callbackObj = DLBindStatusCallbackConstructor(file);
405 if (!callbackObj)
407 ERR("Out of memory\n");
408 transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR);
409 return FALSE;
412 EnterCriticalSection(&job->cs);
413 file->fileProgress.BytesTotal = BG_SIZE_UNKNOWN;
414 file->fileProgress.BytesTransferred = 0;
415 file->fileProgress.Completed = FALSE;
416 LeaveCriticalSection(&job->cs);
418 TRACE("Transferring: %s -> %s -> %s\n",
419 debugstr_w(file->info.RemoteName),
420 debugstr_w(tmpName),
421 debugstr_w(file->info.LocalName));
423 transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSFERRING);
425 DeleteUrlCacheEntryW(file->info.RemoteName);
426 hr = URLDownloadToFileW(NULL, file->info.RemoteName, tmpName, 0,
427 &callbackObj->IBindStatusCallback_iface);
428 IBindStatusCallback_Release(&callbackObj->IBindStatusCallback_iface);
429 if (hr == INET_E_DOWNLOAD_FAILURE)
431 TRACE("URLDownload failed, trying local file copy\n");
432 if (!CopyFileExW(file->info.RemoteName, tmpName, copyProgressCallback,
433 file, NULL, 0))
435 ERR("Local file copy failed: error %d\n", GetLastError());
436 transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
437 return FALSE;
440 else if (FAILED(hr))
442 ERR("URLDownload failed: eh 0x%08x\n", hr);
443 transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
444 return FALSE;
447 if (transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_QUEUED))
449 lstrcpyW(file->tempFileName, tmpName);
451 EnterCriticalSection(&job->cs);
452 file->fileProgress.Completed = TRUE;
453 job->jobProgress.FilesTransferred++;
454 LeaveCriticalSection(&job->cs);
456 return TRUE;
458 else
460 DeleteFileW(tmpName);
461 return FALSE;