summaryrefslogtreecommitdiff
path: root/backup.c
blob: 68368aeff2b2b553256d7326cab7e51bdc0b1c07 (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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
/*
 * nvidia-installer: A tool for installing NVIDIA software packages on
 * Unix and Linux systems.
 *
 * Copyright (C) 2003 NVIDIA Corporation
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 *
 *
 * backup.c - this source file contains functions used for backing up
 * (and restoring) files that need to be moved out of the way during
 * installation.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <ctype.h>
#include <stdlib.h>

#include "nvidia-installer.h"
#include "user-interface.h"
#include "backup.h"
#include "files.h"
#include "crc.h"
#include "misc.h"

#define BACKUP_DIRECTORY "/var/lib/nvidia"
#define BACKUP_LOG       (BACKUP_DIRECTORY "/log")
#define BACKUP_MKDIR_LOG (BACKUP_DIRECTORY "/dirs")

#define RMMOD_MODULE_NAME "nvidia"




/*
 * Syntax for the backup log file:
 *
 * 1. The first line is the version string, assumed to be in the form:
 *    MAJOR.MINOR-PATCH
 *
 * 2. The second line is the driver description.
 *
 * XXX do we need anything to distinguish between official builds,
 * nightlies, etc...?
 *
 * 3. The rest of the file is file entries; a file entry can be any one of:
 *
 * INSTALLED_FILE: <filename>
 *
 * INSTALLED_SYMLINK: <filename>
 *  <target>
 *
 * BACKED_UP_SYMLINK: <filename>
 *  <target>
 *  <permissions> <uid> <gid>
 *
 * BACKED_UP_FILE_NUM: <filename>
 *  <filesize> <permissions> <uid> <gid>
 *
 */

#define BACKUP_LOG_PERMS (S_IRUSR|S_IWUSR)

#define BACKUP_DIRECTORY_PERMS (S_IRUSR|S_IWUSR|S_IXUSR)


/*
 * uninstall struct
 *
 */

typedef struct {
    
    int    num;
    char  *filename;
    char  *target;
    uint32 crc;
    mode_t mode;
    uid_t  uid;
    gid_t  gid;
    int    ok;
    
} BackupLogEntry;


typedef struct {
    char *version;
    char *description;
    BackupLogEntry *e;
    int n;
} BackupInfo;


static BackupInfo *read_backup_log_file(Options *op);

static void free_backup_info(BackupInfo *b);

static int check_backup_log_entries(Options *op, BackupInfo *b);

static int do_uninstall(Options *op, const char *version);

static int sanity_check_backup_log_entries(Options *op, BackupInfo *b);

static char *create_backwards_compatible_version_string(const char *str);






/*
 * init_backup() - initialize the backup engine; this consists of
 * creating a new backup directory, and writing to the log file that
 * we're about to install a new driver version.
 */

int init_backup(Options *op, Package *p)
{
    mode_t orig_mode;
    char *version;
    FILE *log;
    
    /* remove the directory, if it already exists */

    if (directory_exists(op, BACKUP_DIRECTORY)) {
        if (!remove_directory(op, BACKUP_DIRECTORY)) {
            return FALSE;
        }
    }

    /* create the backup directory, with perms only for owner */

    if (!mkdir_recursive(op, BACKUP_DIRECTORY, BACKUP_DIRECTORY_PERMS)) {
        return FALSE;
    }

    /*
     * fopen below creates the file with mode 0666 & ~umask.
     * In order to ensure the result of that calculation is BACKUP_LOG_PERMS,
     * we temporarily set umask to ~BACKUP_LOG_PERMS to leave just the bits
     * we want.
     *
     * This assumes that BACKUP_LOG does not already exist (if it does, the
     * file permissions will not be modified.) This is assured by the
     * directory removal and re-creation code above.
     */
    orig_mode = umask(~BACKUP_LOG_PERMS);
 
    /* create the log file */
    
    log = fopen(BACKUP_LOG, "a");
    umask(orig_mode);
    if (!log) {
        ui_error(op, "Unable to create backup log file '%s' (%s).",
                 BACKUP_LOG, strerror(errno));
        return FALSE;
    }

    /* write the version and description */

    version = create_backwards_compatible_version_string(p->version);
    
    fprintf(log, "%s\n", version);
    fprintf(log, "%s\n", p->description);

    nvfree(version);
        
    /* close the log file */

    if (fclose(log) != 0) {
        ui_error(op, "Error while closing backup log file '%s' (%s).",
                 BACKUP_LOG, strerror(errno));
        return FALSE;
    }

    return TRUE;
    
} /* init_backup() */



/*
 * do_backup() - backup the specified file.  If it is a regular file,
 * just move it into the backup directory, and add an entry to the log
 * file.
 */

int do_backup(Options *op, const char *filename)
{
    int len, ret, ret_val;
    struct stat stat_buf;
    char *tmp;
    FILE *log;
    uint32 crc;

    static int backup_file_number = BACKED_UP_FILE_NUM;

    ret_val = FALSE;

    log = fopen(BACKUP_LOG, "a");
    if (!log) {
        ui_error(op, "Unable to open backup log file '%s' (%s).",
                 BACKUP_LOG, strerror(errno));
        return FALSE;
    }
    
    if (lstat(filename, &stat_buf) == -1) {
        switch (errno) {
        case ENOENT:
            ret_val = TRUE;
            break;
        default:
            ui_error(op, "Unable to determine properties for file '%s' (%s).",
                     filename, strerror(errno));
        }
        goto done;
    }

    if (S_ISREG(stat_buf.st_mode)) {
        crc = compute_crc(op, filename);
        len = strlen(BACKUP_DIRECTORY) + 64;
        tmp = nvalloc(len + 1);
        snprintf(tmp, len, "%s/%d", BACKUP_DIRECTORY, backup_file_number);
        if (!nvrename(op, filename, tmp)) {
            ui_error(op, "Unable to backup file '%s'.", filename);
            goto done;
        }
        
        fprintf(log, "%d: %s\n", backup_file_number, filename);
        
        /* write the filesize, permissions, uid, gid */
        fprintf(log, "%u %04o %d %d\n", crc, stat_buf.st_mode,
                stat_buf.st_uid, stat_buf.st_gid);
        
        free(tmp);
        backup_file_number++;
    } else if (S_ISLNK(stat_buf.st_mode)) {
        tmp = get_symlink_target(op, filename);
        
        ret = unlink(filename);
        if (ret == -1) {
            ui_error(op, "Unable to remove symbolic link '%s' (%s).",
                     filename, strerror(errno));
            goto done;
        }

        fprintf(log, "%d: %s\n", BACKED_UP_SYMLINK, filename);
        fprintf(log, "%s\n", tmp);
        fprintf(log, "%04o %d %d\n", stat_buf.st_mode,
                stat_buf.st_uid, stat_buf.st_gid);
        free(tmp);
    } else if (S_ISDIR(stat_buf.st_mode)) {

        /* XXX IMPLEMENT ME: recursive moving of a directory */

        ui_error(op, "Unable to backup directory '%s'.", filename);
        goto done;
    } else {
        ui_error(op, "Unable to backup file '%s' (don't know how to deal with "
                 "file type).", filename);
        goto done;
    }
    
    ret_val = TRUE;

 done:

    /* close the log file */

    if (fclose(log) != 0) {
        ui_error(op, "Error while closing backup log file '%s' (%s).",
                 BACKUP_LOG, strerror(errno));
        ret_val = FALSE;
    }
    
    return ret_val;
    
} /* do_backup() */



int log_install_file(Options *op, const char *filename)
{
    FILE *log;
    uint32 crc;
    
    /* open the log file */

    log = fopen(BACKUP_LOG, "a");
    if (!log) {
        ui_error(op, "Unable to open backup log file '%s' (%s).",
                 BACKUP_LOG, strerror(errno));
        return FALSE;
    }
    
    fprintf(log, "%d: %s\n", INSTALLED_FILE, filename);
    
    crc = compute_crc(op, filename);

    fprintf(log, "%u\n", crc);
    
    /* close the log file */

    if (fclose(log) != 0) {
        ui_error(op, "Error while closing backup log file '%s' (%s).",
                 BACKUP_LOG, strerror(errno));
        return FALSE;
    }
    
    return TRUE;

} /* log_install_file() */



int log_create_symlink(Options *op, const char *filename, const char *target)
{
    FILE *log;
    
    /* open the log file */

    log = fopen(BACKUP_LOG, "a");
    if (!log) {
        ui_error(op, "Unable to open backup log file '%s' (%s).",
                 BACKUP_LOG, strerror(errno));
        return FALSE;
    }
    
    fprintf(log, "%d: %s\n", INSTALLED_SYMLINK, filename);
    fprintf(log, "%s\n", target);
    
    /* close the log file */

    if (fclose(log) != 0) {
        ui_error(op, "Error while closing backup log file '%s' (%s).",
                 BACKUP_LOG, strerror(errno));
        return FALSE;
    }
    
    return TRUE;

} /* log_create_symlink() */



static int parse_first_line(const char *buf, int *num, char **filename)
{
    char *c, *local_buf;
        
    if (!buf || !num || !filename) return FALSE;

    local_buf = nvstrdup(buf);
    
    c = local_buf;
    while ((*c != '\0') && (*c != ':')) {
        if (!isdigit(*c)) return FALSE;
        c++;
    }
    if (*c == '\0') return FALSE;

    *c = '\0';
    
    *num = strtol(local_buf, NULL, 10);

    c++;
    while(isspace(*c)) c++;

    *filename = nvstrdup(c);

    free(local_buf);

    return TRUE;
}


static int parse_mode_uid_gid(const char *buf, mode_t *mode,
                              uid_t *uid, gid_t *gid)
{
    char *c, *local_buf, *str;

    if (!buf || !mode || !uid || !gid) return FALSE;

    local_buf = nvstrdup(buf);

    c = str = local_buf;
    while ((*c != '\0') && (isdigit(*c))) c++;
    if (*c == '\0') return FALSE;
    *c = '\0';
    *mode = strtol(str, NULL, 8);

    str = ++c;
    while ((*c != '\0') && (isdigit(*c))) c++;
    if (*c == '\0') return FALSE;
    *c = '\0';
    *uid = strtol(str, NULL, 10);

    str = ++c;
    while ((*c != '\0') && (isdigit(*c))) c++;
    *c = '\0';
    *gid = strtol(str, NULL, 10);
      
    free(local_buf);

    return TRUE;
}


static int parse_crc_mode_uid_gid(const char *buf, uint32 *crc, mode_t *mode,
                                  uid_t *uid, gid_t *gid)
{
    char *c, *local_buf, *str;

    if (!buf || !crc || !mode || !uid || !gid) return FALSE;

    local_buf = nvstrdup(buf);

    c = str = local_buf;
    while ((*c != '\0') && (isdigit(*c))) c++;
    if (*c == '\0') return FALSE;
    *c = '\0';
    *crc = strtoul(str, NULL, 10);

    str = ++c;
    while ((*c != '\0') && (isdigit(*c))) c++;
    if (*c == '\0') return FALSE;
    *c = '\0';
    *mode = strtol(str, NULL, 8);

    str = ++c;
    while ((*c != '\0') && (isdigit(*c))) c++;
    if (*c == '\0') return FALSE;
    *c = '\0';
    *uid = strtol(str, NULL, 10);

    str = ++c;
    while ((*c != '\0') && (isdigit(*c))) c++;
    *c = '\0';
    *gid = strtol(str, NULL, 10);
      
    free(local_buf);

    return TRUE;
}


static int parse_crc(const char *buf, uint32 *crc)
{
    char *c, *local_buf, *str;

    if (!buf || !crc) return FALSE;

    local_buf = nvstrdup(buf);

    c = str = local_buf;
    while ((*c != '\0') && (isdigit(*c))) c++;
    *c = '\0';
    *crc = strtoul(str, NULL, 10);

    free(local_buf);

    return TRUE;

} /* parse_crc() */


/*
 * Syntax for the mkdir log file:
 *
 * Each line in the file contains the name of a directory that was created
 * during driver installation.
 *
 * XXX pathnames containing '\n' will break both this file, and the regular
 * backup log file.
 */


/*
 * log_mkdir() - takes a newline-delimited list of directories and appends
 * them to the log of directories created during installation. The caller
 * should ensure that directories are listed in decreasing order of depth,
 * so that removing them in that order will work.
 */
int log_mkdir(Options *op, const char *dirs)
{
    FILE *log;

    /*
     * create the backup directory if it doesn't exist; BACKUP_MKDIR_LOG
     * is within BACKUP_DIRECTORY, so the below fopen(3) call depends on
     * the existence of BACKUP_DIRECTORY
     */
    if (!directory_exists(op, BACKUP_DIRECTORY) &&
        !mkdir_with_log(op, BACKUP_DIRECTORY, BACKUP_DIRECTORY_PERMS, FALSE)) {
        return FALSE;
    }

    /* open the log file */

    log = fopen(BACKUP_MKDIR_LOG, "a");
    if (!log) {
        ui_error(op, "Unable to open mkdir log file '%s' (%s).",
                 BACKUP_MKDIR_LOG, strerror(errno));
        return FALSE;
    }

    fprintf(log, "%s", dirs);
 
    /* close the log file */

    if (fclose(log) != 0) {
        ui_error(op, "Error while closing mkdir log file '%s' (%s).",
                 BACKUP_MKDIR_LOG, strerror(errno));
        return FALSE;
    }
    
    return TRUE;
}

/*
 * rmdir_recursive() - Use BACKUP_MKDIR_LOG to find directories that were
 * created by a previous nvidia-installer, and delete any such directories.
 * Returns TRUE if the log is found and all directories are successfully
 * deleted; returns FALSE if any directories failed to be deleted or the
 * log isn't found.
 */
static int rmdir_recursive(Options *op)
{
    FILE *log;
    char *dir;
    int eof = FALSE, ret = TRUE;

    /* open the log file */

    log = fopen(BACKUP_MKDIR_LOG, "r");
    if (!log) {
        /* Fail silently: most likely, the current driver was simply installed
         * with an nvidia-installer that didn't log created directories. */
        return FALSE;
    }

    while (!eof) {
        dir = fget_next_line(log, &eof);
        if (dir) {
            /* Ignore empty lines and the backup directory itself, since it is 
             * never empty as long as the dirs file is still around. */
            if (strlen(dir) && strcmp(dir, BACKUP_DIRECTORY) != 0) {
                if (rmdir(dir) != 0) {
                    ui_warn(op, "Failed to delete the directory '%s' (%s).",
                            dir, strerror(errno));
                    ret = FALSE;
                }
            }
        }
        free(dir);
    }

    /* close the log file */

    if (fclose(log) != 0) {
        ui_error(op, "Error while closing mkdir log file '%s' (%s).",
                 BACKUP_MKDIR_LOG, strerror(errno));
        return FALSE;
    }

    return ret;
}

/*
 * do_uninstall() - this function uninstalls a previously installed
 * driver, by parsing the BACKUP_LOG file.
 */

static int do_uninstall(Options *op, const char *version)
{
    BackupLogEntry *e;
    BackupInfo *b;
    int i, len, ok;
    char *tmpstr, *cmd;
    float percent;

    static const char existing_installation_is_borked[] = 
        "Your driver installation has been "
        "altered since it was initially installed; this may happen, "
        "for example, if you have since installed the NVIDIA driver through "
        "a mechanism other than nvidia-installer (such as your "
        "distribution's native package management system).  "
        "nvidia-installer will attempt to uninstall as best it can.";
    
    /* do we even have a backup directory? */

    if (access(BACKUP_DIRECTORY, F_OK) == -1) {
        ui_message(op, "No driver backed up.");
        return FALSE;
    }
    
    if ((b = read_backup_log_file(op)) == NULL) return FALSE;

    ok = check_backup_log_entries(op, b);

    if (!ok) {
        if (op->logging) {
            ui_warn(op, "%s  Please see the file '%s' for details.",
                    existing_installation_is_borked, op->log_file_name);

        } else {
            ui_warn(op, "%s", existing_installation_is_borked);
        }
    }
    
    tmpstr = nvstrcat("Uninstalling ", b->description, " (",
                      b->version, "):", NULL);

    run_distro_hook(op, "pre-uninstall");

    ui_status_begin(op, tmpstr, "Uninstalling");

    free(tmpstr);

    /* Remove any installed DKMS modules */

    if (dkms_module_installed(op, version)) {
        ui_log(op, "DKMS module detected; removing...");
        if (!dkms_remove_module(op, version)) {
            ui_warn(op, "Failed to remove installed DKMS module!");
        }
    }

    /*
     * given the list of Backup logfile entries, perform the necessary
     * operations:
     *
     * Step 1: remove everything that was previously installed
     *
     * Step 2: restore everything that was previously backed up
     */

    for (i = 0; i < b->n; i++) {

        percent = (float) i / (float) (b->n * 2);

        e = &b->e[i];

        if (!e->ok) continue;
        switch (e->num) {
            
            /*
             * This is a file that was installed -- now delete it.
             */

        case INSTALLED_FILE:
            if (unlink(e->filename) == -1) {
                ui_warn(op, "Unable to remove installed file '%s' (%s).",
                        e->filename, strerror(errno));
            }
            ui_status_update(op, percent, e->filename);
            break;

        case INSTALLED_SYMLINK:
            if (unlink(e->filename) == -1) {
                ui_warn(op, "Unable to remove installed symlink '%s' (%s).",
                        e->filename, strerror(errno));
            }
            ui_status_update(op, percent, e->filename);
            break;
        }
    }
    
    for (i = 0; i < b->n; i++) {
        
        percent = (float) (i + b->n) / (float) (b->n * 2);
        
        e = &b->e[i];
        
        if (!e->ok) continue;
        
        switch (e->num) {
            
          case INSTALLED_FILE:
          case INSTALLED_SYMLINK:
            /* nothing to do */
            break;

          case BACKED_UP_SYMLINK:
            if (symlink(e->target, e->filename) == -1) {
                
                /*
                 * XXX only print this warning if
                 * check_backup_log_entries() didn't see any problems.
                 */

                if (ok) {
                    ui_warn(op, "Unable to restore symbolic link "
                            "%s -> %s (%s).", e->filename, e->target,
                            strerror(errno));
                } else {
                    ui_log(op, "Unable to restore symbolic link "
                           "%s -> %s (%s).", e->filename, e->target,
                           strerror(errno));
                }
            } else {
                
                /* XXX do we need to chmod the symlink? */

                if (lchown(e->filename, e->uid, e->gid)) {
                    ui_warn(op, "Unable to restore owner (%d) and group "
                            "(%d) for symbolic link '%s' (%s).",
                            e->uid, e->gid, e->filename, strerror(errno));
                }
            }
            ui_status_update(op, percent, e->filename);
            break;
            
          default:
            len = strlen(BACKUP_DIRECTORY) + 64;
            tmpstr = nvalloc(len + 1);
            snprintf(tmpstr, len, "%s/%d", BACKUP_DIRECTORY, e->num);
            if (!nvrename(op, tmpstr, e->filename)) {
                ui_warn(op, "Unable to restore file '%s'.", e->filename);
            } else {
                if (chown(e->filename, e->uid, e->gid)) {
                    ui_warn(op, "Unable to restore owner (%d) and group "
                            "(%d) for file '%s' (%s).",
                            e->uid, e->gid, e->filename, strerror(errno));
                } else {
                    if (chmod(e->filename, e->mode) == -1) {
                        ui_warn(op, "Unable to restore permissions %04o for "
                                "file '%s'.", e->mode, e->filename);
                    }
                }
            }
            ui_status_update(op, percent, e->filename);
            free(tmpstr);
            break;
        }
    }

    if (!rmdir_recursive(op)) {
        ui_log(op, "Unable to delete directories created by previous "
               "installation.");
    }

    ui_status_end(op, "done.");

    /* remove the backup directory */

    if (!remove_directory(op, BACKUP_DIRECTORY)) {
        /* XXX what to do if this fails?... nothing */
    }

    if (!op->skip_module_unload) {
        /*
         * attempt to unload the kernel module, but don't abort if this fails:
         * the kernel may not have been configured with support for module
         * unloading (Linux 2.6) or the user might have unloaded it themselves.
         */
        cmd = nvstrcat(op->utils[RMMOD], " ", RMMOD_MODULE_NAME, NULL);
        run_command(op, cmd, NULL, FALSE, 0, TRUE);
        nvfree(cmd);
    }

    run_distro_hook(op, "post-uninstall");

    free_backup_info(b);

    return TRUE;
    
} /* do_uninstall() */





/*
 * read_backup_log_file() - 
 */

static BackupInfo *read_backup_log_file(Options *op)
{
    struct stat stat_buf;
    char *buf, *c, *line, *filename;
    int fd, num, length, line_num = 0;
    float percent;
    
    BackupLogEntry *e;
    BackupInfo *b = NULL;

    /* check the permissions of the backup directory */

    if (stat(BACKUP_DIRECTORY, &stat_buf) == -1) {
        ui_error(op, "Unable to get properties of %s (%s).",
                 BACKUP_DIRECTORY, strerror(errno));
        return NULL;
    }
    
    if ((stat_buf.st_mode & PERM_MASK) != BACKUP_DIRECTORY_PERMS) {
        ui_error(op, "The directory permissions of %s have been changed since"
                 "the directory was created!", BACKUP_DIRECTORY);
        return NULL;
    }

    if ((fd = open(BACKUP_LOG, O_RDONLY)) == -1) {
        ui_error(op, "Failure opening %s (%s).", BACKUP_LOG, strerror(errno));
        return NULL;
    }

    if (fstat(fd, &stat_buf) == -1) {
        ui_error(op, "Failure getting file properties for %s (%s).",
                 BACKUP_LOG, strerror(errno));
        return NULL;
    }

    if ((stat_buf.st_mode & PERM_MASK) != BACKUP_LOG_PERMS) {
        ui_error(op, "The file permissions of %s have been changed since "
                 "the file was written!", BACKUP_LOG);
        return NULL;
    }

    /* map the file */

    length = stat_buf.st_size;

    buf = mmap(0, length, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
    if (!buf) {
        ui_error(op, "Unable to mmap file '%s' (%s).", strerror(errno));
        return NULL;
    }
    
    ui_status_begin(op, "Parsing log file:", "Parsing");

    b = nvalloc(sizeof(BackupInfo));

    b->version = get_next_line(buf, &c, buf, length);
    if (!b->version || !c) goto parse_error;
    
    percent = (float) (c - buf) / (float) stat_buf.st_size;
    ui_status_update(op, percent, NULL);

    b->description = get_next_line(c, &c, buf, length);
    if (!b->description || !c) goto parse_error;

    b->n = 0;
    b->e = NULL;
    line_num = 3;

    while(1) {

        percent = (float) (c - buf) / (float) stat_buf.st_size;
        ui_status_update(op, percent, NULL);
        
        /* read and parse the next line */

        line = get_next_line(c, &c, buf, length);
        if (!line) break;

        if (!parse_first_line(line, &num, &filename)) goto parse_error;
        line_num++;
        free(line);

        /* grow the BackupLogEntry array */

        b->n++;
        b->e = (BackupLogEntry *)
            nvrealloc(b->e, sizeof(BackupLogEntry) * b->n);

        memset(&b->e[b->n - 1], 0, sizeof(BackupLogEntry));

        e = &b->e[b->n - 1];
        e->num = num;
        e->filename = filename;
        e->ok = TRUE;
        
        switch(e->num) {

        case INSTALLED_FILE:
            line = get_next_line(c, &c, buf, length);
            if (line == NULL) goto parse_error;
            line_num++;

            if (!parse_crc(line, &e->crc)) goto parse_error;
            free(line);
        
            break;

        case INSTALLED_SYMLINK:
            line = get_next_line(c, &c, buf, length);
            if (line == NULL) goto parse_error;
            line_num++;
            
            e->target = line;
            
            break;
            
        case BACKED_UP_SYMLINK:
            line = get_next_line(c, &c, buf, length);
            if (line == NULL) goto parse_error;
            line_num++;
            
            e->target = line;

            line = get_next_line(c, &c, buf, length);
            if (line == NULL) goto parse_error;
            line_num++;

            if (!parse_mode_uid_gid(line, &e->mode, &e->uid, &e->gid))
                goto parse_error;
            free(line);
          
            break;
            
        default:
            if (num < BACKED_UP_FILE_NUM) goto parse_error;
            
            line = get_next_line(c, &c, buf, length);
            if (line == NULL) goto parse_error;
            line_num++;

            if (!parse_crc_mode_uid_gid(line, &e->crc, &e->mode,
                                        &e->uid, &e->gid)) goto parse_error;
            free(line);

            break;
        }
        
        if (!c) break;
    }

    ui_status_end(op, "done.");
    
    munmap(buf, stat_buf.st_size);
    close(fd);
    
    return b;

 parse_error:
    
    ui_status_end(op, "error.");

    munmap(buf, stat_buf.st_size);
    close(fd);

    ui_error(op, "Error while parsing line %d of '%s'.", line_num, BACKUP_LOG);

    if (b) free(b);
    return NULL;

} /* read_backup_log_file() */



/*
 * free_backup_info() - free the data associated with BackupInfo
 */

static void free_backup_info(BackupInfo *b)
{
    int i;

    if (!b) return;
    
    nvfree(b->version);
    nvfree(b->description);
    
    for (i = 0; i < b->n; i++) {
        nvfree(b->e[i].filename);
        nvfree(b->e[i].target);
    }

    nvfree((char *) b->e);
    
    nvfree((char *) b);

} /* free_backup_info() */



/*
 * check_backup_log_entries() - for each backup log entry, perform
 * some basic sanity checks.  Set the 'ok' field to FALSE if a
 * particular entry should not be uninstalled/restored.
 */

static int check_backup_log_entries(Options *op, BackupInfo *b)
{
    BackupLogEntry *e;
    uint32 crc;
    char *tmpstr;
    int i, j, len, ret = TRUE;
    float percent;

    ui_status_begin(op, "Validating previous installation:", "Validating");
    
    for (i = 0; i < b->n; i++) {

        percent = (float) i / (float) (b->n);

        e = &b->e[i];

        switch (e->num) {

        case INSTALLED_FILE:
            
            /* check if the file is still there, and has the same crc */
            
            if (access(e->filename, F_OK) == -1) {
                ui_log(op, "Unable to access previously installed file "
                       "'%s' (%s).", e->filename, strerror(errno));
                ret = e->ok = FALSE;
            } else {
                crc = compute_crc(op, e->filename);
                
                if (crc != e->crc) {
                    ui_log(op, "The previously installed file '%s' has a "
                           "different checksum (%lu) than "
                           "when it was installed (%lu).  %s will not be "
                           "uninstalled.",
                           e->filename, crc, e->crc, e->filename);
                    ret = e->ok = FALSE;
                }
            }
            ui_status_update(op, percent, e->filename);

            break;

        case INSTALLED_SYMLINK:
            
            /*
             * check if the symlink is still there, and has the same
             * target
             */
            
            if (access(e->filename, F_OK) == -1) {
                ui_log(op, "Unable to access previously installed "
                       "symlink '%s' (%s).", e->filename, strerror(errno));
                ret = e->ok = FALSE;
            } else {
                tmpstr = get_symlink_target(op, e->filename);
                if (!tmpstr) {
                    ret = e->ok = FALSE;
                } else {
                    if (strcmp(tmpstr, e->target) != 0) {
                        ui_log(op, "The previously installed symlink '%s' "
                               "has target '%s', but it was installed "
                               "with target '%s'.  %s will not be "
                               "uninstalled.",
                               e->filename, tmpstr, e->target, e->filename);
                        ret = e->ok = FALSE;
                        
                        /*
                         * if an installed symbolic link has a
                         * different target, then we don't remove it.
                         * This also means that we shouldn't attempt
                         * to restore a backed up symbolic link of the
                         * same name, or whose target matches this
                         * target.
                         */

                        for (j = 0; j < b->n; j++) {
                            if ((b->e[j].num == BACKED_UP_SYMLINK) &&
                                (strcmp(b->e[j].filename, e->filename) == 0)) {
                                b->e[j].ok = FALSE;
                            }
                        }
                    }
                    free(tmpstr);
                }
            }
            ui_status_update(op, percent, e->filename);

            break;

        case BACKED_UP_SYMLINK:
            
            /* nothing to do */
            
            break;

        default:
            
            /*
             * this is a backed up file; check that the file is still
             * present and has the same crc
             */

            len = strlen(BACKUP_DIRECTORY) + 64;
            tmpstr = nvalloc(len + 1);
            snprintf(tmpstr, len, "%s/%d", BACKUP_DIRECTORY, e->num);
            if (access(tmpstr, F_OK) == -1) {
                ui_log(op, "Unable to access backed up file '%s' "
                       "(saved as '%s') (%s).",
                       e->filename, tmpstr, strerror(errno));
                ret = e->ok = FALSE;
            } else {
                crc = compute_crc(op, tmpstr);
                
                if (crc != e->crc) {
                    ui_log(op, "Backed up file '%s' (saved as '%s) has "
                           "different checksum (%lu) than"
                           "when it was backed up (%lu).  %s will not be "
                           "restored.", e->filename, tmpstr,
                           crc, e->crc, e->filename);
                    ret = e->ok = FALSE;
                }
            }
            ui_status_update(op, percent, tmpstr);
            free(tmpstr);
            break;
        }
    }

    ui_status_end(op, "done.");

    return (ret);

} /* check_backup_log_entries() */



/*
 * get_installed_driver_version_and_descr() - determine the currently
 * installed driver version and description.  Returns the description
 * string if a previous driver is installed.  Returns NULL if no
 * driver is currently installed.
 *
 * XXX for now, we'll just get the installed driver version by reading
 * BACKUP_LOG.  This is probably insufficient, though; how do we
 * detect a driver that was installed prior to the new installer?
 * Should we look for the installed files on the system and pull nvid
 * from them?
 *
 * XXX we should probably check the file permissions of BACKUP_LOG.
 */

int get_installed_driver_version_and_descr(Options *op,
                                           char **pVersion, char **pDescr)
{
    struct stat stat_buf;
    char *c, *version = NULL, *descr = NULL, *buf = NULL;
    char *version_line = NULL;
    int length, fd = -1;
    int ret = FALSE;

    if ((fd = open(BACKUP_LOG, O_RDONLY)) == -1) goto done;
    
    if (fstat(fd, &stat_buf) == -1) goto done;
    
    /* map the file */

    length = stat_buf.st_size;
    
    buf = mmap(0, length, PROT_READ, MAP_FILE | MAP_SHARED, fd, 0);
    if (!buf) goto done;
    
    version_line = get_next_line(buf, &c, buf, length);
    if (!version_line) goto done;

    version = extract_version_string(version_line);

    if (!version) goto done;
    
    descr = get_next_line(c, NULL, buf, length);

    if (!descr) goto done;

    *pVersion = strdup(version);
    *pDescr = strdup(descr);
    
    ret = TRUE;
    
 done:
    nvfree(version_line);
    nvfree(version);
    nvfree(descr);

    if (buf) munmap(buf, stat_buf.st_size);
    if (fd != -1) close(fd);
    
    return ret;

} /* get_installed_driver_version_and_descr() */



/*
 * check_for_existing_driver() - Get the existing driver description
 * and version from BACKUP_LOG.  If an existing driver is present, ask
 * the user if they really want it to be uninstalled.
 *
 * Returns TRUE if it is OK to continue with the installation process.
 * Returns FALSE if the user decided they didn't want to continue with
 * installation.
 *
 * If we are only installing a kernel module, then there must be an
 * existing driver installation, and the version of that installation
 * must match the module we're trying to install.
 */

int check_for_existing_driver(Options *op, Package *p)
{
    char *descr = NULL;
    char *version = NULL;
    int ret = FALSE;
    int localRet;

    if (!check_for_existing_rpms(op)) goto done;

    localRet = get_installed_driver_version_and_descr(op, &version, &descr);

    if (op->kernel_module_only) {
        if (!localRet) {
            ui_error(op, "No NVIDIA driver is currently installed; the "
                     "'--kernel-module-only' option can only be used "
                     "to install the NVIDIA kernel module on top of an "
                     "existing driver installation.");
            goto done;
        } else {
            if (strcmp(p->version, version) != 0) {
                ui_error(op, "The '--kernel-module-only' option can only be "
                         "used to install a kernel module on top of an "
                         "existing driver installation of the same driver "
                         "version.  The existing driver installation is "
                         "%s, but the kernel module is %s.\n",
                         version, p->version);
                goto done;
            } else {
                ret = TRUE;
                goto done;
            }
        }
    }
    
    /* no existing driver -- it is fine to continue with installation */

    if (!localRet) {
        ret = TRUE;
        goto done;
    }

    /*
     * XXX we could do a comparison, here, to check that the Package
     * version is greater than or equal to the installed version, and
     * issue a warning if the user is downgrading.  That doesn't seem
     * necessary, though; I can't think of a good reason why
     * downgrading is any different than upgrading.
     */
    
    if (!ui_yes_no(op, TRUE, "There appears to already be a driver installed "
                   "on your system (version: %s).  As part of "
                   "installing this driver (version: %s), the existing "
                   "driver will be uninstalled.  Are you sure you want to "
                   "continue? ('no' will abort installation)",
                   version, p->version)) {
        
        ui_log(op, "Installation aborted.");
        goto done;
    }
    
    ret = TRUE;

 done:

    nvfree(descr);
    nvfree(version);
    
    return ret;

} /* check_for_existing_driver() */



/*
 * uninstall_existing_driver() - check if there is a driver already
 * installed, and if there is, uninstall it.
 *
 * Currently, nothing about this function should cause installation to
 * stop (so it always returns TRUE).
 */

int uninstall_existing_driver(Options *op, const int interactive)
{
    int ret;
    char *descr = NULL;
    char *version = NULL;
    
    ret = get_installed_driver_version_and_descr(op, &version, &descr);
    if (!ret) {
        if (interactive) {
            ui_message(op, "There is no NVIDIA driver currently installed.");
        }
        return TRUE;
    }

    if (interactive && op->uninstall) {
        ret = ui_yes_no(op, FALSE,
                       "If you plan to no longer use the NVIDIA driver, you "
                       "should make sure that no X screens are configured to "
                       "use the NVIDIA X driver in your X configuration file. "
                       "If you used nvidia-xconfig to configure X, it may have "
                       "created a backup of your original configuration. Would "
                       "you like to run `nvidia-xconfig --restore-original-"
                       "backup` to attempt restoration of the original X "
                       "configuration file?");
        if (ret) {
            run_nvidia_xconfig(op, TRUE);
        }
    }

    ret = do_uninstall(op, version);

    if (ret) {
        if (interactive) {
            ui_message(op, "Uninstallation of existing driver: %s (%s) "
                       "is complete.", descr, version);
        } else {
            ui_log(op, "Uninstallation of existing driver: %s (%s) "
                   "is complete.", descr, version);
        }
    } else {
        ui_error(op, "Uninstallation failed.");
    }
    
    nvfree(descr);
    nvfree(version);

    return TRUE;

} /* uninstall_existing_driver() */



/*
 * run_existing_uninstaller() - attempt to run `nvidia-uninstall` if it
 * exists; if it does not exist or fails, fall back to normal uninstallation.
 */
int run_existing_uninstaller(Options *op)
{
    char *uninstaller = find_system_util("nvidia-uninstall");

    if (uninstaller) {
        /* Run the uninstaller non-interactively, and explicitly log to the
         * uninstall log location: older installers may not do so implicitly. */
        char *uninstall_cmd = nvstrcat(uninstaller, " -s --log-file-name="
                                       DEFAULT_UNINSTALL_LOG_FILE_NAME, NULL);
        char *data = NULL;
        int ret;

        ui_log(op, "Uninstalling the previous installation with %s.",
               uninstaller);

        if (!op->no_kernel_module && !op->kernel_name) {
            /*
             * Attempt to run the uninstaller with the --skip-module-unload
             * option first.  If that fails, fall back to running it without
             * that option.
             *
             * We don't want the uninstaller to unload the module because this
             * instance of the installer already unloaded the old module and
             * loaded the new one.
             */
            char *uninstall_skip_unload_cmd =
                nvstrcat(uninstall_cmd, " --skip-module-unload", NULL);
            ret = run_command(op, uninstall_skip_unload_cmd, NULL, FALSE, 0, TRUE);
            nvfree(uninstall_skip_unload_cmd);
        } else {
            /*
             * If installing the kernel module was skipped or we're
             * building/installing for a different kernel, then the new kernel
             * module wasn't automatically loaded and we should unload whichever
             * one is loaded now.
             */
            ret = 1;
        }

        if (ret) {
            /* Try again without --skip-module-unload */
            ret = run_command(op, uninstall_cmd, &data, FALSE, 0, TRUE);
        }

        nvfree(uninstall_cmd);

        /* if nvidia-uninstall succeeded, return early; otherwise, fall back to
         * uninstalling via the backup log file. */
        if (ret == 0) {
            nvfree(data);
            return TRUE;
        } else {
            ui_log(op, "%s failed; see %s for more details.", uninstaller,
                   DEFAULT_UNINSTALL_LOG_FILE_NAME);
            if (data && strlen(data)) {
                ui_log(op, "The output from %s was:\n%s", uninstaller, data);
            }
            nvfree(data);
        }

        nvfree(uninstaller);
    }

    return uninstall_existing_driver(op, FALSE);
}



/*
 * report_driver_information() - report basic information about the
 * currently installed driver.
 */

int report_driver_information(Options *op)
{
    char *descr, *version;
    int ret;
    
    ret = get_installed_driver_version_and_descr(op, &version, &descr);
    if (!ret) {
        ui_message(op, "There is no NVIDIA driver currently installed.");
        return FALSE;
    }

    ui_message(op, "The currently installed driver is: '%s' "
               "(version: %s).", descr, version);
    
    nvfree(descr);
    nvfree(version);

    return TRUE;
    
} /* report_driver_information() */



/*
 * test_installed_files() - 
 */

int test_installed_files(Options *op)
{
    BackupInfo *b;
    int ret;
    
    b = read_backup_log_file(op);
    if (!b) return FALSE;
    
    ret = sanity_check_backup_log_entries(op, b);
    
    /* free resources associated with b */
    
    free_backup_info(b);

    return ret;
    
} /* test_installed_files() */



/*
 * find_installed_file() - scan the backup log file for the specified
 * filename; return TRUE if the filename is listed as an installed file.
 */

int find_installed_file(Options *op, char *filename)
{
    BackupInfo *b;
    BackupLogEntry *e;
    int i, ret = FALSE;
    
    if ((b = read_backup_log_file(op)) == NULL) return FALSE;

    for (i = 0; i < b->n; i++) {
        e = &b->e[i];
        if ((e->num == INSTALLED_FILE) && strcmp(filename, e->filename) == 0) {
            
            /* XXX should maybe compare inodes rather than
               filenames? */

            ret =  TRUE;
            break;
        }
    }

    free_backup_info(b);

    return ret;

} /* find_installed_file() */



/*
 * sanity_check_backup_log_entries() - this function is very similar
 * to check_backup_log_entries(); however, it varies in it's error
 * messages because it is used as part of the sanity check path.
 */

static int sanity_check_backup_log_entries(Options *op, BackupInfo *b)
{
    BackupLogEntry *e;
    uint32 crc;
    char *tmpstr;
    int i, len, ret = TRUE;
    float percent;
    
    ui_status_begin(op, "Validating installation:", "Validating");

    for (i = 0; i < b->n; i++) {
        
        e = &b->e[i];
        
        switch (e->num) {

        case INSTALLED_FILE:
            
            /* check if the file is still there, and has the same crc */
            
            if (access(e->filename, F_OK) == -1) {
                ui_error(op, "The installed file '%s' no longer exists.",
                         e->filename);
                ret = FALSE;
            } else {
                crc = compute_crc(op, e->filename);
                
                if (crc != e->crc) {
                    ui_error(op, "The installed file '%s' has a different "
                             "checksum (%lu) than when it was installed "
                             "(%lu).", e->filename, crc, e->crc);
                    ret = FALSE;
                }
            }
            break;

        case INSTALLED_SYMLINK:
            
            /*
             * check if the symlink is still there, and has the same
             * target
             */
            
            if (access(e->filename, F_OK) == -1) {
                ui_error(op, "The installed symbolic link '%s' no "
                         "longer exists.", e->filename);
                ret = FALSE;
            } else {
                tmpstr = get_symlink_target(op, e->filename);
                if (!tmpstr) {
                    ret = FALSE;
                } else {
                    if (strcmp(tmpstr, e->target) != 0) {
                        ui_error(op, "The installed symbolic link '%s' "
                                 "has target '%s', but it was installed "
                                 "with target '%s'.",
                                 e->filename, tmpstr, e->target);
                        ret = FALSE;
                    }
                    free(tmpstr);
                }
            }
            break;

        case BACKED_UP_SYMLINK:
            
            /* nothing to do */
            
            break;
       
        default:
            
            /*
             * this is a backed up file; check that the file is still
             * present and has the same crc
             */
            
            len = strlen(BACKUP_DIRECTORY) + 64;
            tmpstr = nvalloc(len + 1);
            snprintf(tmpstr, len, "%s/%d", BACKUP_DIRECTORY, e->num);
            if (access(tmpstr, F_OK) == -1) {
                ui_error(op, "The backed up file '%s' (saved as '%s') "
                         "no longer exists.", e->filename, tmpstr);
                ret = FALSE;
            } else {
                crc = compute_crc(op, tmpstr);
                
                if (crc != e->crc) {
                    ui_error(op, "Backed up file '%s' (saved as '%s) has a "
                             "different checksum (%lu) than"
                             "when it was backed up (%lu).",
                             e->filename, tmpstr, crc, e->crc);
                    ret = FALSE;
                }
            }
            free(tmpstr);
            break;
        }
        
        percent = (float) i / (float) (b->n);
        ui_status_update(op, percent, e->filename);
    }

    ui_status_end(op, "done.");
    
    return ret;
    
} /* sanity_check_backup_log_entries */



/*
 * create_backwards_compatible_version_string() - given the version
 * string 'str', generate a version string to write to the backup log
 * file that can be read by older nvidia-installers that assumed the
 * X.Y-ZZZZ version format.
 *
 * Fortunately, old nvidia-installers' version parsing didn't care if
 * there were extra digits beyond the four Z's (e.g., it could be
 * X.Y-ZZZZZ, or X.Y-ZZZZZZZZ); they would just look for the first 4
 * Z's if they needed to parse the version.
 *
 * So, the strategy is to take the new version string, remove any
 * periods, and print that out as the old style version, followed by
 * the real version string in parenthesis; e.g.,
 *
 *  "1.0-105917 (105.9.17)"
 *
 * In this way, an old nvidia-installer will atleast be able to parse
 * the string, even though it may not understand it, but a new
 * nvidia-installer can be smart and pull out the new version string.
 */

static char *create_backwards_compatible_version_string(const char *str)
{
    char *version, *scratch, *s, *t;
    int len;

    /*
     * create a scratch string by duping the passed in str, but
     * collapse the scratch string by only retaining the digits; e.g.,
     * "105.9.17" --> "105917"
     */
    
    scratch = nvstrdup(str);

    for (s = t = scratch; *t; t++) {
        if (isdigit(*t)) {
            *s++ = *t;
        }
    }
    
    *s = '\0';

    /* allocate a new string to contain the result */

    len = strlen(str);
    version = nvalloc((len * 2) + 16);
    
    sprintf(version, "1.0-%s (%s)", scratch, str);
    
    nvfree(scratch);

    return version;
    
} /* create_backwards_compatible_version_string() */