summaryrefslogtreecommitdiffstats
path: root/private/nw/convert/nwconv/fastcopy.c
blob: e97191e10a67ebad0d7779b474add5cd573fee66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
  +-------------------------------------------------------------------------+
  |                      File Copying Routines                              |
  +-------------------------------------------------------------------------+
  |                     (c) Copyright 1993-1994                             |
  |                          Microsoft Corp.                                |
  |                        All rights reserved                              |
  |                                                                         |
  | Program               : [FastCopy.c]                                    |
  | Programmer            : Arthur Hanson                                   |
  | Original Program Date : [Jul 27, 1993]                                  |
  | Last Update           : [Jun 18, 1994]                                  |
  |                                                                         |
  | Version:  1.00                                                          |
  |                                                                         |
  |   Use multiple threads to whack data from one file to another           |
  |                                                                         |
  | Modifications:                                                          |
  |    18-Oct-1990 w-barry Removed 'dead' code.                             |
  |    21-Nov-1990 w-barry Updated API's to the Win32 set.                  |
  |                                                                         |
  +-------------------------------------------------------------------------+
*/

#define INCL_DOSPROCESS
#define INCL_DOSSEMAPHORES

#include "globals.h"
#include <stdio.h>
#include <process.h>
#include <windows.h>
#include <malloc.h>
#include "mem.h"
#include "debug.h"
#include "utils.h"
#include "convapi.h"

#define BUFSIZE     0xFE00       //  full segment minus sector
#define STACKSIZE   256          //  stack size for child thread

typedef struct BUF BUF;

struct BUF {
    BOOL  flag;
    DWORD cbBuf;
    BUF  *fpbufNext;
    BYTE ach[BUFSIZE];
};

#define LAST    TRUE
#define NOTLAST FALSE

static HANDLE hevQNotEmpty;
static CRITICAL_SECTION  hcrtQLock;
static BUF *fpbufHead = NULL;
static BUF *fpbufTail = NULL;
static HANDLE hfSrc, hfDst;
static HANDLE hThread;
static BOOLEAN fAbort;

//  forward type definitions

LPTSTR writer( void );
DWORD reader( void );
BUF  *dequeue( void );
void  enqueue( BUF *fpbuf );
TCHAR *fastcopy( HANDLE hfSrcParm, HANDLE hfDstParm );

/*+-------------------------------------------------------------------------+
  | writer()
  |
  +-------------------------------------------------------------------------+*/
LPTSTR writer ( void ) {
    BUF *fpbuf;
    DWORD cbBytesOut;
    BOOL f = !LAST;
    LPTSTR npsz = NULL;
    ULONG BytesCopied = 0;

    TCHAR buffer[MAX_PATH];

    Status_Bytes(lToStr(BytesCopied));

    while (f != LAST && npsz == NULL) {
        fpbuf = dequeue ();

        if (fpbuf) {
           if ((f = fpbuf->flag) != LAST) {
               if (!WriteFile(hfDst, fpbuf->ach, fpbuf->cbBuf, &cbBytesOut, NULL)) {
                   npsz = Lids(IDS_L_4);
#ifdef DEBUG
dprintf(TEXT("ERROR: WriteFile\n"));
#endif
               } else {
                  BytesCopied += cbBytesOut;
                  Status_Bytes(lToStr(BytesCopied));


                  if (cbBytesOut != (DWORD)fpbuf->cbBuf) {
                     npsz = Lids(IDS_L_5);
#ifdef DEBUG
dprintf(TEXT("ERROR: WriteFile: out-of-space\n"));
#endif
                  }
               }
           }
           else
               npsz = *(LPTSTR *)fpbuf->ach;

           FreeMemory(fpbuf);

        } else {
           wsprintf (buffer, Lids(IDS_L_4), fpbuf);
#ifdef DEBUG
dprintf(TEXT("ERROR: WriteFile - FileBuffer is NULL\n"));
#endif
        }
    }

    if (f != LAST)
        fAbort = TRUE;

    WaitForSingleObject(hThread, (DWORD)-1);
    CloseHandle(hThread);
    CloseHandle(hevQNotEmpty);
    DeleteCriticalSection(&hcrtQLock);

    return (npsz);
} // writer


/*+-------------------------------------------------------------------------+
  | reader()
  |
  +-------------------------------------------------------------------------+*/
DWORD reader( void ) {
    BUF *fpbuf;
    BOOL f = !LAST;

    while (!fAbort && f != LAST) {
        if ((fpbuf = AllocMemory(sizeof(BUF))) == NULL) {
#ifdef DEBUG
dprintf(TEXT("ERROR: MemoryAlloc error %ld\n"), GetLastError());
#endif
            return(0);
        }

        f = fpbuf->flag = NOTLAST;
        if (!ReadFile(hfSrc, fpbuf->ach, BUFSIZE, &fpbuf->cbBuf, NULL) || fpbuf->cbBuf == 0) {
            f = fpbuf->flag = LAST;
            *(LPTSTR *)(fpbuf->ach) = NULL;
        }

        enqueue (fpbuf);
    }

    return(0);
} // reader


/*+-------------------------------------------------------------------------+
  | dequeue()
  |
  +-------------------------------------------------------------------------+*/
BUF *dequeue( void ) {
    BUF *fpbuf;

    while (TRUE) {
        if (fpbufHead != NULL) {
            EnterCriticalSection(&hcrtQLock);
            fpbufHead = (fpbuf = fpbufHead)->fpbufNext;

            if (fpbufTail == fpbuf)
                fpbufTail = NULL;

            LeaveCriticalSection(&hcrtQLock);
            break;
        }

        // the head pointer is null so the list is empty.  Block on eventsem 
        // until enqueue posts (ie. adds to queue)
        WaitForSingleObject(hevQNotEmpty, (DWORD)-1);
    }

    return fpbuf;
} // dequeue


/*+-------------------------------------------------------------------------+
  | enqueue()
  |
  +-------------------------------------------------------------------------+*/
void enqueue( BUF *fpbuf ) {
    fpbuf->fpbufNext = NULL;

    EnterCriticalSection(&hcrtQLock);

    if (fpbufTail == NULL)
        fpbufHead = fpbuf;
    else
        fpbufTail->fpbufNext = fpbuf;

    fpbufTail = fpbuf;
    LeaveCriticalSection(&hcrtQLock);

    SetEvent(hevQNotEmpty);
} // enqueue


/*+-------------------------------------------------------------------------+
  | fastcopy()
  |
  |  hfSrcParm       file handle to read from
  |  hfDstParm       file handle to write to
  |
  |  returns         NULL if successful
  |                  pointer to error string otherwise
  +-------------------------------------------------------------------------+*/
TCHAR *fastcopy (HANDLE hfSrcParm, HANDLE hfDstParm) {
    DWORD dwReader;

    hfSrc = hfSrcParm;
    hfDst = hfDstParm;


    hevQNotEmpty = CreateEvent(NULL, (BOOL)FALSE, (BOOL)FALSE, NULL);
    if (hevQNotEmpty == INVALID_HANDLE_VALUE)
        return NULL;

    InitializeCriticalSection(&hcrtQLock);

    fAbort = FALSE;
    hThread = CreateThread(0, STACKSIZE, (LPTHREAD_START_ROUTINE)reader, 0, 0, &dwReader);
    if (hThread == INVALID_HANDLE_VALUE) {
#ifdef DEBUG
dprintf(TEXT("ERROR: Can't create thread - FileCopy\n"));
#endif
        return Lids(IDS_L_6);
    }

    return(writer());
} // fastcopy