-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
detect.c
1168 lines (1029 loc) · 30.5 KB
/
detect.c
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
#include "config.h"
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/param.h>
#ifdef HAVE_SYS_MOUNT_H
# include <sys/mount.h>
#endif
#ifdef HAVE_SYS_MNTTAB_H
# include <sys/mnttab.h>
#endif
#include <sys/utsname.h>
#ifdef HAVE_SYS_UCRED_H
# include <sys/ucred.h>
#endif
#ifdef HAVE_FSTAB_H
# include <fstab.h>
#endif
#ifdef HAVE_MNTENT_H
# include <mntent.h>
#endif
#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#ifndef CODESET
# define CODESET _NL_CTYPE_CODESET_NAME
#endif
#endif
#ifdef HAVE_OSRELDATE_H
# include <osreldate.h>
# if defined(__FreeBSD_version) && !defined(__FreeBSD__)
# define __FreeBSD__
# endif
#endif
/* From libxml */
#include <libxml/encoding.h>
#include "install.h"
#include "install_ui.h"
#include "install_log.h"
#include "detect.h"
#include "bools.h"
#ifndef MNTTYPE_CDROM
#if defined(__FreeBSD__)
#define MNTTYPE_CDROM "cd9660"
#elif defined(hpux)
#define MNTTYPE_CDROM "cdfs"
#elif defined(_AIX)
#define MNTTYPE_CDROM "cdrfs"
#elif defined(sco)
#define MNTTYPE_CDROM "ISO9660"
#elif defined(__svr4__)
#define MNTTYPE_CDROM "hsfs"
#else
#define MNTTYPE_CDROM "iso9660"
#endif
#endif
#ifndef MNTTYPE_SUPER
#define MNTTYPE_SUPER "supermount"
#endif
/* #define MOUNTS_FILE "/proc/mounts" */
#ifdef __svr4__
#define MOUNTS_FILE MNTTAB
#elif defined(MNT_MNTTAB)
#define MOUNTS_FILE MNT_MNTTAB
#elif defined(MOUNTED)
#define MOUNTS_FILE MOUNTED
#elif defined(__FreeBSD__)
#define MOUNTS_FILE ""
#else
#define MOUNTS_FILE _PATH_MOUNTED
#endif
#ifdef MNTTAB
#define SETUP_FSTAB MNTTAB
#elif defined(__FreeBSD__)
#define SETUP_FSTAB ""
#else
#define SETUP_FSTAB _PATH_MNTTAB
#endif
/* Ignore devices that begin by this string - we only focus on other removable devices */
#define DEVICE_FLOPPY "/dev/fd"
/* The filesystems that were mounted by setup */
static
struct mounted_elem {
char *device;
char *dir;
struct mounted_elem *next;
} *mounted_list = NULL;
/* The current locale and character encoding */
static char *current_locale = NULL, *current_encoding = NULL;
extern Install_UI UI;
#if defined(darwin)
/*
* MacOS 10.4 ("Tiger") includes statvfs(), which break binary compat with
* previous MacOS releases, so force down the older codepath...
*/
#ifdef HAVE_SYS_STATVFS_H
#undef HAVE_SYS_STATVFS_H
#endif
/*
* Code based on sample from Apple Developer Connection:
* http://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm
*/
# include <CoreFoundation/CoreFoundation.h>
# include <CoreServices/CoreServices.h>
# include <IOKit/IOKitLib.h>
# include <IOKit/storage/IOMedia.h>
# include <IOKit/storage/IOCDMedia.h>
# include <IOKit/storage/IODVDMedia.h>
static char darwinEjectThisCDDevice[MAXPATHLEN];
static int darwinIsWholeMedia(io_service_t service)
{
int retval = 0;
CFTypeRef wholeMedia;
if (!IOObjectConformsTo(service, kIOMediaClass))
return(0);
wholeMedia = IORegistryEntryCreateCFProperty(service,
CFSTR(kIOMediaWholeKey),
kCFAllocatorDefault, 0);
if (wholeMedia == NULL)
return(0);
retval = CFBooleanGetValue(wholeMedia);
CFRelease(wholeMedia);
return retval;
} /* darwinIsWholeMedia */
static int darwinIsMountedDisc(char *bsdName, mach_port_t masterPort)
{
int retval = 0;
CFMutableDictionaryRef matchingDict;
kern_return_t rc;
io_iterator_t iter;
io_service_t service;
if ((matchingDict = IOBSDNameMatching(masterPort, 0, bsdName)) == NULL)
return(0);
rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter);
if ((rc != KERN_SUCCESS) || (!iter))
return(0);
service = IOIteratorNext(iter);
IOObjectRelease(iter);
if (!service)
return(0);
rc = IORegistryEntryCreateIterator(service, kIOServicePlane,
kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);
if (!iter)
return(0);
if (rc != KERN_SUCCESS)
{
IOObjectRelease(iter);
return(0);
} /* if */
IOObjectRetain(service); /* add an extra object reference... */
do
{
if (darwinIsWholeMedia(service))
{
if ( (IOObjectConformsTo(service, kIOCDMediaClass)) ||
(IOObjectConformsTo(service, kIODVDMediaClass)) )
{
retval = 1;
} /* if */
} /* if */
IOObjectRelease(service);
} while ((service = IOIteratorNext(iter)) && (!retval));
IOObjectRelease(iter);
IOObjectRelease(service);
return(retval);
} /* darwinIsMountedDisc */
#endif /* defined(darwin) */
struct mounted_elem *add_mounted_entry(const char *device, const char *dir)
{
struct mounted_elem *elem;
elem = (struct mounted_elem *)malloc(sizeof *elem);
if ( elem ) {
elem->device = strdup(device);
elem->dir = strdup(dir);
elem->next = mounted_list;
mounted_list = elem;
}
return elem;
}
/* Indicates if any filesystems were mounted by setup */
int mounted_filesystems(void)
{
return mounted_list != NULL;
}
int is_fs_mounted(const char *dev)
{
int found = 0;
#ifdef __FreeBSD__
int count, i;
struct statfs *mntbuf;
count = getmntinfo(&mntbuf, 0);
if ( count > 0 ) {
for ( i = 0; i < count; ++i ) {
if ( !strcmp(mntbuf[i].f_mntfromname, dev) ) {
found = 1;
break;
}
}
}
#elif defined(hpux)
const char *mtab, *mnttype;
FILE *mountfp;
struct mntent *mntent;
if ( !access("/etc/pfs_mtab", F_OK) ) {
mtab = "/etc/pfs_mtab";
mnttype = "pfs-rrip";
} else {
mtab = MOUNTS_FILE;
mnttype = MNTTYPE_CDROM;
}
mountfp = setmntent(mtab, "r" );
if ( mountfp ) {
while( (mntent = getmntent( mountfp )) != NULL ) {
if ( !strcmp(mntent->mnt_type, mnttype) ) {
found = 1;
break;
}
}
endmntent( mountfp );
}
#elif defined(sco)
FILE *cmd = popen("/etc/mount", "r");
if ( cmd ) {
char device[32] = "";
while ( fscanf(cmd, "%*s on %32s %*[^\n]", device) > 0 ) {
if ( !strcmp(device, dev) ) {
found = 1;
break;
}
}
pclose(cmd);
}
#elif defined(__svr4__)
struct mnttab mnt;
FILE *mtab = fopen(MOUNTS_FILE, "r");
if ( mtab != NULL ) {
while ( getmntent(mtab, &mnt)==0 ) {
if ( !strcmp(mnt.mnt_special, dev) ) {
found = 1;
break;
}
}
fclose(mtab);
}
#elif defined(darwin)
// Taken from FreeBSD section (since Darwin is based on FreeBSD)
int count, i;
struct statfs *mntbuf;
count = getmntinfo(&mntbuf, 0);
if ( count > 0 ) {
for ( i = 0; i < count; ++i ) {
if ( !strcmp(mntbuf[i].f_mntfromname, dev) ) {
found = 1;
break;
}
}
}
#else
struct mntent *mnt;
FILE *mtab = setmntent(MOUNTS_FILE, "r" );
if( mtab != NULL ) {
while( (mnt = getmntent( mtab )) != NULL ){
if ( !strcmp(mnt->mnt_fsname, dev) ) {
found = 1;
break;
}
}
endmntent(mtab);
}
#endif
return found;
}
/* stolen from gtk_ui.c to validate write access to install directory */
void topmost_valid_path(char *target, const char *src)
{
char *cp;
/* Get the topmost valid path */
strcpy(target, src);
if ( target[0] == '/' ) {
cp = target+strlen(target);
while ( access(target, F_OK) < 0 ) {
while ( (cp > (target+1)) && (*cp != '/') ) {
--cp;
}
*cp = '\0';
}
}
}
void unmount_filesystems(void)
{
const char *fstab, *mtab, *mount_cmd, *mnttype;
FILE *mountfp;
struct mntent *mntent;
struct mounted_elem *mnt = mounted_list, *oldmnt;
while ( mnt ) {
log_normal(_("Unmounting device %s"), mnt->device);
if ( run_command(NULL, UMOUNT_PATH, "-l", mnt->dir, 1) ) {
log_warning(_("Failed to unmount device %s mounted on %s"), mnt->device, mnt->dir);
}
free(mnt->device);
free(mnt->dir);
oldmnt = mnt;
mnt = mnt->next;
free(oldmnt);
}
//Now go through the list of filesystems left that may not be in fstab,
//such as ones automounted through the gnome automounter or manually
//mounted on loop - MS:LGP
if ( !access("/etc/pfs_fstab", F_OK) ) {
fstab = "/etc/pfs_fstab";
mtab = "/etc/pfs_mtab";
mount_cmd = "/usr/sbin/pfs_mount";
mnttype = "pfs-rrip";
} else {
fstab = SETUP_FSTAB;
mtab = MOUNTS_FILE;
mnttype = MNTTYPE_CDROM;
mount_cmd = MOUNT_PATH;
}
#if defined(__svr4__)
mountfp = fopen(MOUNTS_FILE, "r");
if ( mountfp != NULL ) {
struct mnttab mnt;
while ( getmntent(mountfp, &mnt)==0 ) {
if ( !strcmp(mnt.mnt_fstype, mnttype) ) {
if ( ! run_command(NULL, UMOUNT_PATH, "-l", mnt.mnt_special, 1) ) {
log_warning(_("Failed to unmount device %s mounted on %s"), mnt.mnt_special, mnt.mnt_mountp);
}
}
}
fclose(mountfp);
}
#elif !defined(__FreeBSD__) // FIXME
mountfp = setmntent( mtab, "r" );
if( mountfp != NULL ) {
while( (mntent = getmntent( mountfp )) != NULL ){
if ( !strcmp(mntent->mnt_type, mnttype) ) {
char *fsname = strdup(mntent->mnt_fsname);
char *dir = strdup(mntent->mnt_dir);
if ( ! run_command(NULL, UMOUNT_PATH, "-l", fsname, 1) ) {
log_warning(_("Failed to unmount device %s mounted on %s"), fsname, dir);
}
free(fsname);
free(dir);
}
}
endmntent(mountfp);
}
#endif
mounted_list = NULL;
}
/* Function to detect the MB of diskspace on a path */
int detect_diskspace(const char *path)
{
long long avail = 0LL;
if ( path[0] == '/' ) {
#ifdef HAVE_SYS_STATVFS_H
struct statvfs fs;
#else
struct statfs fs;
#endif
char buf[PATH_MAX], *cp;
strcpy(buf, path);
cp = buf+strlen(buf);
while ( buf[0] && (access(buf, F_OK) < 0) ) {
while ( (cp > (buf+1)) && (*cp != '/') ) {
--cp;
}
*cp = '\0';
}
if ( buf[0] ) {
#ifdef HAVE_SYS_STATVFS_H
if ( statvfs(buf, &fs) ) {
#else
if ( statfs(buf, &fs) ) {
#endif
perror("statfs");
return 0;
}
#ifdef HAVE_SYS_STATVFS_H
avail = fs.f_frsize;
#else
avail = fs.f_bsize;
#endif
avail *= fs.f_bavail;
}
}
return avail / (1024*1024LL);
}
int detect_and_mount_cdrom(char *path[SETUP_MAX_DRIVES])
{
int num_cdroms = 0;
#ifdef __FreeBSD__
int mounted;
struct fstab *fstab;
/* Try to mount unmounted CDROM filesystems */
while( (fstab = getfsent()) != NULL ){
if ( !strcmp(fstab->fs_vfstype, MNTTYPE_CDROM)) {
if ( !is_fs_mounted(fstab->fs_spec)) {
if ( ! run_command(NULL, MOUNT_PATH, fstab->fs_spec, NULL, 1) ) {
add_mounted_entry(fstab->fs_spec, fstab->fs_file);
log_normal(_("Mounted device %s"), fstab->fs_spec);
}
}
}
}
endfsent();
mounted = getfsstat(NULL, 0, MNT_WAIT);
if ( mounted > 0 ) {
int i;
struct statfs *mnts = (struct statfs *)malloc(sizeof(struct statfs) * mounted);
mounted = getfsstat(mnts, mounted * sizeof(struct statfs), MNT_WAIT);
for ( i = 0; i < mounted && num_cdroms < SETUP_MAX_DRIVES; ++ i ) {
if ( ! strcmp(mnts[i].f_fstypename, MNTTYPE_CDROM) ) {
path[num_cdroms ++] = strdup(mnts[i].f_mntonname);
}
}
free(mnts);
}
#elif defined(darwin)
{
const char *devPrefix = "/dev/";
int prefixLen = strlen(devPrefix);
mach_port_t masterPort = 0;
struct statfs *mntbufp;
int i, mounts;
if (IOMasterPort(MACH_PORT_NULL, &masterPort) == KERN_SUCCESS)
{
mounts = getmntinfo(&mntbufp, MNT_WAIT); /* NOT THREAD SAFE! */
for (i = 0; i < mounts && num_cdroms < SETUP_MAX_DRIVES; i++)
{
char *dev = mntbufp[i].f_mntfromname;
char *mnt = mntbufp[i].f_mntonname;
if (strncmp(dev, devPrefix, prefixLen) != 0) /* virtual device? */
continue;
/* darwinIsMountedDisc needs to skip "/dev/" part of string... */
if (darwinIsMountedDisc(dev + prefixLen, masterPort))
{
strcpy(darwinEjectThisCDDevice, dev + prefixLen);
path[num_cdroms ++] = strdup(mnt);
}
}
}
}
#elif defined(sco)
/* Quite horrible. We have to parse mount's output :( */
/* And of course, we can't try to mount unmounted filesystems */
FILE *cmd = popen("/etc/mount", "r");
if ( cmd ) {
char device[32] = "", mountp[PATH_MAX] = "";
while ( fscanf(cmd, "%s on %32s %*[^\n]", mountp, device) > 0 ) {
if ( !strncmp(device, "/dev/cd", 7) ) {
path[num_cdroms ++] = strdup(mountp);
break;
}
}
pclose(cmd);
}
#elif defined(hpux)
char mntdevpath[PATH_MAX];
FILE *mountfp;
struct mntent *mntent;
const char *fstab, *mtab, *mount_cmd, *mnttype;
/* HPUX: We need to support PFS */
if ( !access("/etc/pfs_fstab", F_OK) ) {
fstab = "/etc/pfs_fstab";
mtab = "/etc/pfs_mtab";
mount_cmd = "/usr/sbin/pfs_mount";
mnttype = "pfs-rrip";
} else {
fstab = SETUP_FSTAB;
mtab = MOUNTS_FILE;
mnttype = MNTTYPE_CDROM;
mount_cmd = MOUNT_PATH;
}
/* Try to mount unmounted CDROM filesystems */
mountfp = setmntent( fstab, "r" );
if( mountfp != NULL ) {
while( (mntent = getmntent( mountfp )) != NULL ){
if ( !strcmp(mntent->mnt_type, mnttype) ) {
char *fsname = strdup(mntent->mnt_fsname);
char *dir = strdup(mntent->mnt_dir);
if ( !is_fs_mounted(fsname)) {
if ( ! run_command(NULL, mount_cmd, fsname, NULL, 1) ) {
add_mounted_entry(fsname, dir);
log_normal(_("Mounted device %s"), fsname);
}
}
free(fsname);
free(dir);
}
}
endmntent(mountfp);
}
mountfp = setmntent(mtab, "r" );
if( mountfp != NULL ) {
while( (mntent = getmntent( mountfp )) != NULL && num_cdroms < SETUP_MAX_DRIVES){
char mntdev[1024], mnt_type[32];
strcpy(mntdev, mntent->mnt_fsname);
strcpy(mnt_type, mntent->mnt_type);
if( strncmp(mntdev, "/dev", 4) ||
realpath(mntdev, mntdevpath) == NULL ) {
continue;
}
if ( strcmp(mnt_type, mnttype) == 0 ) {
path[num_cdroms ++] = strdup(mntent->mnt_dir);
}
}
endmntent( mountfp );
}
#elif defined(__svr4__)
struct mnttab mnt;
FILE *fstab = fopen(SETUP_FSTAB, "r");
if ( fstab != NULL ) {
while ( getmntent(fstab, &mnt)==0 ) {
if ( !strcmp(mnt.mnt_fstype, MNTTYPE_CDROM) ) {
char *fsname = strdup(mnt.mnt_special);
char *dir = strdup(mnt.mnt_mountp);
if ( !is_fs_mounted(fsname)) {
if ( ! run_command(NULL, MOUNT_PATH, fsname, NULL, 1) ) {
add_mounted_entry(fsname, dir);
log_normal(_("Mounted device %s"), fsname);
}
}
free(fsname);
free(dir);
break;
}
}
fclose(fstab);
fstab = fopen(MOUNTS_FILE, "r");
if (fstab) {
while ( getmntent(fstab, &mnt)==0 && num_cdroms < SETUP_MAX_DRIVES) {
if ( !strcmp(mnt.mnt_fstype, MNTTYPE_CDROM) ) {
path[num_cdroms ++] = strdup(mnt.mnt_mountp);
}
}
fclose(fstab);
}
}
#else
//#ifndef darwin
char mntdevpath[PATH_MAX];
FILE *mountfp;
struct mntent *mntent;
/* Try to mount unmounted CDROM filesystems */
mountfp = setmntent( SETUP_FSTAB, "r" );
if( mountfp != NULL ) {
while( (mntent = getmntent( mountfp )) != NULL ){
if ( (!strcmp(mntent->mnt_type, MNTTYPE_CDROM)
#ifdef sgi
|| !strcmp(mntent->mnt_type, "cdfs")
|| !strcmp(mntent->mnt_type, "efs")
#endif
|| !strcmp(mntent->mnt_type, "cd9660")
|| !strcmp(mntent->mnt_type, "udf")
|| !strcmp(mntent->mnt_type, "auto"))
&& strncmp(mntent->mnt_fsname, DEVICE_FLOPPY, strlen(DEVICE_FLOPPY)) ) {
char *fsname = strdup(mntent->mnt_fsname);
char *dir = strdup(mntent->mnt_dir);
if ( !is_fs_mounted(fsname)) {
if ( ! run_command(NULL, MOUNT_PATH, fsname, NULL, 1) ) {
add_mounted_entry(fsname, dir);
log_normal(_("Mounted device %s"), fsname);
}
}
free(fsname);
free(dir);
}
}
endmntent(mountfp);
}
mountfp = setmntent(MOUNTS_FILE, "r" );
if( mountfp != NULL ) {
while( (mntent = getmntent( mountfp )) != NULL && num_cdroms < SETUP_MAX_DRIVES){
char *tmp, mntdev[1024], mnt_type[1024];
if ( strncmp(mntent->mnt_fsname, DEVICE_FLOPPY, strlen(DEVICE_FLOPPY)) == 0)
continue;
#define XXXstrcpy(d,s) \
do { strncpy(d,s,sizeof(d)); d[sizeof(d)-1] = '\0'; } while(0);
XXXstrcpy(mntdev, mntent->mnt_fsname);
XXXstrcpy(mnt_type, mntent->mnt_type);
if ( strcmp(mnt_type, MNTTYPE_SUPER) == 0 ) {
tmp = strstr(mntent->mnt_opts, "fs=");
if ( tmp ) {
XXXstrcpy(mnt_type, tmp+strlen("fs="));
tmp = strchr(mnt_type, ',');
if ( tmp ) {
*tmp = '\0';
}
}
tmp = strstr(mntent->mnt_opts, "dev=");
if ( tmp ) {
XXXstrcpy(mntdev, tmp+strlen("dev="));
tmp = strchr(mntdev, ',');
if ( tmp ) {
*tmp = '\0';
}
}
}
if ( strcmp(mnt_type, "subfs") == 0 ) {
tmp = strstr(mntent->mnt_opts, "fs=");
if ( tmp ) {
XXXstrcpy(mnt_type, tmp+strlen("fs="));
tmp = strchr(mnt_type, ',');
if ( tmp ) {
*tmp = '\0';
}
}
if(!strcmp(mnt_type, "cdfss"))
XXXstrcpy(mnt_type, MNTTYPE_CDROM);
}
tmp = strstr(mntent->mnt_opts, "loop=");
if ( tmp ) {
XXXstrcpy(mntdev, tmp+strlen("loop="));
tmp = strchr(mntdev, ',');
if ( tmp ) {
*tmp = '\0';
}
}
#undef XXXstrcpy
if( strncmp(mntdev, "/dev", 4) ||
realpath(mntdev, mntdevpath) == NULL ) {
continue;
}
if ( strcmp(mnt_type, MNTTYPE_CDROM) == 0 ) {
path[num_cdroms ++] = strdup(mntent->mnt_dir);
} else if ( strcmp(mnt_type, "cd9660") == 0 ) {
path[num_cdroms ++] = strdup(mntent->mnt_dir);
} else if ( strcmp(mnt_type, "auto") == 0 &&
strncmp(mntdev, DEVICE_FLOPPY, strlen(DEVICE_FLOPPY))) {
path[num_cdroms ++] = strdup(mntent->mnt_dir);
}
#ifdef sgi
else if ( strcmp(mnt_type, "cdfs") == 0 ||
strcmp(mnt_type, "efs") == 0 ) {
path[num_cdroms ++] = strdup(mntent->mnt_dir);
}
#endif
}
endmntent( mountfp );
}
//#endif
#endif
return num_cdroms;
}
void free_mounted_cdrom(int nb, char *path[SETUP_MAX_DRIVES])
{
while ( nb ) {
free(path[--nb]);
}
}
/* Function to detect the CDROM drives, returns the number of drives and fills in the CDROM info */
int detect_cdrom(install_info *info)
{
struct cdrom_elem *cd;
int num_cdroms = 0, num_mounted = 0, i;
char file[PATH_MAX], *cds[SETUP_MAX_DRIVES];
/* Clear all of the mount points */
for( cd = info->cdroms_list; cd; cd = cd->next ) {
set_cdrom_mounted(cd, NULL);
}
/* Override the CD detection ? */
for ( cd = info->cdroms_list; cd; cd = cd->next ) {
char *env = getenv("SETUP_CDROM");
if ( ! env ) {
char cdenv[256], *ptr, *pid;
strcpy(cdenv, "SETUP_CDROM_");
ptr = cdenv + strlen(cdenv);
for ( pid = cd->id; *pid; pid ++ ) {
*ptr ++ = toupper(*pid);
}
*ptr = '\0';
env = getenv(cdenv);
if ( !env )
continue;
}
snprintf(file, sizeof(file), "%s/%s", env, cd->file);
if ( ! access(file, F_OK) ) {
set_cdrom_mounted(cd, env);
num_cdroms ++;
}
}
if ( num_cdroms )
return num_cdroms;
num_cdroms = detect_and_mount_cdrom(cds);
for ( i = 0; i < num_cdroms; ++i ) {
for ( cd = info->cdroms_list; cd; cd = cd->next ) {
snprintf(file, sizeof(file), "%s/%s", cds[i], cd->file);
if ( !access(file, F_OK) ) {
set_cdrom_mounted(cd, cds[i]);
++ num_mounted;
break;
}
}
}
free_mounted_cdrom(num_cdroms, cds);
return(num_mounted);
}
/* Get a mount point for the specified CDROM, and return its path.
If the CDROM is not mounted, prompt the user to mount it */
const char *get_cdrom(install_info *info, const char *id)
{
const char *mounted = NULL, *desc = info->desc;
struct cdrom_elem *cd;
while ( ! mounted ) {
detect_cdrom(info);
for ( cd = info->cdroms_list; cd; cd = cd->next ) {
if ( !strcmp(id, cd->id) ) {
desc = cd->name;
if ( cd->mounted ) {
mounted = cd->mounted;
break;
}
}
}
if ( ! mounted ) {
yesno_answer response;
char buf[1024];
char *prompt;
#if defined(darwin)
// !!! TODO: do this right. I just hacked this in. --ryan.
char *discs[SETUP_MAX_DRIVES];
// This only detects mounted discs and doesn't mount itself on OSX.
int discCount = detect_and_mount_cdrom(discs);
if (discCount > 0)
{
char cmd[128];
strcpy(cmd, "/usr/sbin/disktool -e ");
strcat(cmd, darwinEjectThisCDDevice);
strcat(cmd, " &");
system(cmd);
for (discCount--; discCount >= 0; discCount--)
free(discs[discCount]);
}
// end ryan's hack.
prompt = _("\nPlease insert %s.\n"
"Choose Yes to retry, No to cancel");
#else
if ( mounted_filesystems() ) { /* We were able to mount at least one CDROM */
prompt = _("\nPlease insert %s.\n"
"Choose Yes to retry, No to cancel");
} else {
prompt = _("\nPlease mount %s.\n"
"Choose Yes to retry, No to cancel");
}
#endif
unmount_filesystems();
snprintf(buf, sizeof(buf), prompt, desc);
response = UI.prompt(buf, RESPONSE_NO);
if ( response == RESPONSE_NO ) {
abort_install();
return NULL;
}
}
}
return mounted;
}
char *convert_encoding(char *str)
{
static xmlCharEncodingHandlerPtr handler = NULL;
static char buf[1024];
if ( handler == NULL && current_encoding ) {
handler = xmlFindCharEncodingHandler(current_encoding);
}
/* fprintf(stderr, "convert_encoding(%s) handler=%s\n", str, handler->name); */
if ( handler && handler->output ) {
int outlen = sizeof(buf), inlen = strlen(str)+1;
if (
#if LIBXML_VERSION < 20000
handler->output((unsigned char*)buf, outlen, (unsigned char*)str, inlen)
#else
handler->output((unsigned char*)buf, &outlen, (unsigned char*)str, &inlen)
#endif
< 0 ) {
return str;
} else {
/* fprintf(stderr, "Converted to %s\n", buf); */
return buf;
}
}
return str;
}
void DetectLocale(void)
{
/* Free previous values, in case we get called twice. */
free(current_locale);
free(current_encoding);
current_locale = setlocale(LC_MESSAGES, NULL);
if ( current_locale && (!strcmp(current_locale, "C") || !strcmp(current_locale,"POSIX")) ) {
current_locale = NULL;
}
#ifdef HAVE_NL_LANGINFO
current_encoding = nl_langinfo(CODESET);
#else
if ( current_locale ) { /* Try to extract an encoding */
char *ptr = strchr(current_locale, '.');
if ( ptr ) {
current_encoding = ptr+1;
/* Special cases */
if ( !strncasecmp(current_encoding, "iso88591", 9) )
current_encoding = "ISO-8859-1";
if ( !strncasecmp(current_encoding, "utf8", 9) )
current_encoding = "UTF-8";
} else {
current_encoding = "ISO-8859-1"; /* Assume Latin-1 !!! FIXME: UTF-8, now? */
}
}
#endif
/*
* setlocale() and nl_langinfo return internal glibc buffers...gtk+
* calls setlocale internally, which may cause our pointer to get
* free()'d, so make a copy. --ryan.
*/
current_locale = ((current_locale) ? strdup(current_locale) : NULL);
current_encoding = ((current_encoding) ? strdup(current_encoding) : NULL);
#if 0
/* log_debug doesn't work here as logging is initialized later */
if ( current_locale )
fprintf(stderr, _("Detected locale is %s\n"), current_locale);
#endif
}
void SetLocaleBools(void)
{
if ( current_locale ) {
char *dup = strdup(current_locale), *ptr;
ptr = strchr(dup, '.');
if ( ptr ) {
*ptr = '\0';
setup_add_bool(dup, 1);
}
free(dup);
setup_add_bool(current_locale, 1);
}
if ( current_encoding ) {
setup_add_bool(current_encoding, 1);
}
}
/*
* strncpy is silly...it doesn't null-terminate the string if (dst) is small,
* and doesn't tell you. This function just promises null-termination.
*/
static char *xstrncpy(char *dst, const char *src, size_t size)
{
strncpy(dst, src, size);
dst[size-1] = '\0';
return dst;
}
typedef struct locale_elements
{
char language[32];
char territory[32];
char encoding[32];
char modifier[32];
} locale_elements;
/* split a locale string into it's fundamental parts. */
static void split_locale_string(const char *_str, locale_elements *e)
{
char *ptr;
char *str;
memset(e, '\0', sizeof (*e));
if (_str == NULL)
return;
str = (char *) alloca(strlen(_str) + 1);
strcpy(str, _str);
/* parse backwards from last element. */
memset(e, '\0', sizeof (locale_elements));
ptr = strchr(str, '@');
if (ptr) { *ptr=0; xstrncpy(e->modifier, ptr+1, sizeof (e->modifier)); }
ptr = strchr(str, '.');
if (ptr) { *ptr=0; xstrncpy(e->encoding, ptr+1, sizeof (e->encoding)); }
ptr = strchr(str, '_');
if (ptr) { *ptr=0; xstrncpy(e->territory, ptr+1, sizeof (e->territory)); }