summaryrefslogtreecommitdiff
path: root/win32/droplet/replace.cpp
blob: 8a7b51b7f2dc2c0b42a350dde7286a4c274f9dc1 (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * AUTHOR
 * N. Nielsen
 *
 * LICENSE
 * This software is in the public domain.
 *
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties
 * of merchantability, fitness for a particular purpose, and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages, or other liability, whether in an action of
 * contract, tort, or otherwise, arising from, out of, or in connection
 * with the software or the use or other dealings in the software.
 *
 * SUPPORT
 * Send bug reports to: <nielsen@memberwebs.com>
 */

#include "stdafx.h"
#include "Replace.h"
#include "common/errutil.h"
#include "common/rliberr.h"
#include "common/mystring.h"


// Error Macros: -------------------------------------------------
//  Display error messages and all that
//  Only valid in this context
//  Each of these jumps to the 'cleanup' label for wrapping
//  up any open objects.

#define RETURN(v) \
	{ ret = (v); goto cleanup; }
#define WINERR(f) \
	{ HRESULT hr = HRESULT_FROM_WIN32(::GetLastError()); RETURN(errorMessage(m_dlg.m_hWnd, hr, f)); }
#define WINERR_1(f, a) \
	{ HRESULT hr = HRESULT_FROM_WIN32(::GetLastError()); RETURN(errorMessage(m_dlg.m_hWnd, hr, f, a)); }
#define WINERR_E(e, f) \
	{ RETURN(errorMessage(m_dlg.m_hWnd, e, f)); }
#define REPERR(c, s) \
	{ RETURN(rlibError(m_dlg.m_hWnd, c, s)); }


// The extension appended to backup or temp files
LPCTSTR kBackupExt = _T(".x_r");

// (Con|De)struction: -------------------------------------------

Replace::Replace()
{
	m_outFile = NULL;
}

Replace::~Replace()
{
	ASSERT(m_outFile == NULL);
}


// initialize: ---------------------------------------------------
//  Loads the script from resources and sets up dialog
//  in another thread

HRESULT Replace::initialize()
{
	HRESULT ret = S_OK;

	{
		if(!m_droplet.load(GetModuleHandle(NULL)))
		{
			errorMessage(NULL, 0, _T("Couldn't access droplet rep script. The droplet is probably corrupted."));
			RETURN(E_FAIL);
		}

		m_dlg.setTitle(m_droplet.getTitle());

		r_context& ctx = m_droplet.getContext();

		int r = repInit(&ctx);
		if(r < 0)
			REPERR(r, &(ctx.script));

		// This starts the dialog in another thread
		// in order to keep it responsive
		m_dlg.startDialog();
	}

cleanup:
	return ret;
}


// terminate: ----------------------------------------------------
//  Undoes above

HRESULT Replace::terminate()
{
	m_dlg.stopDialog();
	r_context& ctx = m_droplet.getContext();
	repFree(&ctx);
	return S_OK;
}


// writeFile: ----------------------------------------------------
//  The callback called from within rlib when data output
//  is required. The argument passed through the r_stream
//  is a pointer to the Replace object.

int Replace::writeFile(r_stream* stream, byte* data, size_t len)
{
	Replace* replace = (Replace*)stream->arg;

	// The m_outFile member should be set (and only set)
	// whenever we're inside rlibRun
	ASSERT(replace->m_outFile != NULL);
	DWORD written = 0;

	// Double check that everything was written
	return WriteFile(replace->m_outFile, data, len, &written, NULL) && len == written;
}

// matchStatus: --------------------------------------------------
//  Callback whenever something is replaced in rlib
//  Updates the dialog.

int Replace::matchStatus(r_stream* stream, r_replace* repl)
{
	Replace* replace = (Replace*)stream->arg;
	ASSERT_PTR(replace);

	replace->m_dlg.onReplaced();
	return replace->m_dlg.isCancelled() ? 1 : 0;
}


// replaceFolder: ------------------------------------------------
//  Recursively processes a folder using replaceSingleFile

HRESULT Replace::replaceFolder(LPCTSTR folder)
{
	HANDLE hFindFile = NULL;
	WIN32_FIND_DATA findData;
	TCHAR old[260];			//  Save the current folder (as we'll change it)
	HRESULT ret = S_FALSE;

	{
		//  Backup the current directory
		if(!GetCurrentDirectory(260, old))
			WINERR("Couldn't get the current directory.");

		if(!SetCurrentDirectory(folder))
			WINERR_1("Couldn't change the current directory to %s", folder);

		if(hFindFile = FindFirstFile(_T("*.*"), &findData))
		{
			// And for every file
			do
			{
				HRESULT r = S_FALSE;
				if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					if(findData.cFileName[0] == '.' &&
					   (findData.cFileName[1] == '.' || findData.cFileName[1] == '\0'))
					   continue;

					r = replaceFolder(findData.cFileName);
				}
				else
				{
					r = replaceSingleFile(findData.cFileName);
				}

				if(FAILED(r))
					RETURN(r);
				if(r == S_OK)
					ret = r;
			}
			while(FindNextFile(hFindFile, &findData));
		}

		// Make sure we didn't miss anything
		if(::GetLastError() != 0 && ::GetLastError() != ERROR_NO_MORE_FILES)
			WINERR_1("Couldn't list the directory %s", folder);

		// Change directory back
		SetCurrentDirectory(old);
	}

cleanup:
	if(hFindFile)
		FindClose(hFindFile);

	return ret;
}


