[5/6] qmgr: Implement local file background "downloads."

Dan Hipschman dsh at linux.ucla.edu
Thu Mar 6 21:07:45 CST 2008


This patch adds code to copy local files around in the background, although
since IBackgroundCopyJob_Complete is not implemented until the next patch,
they remain in temp files.  I verified the files match by hand, but there's
a test with the next patch anyway.

---
 dlls/qmgr/file.c |  116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 dlls/qmgr/job.c  |   24 +++++++++++
 dlls/qmgr/qmgr.c |    2 +-
 dlls/qmgr/qmgr.h |   11 +++++
 4 files changed, 152 insertions(+), 1 deletions(-)

diff --git a/dlls/qmgr/file.c b/dlls/qmgr/file.c
index 1377c83..cd469e5 100644
--- a/dlls/qmgr/file.c
+++ b/dlls/qmgr/file.c
@@ -176,3 +176,119 @@ HRESULT BackgroundCopyFileConstructor(LPCWSTR remoteName,
     *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, mutexes[2];
+    char buf[4096];
+    DWORD n;
+
+    FIXME("Assuming local file transfer\n");
+    src = CreateFileW(file->info.RemoteName, GENERIC_READ, FILE_SHARE_READ,
+                      NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+    if (src == INVALID_HANDLE_VALUE)
+    {
+        setJobStateAtomic(job, 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 */
+        setJobStateAtomic(job, 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 */
+        setJobStateAtomic(job, 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 */
+        setJobStateAtomic(job, BG_JOB_STATE_TRANSIENT_ERROR);
+        return FALSE;
+    }
+
+    FIXME("Figure out the total file size\n");
+    WaitForSingleObject(file->mutex, INFINITE);
+    file->fileProgress.BytesTotal = BG_SIZE_UNKNOWN;
+    file->fileProgress.BytesTransferred = 0;
+    file->fileProgress.Completed = FALSE;
+    ReleaseMutex(file->mutex);
+
+    mutexes[0] = job->mutex;
+    mutexes[1] = file->mutex;
+
+    TRACE("Temp file name: %s -> %s -> %s\n",
+          debugstr_w(file->info.RemoteName),
+          debugstr_w(tmpName),
+          debugstr_w(file->info.LocalName));
+    setJobStateAtomic(job, BG_JOB_STATE_TRANSFERRING);
+
+    /* Don't think we need a mutex on enumeration reads */
+    while (job->state == BG_JOB_STATE_TRANSFERRING)
+    {
+        DWORD written;
+
+        if (!ReadFile(src, buf, sizeof buf, &n, NULL))
+        {
+            ERR("Error reading file: %d\n", GetLastError());
+            setJobStateAtomic(job, BG_JOB_STATE_ERROR);
+            break;
+        }
+
+        if (n == 0)
+        {
+            setJobStateAtomic(job, BG_JOB_STATE_QUEUED);
+            break;
+        }
+
+        if (!WriteFile(dst, buf, n, &written, NULL) || written != n)
+        {
+            ERR("Error writing file: %d\n", GetLastError());
+            setJobStateAtomic(job, BG_JOB_STATE_ERROR);
+            break;
+        }
+
+        WaitForMultipleObjects(2, mutexes, TRUE, INFINITE);
+        file->fileProgress.BytesTransferred += n;
+        job->jobProgress.BytesTransferred += n;
+        ReleaseMutex(file->mutex);
+        ReleaseMutex(job->mutex);
+    }
+
+    CloseHandle(dst);
+    CloseHandle(src);
+    if (job->state == BG_JOB_STATE_ERROR || job->state == BG_JOB_STATE_CANCELLED)
+    {
+        DeleteFileW(tmpName);
+        return FALSE;
+    }
+
+    /* Don't rename until the user calls Complete */
+    lstrcpyW(file->tempFileName, tmpName);
+
+    WaitForMultipleObjects(2, mutexes, TRUE, INFINITE);
+    file->fileProgress.Completed = TRUE;
+    job->jobProgress.FilesTransferred++;
+    ReleaseMutex(file->mutex);
+    ReleaseMutex(job->mutex);
+
+    return TRUE;
+}
diff --git a/dlls/qmgr/job.c b/dlls/qmgr/job.c
index e8158a2..43ba3b9 100644
--- a/dlls/qmgr/job.c
+++ b/dlls/qmgr/job.c
@@ -489,3 +489,27 @@ HRESULT BackgroundCopyJobConstructor(LPCWSTR displayName, BG_JOB_TYPE type,
     *ppObj = &This->lpVtbl;
     return S_OK;
 }
+
+void processJob(IBackgroundCopyJob *job)
+{
+    BackgroundCopyJobImpl *This = (BackgroundCopyJobImpl *) job;
+    for (;;)
+    {
+        BackgroundCopyFileImpl *file;
+        BOOL done = TRUE;
+
+        WaitForSingleObject(This->mutex, INFINITE);
+        LIST_FOR_EACH_ENTRY(file, &This->files, BackgroundCopyFileImpl, entryFromJob)
+            if (!file->fileProgress.Completed)
+            {
+                done = FALSE;
+                break;
+            }
+        ReleaseMutex(This->mutex);
+        if (done)
+            return;
+
+        if (!processFile(file, This))
+          return;
+    }
+}
diff --git a/dlls/qmgr/qmgr.c b/dlls/qmgr/qmgr.c
index 3d47c68..018797e 100644
--- a/dlls/qmgr/qmgr.c
+++ b/dlls/qmgr/qmgr.c
@@ -183,7 +183,7 @@ DWORD WINAPI fileTransfer(void *param)
                 switch (state)
                 {
                 case BG_JOB_STATE_QUEUED:
-                    /* Connect and start downloading */
+                    processJob(job);
                     break;
                 case BG_JOB_STATE_CONNECTING:
                 case BG_JOB_STATE_TRANSFERRING:
diff --git a/dlls/qmgr/qmgr.h b/dlls/qmgr/qmgr.h
index b89bacc..0694878 100644
--- a/dlls/qmgr/qmgr.h
+++ b/dlls/qmgr/qmgr.h
@@ -73,6 +73,7 @@ typedef struct
     BG_FILE_INFO info;
     BG_FILE_PROGRESS fileProgress;
     HANDLE mutex;
+    WCHAR tempFileName[MAX_PATH];
     struct list entryFromJob;
 } BackgroundCopyFileImpl;
 
@@ -101,6 +102,8 @@ HRESULT BackgroundCopyFileConstructor(LPCWSTR remoteName,
 HRESULT EnumBackgroundCopyFilesConstructor(LPVOID *ppObj,
                                            IBackgroundCopyJob* copyJob);
 DWORD WINAPI fileTransfer(void *param);
+void processJob(IBackgroundCopyJob *job);
+BOOL processFile(BackgroundCopyFileImpl *file, BackgroundCopyJobImpl *job);
 
 /* Little helper functions */
 static inline char *
@@ -111,4 +114,12 @@ qmgr_strdup(const char *s)
     return d ? memcpy(d, s, n) : NULL;
 }
 
+static inline void
+setJobStateAtomic(BackgroundCopyJobImpl *job, BG_JOB_STATE state)
+{
+    WaitForSingleObject(job->mutex, INFINITE);
+    job->state = state;
+    ReleaseMutex(job->mutex);
+}
+
 #endif /* __QMGR_H__ */



More information about the wine-patches mailing list