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

1.1       snw         1: /*
1.5     ! snw         2:  *   $Id: tp_check.c,v 1.4 2025/03/22 18:43:54 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.5     ! snw        27:  *   Revision 1.4  2025/03/22 18:43:54  snw
        !            28:  *   Make STRLEN 255 chars and add BIGSTR macro for larger buffers
        !            29:  *
1.4       snw        30:  *   Revision 1.3  2025/03/09 19:50:47  snw
                     31:  *   Second phase of REUSE compliance and header reformat
                     32:  *
1.3       snw        33:  *
                     34:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                     35:  * SPDX-License-Identifier: AGPL-3.0-or-later
1.1       snw        36:  **/
                     37: 
                     38: #include <stdlib.h>
                     39: #include <string.h>
                     40: #include <unistd.h>
                     41: #include "tp_check.h"
                     42: #include "mpsdef.h"
                     43: #include "transact.h"
                     44: #include "journal.h"
                     45: #include "fs.h"
                     46: 
                     47: short frm_global_exists(char *, char *, char *);
                     48: 
                     49: cptab *cptab_head[TP_MAX_NEST];
                     50: 
                     51: cptab *cptab_insert(int tlevel, char *global)
                     52: {
                     53:     cptab *t;
                     54:     short g_exists;
                     55:     char *gc_ns;
                     56:     char *gc_pth;
                     57:     
                     58:     gc_ns = (char *) malloc (STRLEN * sizeof (char));
                     59:     NULLPTRCHK(gc_ns,"cptab_insert");
                     60: 
1.4       snw        61:     gc_pth = (char *) malloc (PATHLEN * sizeof (char));
1.1       snw        62:     NULLPTRCHK(gc_pth,"cptab_insert");
                     63: 
                     64:     for (t = cptab_head[tlevel]; t != NULL; t = t->next) {
                     65: 
                     66:         if ((strcmp (t->global, global) == 0) && (t->mode > CP_UNUSED)) {
                     67:             /* found match */
                     68:             return t;
                     69:         }
                     70: 
                     71:     }
                     72: 
                     73:     /* insert */
                     74:     t = (cptab *) malloc (sizeof (cptab));
                     75:     NULLPTRCHK(t,"cptab_insert");
                     76: 
                     77:     t->global = (char *) malloc (sizeof (char) * (strlen (global) + 1));
                     78:     NULLPTRCHK(t->global,"cptab_insert");
                     79: 
                     80:     strcpy (t->global, global);
                     81: 
                     82:     g_exists = frm_global_exists (gc_ns, gc_pth, global);
                     83:     
                     84:     t->file = (char *) malloc (sizeof (char) * (strlen (gc_pth)));
                     85:     NULLPTRCHK(t->file,"cptab_insert");
                     86: 
1.4       snw        87:     t->cp_file = (char *) malloc (sizeof (char) * PATHLEN);
1.1       snw        88:     NULLPTRCHK(t->cp_file,"cptab_insert");
                     89:     
                     90:     strcpy (t->file, gc_pth);
                     91:     stcnv_m2c (t->file);
                     92:     
1.4       snw        93:     snprintf (t->cp_file, PATHLEN, "%s.%d.%d.chk", t->file, pid, tp_level);
1.1       snw        94: 
                     95:     free (gc_ns);
                     96:     free (gc_pth);
                     97:     
                     98:     if (!g_exists) {
                     99:         t->mode = CP_REMOVE;
                    100:     }
                    101:     else {
                    102:         t->mode = CP_RESTORE;
                    103:     }
                    104: 
                    105:     t->next = cptab_head[tlevel];
                    106:     cptab_head[tlevel] = t;
                    107: 
                    108:     return t;
                    109: }
                    110: 
                    111: short cptab_precommit(int tlevel)
                    112: {
                    113:     cptab *t;
                    114:     /*char *cmd;*/
                    115:     char *pctmp;
                    116:     int rc;
                    117: 
                    118:     /*
                    119:     cmd = (char *) malloc (STRLEN * sizeof (char));
                    120:     NULLPTRCHK(cmd,"cptab_precommit");
                    121:     */
                    122:     
                    123:     pctmp = (char *) malloc (STRLEN * sizeof (char));
                    124:     NULLPTRCHK(pctmp,"cptab_precommit");
                    125:     
                    126:     for (t = cptab_head[tlevel]; t != NULL; t = t->next) {
                    127:         
                    128:         if (t->mode == CP_RESTORE) {
                    129: 
                    130:             /*
                    131:             snprintf (cmd, STRLEN - 1, "/bin/cp %s %s", t->file, t->cp_file);
                    132:             rc = system (cmd);
                    133:             */
                    134:             
                    135:             rc = cp (t->cp_file, t->file);
                    136:             
                    137:             if (rc != 0) {
                    138: 
                    139:                 strcpy (pctmp, t->file);
                    140:                 stcnv_c2m (pctmp);
                    141:                 
                    142:                 jnl_ent_write (JNLA_CHECKPOINT_FAIL, " \201", pctmp);
                    143:                 
                    144:                 /*free (cmd);*/
                    145:                 free (pctmp);
                    146: 
                    147:                 return FALSE;
                    148:                 
                    149:             }
                    150:             else {
                    151: 
                    152:                 strcpy (pctmp, t->file);
                    153:                 stcnv_c2m (pctmp);
                    154:                 
                    155:                 jnl_ent_write (JNLA_CHECKPOINT_OK, " \201", pctmp);
                    156: 
                    157:             }
                    158: 
                    159:         }
                    160:         
                    161:     }
                    162:     
                    163:     /*free (cmd);*/
                    164:     free (pctmp);
                    165:     
                    166:     return TRUE;    
                    167: }
                    168: 
                    169: void cptab_postcommit(int tlevel)
                    170: {
                    171:     cptab *t;
                    172:     /*char *cmd;*/
                    173: 
                    174:     /*
                    175:     cmd = (char *) malloc (STRLEN * sizeof (char));
                    176:     NULLPTRCHK(cmd,"cptab_postcommit");
                    177:     */
                    178: 
                    179:     for (t = cptab_head[tlevel]; t != NULL; t = t->next) {
                    180: 
                    181:         if (t->mode == CP_RESTORE) {
                    182:             /*
                    183:             snprintf (cmd, STRLEN - 1, "/bin/rm -f '%s'", t->cp_file);
                    184:             rc = system (cmd);
                    185:             */
                    186:             unlink (t->cp_file);
                    187:         }
                    188:         
                    189:     }
                    190: 
                    191:     cptab_head[tlevel] = NULL;
                    192: }
                    193: 
                    194: short cptab_rollback(int tlevel)
                    195: {
                    196:     cptab *t;
                    197:     /*char *cmd;*/
                    198:     int rc;
                    199: 
                    200:     /*
                    201:     cmd = (char *) malloc (STRLEN * sizeof (char));
                    202:     NULLPTRCHK(cmd,"cptab_rollback");
                    203:     */
                    204:     
                    205:     for (t = cptab_head[tlevel]; t != NULL; t = t->next) {
                    206:         
                    207:         switch (t->mode) {
                    208: 
                    209:             case CP_REMOVE:
                    210:                 unlink (t->file);
                    211:                 /*
                    212:                 snprintf (cmd, STRLEN - 1, "/bin/rm -f '%s'", t->file);
                    213:                 rc = system (cmd);
                    214:                 */
                    215:                 break;
                    216: 
                    217:             case CP_RESTORE:
                    218:                 /*
                    219:                 snprintf (cmd, STRLEN - 1, "/bin/cp '%s' '%s'", t->cp_file, t->file);
                    220:                 rc = system (cmd);
                    221:                 */
                    222:                 rc = cp (t->file, t->cp_file);
                    223:                 
                    224:                 if (rc != 0) {
                    225:                     cptab_head[tlevel] = NULL;
                    226:                     /*free (cmd);*/
                    227:                     return FALSE;
                    228:                 }
                    229: 
                    230:                 /*
                    231:                 snprintf (cmd, STRLEN - 1, "/bin/rm -f %s", t->cp_file);
                    232:                 rc = system (cmd);
                    233:                 */
                    234: 
                    235:                 unlink (t->cp_file);
                    236:                 if (rc != 0) {
                    237:                     cptab_head[tlevel] = NULL;
                    238:                     /*free (cmd);*/
                    239:                     return FALSE;
                    240:                 }
                    241: 
                    242:                 break;
                    243: 
                    244:                 
                    245:         }
                    246: 
                    247:     }
                    248: 
                    249:     cptab_head[tlevel] = NULL;
                    250:     
                    251:     return TRUE;
                    252: 
                    253: }
                    254: 
                    255: void cptab_dump(int tlevel)
                    256: {
                    257:     cptab *gt;
                    258:     char cp_mode[15];
                    259:     
                    260:     printf ("\n  Global database checkpoints:\n");
                    261: 
                    262:     printf ("\n   %-30s%-20s%s\n", "GLOBAL", "MODE", "FILES");
                    263:     printf ("   %-30s%-20s%s\n", "------", "----", "-----");
                    264: 
                    265:     for (gt = cptab_head[tlevel]; gt != NULL; gt = gt->next) {
                    266: 
                    267:         switch (gt->mode) {
                    268: 
                    269:             case CP_UNUSED:
                    270:                 strcpy (cp_mode, "CP_UNUSED");
                    271:                 break;
                    272: 
                    273:             case CP_REMOVE:
                    274:                 strcpy (cp_mode, "CP_REMOVE");
                    275:                 break;
                    276: 
                    277:             case CP_RESTORE:
                    278:                 strcpy (cp_mode, "CP_RESTORE");
                    279:                 break;
                    280: 
                    281:         }
                    282: 
                    283:         if (gt->mode > CP_UNUSED) {
                    284:             printf ("   %-30s%-20sIN:   %s\n", gt->global, cp_mode, gt->file); 
                    285:         }
                    286:         else {
                    287:             printf ("   N/A\n");
                    288:         }
                    289:         
                    290:         if (gt->mode == CP_RESTORE) {
                    291:             printf ("   %-30s%-20sOUT:  %s\n", "", "", gt->cp_file);
                    292:         }
                    293:         
                    294:     }
                    295: }
                    296: 

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