// replaceSingleFile: ----------------------------------------------
//  Process a file into a temp file or backup etc...
//  Calls replaceFilename for actual processing

HRESULT Replace::replaceSingleFile(LPCTSTR fileName)
{
	HRESULT ret = S_FALSE;
	string temp;
	USES_CONVERSION;

	r_context& ctx = m_droplet.getContext();

	{
		// Update the dialog display
		m_dlg.onNewFile(fileName);

		// This lets us use the filename from within the script
		ret = rlibSetVar(&(ctx.stream), "FILENAME", T2CA(fileName));
		if(ret < 0)
			REPERR(ret, &(ctx.script));

		// Make a temp/backup name
		temp.assign(fileName);
		temp.append(kBackupExt);

		// Now we do this 2 ways, if we're keeping backups then...
		if(m_droplet.keepBackups())
		{
			DeleteFile(temp);

			// Rename the original file to the backup
			if(!MoveFile(fileName, temp))
				WINERR_1("There was an error while creating the backup file: %s", temp.c_str());

			// Do a replacement operation back to the original
			// from the backup
			ret = replaceFilename(temp, fileName);

			// If there were no replacements
			if(ret != S_OK)
			{
				DeleteFile(fileName);

				// And rename the backup file back
				if(!MoveFile(temp, fileName))
					WINERR_1("There was an error while renaming the file: %s", fileName);
			}
		}

		// No backups, much faster (when there's no replacements)!
		else
		{
			// Do a replacement operation to a temp file
			ret = replaceFilename(fileName, temp);

			// If there were replacements
			if(ret == S_OK)
			{
				DeleteFile(fileName);

				// Rename temp to original
				if(!MoveFile(temp, fileName))
					WINERR_1("There was an error while renaming the file: %s", fileName);
			}

			// If no replacements
			else
			{
				// Remove temp file
				DeleteFile(temp);
			}
		}
	}

cleanup:

	return ret;
}

// replaceFilename: -------------------------------------------------
//  Process one file into another
//  Open the files and passes them to replaceFile

