source: trunk/src/helpers/prfh2.c@ 13

Last change on this file since 13 was 13, checked in by umoeller, 25 years ago

Updates for V0.9.6.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 22.1 KB
Line 
1
2/*
3 *@@sourcefile prfh2.c:
4 * contains more Presentation Manager helper functions
5 * which deal with Profile (Prf*) functions.
6 *
7 * This file is new with V0.9.4 (2000-07-26) [umoeller].
8 *
9 * As opposed to the functions in prfh.c, these require
10 * linking against other helpers. As a result, these have
11 * been separated from prfh.c to allow linking against
12 * prfh.obj only.
13 *
14 * Function prefixes:
15 * -- prfh* Prf (profile, INI) helper functions
16 *
17 * Note: Version numbering in this file relates to XWorkplace version
18 * numbering.
19 *
20 *@@header "helpers\prfh.h"
21 *@@added V0.9.4 (2000-07-27) [umoeller]
22 */
23
24/*
25 * Copyright (C) 1997-2000 Ulrich M”ller.
26 * This file is part of the XWorkplace source package.
27 * XWorkplace is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published
29 * by the Free Software Foundation, in version 2 as it comes in the
30 * "COPYING" file of the XWorkplace main distribution.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 */
36
37#define OS2EMX_PLAIN_CHAR
38 // this is needed for "os2emx.h"; if this is defined,
39 // emx will define PSZ as _signed_ char, otherwise
40 // as unsigned char
41
42#define INCL_DOS
43#define INCL_DOSERRORS
44#define INCL_WIN
45#include <os2.h>
46
47#include <stdlib.h>
48#include <stdio.h>
49#include <string.h>
50
51#include "setup.h" // code generation and debugging options
52
53#include "helpers\dosh.h"
54#include "helpers\stringh.h"
55
56#include "helpers\prfh.h"
57
58#pragma hdrstop
59
60/*
61 *@@category: Helpers\Profile (INI) helpers
62 */
63
64/*
65 * prfhINIError:
66 * this is called whenever an error occurs saving a
67 * profile. This will compose an error string and
68 * all the callback func fncbError.
69 *
70 *@@changed V0.9.0 [umoeller]: changed prototype's logfile to stdio FILE*
71 */
72
73ULONG prfhINIError(ULONG ulOptions,
74 FILE* fLog,
75 PFNWP fncbError,
76 PSZ pszErrorString)
77{
78 ULONG ulrc;
79 CHAR szError2[2000];
80
81 if (fncbError)
82 {
83 if (ulOptions == MB_ABORTRETRYIGNORE)
84 sprintf(szError2, "%s"
85 "\nPress 'Abort' to abort saving the INI files. This will restart the WPS to avoid loss of data."
86 "\nPress 'Retry' to attempt saving this INI file again. "
87 "\nPress 'Ignore' to ignore this error. This might help, but "
88 "also lead to more errors or loss of the currently processed data.",
89 pszErrorString);
90 else
91 sprintf(szError2, "%s\nPress 'Cancel' to abort shutdown. ", pszErrorString);
92 ulrc = ( (ULONG)(*fncbError)(0, 0, szError2, (MPARAM)ulOptions) );
93
94 }
95 else
96 ulrc = (MBID_ABORT);
97
98 if (fLog)
99 {
100 fprintf(fLog, " Error occured: %s\n Return code: %s (%lu)\n",
101 pszErrorString,
102 (ulrc == MBID_ABORT) ? "MBID_ABORT"
103 : (ulrc == MBID_IGNORE) ? "MBID_IGNORE"
104 : (ulrc == MBID_RETRY) ? "MBID_RETRY"
105 : "unknown",
106 ulrc);
107 fflush(fLog);
108 }
109
110 return (ulrc);
111}
112
113/*
114 * prfhINIError2:
115 * like the previous func, but with additional info
116 * for the error string.
117 *
118 *@@changed V0.9.0 [umoeller]: changed prototype's logfile to stdio FILE*
119 */
120
121ULONG prfhINIError2(ULONG ulOptions,
122 PSZ pszINI,
123 FILE* fLog,
124 PFNWP fncbError,
125 PSZ pszErrorString)
126{
127 CHAR szError2[2000];
128 sprintf(szError2, "An error occured copying the profile %s: \n%s",
129 pszINI, pszErrorString);
130 return (prfhINIError(ulOptions, fLog, fncbError, szError2));
131}
132
133/*
134 *@@ prfhCopyProfile:
135 * this function copies a given profile entirely into a
136 * new file. hOld is the handle, pszOld the filename
137 * of the profile to be copied (e.g. HINI_USERPROFILE
138 * and "?:\OS2\OS2.INI"; pszNew must point to a
139 * buffer which will contain the new filename ending
140 * in ".XFL" after prfhCopyProfile returns
141 * (e.g. "?:\OS2\OS2.XFL").
142 *
143 * You may specify a callback procedure which gets
144 * called upon every copied application in the INI
145 * file; this way, you can provide a progress bar.
146 * The second callback, fncbError, gets called upon
147 * errors.
148 *
149 *@@changed V0.9.0 [umoeller]: this now calls prfhCopyApp.
150 *@@changed V0.9.0 [umoeller]: fixed crashes with lower-case INI file names.
151 *@@changed V0.9.0 [umoeller]: changed prototype's logfile to stdio FILE*
152 *@@changed V0.9.4 (2000-07-15) [umoeller]: this never reported any errors, causing 0-byte INI files without warning. Fixed.
153 */
154
155BOOL prfhCopyProfile(HAB hab, // in: anchor block
156 FILE* fLog, // in: stdio logfile or NULL for no log
157 HINI hOld, // in: HINI to copy
158 PSZ pszOld, // in: fully qualif. filename of hOld
159 PSZ pszNew, // out: new filename
160 PFNWP fncbUpdate, // in: progress callback
161 HWND hwnd, ULONG msg, ULONG ulCount, ULONG ulMax,
162 // in: passed to fncbUpdate
163 PFNWP fncbError) // in: error callback
164{
165 // BOOL rc = TRUE;
166 HINI hNew;
167 PSZ pApps, // pKeys,
168 pApp2; // , pKey2;
169 ULONG ulSizeOfAppsList; // , ulSizeOfKeysList; // , ulSizeOfData;
170 // CHAR szLog[1000]; // removed V0.9.0
171 #define MBID_NOERROR 999
172 ULONG ulErrorStatus = MBID_NOERROR;
173 PSZ p_;
174 CHAR szError2[2000];
175
176 if (fLog)
177 {
178 fprintf(fLog, " Entering prfhCopyProfile, params: "
179 "hiniOld 0x%lX, pszOld %s, pfnwpCallback 0x%lX, "
180 "hwnd 0x%lX, msg 0x%lX, ulCount 0x%lX, ulMax 0x%lX\n",
181 hOld,
182 pszOld,
183 (ULONG)fncbUpdate,
184 hwnd,
185 msg,
186 ulCount,
187 ulMax);
188 fflush(fLog);
189 }
190
191 if ( (!pszOld) || (!pszNew) )
192 ulErrorStatus = prfhINIError2(MB_CANCEL, pszOld, fLog, fncbError,
193 "Invalid name buffers given.");
194
195 if (ulErrorStatus == MBID_NOERROR)
196 {
197 strupr(pszOld);
198 strcpy(pszNew, pszOld);
199 p_ = strhistr(pszNew, ".INI");
200 if (!p_)
201 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
202 "Error composing new filename.");
203 }
204
205 if (ulErrorStatus == MBID_NOERROR)
206 {
207 strcpy(p_, ".XFL");
208
209 // delete new profile, in case something's left
210 if (fLog)
211 fprintf(fLog, " DEL %s\n", pszNew);
212
213 DosDelete(pszNew);
214
215 // open new profile
216 if (fLog)
217 fprintf(fLog, " PrfOpenProfile %s\n", pszNew);
218
219 hNew = PrfOpenProfile(hab, pszNew);
220 if (!hNew)
221 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
222 "Error creating temporary profile.");
223 }
224
225 // get size of applications list
226 if (ulErrorStatus == MBID_NOERROR)
227 {
228 if (!PrfQueryProfileSize(hOld, NULL, NULL, &ulSizeOfAppsList))
229 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
230 "Error querying applications list size.");
231 else
232 if (ulSizeOfAppsList == 0)
233 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
234 "Size of applications list cannot be 0.");
235 }
236
237 if (ulErrorStatus == MBID_NOERROR)
238 {
239 // do-while loop in case of MBID_RETRY
240 do
241 {
242 ulErrorStatus = MBID_NOERROR; // if we have a retry in the do-while loop
243
244 // get applications list
245 pApps = (PSZ)malloc(ulSizeOfAppsList);
246 pApp2 = pApps;
247 if (!PrfQueryProfileData(hOld,
248 NULL,
249 NULL,
250 pApps,
251 &ulSizeOfAppsList))
252 ulErrorStatus = prfhINIError2(MB_CANCEL, pszNew, fLog, fncbError,
253 "Could not query application list.");
254
255 // applications loop
256
257 while ( (*pApp2 != 0)
258 && (ulErrorStatus == MBID_NOERROR)
259 )
260 {
261 CHAR szErrorKey[1000];
262 // copy application (this will call prfhCopyKey in turn)
263 ULONG ulrc = prfhCopyApp(hOld,
264 pApp2,
265 hNew,
266 pApp2,
267 szErrorKey);
268
269 ulErrorStatus = MBID_ABORT;
270
271 switch (ulrc)
272 {
273 case 0: // no error
274 ulErrorStatus = MBID_NOERROR;
275 break;
276
277 case PRFERR_KEYSLIST:
278 // couldn't query keys list
279 sprintf(szError2,
280 "Error querying keys list for \"%s\".",
281 pApp2);
282 break;
283
284 case PRFERR_DATASIZE:
285 // couldn't query data size for key
286 sprintf(szError2,
287 "Error querying data size for \"%s\"/\"%s\".",
288 pApp2, szErrorKey);
289 break;
290
291 case ERROR_NOT_ENOUGH_MEMORY:
292 // couldn't allocate memory
293 sprintf(szError2,
294 "Error allocating memory for copying \"%s\"/\"%s\".",
295 pApp2, szErrorKey);
296 break;
297
298 case PRFERR_READ:
299 // couldn't read data from source (PrfQueryProfileData error)
300 sprintf(szError2,
301 "Error reading data from app \"%s\", key \"%s\".",
302 pApp2, szErrorKey);
303 break;
304
305 case PRFERR_WRITE:
306 // couldn't write data to target (PrfWriteProfileData error)
307 sprintf(szError2,
308 "Error writing data to app \"%s\", key \"%s\".",
309 pApp2, szErrorKey);
310 break;
311 }
312
313 if (ulErrorStatus != MBID_NOERROR) // V0.9.4 (2000-07-15) [umoeller]
314 {
315 ulErrorStatus = prfhINIError2(MB_ABORTRETRYIGNORE, pszNew, fLog, fncbError,
316 szError2);
317 }
318
319 if (fncbUpdate)
320 {
321 ULONG ulNow2, ulMax2;
322 ulNow2 = ((1000*(pApp2-pApps))/ulSizeOfAppsList) + (ulCount*1000);
323 ulMax2 = (ulMax+1)*1000; // ulAppsSize;
324 (*fncbUpdate)(hwnd, msg, (MPARAM)ulNow2, (MPARAM)ulMax2);
325 }
326
327 // go for next app
328 pApp2 += strlen(pApp2)+1;
329
330 if (ulErrorStatus == MBID_IGNORE)
331 // error occured, but user pressed "Ignore":
332 // skip this app
333 ulErrorStatus = MBID_NOERROR;
334
335 } // end while (*pApp2 != 0) && MBID_NOERROR
336
337 if (pApps)
338 free(pApps);
339
340 if (fLog)
341 fprintf(fLog, " Done copying apps\n");
342
343 } while (ulErrorStatus == MBID_RETRY);
344 } // end if (ulErrorOccured == MBID_NOERROR)
345
346 if (fLog)
347 fprintf(fLog, " PrfCloseProfile %s\n", pszNew);
348
349 PrfCloseProfile(hNew);
350 if (fncbUpdate)
351 (*fncbUpdate)(hwnd, msg, (MPARAM)(ulCount+1), (MPARAM)(ulMax+1));
352
353 if (fLog)
354 {
355 fprintf(fLog, " Leaving prfhCopyProfile.\n");
356 fflush(fLog);
357 }
358
359 return (ulErrorStatus == MBID_NOERROR); // FALSE if error occured
360}
361
362/*
363 *@@ prfhSaveINIs:
364 * this will enforce saving of OS2.INI and OS2SYS.INI
365 * by calling prfhCopyProfile (above) on them; the therefrom
366 * resulting ".XFL" files will replace the original
367 * ".INI" files.
368 * Specify fncbUpdate and fncbError like in prfhCopyProfile.
369 *
370 *@@changed V0.9.0 [umoeller]: fixed crashes with lower-case INI file names.
371 *@@changed V0.9.0 [umoeller]: changed prototype's logfile to stdio FILE*
372 */
373
374APIRET prfhSaveINIs(HAB hab, // in: anchor block
375 FILE* fLog, // in: stdio logfile or NULL for no log
376 PFNWP fncbUpdate, // in: progress callback
377 HWND hwnd, ULONG msg, // in: params passed to fncbUpdate
378 PFNWP fncbError) // in: error callback
379{
380 PRFPROFILE Profiles;
381 APIRET arc = NO_ERROR, arc2;
382 BOOL brc = TRUE;
383
384 // FILESTATUS3 fs3;
385 CHAR szSysNew[CCHMAXPATH],
386 szUserNew[CCHMAXPATH],
387 szSysBackup[CCHMAXPATH],
388 szUserBackup[CCHMAXPATH];
389
390 // the following flag may be one of the following:
391 // MBID_NOERROR--- everything's fine, continue
392 // MBID_IGNORE --- error occured, but ignore
393 // MBID_RETRY --- error occured, but retry
394 // MBID_ABORT --- error occured, abort saving
395 ULONG ulErrorOccured = MBID_IGNORE;
396
397 if (fLog)
398 fprintf(fLog, " Entering prfhSaveINIs...\n");
399
400 /*
401 * get system profiles:
402 *
403 */
404
405 Profiles.cchUserName = Profiles.cchSysName = 0;
406 brc = PrfQueryProfile(hab, &Profiles);
407
408 if (!brc)
409 ulErrorOccured = prfhINIError(MB_CANCEL, fLog, fncbError,
410 "Error querying system profiles size.");
411
412 if (ulErrorOccured == MBID_IGNORE)
413 {
414 Profiles.pszUserName = (PSZ)malloc(Profiles.cchUserName);
415 Profiles.pszSysName = (PSZ)malloc(Profiles.cchSysName);
416 if ( (Profiles.pszSysName == NULL)
417 || (Profiles.pszUserName == NULL)
418 )
419 ulErrorOccured = prfhINIError(MB_CANCEL, fLog, fncbError,
420 "Error allocating memory (1).");
421 }
422
423 if (ulErrorOccured == MBID_IGNORE)
424 {
425 if (!PrfQueryProfile(hab, &Profiles))
426 ulErrorOccured = prfhINIError(MB_CANCEL, fLog, fncbError,
427 "Error querying profiles (2).");
428 }
429
430 if (ulErrorOccured == MBID_IGNORE)
431 {
432 if (fLog)
433 fprintf(fLog, " System profiles are %s, %s\n",
434 Profiles.pszUserName, Profiles.pszSysName);
435
436 /*
437 * create new profile names:
438 *
439 */
440
441 strcpy(szSysBackup, Profiles.pszSysName);
442 strcpy(strhistr(szSysBackup, ".INI"), ".BAK");
443 strcpy(szUserBackup, Profiles.pszUserName);
444 strcpy(strhistr(szUserBackup, ".INI"), ".BAK");
445
446 /*
447 * create OS2SYS.XFL:
448 *
449 */
450
451 if (fLog)
452 fprintf(fLog, " Storing %s in *.XFL\n",
453 Profiles.pszSysName);
454
455 if (!prfhCopyProfile(hab,
456 fLog,
457 HINI_SYSTEMPROFILE,
458 Profiles.pszSysName, // old filename
459 szSysNew, // new filename
460 fncbUpdate, hwnd, msg, 0, 1,
461 fncbError))
462 {
463 // abort, since prfhCopyProfile already has error handling
464 ulErrorOccured = MBID_ABORT;
465 }
466 }
467
468 if (ulErrorOccured == MBID_IGNORE)
469 {
470 // create OS2.XFL
471
472 if (fLog)
473 fprintf(fLog, " Storing %s in *.XFL\n",
474 Profiles.pszUserName);
475
476 if (!prfhCopyProfile(hab,
477 fLog,
478 HINI_USERPROFILE,
479 Profiles.pszUserName, szUserNew,
480 fncbUpdate, hwnd, msg, 1, 1,
481 fncbError))
482 {
483 // abort, since prfhCopyProfile already has error handling
484 ulErrorOccured = MBID_ABORT;
485 }
486 }
487
488 /*
489 * renaming stuff for OS2SYS.INI
490 *
491 */
492
493 if (ulErrorOccured == MBID_IGNORE)
494 {
495 do
496 {
497 ulErrorOccured = MBID_IGNORE; // if we have a retry in the do-while loop
498
499 if (ulErrorOccured == MBID_IGNORE)
500 {
501 // attrib -r -s -h -a OS2SYS.BAK
502 if (fLog)
503 fprintf(fLog, " ATTRIB -R -S -H -A %s\n",
504 szSysBackup);
505
506 arc2 = doshSetPathAttr(szSysBackup, FILE_NORMAL);
507 if (fLog)
508 fprintf(fLog, " rc2: %lu\n", arc2);
509
510 // delete OS2SYS.BAK
511 if (fLog)
512 fprintf(fLog, " DEL %s\n",
513 szSysBackup);
514
515 arc2 = DosDelete(szSysBackup);
516 if (fLog)
517 fprintf(fLog, " rc2: %lu\n", arc2);
518
519 // attrib -r -s -h -a OS2SYS.INI
520 if (fLog)
521 fprintf(fLog, " ATTRIB -R -S -H -A %s\n",
522 Profiles.pszSysName);
523
524 arc2 = doshSetPathAttr(Profiles.pszSysName, FILE_NORMAL);
525 if (fLog)
526 fprintf(fLog, " rc2: %lu\n", arc2);
527
528 // move OS2SYS.INI OS2SYS.BAK
529 if (fLog)
530 fprintf(fLog, " MOVE %s %s\n",
531 Profiles.pszSysName, szSysBackup);
532 arc = DosMove(Profiles.pszSysName, szSysBackup);
533 if (fLog)
534 fprintf(fLog, " rc: %lu\n", arc);
535
536 if (arc)
537 ulErrorOccured = prfhINIError(MB_ABORTRETRYIGNORE, fLog, fncbError, "Error moving original system profile to backup.");
538 }
539
540 if (ulErrorOccured == MBID_IGNORE)
541 {
542 if (fLog)
543 fprintf(fLog, " MOVE %s %s\n",
544 szSysNew, Profiles.pszSysName);
545 arc = DosMove(szSysNew, Profiles.pszSysName);
546 if (fLog)
547 fprintf(fLog, " rc: %lu\n", arc);
548 if (arc)
549 ulErrorOccured = prfhINIError(MB_ABORTRETRYIGNORE, fLog, fncbError, "Error moving newly created profile to system profile.");
550 }
551 } while (ulErrorOccured == MBID_RETRY);
552 }
553
554 /*
555 * renaming stuff for OS2.INI
556 *
557 */
558
559 if (ulErrorOccured == MBID_IGNORE)
560 {
561 do
562 {
563 ulErrorOccured = MBID_IGNORE; // if we have a retry in the do-while loop
564
565 if (ulErrorOccured == MBID_IGNORE)
566 {
567 // attrib -r -s -h -a OS2.BAK
568 if (fLog)
569 fprintf(fLog, " ATTRIB -R -S -H -A %s\n",
570 szUserBackup);
571
572 arc2 = doshSetPathAttr(szUserBackup, FILE_NORMAL);
573 if (fLog)
574 fprintf(fLog, " rc2: %lu\n", arc2);
575
576 // delete OS2.BAK
577 if (fLog)
578 fprintf(fLog," DEL %s\n",
579 szUserBackup);
580
581 arc2 = DosDelete(szUserBackup);
582 if (fLog)
583 fprintf(fLog, " rc2: %lu\n", arc2);
584
585 // attrib -r -s -h -a OS2.INI
586 if (fLog)
587 fprintf(fLog, " ATTRIB -R -S -H -A %s\n",
588 Profiles.pszUserName);
589 arc2 = doshSetPathAttr(Profiles.pszUserName, FILE_NORMAL);
590 if (fLog)
591 fprintf(fLog, " rc2: %lu\n", arc2);
592
593 // move OS2.INI OS2.BAK
594 if (fLog)
595 fprintf(fLog, " MOVE %s %s\n",
596 Profiles.pszUserName, szUserBackup);
597 arc = DosMove(Profiles.pszUserName, szUserBackup);
598 if (fLog)
599 fprintf(fLog, " rc: %lu\n", arc);
600
601 if (arc)
602 ulErrorOccured = prfhINIError(MB_ABORTRETRYIGNORE, fLog, fncbError, "Error moving original user profile to backup.");
603 }
604
605 if (ulErrorOccured == MBID_IGNORE)
606 {
607 // move OS2.XFL OS2.INI
608 if (fLog)
609 fprintf(fLog, " MOVE %s %s\n",
610 szUserNew, Profiles.pszUserName);
611
612 arc = DosMove(szUserNew, Profiles.pszUserName);
613 if (fLog)
614 fprintf(fLog, " rc: %lu\n", arc);
615
616 if (arc)
617 ulErrorOccured = prfhINIError(MB_ABORTRETRYIGNORE, fLog, fncbError, "Error moving newly created profile to user profile.");
618 }
619 } while (ulErrorOccured == MBID_RETRY);
620 }
621
622 // DosExitCritSec();
623
624 if (ulErrorOccured != MBID_IGNORE)
625 {
626 DosMove(szSysBackup, Profiles.pszSysName);
627 DosMove(szUserBackup, Profiles.pszUserName);
628 }
629
630 if (Profiles.pszSysName)
631 free(Profiles.pszSysName);
632 if (Profiles.pszUserName)
633 free(Profiles.pszUserName);
634
635 if (fLog)
636 fprintf(fLog, " Done with prfhSaveINIs\n");
637
638 if (ulErrorOccured != MBID_IGNORE)
639 return (999);
640 else
641 return (NO_ERROR);
642}
643
644
Note: See TracBrowser for help on using the repository browser.