Release 1.5.29.
[wine] / dlls / qmgr / file.c
1 /*
2  * Queue Manager (BITS) File
3  *
4  * Copyright 2007, 2008 Google (Roy Shea, Dan Hipschman)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
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"
32
33 #include "qmgr.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
37
38 static inline BackgroundCopyFileImpl *impl_from_IBackgroundCopyFile(IBackgroundCopyFile *iface)
39 {
40     return CONTAINING_RECORD(iface, BackgroundCopyFileImpl, IBackgroundCopyFile_iface);
41 }
42
43 static HRESULT WINAPI BITS_IBackgroundCopyFile_QueryInterface(
44     IBackgroundCopyFile* iface,
45     REFIID riid,
46     void **obj)
47 {
48     BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
49
50     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), obj);
51
52     if (IsEqualGUID(riid, &IID_IUnknown)
53         || IsEqualGUID(riid, &IID_IBackgroundCopyFile))
54     {
55         *obj = iface;
56         IBackgroundCopyFile_AddRef(iface);
57         return S_OK;
58     }
59
60     *obj = NULL;
61     return E_NOINTERFACE;
62 }
63
64 static ULONG WINAPI BITS_IBackgroundCopyFile_AddRef(IBackgroundCopyFile* iface)
65 {
66     BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
67     ULONG ref = InterlockedIncrement(&This->ref);
68     TRACE("(%p)->(%d)\n", This, ref);
69     return ref;
70 }
71
72 static ULONG WINAPI BITS_IBackgroundCopyFile_Release(
73     IBackgroundCopyFile* iface)
74 {
75     BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
76     ULONG ref = InterlockedDecrement(&This->ref);
77
78     TRACE("(%p)->(%d)\n", This, ref);
79
80     if (ref == 0)
81     {
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);
86     }
87
88     return ref;
89 }
90
91 /* Get the remote name of a background copy file */
92 static HRESULT WINAPI BITS_IBackgroundCopyFile_GetRemoteName(
93     IBackgroundCopyFile* iface,
94     LPWSTR *pVal)
95 {
96     BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
97     int n = (lstrlenW(This->info.RemoteName) + 1) * sizeof(WCHAR);
98
99     *pVal = CoTaskMemAlloc(n);
100     if (!*pVal)
101         return E_OUTOFMEMORY;
102
103     memcpy(*pVal, This->info.RemoteName, n);
104     return S_OK;
105 }
106
107 static HRESULT WINAPI BITS_IBackgroundCopyFile_GetLocalName(
108     IBackgroundCopyFile* iface,
109     LPWSTR *pVal)
110 {
111     BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
112     int n = (lstrlenW(This->info.LocalName) + 1) * sizeof(WCHAR);
113
114     *pVal = CoTaskMemAlloc(n);
115     if (!*pVal)
116         return E_OUTOFMEMORY;
117
118     memcpy(*pVal, This->info.LocalName, n);
119     return S_OK;
120 }
121
122 static HRESULT WINAPI BITS_IBackgroundCopyFile_GetProgress(
123     IBackgroundCopyFile* iface,
124     BG_FILE_PROGRESS *pVal)
125 {
126     BackgroundCopyFileImpl *This = impl_from_IBackgroundCopyFile(iface);
127
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);
133
134     return S_OK;
135 }
136
137 static const IBackgroundCopyFileVtbl BITS_IBackgroundCopyFile_Vtbl =
138 {
139     BITS_IBackgroundCopyFile_QueryInterface,
140     BITS_IBackgroundCopyFile_AddRef,
141     BITS_IBackgroundCopyFile_Release,
142     BITS_IBackgroundCopyFile_GetRemoteName,
143     BITS_IBackgroundCopyFile_GetLocalName,
144     BITS_IBackgroundCopyFile_GetProgress
145 };
146
147 HRESULT BackgroundCopyFileConstructor(BackgroundCopyJobImpl *owner,
148                                       LPCWSTR remoteName, LPCWSTR localName,
149                                       BackgroundCopyFileImpl **file)
150 {
151     BackgroundCopyFileImpl *This;
152     int n;
153
154     TRACE("(%s, %s, %p)\n", debugstr_w(remoteName), debugstr_w(localName), file);
155
156     This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
157     if (!This)
158         return E_OUTOFMEMORY;
159
160     n = (lstrlenW(remoteName) + 1) * sizeof(WCHAR);
161     This->info.RemoteName = HeapAlloc(GetProcessHeap(), 0, n);
162     if (!This->info.RemoteName)
163     {
164         HeapFree(GetProcessHeap(), 0, This);
165         return E_OUTOFMEMORY;
166     }
167     memcpy(This->info.RemoteName, remoteName, n);
168
169     n = (lstrlenW(localName) + 1) * sizeof(WCHAR);
170     This->info.LocalName = HeapAlloc(GetProcessHeap(), 0, n);
171     if (!This->info.LocalName)
172     {
173         HeapFree(GetProcessHeap(), 0, This->info.RemoteName);
174         HeapFree(GetProcessHeap(), 0, This);
175         return E_OUTOFMEMORY;
176     }
177     memcpy(This->info.LocalName, localName, n);
178
179     This->IBackgroundCopyFile_iface.lpVtbl = &BITS_IBackgroundCopyFile_Vtbl;
180     This->ref = 1;
181
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);
187
188     *file = This;
189     return S_OK;
190 }
191
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)
201 {
202     BackgroundCopyFileImpl *file = obj;
203     BackgroundCopyJobImpl *job = file->owner;
204     ULONG64 diff;
205
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);
214
215     return (job->state == BG_JOB_STATE_TRANSFERRING
216             ? PROGRESS_CONTINUE
217             : PROGRESS_CANCEL);
218 }
219
220 typedef struct
221 {
222     IBindStatusCallback IBindStatusCallback_iface;
223     BackgroundCopyFileImpl *file;
224     LONG ref;
225 } DLBindStatusCallback;
226
227 static inline DLBindStatusCallback *impl_from_IBindStatusCallback(IBindStatusCallback *iface)
228 {
229     return CONTAINING_RECORD(iface, DLBindStatusCallback, IBindStatusCallback_iface);
230 }
231
232 static ULONG WINAPI DLBindStatusCallback_AddRef(IBindStatusCallback *iface)
233 {
234     DLBindStatusCallback *This = impl_from_IBindStatusCallback(iface);
235     return InterlockedIncrement(&This->ref);
236 }
237
238 static ULONG WINAPI DLBindStatusCallback_Release(IBindStatusCallback *iface)
239 {
240     DLBindStatusCallback *This = impl_from_IBindStatusCallback(iface);
241     ULONG ref = InterlockedDecrement(&This->ref);
242
243     if (ref == 0)
244     {
245         IBackgroundCopyFile_Release(&This->file->IBackgroundCopyFile_iface);
246         HeapFree(GetProcessHeap(), 0, This);
247     }
248
249     return ref;
250 }
251
252 static HRESULT WINAPI DLBindStatusCallback_QueryInterface(
253     IBindStatusCallback *iface,
254     REFIID riid,
255     void **ppvObject)
256 {
257     DLBindStatusCallback *This = impl_from_IBindStatusCallback(iface);
258
259     if (IsEqualGUID(riid, &IID_IUnknown)
260         || IsEqualGUID(riid, &IID_IBindStatusCallback))
261     {
262         *ppvObject = &This->IBindStatusCallback_iface;
263         DLBindStatusCallback_AddRef(iface);
264         return S_OK;
265     }
266
267     *ppvObject = NULL;
268     return E_NOINTERFACE;
269 }
270
271 static HRESULT WINAPI DLBindStatusCallback_GetBindInfo(
272     IBindStatusCallback *iface,
273     DWORD *grfBINDF,
274     BINDINFO *pbindinfo)
275 {
276     return E_NOTIMPL;
277 }
278
279 static HRESULT WINAPI DLBindStatusCallback_GetPriority(
280     IBindStatusCallback *iface,
281     LONG *pnPriority)
282 {
283     return E_NOTIMPL;
284 }
285
286 static HRESULT WINAPI DLBindStatusCallback_OnDataAvailable(
287     IBindStatusCallback *iface,
288     DWORD grfBSCF,
289     DWORD dwSize,
290     FORMATETC *pformatetc,
291     STGMEDIUM *pstgmed)
292 {
293     return E_NOTIMPL;
294 }
295
296 static HRESULT WINAPI DLBindStatusCallback_OnLowResource(
297     IBindStatusCallback *iface,
298     DWORD reserved)
299 {
300     return E_NOTIMPL;
301 }
302
303 static HRESULT WINAPI DLBindStatusCallback_OnObjectAvailable(
304     IBindStatusCallback *iface,
305     REFIID riid,
306     IUnknown *punk)
307 {
308     return E_NOTIMPL;
309 }
310
311 static HRESULT WINAPI DLBindStatusCallback_OnProgress(
312     IBindStatusCallback *iface,
313     ULONG progress,
314     ULONG progressMax,
315     ULONG statusCode,
316     LPCWSTR statusText)
317 {
318     DLBindStatusCallback *This = impl_from_IBindStatusCallback(iface);
319     BackgroundCopyFileImpl *file = This->file;
320     BackgroundCopyJobImpl *job = file->owner;
321     ULONG64 diff;
322
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);
331
332     return S_OK;
333 }
334
335 static HRESULT WINAPI DLBindStatusCallback_OnStartBinding(
336     IBindStatusCallback *iface,
337     DWORD dwReserved,
338     IBinding *pib)
339 {
340     return E_NOTIMPL;
341 }
342
343 static HRESULT WINAPI DLBindStatusCallback_OnStopBinding(
344     IBindStatusCallback *iface,
345     HRESULT hresult,
346     LPCWSTR szError)
347 {
348     return E_NOTIMPL;
349 }
350
351 static const IBindStatusCallbackVtbl DLBindStatusCallback_Vtbl =
352 {
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
364 };
365
366 static DLBindStatusCallback *DLBindStatusCallbackConstructor(
367     BackgroundCopyFileImpl *file)
368 {
369     DLBindStatusCallback *This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
370     if (!This)
371         return NULL;
372
373     This->IBindStatusCallback_iface.lpVtbl = &DLBindStatusCallback_Vtbl;
374     IBackgroundCopyFile_AddRef(&file->IBackgroundCopyFile_iface);
375     This->file = file;
376     This->ref = 1;
377     return This;
378 }
379
380 BOOL processFile(BackgroundCopyFileImpl *file, BackgroundCopyJobImpl *job)
381 {
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;
387
388     if (!GetTempPathW(MAX_PATH, tmpDir))
389     {
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;
394     }
395
396     if (!GetTempFileNameW(tmpDir, prefix, 0, tmpName))
397     {
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;
402     }
403
404     callbackObj = DLBindStatusCallbackConstructor(file);
405     if (!callbackObj)
406     {
407         ERR("Out of memory\n");
408         transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR);
409         return FALSE;
410     }
411
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);
417
418     TRACE("Transferring: %s -> %s -> %s\n",
419           debugstr_w(file->info.RemoteName),
420           debugstr_w(tmpName),
421           debugstr_w(file->info.LocalName));
422
423     transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSFERRING);
424
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)
430     {
431         TRACE("URLDownload failed, trying local file copy\n");
432         if (!CopyFileExW(file->info.RemoteName, tmpName, copyProgressCallback,
433                          file, NULL, 0))
434         {
435             ERR("Local file copy failed: error %d\n", GetLastError());
436             transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
437             return FALSE;
438         }
439     }
440     else if (FAILED(hr))
441     {
442         ERR("URLDownload failed: eh 0x%08x\n", hr);
443         transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
444         return FALSE;
445     }
446
447     if (transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_QUEUED))
448     {
449         lstrcpyW(file->tempFileName, tmpName);
450
451         EnterCriticalSection(&job->cs);
452         file->fileProgress.Completed = TRUE;
453         job->jobProgress.FilesTransferred++;
454         LeaveCriticalSection(&job->cs);
455
456         return TRUE;
457     }
458     else
459     {
460         DeleteFileW(tmpName);
461         return FALSE;
462     }
463 }