summaryrefslogtreecommitdiff
path: root/files.c
blob: 02934e00a8cb2c93096ccaa31f858e9f8d5d8bb1 (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
/*
 * 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 of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * 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, write to the:
 *
 *      Free Software Foundation, Inc.
 *      59 Temple Place - Suite 330
 *      Boston, MA 02111-1307, USA
 *
 *
 * files.c - this source file contains routines for manipulating
 * files and directories for the nv-instaler.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <limits.h>
#include <libgen.h>
#include <utime.h>
#include <time.h>

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

/*
 * remove_directory() - recursively delete a direcotry (`rm -rf`)
 */

int remove_directory(Options *op, const char *victim)
{
    struct stat stat_buf;
    DIR *dir;
    struct dirent *ent;
    char *filename;
    int len;
    
    if (lstat(victim, &stat_buf) == -1) {
        ui_error(op, "failure to open '%s'", victim);
        return FALSE;
    }
    
    if (S_ISDIR(stat_buf.st_mode) == 0) {
        ui_error(op, "%s is not a directory", victim);
        return FALSE;
    }
    
    if ((dir = opendir(victim)) == NULL) {
        ui_error(op, "Failure reading directory %s", victim);
        return FALSE;
    }
    
    while ((ent = readdir(dir)) != NULL) {

        if (((strcmp(ent->d_name, ".")) == 0) ||
            ((strcmp(ent->d_name, "..")) == 0)) continue;
        
        len = strlen(victim) + strlen(ent->d_name) + 2;
        filename = (char *) nvalloc(len);
        snprintf(filename, len, "%s/%s", victim, ent->d_name);
        
        if (lstat(filename, &stat_buf) == -1) {
            ui_error(op, "failure to open '%s'", filename);
            free(filename);
            return FALSE;
        }
        
        if (S_ISDIR(stat_buf.st_mode)) {
            remove_directory(op, filename);
        } else {
            if (unlink(filename) != 0) {
                ui_error(op, "Failure removing file %s (%s)",
                         filename, strerror(errno));
            }
        }
        free(filename);
    }

    if (rmdir(victim) != 0) {
        ui_error(op, "Failure removing directory %s (%s)",
                 victim, strerror(errno));
        return FALSE;
    }

    return TRUE;
    
} /* remove_directory() */



/*
 * touch_directory() - recursively touch all files (and directories)
 * in the specified directory, bringing their access and modification
 * times up to date.
 */

int touch_directory(Options *op, const char *victim)
{
    struct stat stat_buf;
    DIR *dir;
    struct dirent *ent;
    struct utimbuf time_buf;
    char *filename;

    if (lstat(victim, &stat_buf) == -1) {
        ui_error(op, "failure to open '%s'", victim);
        return FALSE;
    }
    
    if (S_ISDIR(stat_buf.st_mode) == 0) {
        ui_error(op, "%s is not a directory", victim);
        return FALSE;
    }
    
    if ((dir = opendir(victim)) == NULL) {
        ui_error(op, "Failure reading directory %s", victim);
        return FALSE;
    }

    /* get the current time */

    time_buf.actime = time(NULL);
    time_buf.modtime = time_buf.actime;

    /* loop over each entry in the directory */

    while ((ent = readdir(dir)) != NULL) {
        
        if (((strcmp(ent->d_name, ".")) == 0) ||
            ((strcmp(ent->d_name, "..")) == 0)) continue;
        
        filename = nvstrcat(victim, "/", ent->d_name, NULL);
        
        /* stat the file to get the type */
        
        if (lstat(filename, &stat_buf) == -1) {
            ui_error(op, "failure to open '%s'", filename);
            nvfree(filename);
            return FALSE;
        }
        
        /* if it is a directory, call this recursively */

        if (S_ISDIR(stat_buf.st_mode)) {
            if (!touch_directory(op, filename)) {
                nvfree(filename);
                return FALSE;
            }
        }

        /* finally, set the access and modification times */
        
        if (utime(filename, &time_buf) != 0) {
            ui_error(op, "Error setting modification time for %s", filename);
            nvfree(filename);
            return FALSE;
        }

        nvfree(filename);
    }

    if (closedir(dir) != 0) {
        ui_error(op, "Error while closing directory %s.", victim);
        return FALSE;
    }
    
    return TRUE;

} /* touch_directory() */


/*
 * copy_file() - copy the file specified by srcfile to dstfile, using
 * mmap and memcpy.  The destination file is created with the
 * permissions specified by mode.  Roughly based on code presented by
 * Richard Stevens, in Advanced Programming in the Unix Environment,
 * 12.9.
 */

int copy_file(Options *op, const char *srcfile,
              const char *dstfile, mode_t mode)
{
    int src_fd, dst_fd;
    struct stat stat_buf;
    char *src, *dst;
    
    if ((src_fd = open(srcfile, O_RDONLY)) == -1) {
        ui_error (op, "Unable to open '%s' for copying (%s)",
                  srcfile, strerror (errno));
        goto fail;
    }
    if ((dst_fd = open(dstfile, O_RDWR | O_CREAT | O_TRUNC, mode)) == -1) {
        ui_error (op, "Unable to create '%s' for copying (%s)",
                  dstfile, strerror (errno));
        goto fail;
    }
    if (fstat(src_fd, &stat_buf) == -1) {
        ui_error (op, "Unable to determine size of '%s' (%s)",
                  srcfile, strerror (errno));
        goto fail;
    }
    if (lseek(dst_fd, stat_buf.st_size - 1, SEEK_SET) == -1) {
        ui_error (op, "Unable to set file size for '%s' (%s)",
                  dstfile, strerror (errno));
        goto fail;
    }
    if (write(dst_fd, "", 1) != 1) {
        ui_error (op, "Unable to write file size for '%s' (%s)",
                  dstfile, strerror (errno));
        goto fail;
    }
    if ((src = mmap(0, stat_buf.st_size, PROT_READ,
                    MAP_FILE | MAP_SHARED, src_fd, 0)) == (void *) -1) {
        ui_error (op, "Unable to map source file '%s' for copying (%s)",
                  srcfile, strerror (errno));
        goto fail;
    }
    if ((dst = mmap(0, stat_buf.st_size, PROT_READ | PROT_WRITE,
                    MAP_FILE | MAP_SHARED, dst_fd, 0)) == (void *) -1) {
        ui_error (op, "Unable to map destination file '%s' for copying (%s)",
                  dstfile, strerror (errno));
        goto fail;
    }
    
    memcpy (dst, src, stat_buf.st_size);
    
    if (munmap (src, stat_buf.st_size) == -1) {
        ui_error (op, "Unable to unmap source file '%s' after copying (%s)",
                 srcfile, strerror (errno));
        goto fail;
    }
    if (munmap (dst, stat_buf.st_size) == -1) {
        ui_error (op, "Unable to unmap destination file '%s' after "
                 "copying (%s)", dstfile, strerror (errno));
        goto fail;
    }

    /*
     * the mode used to create dst_fd may have been affected by the
     * user's umask; so explicitly set the mode again
     */

    fchmod(dst_fd, mode);

    close (src_fd);
    close (dst_fd);
    
    return TRUE;

 fail:
    return FALSE;

} /* copy_file() */



/*
 * write_temp_file() - write the given data to a temporary file,
 * setting the file's permissions to those specified in perm.  On
 * success the name of the temporary file is returned; on error NULL
 * is returned.
 */

char *write_temp_file(Options *op, const int len,
                      const unsigned char *data, mode_t perm)
{
    unsigned char *dst = (void *) -1;
    char *tmpfile = NULL;
    int fd = -1;
    int ret = FALSE;

    /* create a temporary file */

    tmpfile = nvstrcat(op->tmpdir, "/nv-tmp-XXXXXX", NULL);
    
    fd = mkstemp(tmpfile);
    if (fd == -1) {
        ui_warn(op, "Unable to create temporary file (%s).",
                strerror(errno));
        goto done;
    }

    /* set the temporary file's size */

    if (lseek(fd, len - 1, SEEK_SET) == -1) {
        ui_warn(op, "Unable to set file size for temporary file (%s).",
                strerror(errno));
        goto done;
    }
    if (write(fd, "", 1) != 1) {
        ui_warn(op, "Unable to write file size for temporary file (%s).",
                strerror(errno));
        goto done;
    }
    
    /* mmap the temporary file */

    if ((dst = mmap(0, len, PROT_READ | PROT_WRITE,
                    MAP_FILE | MAP_SHARED, fd, 0)) == (void *) -1) {
        ui_warn(op, "Unable to map temporary file (%s).", strerror(errno));
        goto done;
    }
    
    /* copy the data out to the file */
    
    memcpy(dst, data, len);

    /* set the desired permissions on the file */
    
    if (fchmod(fd, perm) == -1) {
        ui_warn(op, "Unable to set permissions %04o on temporary "
                "file (%s)", perm, strerror(errno));
        goto done;
    }
    
    ret = TRUE;

 done:
    
    /* unmap the temporary file */
    
    if (dst != (void *) -1) {
        if (munmap(dst, len) == -1) {
            ui_warn(op, "Unable to unmap temporary file (%s).",
                    strerror(errno));
        }
    }
    
    /* close the temporary file */

    if (fd != -1) close(fd);
    
    if (ret) {
        return tmpfile;
    } else {
        if (tmpfile) nvfree(tmpfile);
        return NULL;
    }
    
} /* write_temp_file() */



/*
 * select_tls_class() - determine which tls class should be installed
 * on the user's machine; if tls_test() fails, just install the
 * classic tls libraries.  If tls_test() passes, install both OpenGL
 * sets, but only the new tls libglx.
 */

void select_tls_class(Options *op, Package *p)
{
    int i;

    if (!tls_test(op, FALSE)) {
        
        /*
         * tls libraries will not run on this system; just install the
         * classic OpenGL libraries: clear the FILE_TYPE of any
         * FILE_CLASS_NEW_TLS package entries.
         */

        ui_log(op, "Installing classic TLS OpenGL libraries.");
        
        for (i = 0; i < p->num_entries; i++) {
            if (p->entries[i].flags & FILE_CLASS_NEW_TLS) {
                p->entries[i].flags &= ~FILE_TYPE_MASK;
                p->entries[i].dst = NULL;
            }
        }
    } else {

        /*
         * tls libraries will run on this system: install both the
         * classic and new TLS libraries.
         */

        ui_log(op, "Installing both new and classic TLS OpenGL libraries.");
    }

#if defined(NV_X86_64)

    /*
     * If we are installing on amd64, then we need to perform a
     * similar test for the 32bit compatibility libraries
     */

    if (!tls_test(op, TRUE)) {
        
        /*
         * 32bit tls libraries will not run on this system; just
         * install the classic OpenGL libraries: clear the FILE_TYPE
         * of any FILE_CLASS_NEW_TLS_32 package entries.
         */

        ui_log(op, "Installing classic TLS 32bit OpenGL libraries.");
        
        for (i = 0; i < p->num_entries; i++) {
            if (p->entries[i].flags & FILE_CLASS_NEW_TLS_32) {
                p->entries[i].flags &= ~FILE_TYPE_MASK;
                p->entries[i].dst = NULL;
            }
        }
    } else {
        
        /*
         * 32bit tls libraries will run on this system: install both
         * the classic and new TLS libraries.
         */

        ui_log(op, "Installing both new and classic TLS 32bit "
               "OpenGL libraries.");
    }

#endif /* NV_X86_64 */

} /* select_tls_class() */


/*
 * set_destinations() - given the Options and Package structures,
 * assign the destination field in each Package entry, building from
 * the OpenGL and XFree86 prefixes, the path relative to the prefix,
 * and the filename.  This assumes that the prefixes have already been
 * assigned in the Options struct.
 */

int set_destinations(Options *op, Package *p)
{
    char *prefix, *path, *name;
    int i;
    
    for (i = 0; i < p->num_entries; i++) {

        switch (p->entries[i].flags & FILE_TYPE_MASK) {
            
        case FILE_TYPE_KERNEL_MODULE_CMD:
        case FILE_TYPE_KERNEL_MODULE_SRC:
            
            /* we don't install kernel module sources */
            
            p->entries[i].dst = NULL;
            continue;
            
        case FILE_TYPE_OPENGL_LIB:
        case FILE_TYPE_OPENGL_SYMLINK:
            prefix = op->opengl_prefix;
            path = p->entries[i].path;
            break;
            
        case FILE_TYPE_XFREE86_LIB:
        case FILE_TYPE_XFREE86_SYMLINK:
            prefix = op->xfree86_prefix;
            path = p->entries[i].path;
            break;

            /*
             * XXX should the OpenGL headers and documentation also go
             * under the OpenGL installation prefix?  The Linux OpenGL
             * ABI requires that the header files be installed in
             * /usr/include/GL/.
             */

        case FILE_TYPE_OPENGL_HEADER:
            prefix = op->opengl_prefix;
            path = OPENGL_HEADER_DST_PATH;
            break;
            
        case FILE_TYPE_DOCUMENTATION:
            prefix = op->opengl_prefix;
            path = p->entries[i].path;
            break;
        
        case FILE_TYPE_INSTALLER_BINARY:
            prefix = op->installer_prefix;
            path = INSTALLER_BINARY_DST_PATH;
            break;
            
        case FILE_TYPE_UTILITY_BINARY:
            prefix = op->utility_prefix;
            path = UTILITY_BINARY_DST_PATH;
            break;

        case FILE_TYPE_KERNEL_MODULE:
            
            /*
             * the kernel module dst field has already been
             * initialized in add_kernel_module_to_package()
             */
            
            continue;

        default:
            
            /* 
             * silently ignore anything that doesn't match; libraries
             * of the wrong TLS class may fall in here, for example.
             */
            
            p->entries[i].dst = NULL;
            continue;
        }
        
        name = p->entries[i].name;

        p->entries[i].dst = nvstrcat(prefix, "/", path, "/", name, NULL);
    }
    
    return TRUE;

} /* set_destinations() */



/*
 * get_license_acceptance() - stat the license file to find out its
 * length, open the file, mmap it, and pass it to the ui for
 * acceptance.
 */

int get_license_acceptance(Options *op)
{
    struct stat buf;
    char *text;
    int fd;

    /* trivial accept if the user accepted on the command line */

    if (op->accept_license) {
        ui_log(op, "License accepted by command line option.");
        return TRUE;
    }

    if ((fd = open(LICENSE_FILE, 0x0)) == -1) goto failed;
   
    if (fstat(fd, &buf) != 0) goto failed;
    
    if ((text = (char *) mmap(NULL, buf.st_size, PROT_READ,
                              MAP_FILE|MAP_SHARED,
                              fd, 0x0)) == (char *) -1) goto failed;
    
    if (!ui_display_license(op, text)) {
        ui_message(op, "License not accepted.  Aborting installation.");
        munmap(text, buf.st_size);
        close(fd);
        return FALSE;
    }
    
    ui_log(op, "License accepted.");
    
    munmap(text, buf.st_size);
    close(fd);
    
    return TRUE;

 failed:
    
    ui_error(op, "Unable to open License file '%s' (%s)",
             LICENSE_FILE, strerror(errno));
    return FALSE;

} /* get_license_acceptance() */



/*
 * get_prefixes() - if in expert mode, ask the user for the OpenGL and
 * XFree86 installation prefix.  The default prefixes are already set
 * in parse_commandline().
 */

int get_prefixes (Options *op)
{
    char *ret;
 
    if (op->expert) {
        ret = ui_get_input(op, op->xfree86_prefix,
                           "X installation prefix (only under "
                           "rare circumstances should this be changed "
                           "from the default)");
        if (ret && ret[0]) {
            op->xfree86_prefix = ret; 
            if (!confirm_path(op, op->xfree86_prefix)) return FALSE;
        }
    }

    remove_trailing_slashes(op->xfree86_prefix);
    ui_expert(op, "X installation prefix is: '%s'", op->xfree86_prefix);
        
    if (op->expert) {
        ret = ui_get_input(op, op->opengl_prefix,
                           "OpenGL installation prefix (only under "
                           "rare circumstances should this be changed "
                           "from the default)");
        if (ret && ret[0]) {
            op->opengl_prefix = ret;
            if (!confirm_path(op, op->opengl_prefix)) return FALSE;
        }
    }

    remove_trailing_slashes(op->opengl_prefix);
    ui_expert(op, "OpenGL installation prefix is: '%s'", op->opengl_prefix);

    if (op->expert) {
        ret = ui_get_input(op, op->installer_prefix,
                           "Installer installation prefix");
        if (ret && ret[0]) {
            op->installer_prefix = ret;
            if (!confirm_path(op, op->installer_prefix)) return FALSE;
        }
    }
    
    remove_trailing_slashes(op->installer_prefix);
    ui_expert(op, "Installer installation prefix is: '%s'",
              op->installer_prefix);
    
    return TRUE;
    
} /* get_prefixes() */



/*
 * add_kernel_module_to_package() - append the kernel module
 * (contained in p->kernel_module_build_directory) to the package list
 * for installation.
 */

int add_kernel_module_to_package(Options *op, Package *p)
{
    int n, len;

    n = p->num_entries;
    
    p->entries =
        (PackageEntry *) nvrealloc(p->entries, (n + 1) * sizeof(PackageEntry));

    len = strlen(p->kernel_module_build_directory) +
        strlen(p->kernel_module_filename) + 2;
    p->entries[n].file = (char *) nvalloc(len);
    snprintf(p->entries[n].file, len, "%s/%s",
             p->kernel_module_build_directory, p->kernel_module_filename);
    
    p->entries[n].path = NULL;
    p->entries[n].target = NULL;
    p->entries[n].flags = FILE_TYPE_KERNEL_MODULE;
    p->entries[n].mode = 0644;
    
    p->entries[n].name = strrchr(p->entries[n].file, '/');
    if (p->entries[n].name) p->entries[n].name++;
    if (!p->entries[n].name) p->entries[n].name = p->entries[n].file;
    
    len = strlen(op->kernel_module_installation_path) +
        strlen(p->kernel_module_filename) + 2;
    p->entries[n].dst = (char *) nvalloc(len);
    snprintf (p->entries[n].dst, len, "%s/%s",
              op->kernel_module_installation_path, p->kernel_module_filename);
    
    p->num_entries++;

    return TRUE;

} /* add_kernel_module_to_package() */



/*
 * remove_non_kernel_module_files_from_package() - clear the
 * FILE_TYPE_MASK bits for each package entry that is not of type
 * FILE_TYPE_KERNEL_MODULE
 */

void remove_non_kernel_module_files_from_package(Options *op, Package *p)
{
    int i;
    unsigned int flags;

    for (i = 0; i < p->num_entries; i++) {
        flags = p->entries[i].flags & FILE_TYPE_MASK;
        if ((flags != FILE_TYPE_KERNEL_MODULE) &&
            (flags != FILE_TYPE_KERNEL_MODULE_CMD))
            p->entries[i].flags &= ~FILE_TYPE_MASK;
    }
    
} /* remove_non_kernel_module_files_from_package() */



/*
 * remove_trailing_slashes() - begin at the end of the given string,
 * and overwrite slashes with NULL as long as we find slashes.
 */

void remove_trailing_slashes(char *s)
{
    int len = strlen(s);

    while (s[len-1] == '/') s[--len] = '\0';
    
} /* remove_trailing_slashes() */



/*
 * mode_string_to_mode() - convert the string s 
 */

int mode_string_to_mode(Options *op, char *s, mode_t *mode)
{
    char *endptr;
    long int ret;
    
    ret = strtol(s, &endptr, 8);

    if ((ret == LONG_MIN) || (ret == LONG_MAX) || (*endptr != '\0')) {
        ui_error(op, "Error parsing permission string '%s' (%s)",
                 s, strerror (errno));
        return FALSE;
    }

    *mode = (mode_t) ret;

    return TRUE;
    
} /* mode_string_to_mode() */



/*
 * mode_to_permission_string() - given a mode bitmask, allocate and
 * write a permission string.
 */

char *mode_to_permission_string(mode_t mode)
{
    char *s = (char *) nvalloc(10);
    memset (s, '-', 9);
    
    if (mode & (1 << 8)) s[0] = 'r';
    if (mode & (1 << 7)) s[1] = 'w';
    if (mode & (1 << 6)) s[2] = 'x';
    
    if (mode & (1 << 5)) s[3] = 'r';
    if (mode & (1 << 4)) s[4] = 'w';
    if (mode & (1 << 3)) s[5] = 'x';
    
    if (mode & (1 << 2)) s[6] = 'r';
    if (mode & (1 << 1)) s[7] = 'w';
    if (mode & (1 << 0)) s[8] = 'x';
    
    s[9] = '\0';
    return s;

} /* mode_to_permission_string() */



/*
 * directory_exists() - 
 */

int directory_exists(Options *op, const char *dir)
{
    struct stat stat_buf;

    if ((stat (dir, &stat_buf) == -1) || (!S_ISDIR(stat_buf.st_mode))) {
        return FALSE;
    } else {
        return TRUE;
    }
} /* directory_exists() */



/*
 * confirm_path() - check that the path exists; if not, ask the user
 * if it's OK to create it and then do mkdir().
 *
 * XXX for a while, I had thought that it would be better for this
 * function to only ask the user if it was OK to create the directory;
 * there are just too many reasons why mkdir might fail, though, so
 * it's better to do mkdir() so that we can fail at this point in the
 * installation.
 */

int confirm_path(Options *op, const char *path)
{
    /* return TRUE if the path already exists and is a directory */

    if (directory_exists(op, path)) return TRUE;
    
    if (ui_yes_no(op, TRUE, "The directory '%s' does not exist; "
                  "create?", path)) {
        if (mkdir_recursive(op, path, 0755)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    ui_message(op, "Not creating directory '%s'; aborting installation.",
               path);
    
    return FALSE;

} /* confirm_path() */



/* 
 * mkdir_recursive() - create the path specified, also creating parent
 * directories as needed; this is equivalent to `mkdir -p`
 */

int mkdir_recursive(Options *op, const char *path, const mode_t mode)
{
    char *c, *tmp, ch;
    
    if (!path || !path[0]) return FALSE;
        
    tmp = nvstrdup(path);
    remove_trailing_slashes(tmp);
        
    c = tmp;
    do {
        c++;
        if ((*c == '/') || (*c == '\0')) {
            ch = *c;
            *c = '\0';
            if (!directory_exists(op, tmp)) {
                if (mkdir(tmp, mode) != 0) {
                    ui_error(op, "Failure creating directory '%s': (%s)",
                             tmp, strerror(errno));
                    free(tmp);
                    return FALSE;
                }
            }
            *c = ch;
        }
    } while (*c);

    free(tmp);
    return TRUE;

} /* mkdir_recursive() */



/*
 * get_symlink_target() - get the target of the symbolic link
 * 'filename'.  On success, a newly malloced string containing the
 * target is returned.  On error, an error message is printed and NULL
 * is returned.
 */

char *get_symlink_target(Options *op, const char *filename)
{
    struct stat stat_buf;
    int ret, len = 0;
    char *buf = NULL;

    if (lstat(filename, &stat_buf) == -1) {
        ui_error(op, "Unable to get file properties for '%s' (%s).",
                 filename, strerror(errno));
        return NULL;
    }
    
    if (!S_ISLNK(stat_buf.st_mode)) {
        ui_error(op, "File '%s' is not a symbolic link.", filename);
        return NULL;
    }
    
    /*
     * grow the buffer to be passed into readlink(2), until the buffer
     * is big enough to hold the whole target.
     */

    do {
        len += NV_LINE_LEN;
        if (buf) free(buf);
        buf = nvalloc(len);
        ret = readlink(filename, buf, len - 1);
        if (ret == -1) {
            ui_error(op, "Failure while reading target of symbolic "
                     "link %s (%s).", filename, strerror(errno));
            free(buf);
            return NULL;
        }
    } while (ret >= (len - 1));

    buf[ret] = '\0';
    
    return buf;

} /* get_symlink_target() */



/*
 * install_file() - install srcfile as dstfile; this is done by
 * extracting the directory portion of dstfile, and then calling
 * copy_file().
 */ 

int install_file(Options *op, const char *srcfile,
                 const char *dstfile, mode_t mode)
{   
    int retval; 
    char *dirc, *dname;

    dirc = nvstrdup(dstfile);
    dname = dirname(dirc);
    
    if (!mkdir_recursive(op, dname, 0755)) {
        free(dirc);
        return FALSE;
    }

    retval = copy_file(op, srcfile, dstfile, mode);
    free(dirc);

    return retval;

} /* install_file() */



size_t get_file_size(Options *op, const char *filename)
{
    struct stat stat_buf;
    
    if (stat(filename, &stat_buf) == -1) {
        ui_error(op, "Unable to determine file size of '%s' (%s).",
                 filename, strerror(errno));
        return 0;
    }

    return stat_buf.st_size;

} /* get_file_size() */



size_t fget_file_size(Options *op, const int fd)
{
    struct stat stat_buf;
    
    if (fstat(fd, &stat_buf) == -1) {
        ui_error(op, "Unable to determine file size of file "
                 "descriptor %d (%s).", fd, strerror(errno));
        return 0;
    }

    return stat_buf.st_size;

} /* fget_file_size() */



char *get_tmpdir(Options *op)
{
    char *tmpdirs[] = { NULL, "/tmp", ".", NULL };
    int i;

    tmpdirs[0] = getenv("TMPDIR");
    tmpdirs[3] = getenv("HOME");
    
    for (i = 0; i < 4; i++) {
        if (tmpdirs[i] && directory_exists(op, tmpdirs[i])) {
            return (tmpdirs[i]);
        }
    }
    
    return NULL;

} /* get_tmpdir() */



/*
 * make_tmpdir() - create a temporary directory; XXX we should really
 * use mkdtemp, but it is not available on all systems.
 */

char *make_tmpdir(Options *op)
{
    char tmp[32], *tmpdir;
    
    snprintf(tmp, 32, "%d", getpid());
    
    tmpdir = nvstrcat(op->tmpdir, "/nvidia-", tmp, NULL);
    
    if (directory_exists(op, tmpdir)) {
        remove_directory(op, tmpdir);
    }
    
    if (!mkdir_recursive(op, tmpdir, 0655)) {
        return NULL;
    }
    
    return tmpdir;
    
} /* make_tmpdir() */



/*
 * nvrename() - replacement for rename(2), because rename(2) can't
 * cross filesystem boundaries.  Get the src file attributes, copy the
 * src file to the dst file, stamp the dst file with the src file's
 * timestamp, and delete the src file.  Returns FALSE on error, TRUE
 * on success.
 */

int nvrename(Options *op, const char *src, const char *dst)
{
    struct stat stat_buf;
    struct utimbuf utime_buf;

    if (stat(src, &stat_buf) == -1) {
        ui_error(op, "Unable to determine file attributes of file "
                 "%s (%s).", src, strerror(errno));
        return FALSE;
    }
        
    if (!copy_file(op, src, dst, stat_buf.st_mode)) return FALSE;

    utime_buf.actime = stat_buf.st_atime; /* access time */
    utime_buf.modtime = stat_buf.st_mtime; /* modification time */

    if (utime(dst, &utime_buf) == -1) {
        ui_warn(op, "Unable to transfer timestamp from '%s' to '%s' (%s).",
                   src, dst, strerror(errno));
    }
    
    if (unlink(src) == -1) {
        ui_error(op, "Unable to delete '%s' (%s).", src, strerror(errno));
        return FALSE;
    }

    return TRUE;
    
} /* nvrename() */



/*
 * check_for_existing_rpms() - check if any of the previous NVIDIA
 * rpms are installed on the system.  If we find any, ask the user if
 * we may remove them.
 */

int check_for_existing_rpms(Options *op)
{
    /* list of rpms to remove; should be in dependency order */

    const char *rpms[2] = { "NVIDIA_GLX", "NVIDIA_kernel" };

    char *data, *cmd;
    int i, ret;

    if (op->no_rpms) {
        ui_log(op, "Skipping check for conflicting rpms.");
        return TRUE;
    }

    for (i = 0; i < 2; i++) {
        
        cmd = nvstrcat("env LD_KERNEL_ASSUME=2.2.5 rpm --query ",
                       rpms[i], NULL);
        ret = run_command(op, cmd, NULL, FALSE, 0, TRUE);
        nvfree(cmd);

        if (ret == 0) {
            if (!ui_yes_no(op, TRUE, "An %s rpm appears to already be "
                           "installed on your system.  As part of installing "
                           "the new driver, this %s rpm will be uninstalled.  "
                           "Are you sure you want to continue? ('no' will "
                           "abort installation)", rpms[i], rpms[i])) {
                ui_log(op, "Installation aborted.");
                return FALSE;
            }
            
            cmd = nvstrcat("rpm --erase --nodeps ", rpms[i], NULL);
            ret = run_command(op, cmd, &data, op->expert, 0, TRUE);
            nvfree(cmd);
            
            if (ret == 0) {
                ui_log(op, "Removed %s.", rpms[i]);
            } else {
                ui_warn(op, "Unable to erase %s rpm: %s", rpms[i], data);
            }
            
            nvfree(data);
        }
    }

    return TRUE;
    
} /* check_for_existing_rpms() */



/*
 * copy_directory_contents() - copy the contents of directory src to
 * directory dst.  This only copies files; subdirectories are ignored.
 */

int copy_directory_contents(Options *op, const char *src, const char *dst)
{
    DIR *dir;
    struct dirent *ent;
    char *srcfile, *dstfile;
    struct stat stat_buf;

    if ((dir = opendir(src)) == NULL) {
        ui_error(op, "Unable to open directory '%s' (%s).",
                 src, strerror(errno));
        return FALSE;
    }

    while ((ent = readdir(dir)) != NULL) {
        
        if (((strcmp(ent->d_name, ".")) == 0) ||
            ((strcmp(ent->d_name, "..")) == 0)) continue;
        
        srcfile = nvstrcat(src, "/", ent->d_name, NULL);

        /* only copy regular files */

        if ((stat(srcfile, &stat_buf) == -1) || !(S_ISREG(stat_buf.st_mode))) {
            nvfree(srcfile);
            continue;
        }
        
        dstfile = nvstrcat(dst, "/", ent->d_name, NULL);
        
        if (!copy_file(op, srcfile, dstfile, stat_buf.st_mode)) return FALSE;

        nvfree(srcfile);
        nvfree(dstfile);
    }

    if (closedir(dir) != 0) {
        ui_error(op, "Failure while closing directory '%s' (%s).",
                 src, strerror(errno));
        
        return FALSE;
    }
    
    return TRUE;
    
} /* copy_directory_contents() */



/*
 * pack_precompiled_kernel_interface() - 
 */

int pack_precompiled_kernel_interface(Options *op, Package *p)
{
    char *cmd, time_str[256], *proc_version_string;
    char major[16], minor[16], patch[16];
    char *result, *descr;
    time_t t;
    struct utsname buf;
    int ret;

    ui_log(op, "Packaging precompiled kernel interface.");

    /* make sure the precompiled_kernel_interface_directory exists */

    mkdir_recursive(op, p->precompiled_kernel_interface_directory, 0755);
    
    /* use the time in the output string... should be fairly unique */

    t = time(NULL);
    snprintf(time_str, 256, "%lu", t);
    
    /* read the proc version string */

    proc_version_string = read_proc_version(op);

    /* get the version strings */

    snprintf(major, 16, "%d", p->major);
    snprintf(minor, 16, "%d", p->minor);
    snprintf(patch, 16, "%d", p->patch);
    
    /* use the uname string as the description */

    uname(&buf);
    descr = nvstrcat(buf.sysname, " ",
                     buf.release, " ",
                     buf.version, " ",
                     buf.machine, NULL);
    
    /* build the mkprecompiled command */

    cmd = nvstrcat("./usr/bin/mkprecompiled --interface=",
                   p->kernel_module_build_directory, "/",
                   PRECOMPILED_KERNEL_INTERFACE_FILENAME,
                   " --output=", p->precompiled_kernel_interface_directory,
                   "/", PRECOMPILED_KERNEL_INTERFACE_FILENAME,
                   "-", p->version_string, ".", time_str,
                   " --description=\"", descr, "\"",
                   " --proc-version=\"", proc_version_string, "\"",
                   " --major=", major,
                   " --minor=", minor,
                   " --patch=", patch, NULL);

    /* execute the command */
    
    ret = run_command(op, cmd, &result, FALSE, 0, TRUE);
    
    nvfree(cmd);
    nvfree(proc_version_string);
    nvfree(descr);
    
    /* remove the old kernel interface file */

    cmd = nvstrcat(p->kernel_module_build_directory, "/",
                   PRECOMPILED_KERNEL_INTERFACE_FILENAME, NULL);
    
    unlink(cmd); /* XXX what to do if this fails? */

    nvfree(cmd);
    
    if (ret != 0) {
        ui_error(op, "Unable to package precompiled kernel interface: %s",
                 result);
    }

    nvfree(result);

    if (ret == 0) return TRUE;
    else return FALSE;
    
} /* pack_kernel_interface() */



/*
 * nv_strreplace() - we can't assume that the user has sed installed
 * on their system, so use this function to preform simple string
 * search and replacement.  Returns a newly allocated string that is a
 * duplicate of src, with all instances of 'orig' replaced with
 * 'replace'.
 */

static char *nv_strreplace(char *src, char *orig, char *replace)
{
    char *prev_s, *end_s, *s;
    char *d, *dst;
    int len, dst_len, orig_len, replace_len;
    int done = 0;

    prev_s = s = src;
    end_s = src + strlen(src) + 1;
    
    dst = NULL;
    dst_len = 0;

    orig_len = strlen(orig);
    replace_len = strlen(replace);

    do {
        /* find the next instances of orig in src */

        s = strstr(prev_s, orig);
        
        /*
         * if no match, then flag that we are done once we finish
         * copying the src into dst
         */

        if (!s) {
            s = end_s;
            done = 1;
        }
        
        /* copy the characters between prev_s and s into dst */
        
        len = s - prev_s;
        dst = realloc(dst, dst_len + len + 1);
        d = dst + dst_len;
        strncpy(d, prev_s, len);
        d[len] = '\0';
        dst_len += len;
        
        /* if we are not done, then append the replace string */

        if (!done) {
            dst = realloc(dst, dst_len + replace_len + 1);
            d = dst + dst_len;
            strncpy(d, replace, replace_len);
            d[replace_len] = '\0';
            dst_len += replace_len;
        }

        /* skip past the orig string */

        if (!done) prev_s = s + orig_len;

    } while (!done);
    
    return dst;

} /* nv_strreplace() */



/*
 * process_libGL_la_files() - for any libGL.la files in the package,
 * copy them to a temporary file, replacing __GENERATED_BY__ and
 * __LIBGL_PATH__ as appropriate.  Then, add the new file to the
 * package list.
 */

void process_libGL_la_files(Options *op, Package *p)
{
    int i, n, src_fd, dst_fd, len;
    struct stat stat_buf;
    char *src, *dst, *tmp, *tmp0, *tmp1, *tmpfile;

    for (i = 0; i < p->num_entries; i++) {
        if (p->entries[i].flags & FILE_TYPE_LIBGL_LA) {
    
            src_fd = dst_fd = -1;
            tmp0 = tmp1 = src = dst = tmpfile = NULL;
            len = 0;
            
            /* open the file */
        
            if ((src_fd = open(p->entries[i].file, O_RDONLY)) == -1) {
                ui_error(op, "Unable to open '%s' for copying (%s)",
                         p->entries[i].file, strerror(errno));
                goto done;
            }

            /* get the size of the file */

            if (fstat(src_fd, &stat_buf) == -1) {
                ui_error(op, "Unable to determine size of '%s' (%s)",
                         p->entries[i].file, strerror(errno));
                goto done;
            }

            /* mmap the file */

            if ((src = mmap(0, stat_buf.st_size, PROT_READ,
                            MAP_FILE|MAP_SHARED, src_fd, 0)) == (void *) -1) {
                ui_error (op, "Unable to map source file '%s' for "
                          "copying (%s)", p->entries[i].file, strerror(errno));
                src = NULL;
                goto done;
            }

            if (!src) {
                ui_log(op, "%s is empty; skipping.", p->entries[i].file);
                goto done;
            }

            /* replace __LIBGL_PATH__ */

            tmp = nvstrcat(op->opengl_prefix, "/", p->entries[i].path, NULL);
            tmp0 = nv_strreplace(src, "__LIBGL_PATH__", tmp);
            nvfree(tmp);

            /* replace __GENERATED_BY__ */

            tmp = nvstrcat(PROGRAM_NAME, ": ", NVIDIA_INSTALLER_VERSION, NULL);
            tmp1 = nv_strreplace(tmp0, "__GENERATED_BY__", tmp);
            nvfree(tmp);

            /* create a temporary file to store the processed libGL.la file */
            
            tmpfile = nvstrcat(op->tmpdir, "/libGL.la-XXXXXX", NULL);
            if ((dst_fd = mkstemp(tmpfile)) == -1) {
                ui_error(op, "Unable to create temporary file (%s)",
                         strerror(errno));
                goto done;
            }

            /* set the size of the new file */

            len = strlen(tmp1);
     
            if (lseek(dst_fd, len - 1, SEEK_SET) == -1) {
                ui_error(op, "Unable to set file size for '%s' (%s)",
                          tmpfile, strerror(errno));
                goto done;
            }
            if (write(dst_fd, "", 1) != 1) {
                ui_error(op, "Unable to write file size for '%s' (%s)",
                         tmpfile, strerror(errno));
                goto done;
            }

            /* mmap the new file */

            if ((dst = mmap(0, len, PROT_READ | PROT_WRITE,
                            MAP_FILE|MAP_SHARED, dst_fd, 0)) == (void *) -1) {
                ui_error(op, "Unable to map destination file '%s' for "
                         "copying (%s)", tmpfile, strerror(errno));
                dst = NULL;
                goto done;
            }

            /* write the data out to the new file */

            memcpy(dst, tmp1, len);

            /* add this new file to the package */
            
            n = p->num_entries;
            
            p->entries =
                (PackageEntry *) nvrealloc(p->entries,
                                           (n + 1) * sizeof(PackageEntry));
            p->entries[n].file = tmpfile;
            p->entries[n].path = p->entries[i].path;
            p->entries[n].target = NULL;
            p->entries[n].flags = ((p->entries[i].flags & FILE_CLASS_MASK) |
                                   FILE_TYPE_OPENGL_LIB);
            p->entries[n].mode = p->entries[i].mode;
            
            p->entries[n].name = nvstrdup(p->entries[i].name);
            
            p->num_entries++;

        done:

            if (src) {
                if (munmap(src, stat_buf.st_size) == -1) {
                    ui_error(op, "Unable to unmap source file '%s' after "
                             "copying (%s)", p->entries[i].file,
                             strerror(errno));
                }
            }
            
            if (dst) {
                if (munmap(dst, len) == -1) {
                    ui_error (op, "Unable to unmap destination file '%s' "
                              "after copying (%s)", tmpfile, strerror(errno));
                }
            }

            if (src_fd != -1) close(src_fd);
            if (dst_fd != -1) close(dst_fd);
            
            if (tmp0) nvfree(tmp0);
            if (tmp1) nvfree(tmp1);
        }
    }
} /* process_libGL_la_files() */