Debian Codegraph @

manage/pagelock.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/manage/pagelock.cpp $
21 * $Revision: 42 $
22 * $Date: 5/17/99 2:09p $
23 * $Author: Jeff $
24 *
25 * Jason should put something here
26 *
27 * $Log: /DescentIII/Main/manage/pagelock.cpp $
28 *
29 * 42 5/17/99 2:09p Jeff
30 * fixed tablefile logging
31 *
32 * 41 5/14/99 12:15p Jason
33 * added page logging
34 *
35 * 40 4/30/99 8:53p Matt
36 * Added a "voice" directory for gamefiles.
37 *
38 * 39 4/22/99 7:08p Matt
39 * Reduced the number of object sounds from 3 to 2, since we were only
40 * using two.
41 *
42 * 38 4/18/99 4:49p Matt
43 * Took out code that hacked in ammo amounts for weapon powerups, and
44 * added code to set the counts on the page and read and write to the
45 * table file.
46 *
47 * 37 4/14/99 10:46a Kevin
48 * Removed OutrageMessageBox from release builds
49 *
50 * 36 4/14/99 1:33a Jeff
51 * fixed case mismatched #includes
52 *
53 * 35 4/12/99 12:49p Jeff
54 * added recoil_force to weapon's page
55 *
56 * 34 4/09/99 12:51p Jason
57 * corrected bug sound resolving code
58 *
59 * 33 4/06/99 6:02p Matt
60 * Added score system
61 *
62 * 32 3/30/99 6:11p Jason
63 * changed pagelock version
64 *
65 * 31 3/29/99 11:20a Matt
66 * Added more flexibility to death delays, and added fade away deaths.
67 *
68 * 30 3/04/99 1:47p Jason
69 * fixed some manage problems
70 *
71 *
72 * 29 3/01/99 12:54a Matt
73 * Incremented table file version
74 *
75 * 28 2/23/99 12:39p Jason
76 * added more options for generic objects
77 *
78 * 27 2/22/99 2:03p Jason
79 * added different damages for players and generics
80 *
81 * 26 2/03/99 1:58a Matt
82 * Added a system to show all pages locked.
83 *
84 * 25 2/02/99 8:44a Chris
85 * I made buildings with AI work correctly (ie really big robots should be
86 * buildings)
87 * anim to and from states are now shorts instead of bytes
88 *
89 * 24 12/30/98 5:17p Jeff
90 * changes made to handle script name override, to override the name of
91 * the script to use in a module.
92 *
93 * 23 12/21/98 5:27p Jeff
94 * added osiris module information to door page. Fixed up dialogs for
95 * objects and doors for script interface (added a browse button)
96 *
97 * 22 12/17/98 7:26p Jeff
98 * new script system data
99 *
100 * 21 12/17/98 12:50p Jason
101 * changed doorpage
102 *
103 * 20 12/01/98 4:31p Chris
104 * Checked in a massive amount of AI work. Improved flocking code. :)
105 * (Still hacked lightly). In addition, I am moving toward using the
106 * composite dir. :)
107 *
108 * 19 11/19/98 8:25p Chris
109 *
110 * 18 11/13/98 12:30p Jason
111 * changes for weapons
112 *
113 * 17 11/06/98 11:24a Jason
114 * fixed length bug
115 *
116 * 16 11/05/98 7:55p Jason
117 * changes for new manage system
118 *
119 * 15 10/19/98 11:57a Chris
120 * Update the sound system to use the import volume
121 *
122 * 14 10/14/98 5:15p Jason
123 * added version checking to the table file
124 *
125 * 13 6/23/98 2:43p Matt
126 * Changed calls to OutrageMessageBox() & Debug_MessageBox() to deal with
127 * int return value (instead of bool).
128 *
129 * 12 6/05/98 12:54p Jason
130 * fixed stupid pagelock bug
131 *
132 * 11 6/03/98 3:46p Jason
133 * made megacell system more robust
134 *
135 * 10 5/04/98 5:13p Jason
136 * better error handling
137 *
138 * 9 3/19/98 1:15p Jason
139 * more error checking
140 *
141 * 8 3/19/98 12:02p Jason
142 * added better error checking to pagelocks
143 *
144 * 7 2/19/98 1:52p Jason
145 * added emergency override to unlock function
146 *
147 * 6 10/06/97 3:30p Samir
148 * FROM JASON: added error message
149 *
150 * 5 9/25/97 2:19p Jason
151 * added test case to EraseLocker
152 *
153 * 4 9/25/97 11:22a Jason
154 * added error checking to EraseLocker
155 *
156 * 3 8/12/97 12:07p Matt
157 * Treat old robot locks as generic locks
158 *
159 * 2 8/08/97 1:48p Matt
160 * Change mng_MakeLocker() bring up dialog if no network, and give the
161 * player a chance to retry if someone else has the database locked.
162 *
163 * 16 3/17/97 6:52p Jason
164 * added mprintf to locker file error
165 *
166 * 15 3/13/97 7:38p Matt
167 * Added include of string.h
168 *
169 * 14 3/13/97 6:42 PM Jeremy
170 * #included <errno.h> and unincluded <windows.h>
171 *
172 * 13 3/03/97 6:21p Matt
173 * Changed cfile functions to use D3 naming convention
174 *
175 * $NoKeywords: $
176 */
177
178 #include <cerrno>
179 #include <cstdio>
180 #include <cstdlib>
181 #include <cstring>
182 #include <filesystem>
183
184 #include "cfile.h"
185 #include "manage.h"
186 #include "pstypes.h"
187 #include "pserror.h"
188 #include "mono.h"
189 #include "mem.h"
190
191 #ifndef RELEASE
192 #include <ctime>
193 #endif
194
195 #define CURRENT_TABLE_VERSION 22
196 extern const char *PageNames[];
197
198 void mng_InitPagelocks() {
199 // If there is not a pagelock file, create one with a dummy header.
200
201 CFILE *infile = nullptr, *outfile = nullptr;
202 mngs_Pagelock testlock;
203
204 if (!Network_up)
205 return;
206
207 infile = (CFILE *)cfopen(TableLockFilename, "rb");
208 if (infile == NULL) {
209 outfile = (CFILE *)cfopen(TableLockFilename, "wb");
210 if (!outfile) {
211 Error("Error creating table lock file!");
212 return;
213 }
214 testlock.pagetype = PAGETYPE_UNKNOWN;
215 strcpy(testlock.name, "DUMMY PAGE");
216 strcpy(testlock.holder, "NOT HELD AT ALL");
217 mng_WritePagelock(outfile, &testlock);
218
219 cfclose(outfile);
220 } else
221 cfclose(infile);
222 }
223
224 // Checks to see if the locker file is present.
225 // Returns pointer to locker's name, or NULL if the file is unlocked
226 char *mng_CheckIfLockerPresent() {
227 CFILE *infile;
228 int i = 0;
229 static char lockname[200];
230
231 ASSERT(Network_up);
232
233 infile = (CFILE *)cfopen(LockerFile, "rb");
234 if (!infile)
235 return NULL;
236
237 cf_ReadString(lockname, sizeof(lockname), infile);
238
239 mprintf(0, "%s has already got exlusive access to the table file.\n", lockname);
240
241 cfclose(infile);
242
243 return lockname;
244 }
245
246 // Returns 1 if we have the most up to date code for the manage system, else 0
247 int TableVersionCurrent() {
248 CFILE *infile;
249
250 if (!cfexist(VersionFile))
251 return 1; // Doesn't exist, so go ahead
252
253 infile = (CFILE *)cfopen(VersionFile, "rb");
254 if (!infile) {
255 mprintf(0, "Couldn't open the version file for querying!\n");
256 return 0;
257 }
258
259 int version = cf_ReadInt(infile);
260
261 cfclose(infile);
262
263 if (version > CURRENT_TABLE_VERSION)
264 return 0;
265
266 return 1;
267 }
268
269 // Call this before any chokepoint functions are executed.
270 // Locks the whole table system for our exclusive use
271 int mng_MakeLocker() {
272 CFILE *outfile = nullptr;
273 int len;
274 char *locker;
275
276 len = strlen(TableUser);
277 #ifndef RELEASE
278 if (!Network_up) {
279 OutrageMessageBox("Error: You are not connected to the network.");
280 return 0;
281 }
282
283 // See if someone else has the file locked
284 do {
285 locker = mng_CheckIfLockerPresent();
286 if (locker) {
287 strupr(locker);
288 if (OutrageMessageBox(MBOX_YESNO,
289 "The network database is already locked by %s.\nIt will probably become available if you "
290 "wait a few moments.\n\nWould you like to try again?",
291 locker) != IDYES)
292 return 0;
293 }
294 } while (locker);
295
296 // Make sure the version is current
297 if (!TableVersionCurrent()) {
298 OutrageMessageBox("You must do a source update and recompile. Your table file code is outdated!");
299 return 0;
300 }
301
302 outfile = (CFILE *)cfopen(LockerFile, "wb");
303 if (!outfile) {
304 OutrageMessageBox("Error opening the locker file!");
305 mprintf(0, "couldn't open the locker file!\n");
306 return 0;
307 }
308 #endif
309 for (int i = 0; i < len; i++)
310 cf_WriteByte(outfile, TableUser[i]);
311 cfclose(outfile);
312 return 1;
313 }
314
315 // Simply erases the Lockerfile
316 void mng_EraseLocker() {
317
318 #ifndef RELEASE
319 int count = 0;
320 int done = 0;
321
322 while (!done) {
323 CFILE *fp = (CFILE *)cfopen(LockerFile, "rt");
324 if (!fp)
325 return;
326 else
327 cfclose(fp);
328
329 if (remove(LockerFile)) {
330 if (count >= 10) {
331 OutrageMessageBox("There was a problem deleting the locker file...get Jason immediately!");
332 return;
333 } else
334 count++;
335 } else {
336 done = 1;
337 }
338 }
339
340 // Now update the version info
341 count = 0;
342 while (1) {
343 CFILE *fp = (CFILE *)cfopen(VersionFile, "wb");
344 if (!fp) {
345 if (count >= 10) {
346 OutrageMessageBox("There was a problem opening the version file...get Jason immediately!");
347 return;
348 } else
349 count++;
350 } else {
351 cf_WriteInt(fp, CURRENT_TABLE_VERSION);
352 cfclose(fp);
353 return;
354 }
355 }
356 #endif
357 }
358
359 int mng_CheckIfPageLocked(mngs_Pagelock *pl) {
360 // Given a page name, checks to see if it is locked.
361 // Returns 1 if locked, 0 if not.
362
363 CFILE *infile;
364 mngs_Pagelock testlock;
365 int r = -1, done = 0;
366
367 if (!Network_up)
368 return 1;
369
370 infile = (CFILE *)cfopen(TableLockFilename, "rb");
371 if (infile == NULL) {
372 strcpy(ErrorString, "There was a problem opening the table lock file for reading.");
373 return -1;
374 }
375
376 while (!done) {
377 r = mng_ReadPagelock(infile, &testlock);
378 if (r == 0) {
379 done = 1;
380 r = 2;
381 } else {
382 if (!stricmp(pl->name, testlock.name)) {
383 int test = 0;
384 if (pl->pagetype == testlock.pagetype) {
385 if (!stricmp(testlock.holder, "UNLOCKED"))
386 r = 0;
387 else {
388 snprintf(InfoString, sizeof(InfoString), "User %s already has this page locked.", testlock.holder);
389 if (!stricmp(testlock.holder, TableUser))
390 r = 0;
391 else
392 r = 1;
393 }
394 strcpy(pl->holder, testlock.holder);
395 done = 1;
396 }
397 }
398 }
399 }
400
401 cfclose(infile);
402 return r;
403 }
404
405 int mng_CheckIfPageOwned(mngs_Pagelock *pl, char *owner) {
406 // Given a page name, checks to see if it is locked by owner.
407 // Returns 1 if locked by owner, 0 if not.
408
409 CFILE *infile;
410 mngs_Pagelock testlock;
411 int r = -1, done = 0;
412
413 infile = (CFILE *)cfopen(TableLockFilename, "rb");
414 if (infile == NULL) {
415 strcpy(ErrorString, "There was a problem opening the table lock file for reading.");
416 Int3();
417 return -1;
418 }
419
420 while (!done) {
421 r = mng_ReadPagelock(infile, &testlock);
422 if (r == 0) {
423 // We've reached the end of file, must be a new page and nobody owns it
424 done = 1;
425 r = 2;
426 } else {
427 if (!stricmp(pl->name, testlock.name) && pl->pagetype == testlock.pagetype) {
428 if (!stricmp(testlock.holder, owner))
429 r = 1;
430 else {
431 snprintf(InfoString, sizeof(InfoString), "User %s does not have this page locked.", owner);
432 r = 0;
433 }
434 done = 1;
435 }
436 }
437 }
438
439 cfclose(infile);
440 return r;
441 }
442
443 int mng_ReadPagelock(CFILE *fp, mngs_Pagelock *pl) {
444 // Reads a pagelock from the fp file. Returns 1 if successfully read, 0 if eof was encountered.
445
446 char c;
447 int i;
448
449 if (cfeof(fp))
450 return 0;
451
452 // Read type of page
453 pl->pagetype = cf_ReadByte(fp);
454
455 // Convert old robot locks to generic locks
456 if (pl->pagetype == PAGETYPE_ROBOT)
457 pl->pagetype = PAGETYPE_GENERIC;
458
459 // Read the name of this page
460 for (i = 0; i < PAGELOCK_NAME_LEN; i++) {
461 c = cf_ReadByte(fp);
462 pl->name[i] = c;
463 }
464
465 // Read the user who has this page locked.
466 for (i = 0; i < PAGELOCK_NAME_LEN; i++) {
467 c = cf_ReadByte(fp);
468 pl->holder[i] = c;
469 }
470
471 pl->name[PAGELOCK_NAME_LEN] = 0;
472 pl->holder[PAGELOCK_NAME_LEN] = 0;
473
474 if (pl->pagetype == PAGETYPE_POWERUP) {
475 mprintf(0, "Powerup lock: %d, %s, %s\n", pl->pagetype, pl->name, pl->holder);
476 }
477
478 return 1;
479 }
480
481 int mng_ReplacePagelock(char *name, mngs_Pagelock *pl) {
482 // Given a pagelock, replaces the one already inside the lock file, or if not present, adds it to
483 // the lock file. Returns 0 on error, or 1 if successful.
484
485 CFILE *infile, *outfile;
486 int replaced = 0;
487 int done = 0;
488
489 mngs_Pagelock temp_pl;
490
491 infile = (CFILE *)cfopen(TableLockFilename, "rb");
492 if (!infile) {
493 strcpy(ErrorString, "Couldn't open Table lock file!");
494 Int3(); // Get Jason
495 return 0;
496 }
497 outfile = (CFILE *)cfopen(TempTableLockFilename, "wb");
498 if (!outfile) {
499 cfclose(infile);
500 strcpy(ErrorString, "Couldn't open temporary table lock file!");
501 Int3(); // Get Jason
502 return 0;
503 }
504
505 while (!done) {
506 if (mng_ReadPagelock(infile, &temp_pl)) {
507 if (!stricmp(temp_pl.name, name) && pl->pagetype == temp_pl.pagetype) {
508 // This is the page we want to replace, so write the new one out.
509 mng_WritePagelock(outfile, pl);
510 replaced = 1;
511 } else {
512 mng_WritePagelock(outfile, &temp_pl);
513 }
514 } else {
515
516 if (!replaced) {
517 // This is a new page, so append it to the end of file.
518 mng_WritePagelock(outfile, pl);
519 strcpy(InfoString, "Page was successfully added.");
520 } else
521 strcpy(InfoString, "Page was successfully replaced.");
522 done = 1;
523 }
524 }
525 cfclose(infile);
526 cfclose(outfile);
527
528 if (!SwitcherooFiles(TableLockFilename.u8string().c_str(), TempTableLockFilename)) {
529 Int3();
530 return 0;
531 }
532
533 // Log this change
534 #ifndef RELEASE
535 std::filesystem::path pathstr = std::filesystem::path(NetD3Dir) / "TableLog";
536 FILE *logfile = fopen(pathstr.u8string().c_str(), "at");
537 if (logfile) {
538 char str[255 + 32];
539 char date[255];
540 time_t t;
541 t = time(NULL);
542 strftime(date, 254, "[%a %m/%d/%y %H:%M:%S]", localtime(&t));
543 snprintf(str, sizeof(str), "%s Page %s (%s) last touched by %s\n", date, name, PageNames[pl->pagetype], TableUser);
544 fwrite(str, 1, strlen(str), logfile);
545 fflush(logfile);
546 fclose(logfile);
547 }
548 #endif
549
550 return 1; // successful!
551 }
552
553 // Given a name and a pagetype, deletes the one already inside the lock file
554 int mng_DeletePagelock(char *name, int pagetype) {
555 CFILE *infile, *outfile;
556 int done = 0, deleted = 0;
557 mngs_Pagelock temp_pl;
558
559 mprintf(0, "Deleting pagelock %s.\n", name);
560
561 infile = (CFILE *)cfopen(TableLockFilename, "rb");
562 if (!infile) {
563 strcpy(ErrorString, "Couldn't open Table lock file!");
564 return 0;
565 }
566 outfile = (CFILE *)cfopen(TempTableLockFilename, "wb");
567 if (!outfile) {
568 cfclose(infile);
569 strcpy(ErrorString, "Couldn't open temporary table lock file!");
570 return 0;
571 }
572
573 while (!done) {
574 if (mng_ReadPagelock(infile, &temp_pl)) {
575 if (!stricmp(temp_pl.name, name) && pagetype == temp_pl.pagetype)
576 deleted = 1;
577 else
578 mng_WritePagelock(outfile, &temp_pl);
579
580 } else
581 done = 1;
582 }
583 cfclose(infile);
584 cfclose(outfile);
585
586 if (!deleted) {
587 Int3(); // Get Jason!
588 }
589
590 if (remove(TableLockFilename)) {
591 snprintf(ErrorString, sizeof(ErrorString), "There was a problem deleting the temp file - errno %d", errno);
592 return (0);
593 }
594 if (rename(TempTableLockFilename, TableLockFilename.u8string().c_str())) {
595 snprintf(ErrorString, sizeof(ErrorString), "There was a problem renaming the temp file - errno %d", errno);
596
597 return (0);
598 }
599 return 1; // successful!
600 }
601
602 // Given a list of names and a pagetype, deletes the ones already inside the lock file
603 int mng_DeletePagelockSeries(char *names[], int num, int pagetype) {
604 CFILE *infile, *outfile;
605 int done = 0, deleted = 0;
606 mngs_Pagelock temp_pl;
607
608 infile = (CFILE *)cfopen(TableLockFilename, "rb");
609 if (!infile) {
610 strcpy(ErrorString, "Couldn't open Table lock file!");
611 return 0;
612 }
613 outfile = (CFILE *)cfopen(TempTableLockFilename, "wb");
614 if (!outfile) {
615 cfclose(infile);
616 strcpy(ErrorString, "Couldn't open temporary table lock file!");
617 return 0;
618 }
619
620 while (!done) {
621 if (mng_ReadPagelock(infile, &temp_pl)) {
622 if (pagetype == temp_pl.pagetype) {
623 int found = -1;
624
625 for (int i = 0; i < num && found == -1; i++) {
626 if (!stricmp(names[i], temp_pl.name)) {
627 found = i;
628 }
629 }
630
631 if (found == -1)
632 mng_WritePagelock(outfile, &temp_pl);
633 else
634 mprintf(0, "Deleting pagelock %s.\n", names[found]);
635 } else
636 mng_WritePagelock(outfile, &temp_pl);
637
638 } else
639 done = 1;
640 }
641 cfclose(infile);
642 cfclose(outfile);
643
644 if (remove(TableLockFilename)) {
645 snprintf(ErrorString, sizeof(ErrorString), "There was a problem deleting the temp file - errno %d", errno);
646 return (0);
647 }
648 if (rename(TempTableLockFilename, TableLockFilename.u8string().c_str())) {
649 snprintf(ErrorString, sizeof(ErrorString), "There was a problem renaming the temp file - errno %d", errno);
650
651 return (0);
652 }
653 return 1; // successful!
654 }
655
656 // Goes through the pagelock table and deletes all duplicate entries
657 int mng_DeleteDuplicatePagelocks() {
658 CFILE *infile; //,*outfile;
659 int done = 0, deleted = 0;
660 mngs_Pagelock temp_pl;
661 mngs_Pagelock *already_read;
662 int num = 0, duplicates = 0, i;
663
664 already_read = (mngs_Pagelock *)mem_malloc(sizeof(mngs_Pagelock) * 8000);
665 ASSERT(already_read);
666
667 infile = (CFILE *)cfopen(TableLockFilename, "rb");
668 if (!infile) {
669 strcpy(ErrorString, "Couldn't open Table lock file!");
670 return 0;
671 }
672 /* outfile=(CFILE *)cfopen (TempTableLockFilename,"wb");
673 if (!outfile)
674 {
675 cfclose (infile);
676 strcpy (ErrorString,"Couldn't open temporary table lock file!");
677 return 0;
678 }*/
679
680 while (!done) {
681 if (mng_ReadPagelock(infile, &temp_pl)) {
682 int found = -1;
683 for (i = 0; i < num; i++) {
684 if (temp_pl.pagetype == already_read[i].pagetype && !stricmp(temp_pl.name, already_read[i].name)) {
685 mprintf(0, "Found duplicated %s\n", temp_pl.name);
686 found = i;
687 duplicates++;
688 }
689 }
690
691 if (found == -1) {
692 // Hurray, a new entry
693 strcpy(already_read[num].name, temp_pl.name);
694 already_read[num].pagetype = temp_pl.pagetype;
695 num++;
696 ASSERT(num < 8000);
697 }
698
699 } else
700 done = 1;
701 }
702 cfclose(infile);
703
704 mprintf(0, "Found %d duplicates!\n", duplicates);
705 /* cfclose (outfile);
706
707 if (remove (TableLockFilename))
708 {
709 sprintf (ErrorString,"There was a problem deleting the temp file - errno %d",errno);
710 return (0);
711 }
712 if (rename (TempTableLockFilename,TableLockFilename))
713 {
714 sprintf (ErrorString,"There was a problem renaming the temp file - errno %d",errno);
715
716 return (0);
717 }*/
718 mem_free(already_read);
719 return 1; // successful!
720 }
721
722 // Given a list of names and a pagetype, unlocks the ones already inside the lock file
723 int mng_UnlockPagelockSeries(const char *names[], int *pagetypes, int num) {
724 ASSERT(num < 500);
725
726 uint8_t already_done[500];
727 memset(already_done, 0, 500);
728
729 CFILE *infile, *outfile;
730 int done = 0, deleted = 0;
731 mngs_Pagelock temp_pl;
732 int total = 0;
733
734 infile = (CFILE *)cfopen(TableLockFilename, "rb");
735 if (!infile) {
736 strcpy(ErrorString, "Couldn't open Table lock file!");
737 return 0;
738 }
739 outfile = (CFILE *)cfopen(TempTableLockFilename, "wb");
740 if (!outfile) {
741 cfclose(infile);
742 strcpy(ErrorString, "Couldn't open temporary table lock file!");
743 return 0;
744 }
745
746 while (!done) {
747 if (mng_ReadPagelock(infile, &temp_pl)) {
748 int found = -1;
749 for (int i = 0; i < num && found == -1; i++) {
750 if (!stricmp(names[i], temp_pl.name)) {
751 found = i;
752 }
753 }
754
755 if (found == -1)
756 mng_WritePagelock(outfile, &temp_pl);
757 else {
758 if (pagetypes[found] == temp_pl.pagetype) {
759 if (already_done[found]) {
760 mprintf(0, "Found duplicate=%s\n", names[found]);
761 } else {
762 mprintf(0, "Replacing pagelock %s to UNLOCKED.\n", names[found]);
763 strcpy(temp_pl.holder, "UNLOCKED");
764 mng_WritePagelock(outfile, &temp_pl);
765 total++;
766 already_done[found] = 1;
767 }
768
769 } else
770 mng_WritePagelock(outfile, &temp_pl);
771 }
772
773 } else
774 done = 1;
775 }
776 cfclose(infile);
777 cfclose(outfile);
778
779 mprintf(0, "Unlocked %d pages\n", total);
780
781 if (remove(TableLockFilename)) {
782 snprintf(ErrorString, sizeof(ErrorString), "There was a problem deleting the temp file - errno %d", errno);
783 return (0);
784 }
785 if (rename(TempTableLockFilename, TableLockFilename.u8string().c_str())) {
786 snprintf(ErrorString, sizeof(ErrorString), "There was a problem renaming the temp file - errno %d", errno);
787
788 return (0);
789 }
790 return 1; // successful!
791 }
792
793 // Writes a given pagelock out to the file pointed to by fp
794 void mng_WritePagelock(CFILE *fp, mngs_Pagelock *pl) {
795 int i;
796
797 // Write pagetype
798 cf_WriteByte(fp, pl->pagetype);
799
800 // Write name
801 for (i = 0; i < PAGELOCK_NAME_LEN; i++)
802 cf_WriteByte(fp, pl->name[i]);
803
804 // Who owns this page?
805 for (i = 0; i < PAGELOCK_NAME_LEN; i++)
806 cf_WriteByte(fp, pl->holder[i]);
807 }
808
809 int mng_GetListOfLocks(mngs_Pagelock *pl, int max, char *who) {
810 // Given a list of pagelocks, a max number allowed, and a specified user "who", fills in the
811 // given pagelocks with all the pages that are locked by "who".
812
813 CFILE *infile;
814 mngs_Pagelock temp_pl;
815 int done = 0, num = 0;
816
817 infile = (CFILE *)cfopen(TableLockFilename, "rb");
818 if (!infile) {
819 strcpy(ErrorString, "Couldn't open Table lock file!");
820 return -1;
821 }
822
823 while (!done) {
824 // Read in a page, compare it with the owner we're searching for. If found, make a copy of it.
825
826 if (mng_ReadPagelock(infile, &temp_pl)) {
827 if (((who == NULL) && stricmp(temp_pl.holder, "UNLOCKED")) ||
828 ((who != NULL) && (!stricmp(temp_pl.holder, who)))) {
829 pl[num] = temp_pl;
830 num++;
831 if (num >= max)
832 done = 1;
833 }
834
835 } else
836 done = 1;
837 }
838
839 cfclose(infile);
840 return num;
841 }
842
843 // Takes a pagelock and sets its holder name to UNLOCKED
844 void mng_OverrideToUnlocked(mngs_Pagelock *temp_pl) {
845 int r;
846 #ifndef RELEASE
847 r = mng_CheckIfPageOwned(temp_pl, TableUser);
848
849 if (r == 1) {
850 OutrageMessageBox("You own this page, so there is no use in you setting it to unlocked!");
851 return;
852 }
853
854 int answer = OutrageMessageBox(
855 MBOX_YESNO,
856 "Are you sure you wish to override this page to unlocked? (choose 'NO' if you don't know what you're doing!)");
857
858 if (answer == IDNO)
859 return;
860
861 answer = OutrageMessageBox(MBOX_YESNO,
862 "Do you wish to actually erase this pagelock? (Again, answer NO if you are not sure!)");
863
864 if (!mng_MakeLocker())
865 return;
866
867 strcpy(temp_pl->holder, "UNLOCKED");
868
869 if (answer == IDNO) {
870 if (!mng_ReplacePagelock(temp_pl->name, temp_pl))
871 OutrageMessageBox(ErrorString, "Error!");
872 else {
873 OutrageMessageBox("Page set to unlocked.");
874 }
875 } else {
876 // Given a name and a pagetype, deletes the one already inside the lock file
877 if (!mng_DeletePagelock(temp_pl->name, temp_pl->pagetype)) {
878 OutrageMessageBox(ErrorString, "Error!");
879 } else
880 OutrageMessageBox("Pagelock deleted!");
881 }
882 #endif
883 mng_EraseLocker();
884 }