158
161
error (1, errno, _("Cannot open '%s'"), nbuf);
163
166
size_t got = fwrite (buffer, size, count, file);
212
215
dpnt->extent = last_extent;
213
216
dir_size = (dpnt->size + (SECTOR_SIZE - 1)) >> 11;
215
218
last_extent += dir_size;
218
221
* Leave room for the CE entries for this directory. Keep them
219
* close to the reference directory so that access will be
222
* close to the reference directory so that access will be
222
225
if(dpnt->ce_bytes)
238
static void FDECL3(write_one_file, char *, filename,
241
static void FDECL3(write_one_file, char *, filename,
239
242
uint64_t, size, FILE *, outfile)
241
244
char buffer[SECTOR_SIZE * NSECT];
247
if ((infile = fopen(filename, "rb")) == NULL)
250
if ((infile = fopen(filename, "rb")) == NULL)
248
251
error (1, errno, _("cannot open %s\n"), filename);
253
256
use = (remain > SECTOR_SIZE * NSECT - 1 ? NSECT*SECTOR_SIZE : remain);
254
257
use = ROUND_UP(use); /* Round up to nearest sector boundary */
255
258
memset(buffer, 0, use);
256
if (fread(buffer, 1, use, infile) == 0)
257
error (1, errno, _("cannot read %llu bytes from %s"), use, filename);
259
if (fread(buffer, 1, use, infile) == 0)
260
error (1, errno, _("cannot read %llu bytes from %s"), use, filename);
258
261
xfwrite(buffer, 1, use, outfile);
259
262
last_extent_written += use/SECTOR_SIZE;
261
if((last_extent_written % 1000) < use/SECTOR_SIZE)
264
if((last_extent_written % 1000) < use/SECTOR_SIZE)
263
266
fprintf(stderr,"%d..", last_extent_written);
327
static int FDECL2(compare_dirs, const void *, rr, const void *, ll)
330
static int FDECL2(compare_dirs, const void *, rr, const void *, ll)
329
332
char * rpnt, *lpnt;
330
333
struct directory_entry ** r, **l;
332
335
r = (struct directory_entry **) rr;
333
336
l = (struct directory_entry **) ll;
334
337
rpnt = (*r)->isorec.name;
373
376
if((*l)->isorec.name_len[0] == 1 && *lpnt == 1) return 1;
376
while(*rpnt && *lpnt)
379
while(*rpnt && *lpnt)
378
381
if(*rpnt == ';' && *lpnt != ';') return -1;
379
382
if(*rpnt != ';' && *lpnt == ';') return 1;
381
384
if(*rpnt == ';' && *lpnt == ';') return 0;
383
386
if(*rpnt == '.' && *lpnt != '.') return -1;
384
387
if(*rpnt != '.' && *lpnt == '.') return 1;
386
389
if((unsigned char)*rpnt < (unsigned char)*lpnt) return -1;
387
390
if((unsigned char)*rpnt > (unsigned char)*lpnt) return 1;
428
* OK, now we know how many there are. Build a vector for sorting.
431
* OK, now we know how many there are. Build a vector for sorting.
430
sortlist = (struct directory_entry **)
433
sortlist = (struct directory_entry **)
431
434
e_malloc(sizeof(struct directory_entry *) * dcount);
449
452
s_entry->isorec.name[len] = 0;
450
453
s_entry = s_entry->next;
454
457
* Each directory is required to contain at least . and ..
463
466
/* only sort the non-hidden entries */
466
qsort(sortlist, dcount, sizeof(struct directory_entry *),
469
qsort(sortlist, dcount, sizeof(struct directory_entry *),
467
470
(int (*)(const void *, const void *))compare_dirs);
469
qsort(sortlist, dcount, sizeof(struct directory_entry *),
472
qsort(sortlist, dcount, sizeof(struct directory_entry *),
474
* Now reassemble the linked list in the proper sorted order
477
* Now reassemble the linked list in the proper sorted order
475
478
* We still need the hidden entries, as they may be used in the
535
* This saves some space if there are symlinks present
538
* This saves some space if there are symlinks present
537
540
s_hash = find_hash(s_entry->dev, s_entry->inode);
542
fprintf (stderr, _("Cache hit for %s%s%s\n"), s_entry->filedir->de_name,
545
fprintf (stderr, _("Cache hit for %s%s%s\n"), s_entry->filedir->de_name,
543
546
SPATH_SEPARATOR, s_entry->name);
545
548
set_733((char *) s_entry->isorec.extent, s_hash->starting_block);
551
* If this is for a directory that is not a . or a .. entry,
554
* If this is for a directory that is not a . or a .. entry,
552
555
* then look up the information for the entry. We have already
553
556
* assigned extents for directories, so we just need to
554
557
* fill in the blanks here.
556
if (strcmp(s_entry->name,".") && strcmp(s_entry->name,"..") &&
559
if (strcmp(s_entry->name,".") && strcmp(s_entry->name,"..") &&
557
560
s_entry->isorec.flags[0] == 2)
559
562
finddir = dpnt->subdir;
562
565
if(finddir->self == s_entry) break;
563
566
finddir = finddir->next;
565
568
error (1, 0, _("Fatal goof\n"));
567
570
set_733((char *) s_entry->isorec.extent, finddir->extent);
578
581
* If this is . or .., then look up the relevant info from the
581
if(strcmp(s_entry->name,".") == 0)
584
if(strcmp(s_entry->name,".") == 0)
583
586
set_733((char *) s_entry->isorec.extent, dpnt->extent);
586
589
* Set these so that the hash table has the
587
590
* correct information
589
592
s_entry->starting_block = dpnt->extent;
590
593
s_entry->size = ROUND_UP(dpnt->size);
592
595
add_hash(s_entry);
593
596
s_entry->starting_block = dpnt->extent;
594
597
set_733((char *) s_entry->isorec.size, ROUND_UP(dpnt->size));
598
if(strcmp(s_entry->name,"..") == 0)
601
if(strcmp(s_entry->name,"..") == 0)
602
605
total_dir_size += root->size;
604
607
set_733((char *) s_entry->isorec.extent, dpnt->parent->extent);
607
610
* Set these so that the hash table has the
608
611
* correct information
610
613
s_entry->starting_block = dpnt->parent->extent;
611
614
s_entry->size = ROUND_UP(dpnt->parent->size);
613
616
add_hash(s_entry);
614
617
s_entry->starting_block = dpnt->parent->extent;
615
618
set_733((char *) s_entry->isorec.size, ROUND_UP(dpnt->parent->size));
620
623
* Some ordinary non-directory file. Just schedule the
621
624
* file to be written. This is all quite
622
625
* straightforward, just make a list and assign extents
624
627
* directories, we should be ready write out these
629
dwpnt = (struct deferred_write *)
632
dwpnt = (struct deferred_write *)
630
633
e_malloc(sizeof(struct deferred_write));
633
636
dw_tail->next = dwpnt;
641
if(s_entry->inode == TABLE_INODE)
644
if(s_entry->inode == TABLE_INODE)
643
646
dwpnt->table = s_entry->table;
644
647
dwpnt->name = NULL;
645
648
sprintf(whole_path,"%s%sTRANS.TBL",
646
649
s_entry->filedir->whole_name, SPATH_SEPARATOR);
650
653
dwpnt->table = NULL;
651
654
strcpy(whole_path, s_entry->whole_name);
669
672
fprintf (stderr, "Warning: large file %s\n", whole_path);
670
673
fprintf (stderr, "Starting block is %d\n", s_entry->starting_block);
671
674
fprintf (stderr, "Reported file size is %d extents\n", s_entry->size);
675
678
#ifdef NOT_NEEDED /* Never use this code if you like to create a DVD */
677
if(last_extent > (800000000 >> 11))
680
if(last_extent > (800000000 >> 11))
680
* More than 800Mb? Punt
683
* More than 800Mb? Punt
682
685
fprintf(stderr,"Extent overflow processing file %s\n", whole_path);
683
686
fprintf(stderr,"Starting block is %d\n", s_entry->starting_block);
750
753
struct directory_entry * s_entry;
751
754
struct directory_entry * s_entry_d;
752
755
unsigned int total_size;
754
757
total_size = (dpnt->size + (SECTOR_SIZE - 1)) & ~(SECTOR_SIZE - 1);
755
758
directory_buffer = (char *) e_malloc(total_size);
756
759
memset(directory_buffer, 0, total_size);
759
762
ce_size = (dpnt->ce_bytes + (SECTOR_SIZE - 1)) & ~(SECTOR_SIZE - 1);
760
763
ce_buffer = NULL;
764
767
ce_buffer = (char *) e_malloc(ce_size);
765
768
memset(ce_buffer, 0, ce_size);
770
* Absolute byte address of CE entries for this directory
773
* Absolute byte address of CE entries for this directory
772
775
ce_address = last_extent_written + (total_size >> 11);
773
776
ce_address = ce_address << 11;
776
779
s_entry = dpnt->contents;
779
782
/* skip if it's hidden */
780
783
if(s_entry->de_flags & INHIBIT_ISO9660_ENTRY) {
786
* We do not allow directory entries to cross sector boundaries.
787
* Simply pad, and then start the next entry at the next sector
789
* We do not allow directory entries to cross sector boundaries.
790
* Simply pad, and then start the next entry at the next sector
789
792
new_reclen = s_entry->isorec.length[0];
790
793
if( (dir_index & (SECTOR_SIZE - 1)) + new_reclen >= SECTOR_SIZE )
792
dir_index = (dir_index + (SECTOR_SIZE - 1)) &
795
dir_index = (dir_index + (SECTOR_SIZE - 1)) &
793
796
~(SECTOR_SIZE - 1);
796
memcpy(directory_buffer + dir_index, &s_entry->isorec,
799
memcpy(directory_buffer + dir_index, &s_entry->isorec,
797
800
sizeof(struct iso_directory_record) -
798
801
sizeof(s_entry->isorec.name) + s_entry->isorec.name_len[0]);
799
dir_index += sizeof(struct iso_directory_record) -
802
dir_index += sizeof(struct iso_directory_record) -
800
803
sizeof (s_entry->isorec.name)+ s_entry->isorec.name_len[0];
803
* Add the Rock Ridge attributes, if present
806
* Add the Rock Ridge attributes, if present
805
808
if(s_entry->rr_attr_size)
809
812
directory_buffer[dir_index++] = 0;
813
816
* If the RR attributes were too long, then write the
814
817
* CE records, as required.
816
if(s_entry->rr_attr_size != s_entry->total_rr_attr_size)
819
if(s_entry->rr_attr_size != s_entry->total_rr_attr_size)
818
821
unsigned char * pnt;
822
825
* Go through the entire record and fix up the CE entries
823
* so that the extent and offset are correct
826
* so that the extent and offset are correct
826
829
pnt = s_entry->rr_attributes;
827
830
len = s_entry->total_rr_attr_size;
834
837
ce_index, ce_address);
838
if(pnt[0] == 'C' && pnt[1] == 'E')
841
if(pnt[0] == 'C' && pnt[1] == 'E')
840
843
nbytes = get_733( (char *) pnt+20);
842
845
if((ce_index & (SECTOR_SIZE - 1)) + nbytes >=
845
848
ce_index = ROUND_UP(ce_index);
848
set_733( (char *) pnt+4,
851
set_733( (char *) pnt+4,
849
852
(ce_address + ce_index) >> 11);
850
set_733( (char *) pnt+12,
853
set_733( (char *) pnt+12,
851
854
(ce_address + ce_index) & (SECTOR_SIZE - 1));
855
* Now store the block in the ce buffer
858
* Now store the block in the ce buffer
857
memcpy(ce_buffer + ce_index,
860
memcpy(ce_buffer + ce_index,
858
861
pnt + pnt[2], nbytes);
859
862
ce_index += nbytes;
871
874
rockridge_size += s_entry->total_rr_attr_size;
872
memcpy(directory_buffer + dir_index, s_entry->rr_attributes,
875
memcpy(directory_buffer + dir_index, s_entry->rr_attributes,
873
876
s_entry->rr_attr_size);
874
877
dir_index += s_entry->rr_attr_size;
878
881
directory_buffer[dir_index++] = 0;
881
884
s_entry_d = s_entry;
882
885
s_entry = s_entry->next;
885
888
* Joliet doesn't use the Rock Ridge attributes, so we free it here.
887
if (s_entry_d->rr_attributes)
890
if (s_entry_d->rr_attributes)
889
892
free(s_entry_d->rr_attributes);
890
893
s_entry_d->rr_attributes = NULL;
912
915
last_extent_written += ce_size >> 11;
916
919
} /* generate_one_directory(... */
919
922
void FDECL1(build_pathlist, struct directory *, node)
921
924
struct directory * dpnt;
927
930
/* skip if it's hidden */
934
937
} /* build_pathlist(... */
936
static int FDECL2(compare_paths, void const *, r, void const *, l)
939
static int FDECL2(compare_paths, void const *, r, void const *, l)
938
941
struct directory const *ll = *(struct directory * const *)l;
939
942
struct directory const *rr = *(struct directory * const *)r;
946
if (rr->parent->path_index > ll->parent->path_index)
949
if (rr->parent->path_index > ll->parent->path_index)
951
954
return strcmp(rr->self->isorec.name, ll->self->isorec.name);
953
956
} /* compare_paths(... */
955
958
static int generate_path_tables()
968
* First allocate memory for the tables and initialize the memory
971
* First allocate memory for the tables and initialize the memory
970
973
tablesize = path_blocks << 11;
971
974
path_table_m = (char *) e_malloc(tablesize);
974
977
memset(path_table_m, 0, tablesize);
977
* Now start filling in the path tables. Start with root directory
980
* Now start filling in the path tables. Start with root directory
979
982
if( next_path_index > 0xffff )
985
988
path_table_index = 0;
986
pathlist = (struct directory **) e_malloc(sizeof(struct directory *)
989
pathlist = (struct directory **) e_malloc(sizeof(struct directory *)
987
990
* next_path_index);
988
991
memset(pathlist, 0, sizeof(struct directory *) * next_path_index);
989
992
build_pathlist(root);
995
qsort(&pathlist[1], next_path_index-1, sizeof(struct directory *),
998
qsort(&pathlist[1], next_path_index-1, sizeof(struct directory *),
996
999
(int (*)(const void *, const void *))compare_paths);
998
qsort(&pathlist[1], next_path_index-1, sizeof(struct directory *),
1001
qsort(&pathlist[1], next_path_index-1, sizeof(struct directory *),
1017
1020
error (1, 0, _("Entry %d not in path tables\n"), j);
1019
1022
npnt = dpnt->de_name;
1022
* So the root comes out OK
1025
* So the root comes out OK
1024
if( (*npnt == 0) || (dpnt == root) )
1027
if( (*npnt == 0) || (dpnt == root) )
1028
1031
npnt1 = strrchr(npnt, PATH_SEPARATOR);
1031
1034
npnt = npnt1 + 1;
1034
1037
de = dpnt->self;
1037
1040
error (1, 0, _("Fatal goof\n"));
1041
1044
namelen = de->isorec.name_len[0];
1043
1046
path_table_l[path_table_index] = namelen;
1044
1047
path_table_m[path_table_index] = namelen;
1045
1048
path_table_index += 2;
1047
set_731(path_table_l + path_table_index, dpnt->extent);
1048
set_732(path_table_m + path_table_index, dpnt->extent);
1050
set_731(path_table_l + path_table_index, dpnt->extent);
1051
set_732(path_table_m + path_table_index, dpnt->extent);
1049
1052
path_table_index += 4;
1051
set_721(path_table_l + path_table_index,
1052
dpnt->parent->path_index);
1053
set_722(path_table_m + path_table_index,
1054
dpnt->parent->path_index);
1054
set_721(path_table_l + path_table_index,
1055
dpnt->parent->path_index);
1056
set_722(path_table_m + path_table_index,
1057
dpnt->parent->path_index);
1055
1058
path_table_index += 2;
1057
1060
for(i =0; i<namelen; i++)
1059
1062
path_table_l[path_table_index] = de->isorec.name[i];
1060
1063
path_table_m[path_table_index] = de->isorec.name[i];
1061
1064
path_table_index++;
1063
if(path_table_index & 1)
1066
if(path_table_index & 1)
1065
1068
path_table_index++; /* For odd lengths we pad */
1069
1072
free(pathlist);
1070
1073
if(path_table_index != path_table_size)
1109
1112
* OK, all done with that crap. Now write out the directories.
1110
1113
* This is where the fur starts to fly, because we need to keep track of
1111
* each file as we find it and keep track of where we put it.
1114
* each file as we find it and keep track of where we put it.
1114
1117
should_write = last_extent - session_start;
1125
1128
fprintf(stderr,"Total directory extents being written = %llu\n", last_extent);
1128
fprintf (stderr, _("Total extents scheduled to be written = %llu\n"),
1131
fprintf (stderr, _("Total extents scheduled to be written = %llu\n"),
1129
1132
last_extent - session_start);
1133
* Now write all of the files that we need.
1136
* Now write all of the files that we need.
1135
1138
write_files(outfile);
1138
1141
* The rest is just fluff.
1145
1148
fprintf (stderr, _("Total extents actually written = %llu\n"),
1146
1149
last_extent_written - session_start);
1149
* Hard links throw us off here
1152
* Hard links throw us off here
1151
1154
assert (last_extent > session_start);
1152
1155
if(should_write + session_start != last_extent)
1194
1197
* This will break in the year 2000, I supose, but there is no good way
1195
* to get the top two digits of the year.
1198
* to get the top two digits of the year.
1197
1200
sprintf(iso_time, "%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d00", 1900 + local.tm_year,
1198
1201
local.tm_mon+1, local.tm_mday,
1204
1207
iso_time[16] = (local.tm_min + 60*(local.tm_hour + 24*local.tm_yday)) / 15;
1207
* Next we write out the primary descriptor for the disc
1210
* Next we write out the primary descriptor for the disc
1209
1212
memset(&vol_desc, 0, sizeof(vol_desc));
1210
1213
vol_desc.type[0] = ISO_VD_PRIMARY;
1211
1214
memcpy(vol_desc.id, ISO_STANDARD_ID, sizeof(ISO_STANDARD_ID));
1212
1215
vol_desc.version[0] = 1;
1214
1217
memset(vol_desc.system_id, ' ', sizeof(vol_desc.system_id));
1215
1218
memcpy_max(vol_desc.system_id, system_id, strlen(system_id));
1217
1220
memset(vol_desc.volume_id, ' ', sizeof(vol_desc.volume_id));
1218
1221
memcpy_max(vol_desc.volume_id, volume_id, strlen(volume_id));
1220
1223
should_write = last_extent - session_start;
1221
1224
set_733((char *) vol_desc.volume_space_size, should_write);
1222
1225
set_723(vol_desc.volume_set_size, volume_set_size);
1223
1226
set_723(vol_desc.volume_sequence_number, volume_sequence_number);
1224
1227
set_723(vol_desc.logical_block_size, 2048);
1227
1230
* The path tables are used by DOS based machines to cache directory
1231
1234
set_733((char *) vol_desc.path_table_size, path_table_size);
1235
1238
set_732(vol_desc.opt_type_m_path_table, path_table[3]);
1238
* Now we copy the actual root directory record
1241
* Now we copy the actual root directory record
1240
memcpy(vol_desc.root_directory_record, &root_record,
1243
memcpy(vol_desc.root_directory_record, &root_record,
1241
1244
sizeof(struct iso_directory_record) + 1);
1257
1260
if(appid) memcpy_max(vol_desc.application_id, appid, strlen(appid));
1259
1262
FILL_SPACE(copyright_file_id);
1260
if(copyright) memcpy_max(vol_desc.copyright_file_id, copyright,
1263
if(copyright) memcpy_max(vol_desc.copyright_file_id, copyright,
1261
1264
strlen(copyright));
1263
1266
FILL_SPACE(abstract_file_id);
1264
if(abstract) memcpy_max(vol_desc.abstract_file_id, abstract,
1267
if(abstract) memcpy_max(vol_desc.abstract_file_id, abstract,
1265
1268
strlen(abstract));
1267
1270
FILL_SPACE(bibliographic_file_id);
1268
if(biblio) memcpy_max(vol_desc.bibliographic_file_id, biblio,
1271
if(biblio) memcpy_max(vol_desc.bibliographic_file_id, biblio,
1269
1272
strlen(biblio));
1271
1274
FILL_SPACE(creation_date);
1281
1284
memcpy(vol_desc.effective_date, effective_date ? effective_date : iso_time, 17);
1284
* if not a bootable cd do it the old way
1287
* if not a bootable cd do it the old way
1286
1289
xfwrite(&vol_desc, 1, 2048, outfile);
1287
1290
last_extent_written++;
1296
1299
struct iso_primary_descriptor evol_desc;
1299
* Now write the end volume descriptor. Much simpler than the other one
1302
* Now write the end volume descriptor. Much simpler than the other one
1301
1304
memset(&evol_desc, 0, sizeof(evol_desc));
1302
1305
evol_desc.type[0] = ISO_VD_END;
1313
1316
static int FDECL1(pathtab_write, FILE *, outfile)
1316
* Next we write the path tables
1319
* Next we write the path tables
1318
1321
xfwrite(path_table_l, 1, path_blocks << 11, outfile);
1319
1322
xfwrite(path_table_m, 1, path_blocks << 11, outfile);
1421
1427
static int FDECL1(padblock_write, FILE *, outfile)
1426
memset(buffer, 0, sizeof(buffer));
1430
xfwrite(buffer, 1, sizeof(buffer), outfile);
1433
last_extent_written += 16;
1431
buffer = e_malloc (2048 * PADBLOCK_SIZE);
1432
memset (buffer, 0, 2048 * PADBLOCK_SIZE);
1434
if (use_embedded_boot)
1436
FILE *fp = fopen (boot_image_embed, "rb");
1438
error (1, errno, _("Unable to open %s"), boot_image_embed);
1440
fread (buffer, 2048 * PADBLOCK_SIZE, 1, fp);
1441
if (fgetc (fp) != EOF)
1442
error (1, 0, _("%s is too big for embed area"), boot_image_embed);
1445
if (use_protective_msdos_label)
1447
struct msdos_partition_mbr *mbr = (void *) buffer;
1449
memset (mbr->entries, 0, sizeof(mbr->entries));
1451
/* Some idiotic BIOSes refuse to boot if they don't find at least
1452
one partition with active bit set. */
1453
mbr->entries[0].flag = 0x80;
1455
/* Doesn't really matter, as long as it's non-zero. It seems that
1456
0xCD is used elsewhere, so we follow suit. */
1457
mbr->entries[0].type = 0xcd;
1459
/* Start immediately (sector 1). */
1460
mbr->entries[0].start = 1;
1462
/* We don't know yet. Let's keep it safe. */
1463
mbr->entries[0].length = UINT32_MAX;
1465
mbr->signature = MSDOS_PARTITION_SIGNATURE;
1468
xfwrite (buffer, 1, 2048 * PADBLOCK_SIZE, outfile);
1469
last_extent_written += PADBLOCK_SIZE;