[3/4] qmgr: Implement local file background "downloads."

Dan Hipschman dsh at linux.ucla.edu
Wed Mar 12 13:57:11 CDT 2008


No difference from previously sent patch.

---
 dlls/qmgr/file.c |  116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 dlls/qmgr/job.c  |   26 ++++++++++++
 dlls/qmgr/qmgr.c |    7 +---
 dlls/qmgr/qmgr.h |   18 ++++++++
 4 files changed, 161 insertions(+), 6 deletions(-)

diff --git a/dlls/qmgr/file.c b/dlls/qmgr/file.c
index f1ae06a..b290404 100644
--- a/dlls/qmgr/file.c
+++ b/dlls/qmgr/file.c
@@ -170,3 +170,119 @@ HRESULT BackgroundCopyFileConstructor(BackgroundCopyJobImpl *owner,
     *ppObj = &This->lpVtbl;
     return S_OK;
 }
+
+BOOL processFile(BackgroundCopyFileImpl *file, BackgroundCopyJobImpl *job)
+{
+    static WCHAR prefix[] = {'B','I','T', 0};
+    WCHAR tmpDir[MAX_PATH];
+    WCHAR tmpName[MAX_PATH];
+    HANDLE src, dst;
+    LARGE_INTEGER lgint;
+    ULONG64 size;
+    char buf[4096];
+    DWORD n;
+
+    /* FIXME: assuming local file transfer */
+    src = CreateFileW(file->info.RemoteName, GENERIC_READ, FILE_SHARE_READ,
+                      NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+    if (src == INVALID_HANDLE_VALUE)
+    {
+        transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_ERROR);
+        return FALSE;
+    }
+
+    if (!GetTempPathW(MAX_PATH, tmpDir))
+    {
+        ERR("Couldn't create temp file name: %d\n", GetLastError());
+        CloseHandle(src);
+        /* Guessing on what state this should give us */
+        transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR);
+        return FALSE;
+    }
+
+    if (!GetTempFileNameW(tmpDir, prefix, 0, tmpName))
+    {
+        ERR("Couldn't create temp file: %d\n", GetLastError());
+        CloseHandle(src);
+        /* Guessing on what state this should give us */
+        transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR);
+        return FALSE;
+    }
+
+    dst = CreateFileW(tmpName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
+                      FILE_ATTRIBUTE_NORMAL, NULL);
+    if (dst == INVALID_HANDLE_VALUE)
+    {
+        ERR("Couldn't open temp file: %d\n", GetLastError());
+        CloseHandle(src);
+        DeleteFileW(tmpName);
+        /* Guessing on what state this should give us */
+        transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR);
+        return FALSE;
+    }
+
+    if (GetFileSizeEx(src, &lgint))
+        size = lgint.QuadPart;
+    else
+        size = BG_SIZE_UNKNOWN;
+
+    EnterCriticalSection(&job->cs);
+    file->fileProgress.BytesTotal = size;
+    file->fileProgress.BytesTransferred = 0;
+    file->fileProgress.Completed = FALSE;
+    LeaveCriticalSection(&job->cs);
+
+    TRACE("Temp file name: %s -> %s -> %s\n",
+          debugstr_w(file->info.RemoteName),
+          debugstr_w(tmpName),
+          debugstr_w(file->info.LocalName));
+
+    transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSFERRING);
+
+    while (job->state == BG_JOB_STATE_TRANSFERRING)
+    {
+        DWORD written;
+
+        if (!ReadFile(src, buf, sizeof buf, &n, NULL))
+        {
+            ERR("Error reading file: %d\n", GetLastError());
+            transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
+            break;
+        }
+
+        if (n == 0)
+            break;
+
+        if (!WriteFile(dst, buf, n, &written, NULL) || written != n)
+        {
+            ERR("Error writing file: %d\n", GetLastError());
+            transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
+            break;
+        }
+
+        EnterCriticalSection(&job->cs);
+        file->fileProgress.BytesTransferred += n;
+        job->jobProgress.BytesTransferred += n;
+        LeaveCriticalSection(&job->cs);
+    }
+
+    CloseHandle(dst);
+    CloseHandle(src);
+
+    if (transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_QUEUED))
+    {
+        lstrcpyW(file->tempFileName, tmpName);
+
+        EnterCriticalSection(&job->cs);
+        file->fileProgress.Completed = TRUE;
+        job->jobProgress.FilesTransferred++;
+        LeaveCriticalSection(&job->cs);
+
+        return TRUE;
+    }
+    else
+    {
+        DeleteFileW(tmpName);
+        return FALSE;
+    }
+}
diff --git a/dlls/qmgr/job.c b/dlls/qmgr/job.c
index 2fbf1bc..51027c0 100644
--- a/dlls/qmgr/job.c
+++ b/dlls/qmgr/job.c
@@ -485,3 +485,29 @@ HRESULT BackgroundCopyJobConstructor(LPCWSTR displayName, BG_JOB_TYPE type,
     *ppObj = &This->lpVtbl;
     return S_OK;
 }