HRESULT Replace::replaceFilename(LPCTSTR fileIn, LPCTSTR fileOut)
{
	HANDLE in = INVALID_HANDLE_VALUE;
	HANDLE out = INVALID_HANDLE_VALUE;
	HRESULT ret = S_FALSE;

	{
		// Basic readonly open
		in = CreateFile(fileIn, GENERIC_READ, FILE_SHARE_READ, NULL,
						OPEN_EXISTING, 0, NULL);
		if(in == INVALID_HANDLE_VALUE)
			WINERR_1("There was an error opening the file: %s", fileIn);

		// We need to know whether we're NT or not below
		OSVERSIONINFO os;
		os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
		GetVersionEx(&os);

		// This creates a new file for writing
		// If we're running on NT, then use exactly the same
		// attributes and permissions as original file
		out = CreateFile(fileOut, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0,
						 os.dwPlatformId & VER_PLATFORM_WIN32_NT ? in : NULL);
		if(out == INVALID_HANDLE_VALUE)
			WINERR_1("There was an error opening the file: %s", fileIn);


		//  Do replacements!
		ret = replaceFile(in, out);

	}

cleanup:

	if(in != INVALID_HANDLE_VALUE)
		CloseHandle(in);
	if(out != INVALID_HANDLE_VALUE)
		CloseHandle(out);

	return ret;
}


// replaceFile: --------------------------------------------------------
//  Process one open file handle into another
//  This memory maps the input file and passes it to replaceBuffer

HRESULT Replace::replaceFile(HANDLE in, HANDLE out)
{
	HANDLE mapping = NULL;
	void* data = NULL;
	HRESULT ret = S_FALSE;

	{
		// Memory map the input file
		mapping = CreateFileMapping(in, NULL, PAGE_READONLY, 0, 0, NULL);
		if(!mapping)
			WINERR("There was an error reading the input file.");

		data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
		if(!data)
			WINERR("There was an error reading the input file.");

		size_t size = GetFileSize(in, NULL);
		if(size == INVALID_FILE_SIZE)
			WINERR("There was an error reading the input file.");

		// And pass it on
		ret = replaceBuffer((byte*)data, size, out);
	}

cleanup:

	if(data)
		UnmapViewOfFile(data);
	if(mapping)
		CloseHandle(mapping);

	return ret;
}


// replaceBuffer: ----------------------------------------------------
//  Process a memory buffer into a file for output
//  Uses the rlib functions directly

HRESULT Replace::replaceBuffer(byte* data, size_t size, HANDLE out)
{
	ASSERT(m_outFile == NULL);

	size_t blockSize = m_droplet.getBuffSize() * 2;	//  Block size
	size_t batchSize;							//  Current batch size
	int r = R_IN;
	bool dirty = false;
	HRESULT ret = S_OK;

	{
		// If there's no blocksize then we process the
		// entire file at once
		if(blockSize == 0)
			blockSize = size;

		batchSize = blockSize;

		// Setup the rlib stream
		r_context& ctx = m_droplet.getContext();

		ctx.stream.nextIn = data;
		ctx.stream.availIn = blockSize;
		ctx.stream.fWrite = writeFile;
		ctx.stream.arg = this;
		ctx.stream.fMatch = matchStatus;

		// This is used by writeFile
		m_outFile = out;

		//  While we have more data to put in ...
		while(size > 0 || r == R_IN)
		{
			//  If rlib wants data then give it
			if(r == R_IN)
			{
 				batchSize = (blockSize > size) ? size : blockSize;

				ctx.stream.nextIn = data;
				ctx.stream.availIn = batchSize;

			}

			//  call rlib
			r = rlibRun(&(ctx.stream), &(ctx.script), (size - batchSize) <= 0);

			// Oops!
			if(r < 0)
				REPERR(r, &(ctx.script));

			if(ctx.stream.total > 0)
				dirty = true;

			// Increment last batch
			data += (batchSize - ctx.stream.availIn);
 			size -= (batchSize - ctx.stream.availIn);
		}

		// Clears and prepares for next file
		rlibClear(&ctx.stream);
	}

cleanup:

	m_outFile = NULL;

	if(FAILED(ret))
		return ret;

	return dirty ? S_OK : S_FALSE;
}