Annotation of freem/src/tp_check.c, revision 1.7

1.1       snw         1: /*
1.7     ! snw         2:  *   $Id: tp_check.c,v 1.6 2025/04/13 04:22:43 snw Exp $
1.3       snw         3:  *    TP global checkpointing code
1.1       snw         4:  *
                      5:  *  
1.2       snw         6:  *   Author: Serena Willis <snw@coherent-logic.com>
1.1       snw         7:  *    Copyright (C) 1998 MUG Deutschland
1.3       snw         8:  *    Copyright (C) 2022, 2025 Coherent Logic Development LLC
1.1       snw         9:  *
                     10:  *
                     11:  *   This file is part of FreeM.
                     12:  *
                     13:  *   FreeM is free software: you can redistribute it and/or modify
                     14:  *   it under the terms of the GNU Affero Public License as published by
                     15:  *   the Free Software Foundation, either version 3 of the License, or
                     16:  *   (at your option) any later version.
                     17:  *
                     18:  *   FreeM is distributed in the hope that it will be useful,
                     19:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     20:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     21:  *   GNU Affero Public License for more details.
                     22:  *
                     23:  *   You should have received a copy of the GNU Affero Public License
                     24:  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
                     25:  *
1.4       snw        26:  *   $Log: tp_check.c,v $
1.7     ! snw        27:  *   Revision 1.6  2025/04/13 04:22:43  snw
        !            28:  *   Fix snprintf calls
        !            29:  *
1.6       snw        30:  *   Revision 1.5  2025/04/09 19:52:02  snw
                     31:  *   Eliminate as many warnings as possible while building with -Wall
                     32:  *
1.5       snw        33:  *   Revision 1.4  2025/03/22 18:43:54  snw
                     34:  *   Make STRLEN 255 chars and add BIGSTR macro for larger buffers
                     35:  *
1.4       snw        36:  *   Revision 1.3  2025/03/09 19:50:47  snw
                     37:  *   Second phase of REUSE compliance and header reformat
                     38:  *
1.3       snw        39:  *
                     40:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                     41:  * SPDX-License-Identifier: AGPL-3.0-or-later
1.1       snw        42:  **/
                     43: 
                     44: #include <stdlib.h>
                     45: #include <string.h>
                     46: #include <unistd.h>
                     47: #include "tp_check.h"
                     48: #include "mpsdef.h"
                     49: #include "transact.h"
                     50: #include "journal.h"
                     51: #include "fs.h"
                     52: 
1.7     ! snw        53: #if defined(__OS2__)
        !            54: # include <os2.h>
        !            55: #endif
        !            56: 