+
+void processJob(BackgroundCopyJobImpl *job)
+{
+    for (;;)
+    {
+        BackgroundCopyFileImpl *file;
+        BOOL done = TRUE;
+
+        EnterCriticalSection(&job->cs);
+        LIST_FOR_EACH_ENTRY(file, &job->files, BackgroundCopyFileImpl, entryFromJob)
+            if (!file->fileProgress.Completed)
+            {
+                done = FALSE;
+                break;
+            }
+        LeaveCriticalSection(&job->cs);
+        if (done)
+        {
+            transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSFERRED);
+            return;
+        }
+
+        if (!processFile(file, job))
+          return;
+    }
+}
diff --git a/dlls/qmgr/qmgr.c b/dlls/qmgr/qmgr.c
index 54b4973..cd7b7c1 100644
--- a/dlls/qmgr/qmgr.c
+++ b/dlls/qmgr/qmgr.c
@@ -183,11 +183,6 @@ DWORD WINAPI fileTransfer(void *param)
         LeaveCriticalSection(&qmgr->cs);
 
         if (haveJob)
-        {
-            FIXME("Actually process job %p; setting error state\n", job);
-            EnterCriticalSection(&qmgr->cs);
-            job->state = BG_JOB_STATE_ERROR;
-            LeaveCriticalSection(&qmgr->cs);
-        }
+            processJob(job);
     }
 }
diff --git a/dlls/qmgr/qmgr.h b/dlls/qmgr/qmgr.h
index 96635a7..3e76acb 100644
--- a/dlls/qmgr/qmgr.h
+++ b/dlls/qmgr/qmgr.h
@@ -73,6 +73,7 @@ typedef struct
     LONG ref;
     BG_FILE_INFO info;
     BG_FILE_PROGRESS fileProgress;
+    WCHAR tempFileName[MAX_PATH];
     struct list entryFromJob;
     BackgroundCopyJobImpl *owner;
 } BackgroundCopyFileImpl;
@@ -106,6 +107,8 @@ HRESULT BackgroundCopyFileConstructor(BackgroundCopyJobImpl *owner,
 HRESULT EnumBackgroundCopyFilesConstructor(LPVOID *ppObj,
                                            IBackgroundCopyJob* copyJob);
 DWORD WINAPI fileTransfer(void *param);
+void processJob(BackgroundCopyJobImpl *job);
+BOOL processFile(BackgroundCopyFileImpl *file, BackgroundCopyJobImpl *job);
 
 /* Little helper functions */
 static inline char *
@@ -116,4 +119,19 @@ qmgr_strdup(const char *s)
     return d ? memcpy(d, s, n) : NULL;
 }
 
+static inline BOOL
+transitionJobState(BackgroundCopyJobImpl *job, BG_JOB_STATE fromState,
+                   BG_JOB_STATE toState)
+{
+    BOOL rv = FALSE;
+    EnterCriticalSection(&globalMgr.cs);
+    if (job->state == fromState)
+    {
+        job->state = toState;
+        rv = TRUE;
+    }
+    LeaveCriticalSection(&globalMgr.cs);
+    return rv;
+}
+
 #endif /* __QMGR_H__ */



More information about the wine-patches mailing list