1.1       snw        57: short frm_global_exists(char *, char *, char *);
                     58: 
                     59: cptab *cptab_head[TP_MAX_NEST];
                     60: 
                     61: cptab *cptab_insert(int tlevel, char *global)
                     62: {
                     63:     cptab *t;
                     64:     short g_exists;
                     65:     char *gc_ns;
                     66:     char *gc_pth;
                     67:     
                     68:     gc_ns = (char *) malloc (STRLEN * sizeof (char));
                     69:     NULLPTRCHK(gc_ns,"cptab_insert");
                     70: 
1.4       snw        71:     gc_pth = (char *) malloc (PATHLEN * sizeof (char));
1.1       snw        72:     NULLPTRCHK(gc_pth,"cptab_insert");
                     73: 
                     74:     for (t = cptab_head[tlevel]; t != NULL; t = t->next) {
                     75: 
                     76:         if ((strcmp (t->global, global) == 0) && (t->mode > CP_UNUSED)) {
                     77:             /* found match */
                     78:             return t;
                     79:         }
                     80: 
                     81:     }
                     82: 
                     83:     /* insert */
                     84:     t = (cptab *) malloc (sizeof (cptab));
                     85:     NULLPTRCHK(t,"cptab_insert");
                     86: 
                     87:     t->global = (char *) malloc (sizeof (char) * (strlen (global) + 1));
                     88:     NULLPTRCHK(t->global,"cptab_insert");
                     89: 
                     90:     strcpy (t->global, global);
                     91: 
                     92:     g_exists = frm_global_exists (gc_ns, gc_pth, global);
                     93:     
                     94:     t->file = (char *) malloc (sizeof (char) * (strlen (gc_pth)));
                     95:     NULLPTRCHK(t->file,"cptab_insert");
                     96: 
1.4       snw        97:     t->cp_file = (char *) malloc (sizeof (char) * PATHLEN);
1.1       snw        98:     NULLPTRCHK(t->cp_file,"cptab_insert");
                     99:     
                    100:     strcpy (t->file, gc_pth);
                    101:     stcnv_m2c (t->file);
                    102:     
1.6       snw       103:     snprintf (t->cp_file, PATHLEN - 1, "%s.%d.%d.chk", t->file, pid, tp_level);
1.1       snw       104: 
                    105:     free (gc_ns);
                    106:     free (gc_pth);
                    107:     
                    108:     if (!g_exists) {
                    109:         t->mode = CP_REMOVE;
                    110:     }
                    111:     else {
                    112:         t->mode = CP_RESTORE;
                    113:     }
                    114: 
                    115:     t->next = cptab_head[tlevel];
                    116:     cptab_head[tlevel] = t;
                    117: 
                    118:     return t;
                    119: }
                    120: 
                    121: short cptab_precommit(int tlevel)
                    122: {
                    123:     cptab *t;
                    124:     /*char *cmd;*/
                    125:     char *pctmp;
                    126:     int rc;
                    127: 
                    128:     /*
                    129:     cmd = (char *) malloc (STRLEN * sizeof (char));
                    130:     NULLPTRCHK(cmd,"cptab_precommit");
                    131:     */
                    132:     
                    133:     pctmp = (char *) malloc (STRLEN * sizeof (char));
                    134:     NULLPTRCHK(pctmp,"cptab_precommit");
                    135:     
                    136:     for (t = cptab_head[tlevel]; t != NULL; t = t->next) {
                    137:         
                    138:         if (t->mode == CP_RESTORE) {
                    139: 
                    140:             /*
                    141:             snprintf (cmd, STRLEN - 1, "/bin/cp %s %s", t->file, t->cp_file);
                    142:             rc = system (cmd);
                    143:             */
                    144:             
                    145:             rc = cp (t->cp_file, t->file);
                    146:             
                    147:             if (rc != 0) {
                    148: 
                    149:                 strcpy (pctmp, t->file);
                    150:                 stcnv_c2m (pctmp);
                    151:                 
                    152:                 jnl_ent_write (JNLA_CHECKPOINT_FAIL, " \201", pctmp);
                    153:                 
                    154:                 /*free (cmd);*/
                    155:                 free (pctmp);
                    156: 
                    157:                 return FALSE;
                    158:                 
                    159:             }
                    160:             else {
                    161: 
                    162:                 strcpy (pctmp, t->file);
                    163:                 stcnv_c2m (pctmp);
                    164:                 
                    165:                 jnl_ent_write (JNLA_CHECKPOINT_OK, " \201", pctmp);
                    166: 
                    167:             }
                    168: 
                    169:         }
                    170:         
                    171:     }
                    172:     
                    173:     /*free (cmd);*/
                    174:     free (pctmp);
                    175:     
                    176:     return TRUE;    
                    177: }
                    178: 
                    179: void cptab_postcommit(int tlevel)
                    180: {
                    181:     cptab *t;
                    182:     /*char *cmd;*/
                    183: 
                    184:     /*
                    185:     cmd = (char *) malloc (STRLEN * sizeof (char));
                    186:     NULLPTRCHK(cmd,"cptab_postcommit");
                    187:     */
                    188: 
                    189:     for (t = cptab_head[tlevel]; t != NULL; t = t->next) {
                    190: 
                    191:         if (t->mode == CP_RESTORE) {
                    192:             /*
                    193:             snprintf (cmd, STRLEN - 1, "/bin/rm -f '%s'", t->cp_file);
                    194:             rc = system (cmd);
                    195:             */
                    196:             unlink (t->cp_file);
                    197:         }
                    198:         
                    199:     }
                    200: 
                    201:     cptab_head[tlevel] = NULL;
                    202: }
                    203: 
                    204: short cptab_rollback(int tlevel)
                    205: {
                    206:     cptab *t;
                    207:     int rc;
                    208: 
                    209:     for (t = cptab_head[tlevel]; t != NULL; t = t->next) {
                    210:         
                    211:         switch (t->mode) {
                    212: 
                    213:             case CP_REMOVE:
                    214:                 unlink (t->file);
                    215:                 break;
                    216: 
                    217:             case CP_RESTORE:
1.7     ! snw       218: #if !defined(__OS2__)                
1.1       snw       219:                 rc = cp (t->file, t->cp_file);
1.7     ! snw       220: #else
        !           221:                 rc = DosCopy (t->cp_file, t->file, 1);
        !           222: #endif                
1.1       snw       223:                 if (rc != 0) {
                    224:                     cptab_head[tlevel] = NULL;
                    225:                     /*free (cmd);*/
                    226:                     return FALSE;
                    227:                 }
                    228: 
                    229:                 unlink (t->cp_file);
                    230:                 if (rc != 0) {
                    231:                     cptab_head[tlevel] = NULL;
                    232:                     /*free (cmd);*/
                    233:                     return FALSE;
                    234:                 }
                    235: 
                    236:                 break;
                    237: 
                    238:                 
                    239:         }
                    240: 
                    241:     }
                    242: 
                    243:     cptab_head[tlevel] = NULL;
                    244:     
                    245:     return TRUE;
                    246: 
                    247: }
                    248: 
                    249: void cptab_dump(int tlevel)
                    250: {
                    251:     cptab *gt;
                    252:     char cp_mode[15];
                    253:     
                    254:     printf ("\n  Global database checkpoints:\n");
                    255: 
                    256:     printf ("\n   %-30s%-20s%s\n", "GLOBAL", "MODE", "FILES");
                    257:     printf ("   %-30s%-20s%s\n", "------", "----", "-----");
                    258: 
                    259:     for (gt = cptab_head[tlevel]; gt != NULL; gt = gt->next) {
                    260: 
                    261:         switch (gt->mode) {
                    262: 
                    263:             case CP_UNUSED:
                    264:                 strcpy (cp_mode, "CP_UNUSED");
                    265:                 break;
                    266: 
                    267:             case CP_REMOVE:
                    268:                 strcpy (cp_mode, "CP_REMOVE");
                    269:                 break;
                    270: 
                    271:             case CP_RESTORE:
                    272:                 strcpy (cp_mode, "CP_RESTORE");
                    273:                 break;
                    274: 
                    275:         }
                    276: 
                    277:         if (gt->mode > CP_UNUSED) {
                    278:             printf ("   %-30s%-20sIN:   %s\n", gt->global, cp_mode, gt->file); 
                    279:         }
                    280:         else {
                    281:             printf ("   N/A\n");
                    282:         }
                    283:         
                    284:         if (gt->mode == CP_RESTORE) {
                    285:             printf ("   %-30s%-20sOUT:  %s\n", "", "", gt->cp_file);
                    286:         }
                    287:         
                    288:     }
                    289: }
                    290: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>