Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | Add -d <directory> support to populate the image. |
| 2 | |
| 3 | Upstream-Status: Inappropriate [licensing] |
| 4 | We're tracking an old release of dosfstools due to licensing issues. |
| 5 | |
| 6 | Signed-off-by: Scott Garman <scott.a.garman@intel.com> |
| 7 | |
| 8 | Index: dosfstools-2.11/mkdosfs/mkdosfs.c |
| 9 | =================================================================== |
| 10 | --- dosfstools-2.11.orig/mkdosfs/mkdosfs.c 2011-12-06 12:27:55.000000000 +0000 |
| 11 | +++ dosfstools-2.11/mkdosfs/mkdosfs.c 2011-12-06 12:37:13.445950703 +0000 |
| 12 | @@ -18,6 +18,10 @@ |
| 13 | as a rule), and not the block. For example the boot block does not |
| 14 | occupy a full cluster. |
| 15 | |
| 16 | + June 2004 - Jordan Crouse (info.linux@amd.com) |
| 17 | + Added -d <directory> support to populate the image |
| 18 | + Copyright (C) 2004, Advanced Micro Devices, All Rights Reserved |
| 19 | + |
| 20 | Fixes/additions May 1998 by Roman Hodek |
| 21 | <Roman.Hodek@informatik.uni-erlangen.de>: |
| 22 | - Atari format support |
| 23 | @@ -71,6 +75,8 @@ |
| 24 | #include <unistd.h> |
| 25 | #include <time.h> |
| 26 | #include <errno.h> |
| 27 | +#include <libgen.h> |
| 28 | +#include <dirent.h> |
| 29 | |
| 30 | #include <linux/version.h> |
| 31 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) |
| 32 | @@ -110,6 +116,8 @@ |
| 33 | * sufficient (or even better :) for 64 bit offsets in the meantime */ |
| 34 | #define llseek lseek |
| 35 | |
| 36 | +#define ROUND_UP(value, divisor) (value + (divisor - (value % divisor))) / divisor |
| 37 | + |
| 38 | /* Constant definitions */ |
| 39 | |
| 40 | #define TRUE 1 /* Boolean constants */ |
| 41 | @@ -149,7 +157,6 @@ |
| 42 | #define ATTR_VOLUME 8 /* volume label */ |
| 43 | #define ATTR_DIR 16 /* directory */ |
| 44 | #define ATTR_ARCH 32 /* archived */ |
| 45 | - |
| 46 | #define ATTR_NONE 0 /* no attribute bits */ |
| 47 | #define ATTR_UNUSED (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN) |
| 48 | /* attribute bits that are copied "as is" */ |
| 49 | @@ -245,6 +252,19 @@ |
| 50 | __u32 reserved2[4]; |
| 51 | }; |
| 52 | |
| 53 | +/* This stores up to 13 chars of the name */ |
| 54 | + |
| 55 | +struct msdos_dir_slot { |
| 56 | + __u8 id; /* sequence number for slot */ |
| 57 | + __u8 name0_4[10]; /* first 5 characters in name */ |
| 58 | + __u8 attr; /* attribute byte */ |
| 59 | + __u8 reserved; /* always 0 */ |
| 60 | + __u8 alias_checksum; /* checksum for 8.3 alias */ |
| 61 | + __u8 name5_10[12]; /* 6 more characters in name */ |
| 62 | + __u16 start; /* starting cluster number, 0 in long slots */ |
| 63 | + __u8 name11_12[4]; /* last 2 characters in name */ |
| 64 | +}; |
| 65 | + |
| 66 | struct msdos_dir_entry |
| 67 | { |
| 68 | char name[8], ext[3]; /* name and extension */ |
| 69 | @@ -293,6 +313,15 @@ |
| 70 | |
| 71 | #define MESSAGE_OFFSET 29 /* Offset of message in above code */ |
| 72 | |
| 73 | +/* Special structure to keep track of directories as we add them for the -d option */ |
| 74 | + |
| 75 | +struct dir_entry { |
| 76 | + int root; /* Specifies if this is the root dir or not */ |
| 77 | + int count; /* Number of items in the table */ |
| 78 | + int entries; /* Number of entries in the table */ |
| 79 | + struct msdos_dir_entry *table; /* Pointer to the entry table */ |
| 80 | +}; |
| 81 | + |
| 82 | /* Global variables - the root of all evil :-) - see these and weep! */ |
| 83 | |
| 84 | static char *template_boot_code; /* Variable to store a full template boot sector in */ |
| 85 | @@ -326,6 +355,9 @@ |
| 86 | static int size_root_dir; /* Size of the root directory in bytes */ |
| 87 | static int sectors_per_cluster = 0; /* Number of sectors per disk cluster */ |
| 88 | static int root_dir_entries = 0; /* Number of root directory entries */ |
| 89 | +static int root_dir_num_entries = 0; |
| 90 | +static int last_cluster_written = 0; |
| 91 | + |
| 92 | static char *blank_sector; /* Blank sector - all zeros */ |
| 93 | static int hidden_sectors = 0; /* Number of hidden sectors */ |
| 94 | |
| 95 | @@ -399,7 +431,6 @@ |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | - |
| 100 | /* Mark a specified sector as having a particular value in it's FAT entry */ |
| 101 | |
| 102 | static void |
| 103 | @@ -1266,6 +1297,9 @@ |
| 104 | die ("unable to allocate space for root directory in memory"); |
| 105 | } |
| 106 | |
| 107 | + |
| 108 | + last_cluster_written = 2; |
| 109 | + |
| 110 | memset(root_dir, 0, size_root_dir); |
| 111 | if ( memcmp(volume_name, " ", 11) ) |
| 112 | { |
| 113 | @@ -1314,11 +1348,11 @@ |
| 114 | } |
| 115 | |
| 116 | if (!(blank_sector = malloc( sector_size ))) |
| 117 | - die( "Out of memory" ); |
| 118 | + die( "Out of memory" ); |
| 119 | + |
| 120 | memset(blank_sector, 0, sector_size); |
| 121 | } |
| 122 | - |
| 123 | - |
| 124 | + |
| 125 | /* Write the new filesystem's data tables to wherever they're going to end up! */ |
| 126 | |
| 127 | #define error(str) \ |
| 128 | @@ -1340,7 +1374,7 @@ |
| 129 | do { \ |
| 130 | int __size = (size); \ |
| 131 | if (write (dev, buf, __size) != __size) \ |
| 132 | - error ("failed whilst writing " errstr); \ |
| 133 | + error ("failed whilst writing " errstr); \ |
| 134 | } while(0) |
| 135 | |
| 136 | |
| 137 | @@ -1412,6 +1446,452 @@ |
| 138 | free (fat); /* Free up the fat table space reserved during setup_tables */ |
| 139 | } |
| 140 | |
| 141 | +/* Add a file to the specified directory entry, and also write it into the image */ |
| 142 | + |
| 143 | +static void copy_filename(char *filename, char *base, char *ext) { |
| 144 | + |
| 145 | + char *ch = filename; |
| 146 | + int i, len; |
| 147 | + |
| 148 | + memset(base, 0x20, 8); |
| 149 | + memset(ext, 0x20, 3); |
| 150 | + |
| 151 | + for(len = 0 ; *ch && *ch != '.'; ch++) { |
| 152 | + base[len++] = toupper(*ch); |
| 153 | + if (len == 8) break; |
| 154 | + } |
| 155 | + |
| 156 | + for ( ; *ch && *ch != '.'; ch++); |
| 157 | + if (*ch) ch++; |
| 158 | + |
| 159 | + for(len = 0 ; *ch; ch++) { |
| 160 | + ext[len++] = toupper(*ch); |
| 161 | + if (len == 3) break; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +/* Check for an .attrib.<filename> file, and read the attributes therein */ |
| 166 | + |
| 167 | +/* We are going to be pretty pedantic about this. The file needs 3 |
| 168 | + bytes at the beginning, the attributes are listed in this order: |
| 169 | + |
| 170 | + (H)idden|(S)ystem|(A)rchived |
| 171 | + |
| 172 | + A capital HSA means to enable it, anything else will disable it |
| 173 | + (I recommend a '-') The unix user attributes will still be used |
| 174 | + for write access. |
| 175 | + |
| 176 | + For example, to enable system file access for ldlinux.sys, write |
| 177 | + the following to .attrib.ldlinux.sys: -S- |
| 178 | +*/ |
| 179 | + |
| 180 | +unsigned char check_attrib_file(char *dir, char *filename) { |
| 181 | + |
| 182 | + char attrib[4] = { '-', '-', '-' }; |
| 183 | + unsigned char *buffer = 0; |
| 184 | + int ret = ATTR_NONE; |
| 185 | + int fd = -1; |
| 186 | + |
| 187 | + buffer = (char *) calloc(1, strlen(dir) + strlen(filename) + 10); |
| 188 | + if (!buffer) return ATTR_NONE; |
| 189 | + |
| 190 | + sprintf(buffer, "%s/.attrib.%s", dir, filename); |
| 191 | + |
| 192 | + if (access(buffer, R_OK)) |
| 193 | + goto exit_attrib; |
| 194 | + |
| 195 | + if ((fd = open(buffer, O_RDONLY, 0)) < 0) |
| 196 | + goto exit_attrib; |
| 197 | + |
| 198 | + if (read(fd, attrib, 3) < 0) |
| 199 | + goto exit_attrib; |
| 200 | + |
| 201 | + if (attrib[0] == 'H') ret |= ATTR_HIDDEN; |
| 202 | + if (attrib[1] == 'S') ret |= ATTR_SYS; |
| 203 | + if (attrib[2] == 'A') ret |= ATTR_ARCH; |
| 204 | + |
| 205 | + printf("%s: Setting atrribute %x\n", filename, ret); |
| 206 | + |
| 207 | + exit_attrib: |
| 208 | + if (fd >= 0) close(fd); |
| 209 | + if (buffer) free(buffer); |
| 210 | + |
| 211 | + return ret; |
| 212 | +} |
| 213 | + |
| 214 | +static void copy_name(char *buffer, int size, char **pointer) { |
| 215 | + int i; |
| 216 | + |
| 217 | + for(i = 0; i < size; i += 2) { |
| 218 | + if (*pointer) { |
| 219 | + buffer[i] = **pointer; |
| 220 | + buffer[i + 1] = 0x00; |
| 221 | + *pointer = **pointer ? *pointer + 1 : 0; |
| 222 | + } |
| 223 | + else { |
| 224 | + buffer[i] = 0xFF; |
| 225 | + buffer[i + 1] = 0xFF; |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +static int add_file(char *filename, struct dir_entry *dir, unsigned char attr) |
| 231 | +{ |
| 232 | + struct stat stat; |
| 233 | + struct msdos_dir_entry *entry; |
| 234 | + int infile = 0; |
| 235 | + int sectors, clusters; |
| 236 | + struct tm *ctime; |
| 237 | + int c, s; |
| 238 | + int ptr; |
| 239 | + char *buffer, *base; |
| 240 | + int start; |
| 241 | + int usedsec, totalsec; |
| 242 | + |
| 243 | + char name83[8], ext83[3]; |
| 244 | + |
| 245 | + struct msdos_dir_slot *slot; |
| 246 | + int i; |
| 247 | + char *p; |
| 248 | + |
| 249 | + /* The root directory is static, everything else grows as needed */ |
| 250 | + |
| 251 | + if (dir->root) { |
| 252 | + if (dir->count == dir->entries) { |
| 253 | + printf("Error - too many directory entries\n"); |
| 254 | + } |
| 255 | + } |
| 256 | + else { |
| 257 | + if (dir->count == dir->entries) { |
| 258 | + if (!dir->table) |
| 259 | + dir->table = |
| 260 | + (struct msdos_dir_entry *) malloc(sizeof(struct msdos_dir_entry)); |
| 261 | + else { |
| 262 | + dir->table = |
| 263 | + (struct msdos_dir_entry *) realloc(dir->table, (dir->entries + 1) * |
| 264 | + sizeof(struct msdos_dir_entry)); |
| 265 | + |
| 266 | + memset(&dir->table[dir->entries], 0, sizeof(struct msdos_dir_entry)); |
| 267 | + } |
| 268 | + |
| 269 | + dir->entries++; |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + infile = open(filename, O_RDONLY, 0); |
| 274 | + if (!infile) return; |
| 275 | + |
| 276 | + if (fstat(infile, &stat)) |
| 277 | + goto exit_add; |
| 278 | + |
| 279 | + if (S_ISCHR(stat.st_mode) ||S_ISBLK(stat.st_mode) || |
| 280 | + S_ISFIFO(stat.st_mode) || S_ISLNK(stat.st_mode)) { |
| 281 | + printf("Error - cannot create a special file in a FATFS\n"); |
| 282 | + goto exit_add; |
| 283 | + } |
| 284 | + |
| 285 | + /* FIXME: This isn't very pretty */ |
| 286 | + |
| 287 | + usedsec = start_data_sector + (size_root_dir / sector_size) + |
| 288 | + (last_cluster_written * bs.cluster_size); |
| 289 | + |
| 290 | + totalsec = blocks * BLOCK_SIZE / sector_size; |
| 291 | + |
| 292 | + /* Figure out how many sectors / clustors the file requires */ |
| 293 | + |
| 294 | + sectors = ROUND_UP(stat.st_size, sector_size); |
| 295 | + clusters = ROUND_UP(sectors, (int) bs.cluster_size); |
| 296 | + |
| 297 | + if (usedsec + sectors > totalsec) { |
| 298 | + printf("Error - %s is too big (%d vs %d)\n", filename, sectors, totalsec - usedsec); |
| 299 | + close(infile); |
| 300 | + return -1; |
| 301 | + } |
| 302 | + |
| 303 | + printf("ADD %s\n", filename); |
| 304 | + |
| 305 | + /* Grab the basename of the file */ |
| 306 | + base = basename(filename); |
| 307 | + |
| 308 | + /* Extract out the 8.3 name */ |
| 309 | + copy_filename(base, name83, ext83); |
| 310 | + |
| 311 | + /* Make an extended name slot */ |
| 312 | + |
| 313 | + slot = (struct msdos_dir_slot *) &dir->table[dir->count++]; |
| 314 | + slot->id = 'A'; |
| 315 | + slot->attr = 0x0F; |
| 316 | + slot->reserved = 0; |
| 317 | + slot->start = 0; |
| 318 | + |
| 319 | + slot->alias_checksum = 0; |
| 320 | + |
| 321 | + for(i = 0; i < 8; i++) |
| 322 | + slot->alias_checksum = (((slot->alias_checksum&1)<<7)|((slot->alias_checksum&0xfe)>>1)) + name83[i]; |
| 323 | + |
| 324 | + for(i = 0; i < 3; i++) |
| 325 | + slot->alias_checksum = (((slot->alias_checksum&1)<<7)|((slot->alias_checksum&0xfe)>>1)) + ext83[i]; |
| 326 | + |
| 327 | + p = base; |
| 328 | + |
| 329 | + copy_name(slot->name0_4, 10, &p); |
| 330 | + copy_name(slot->name5_10, 12, &p); |
| 331 | + copy_name(slot->name11_12, 4, &p); |
| 332 | + |
| 333 | + |
| 334 | + /* Get the entry from the root filesytem */ |
| 335 | + entry = &dir->table[dir->count++]; |
| 336 | + |
| 337 | + strncpy(entry->name, name83, 8); |
| 338 | + strncpy(entry->ext, ext83, 3); |
| 339 | + |
| 340 | + |
| 341 | + /* If the user has it read only, then add read only to the incoming |
| 342 | + attribute settings */ |
| 343 | + |
| 344 | + if (!(stat.st_mode & S_IWUSR)) attr |= ATTR_RO; |
| 345 | + entry->attr = attr; |
| 346 | + |
| 347 | + /* Set the access time on the file */ |
| 348 | + ctime = localtime(&create_time); |
| 349 | + |
| 350 | + entry->time = CT_LE_W((unsigned short)((ctime->tm_sec >> 1) + |
| 351 | + (ctime->tm_min << 5) + (ctime->tm_hour << 11))); |
| 352 | + |
| 353 | + entry->date = CT_LE_W((unsigned short)(ctime->tm_mday + |
| 354 | + ((ctime->tm_mon+1) << 5) + |
| 355 | + ((ctime->tm_year-80) << 9))); |
| 356 | + |
| 357 | + entry->ctime_ms = 0; |
| 358 | + entry->ctime = entry->time; |
| 359 | + entry->cdate = entry->date; |
| 360 | + entry->adate = entry->date; |
| 361 | + entry->size = stat.st_size; |
| 362 | + |
| 363 | + start = last_cluster_written; |
| 364 | + |
| 365 | + entry->start = CT_LE_W(start); /* start sector */ |
| 366 | + entry->starthi = CT_LE_W((start & 0xFFFF0000) >> 16); /* High start sector (for FAT32) */ |
| 367 | + |
| 368 | + /* We mark all of the clusters we use in the FAT */ |
| 369 | + |
| 370 | + for(c = 0; c < clusters; c++ ) { |
| 371 | + int free; |
| 372 | + int next = c == (clusters - 1) ? FAT_EOF : start + c + 1; |
| 373 | + mark_FAT_cluster(start + c, next); |
| 374 | + last_cluster_written++; |
| 375 | + } |
| 376 | + |
| 377 | + /* This confused me too - cluster 2 starts after the |
| 378 | + root directory data - search me as to why */ |
| 379 | + |
| 380 | + ptr = (start_data_sector * sector_size) + size_root_dir; |
| 381 | + ptr += (start - 2) * bs.cluster_size * sector_size; |
| 382 | + |
| 383 | + buffer = (char *) malloc(sector_size); |
| 384 | + |
| 385 | + if (!buffer) { |
| 386 | + printf("Error - couldn't allocate memory\n"); |
| 387 | + goto exit_add; |
| 388 | + } |
| 389 | + |
| 390 | + /* Write the file into the file block */ |
| 391 | + |
| 392 | + seekto(ptr, "datafile"); |
| 393 | + |
| 394 | + while(1) { |
| 395 | + int size = read(infile, buffer, sector_size); |
| 396 | + if (size <= 0) break; |
| 397 | + |
| 398 | + writebuf(buffer, size, "data"); |
| 399 | + } |
| 400 | + |
| 401 | + exit_add: |
| 402 | + if (infile) close(infile); |
| 403 | +} |
| 404 | + |
| 405 | +/* Add a new directory to the specified directory entry, and in turn populate |
| 406 | + it with its own files */ |
| 407 | + |
| 408 | +/* FIXME: This should check to make sure there is enough size to add itself */ |
| 409 | + |
| 410 | +static void add_directory(char *filename, struct dir_entry *dir) { |
| 411 | + |
| 412 | + struct dir_entry *newdir = 0; |
| 413 | + struct msdos_dir_entry *entry; |
| 414 | + struct tm *ctime; |
| 415 | + DIR *rddir = opendir(filename); |
| 416 | + struct dirent *dentry = 0; |
| 417 | + int remain; |
| 418 | + char *data; |
| 419 | + |
| 420 | + /* If the directory doesn't exist */ |
| 421 | + if (!rddir) return; |
| 422 | + |
| 423 | + if (dir->root) { |
| 424 | + if (dir->count == dir->entries) { |
| 425 | + printf("Error - too many directory entries\n"); |
| 426 | + goto exit_add_dir; |
| 427 | + } |
| 428 | + } |
| 429 | + else { |
| 430 | + if (dir->count == dir->entries) { |
| 431 | + if (!dir->table) |
| 432 | + dir->table = (struct msdos_dir_entry *) malloc(sizeof(struct msdos_dir_entry)); |
| 433 | + else { |
| 434 | + dir->table = (struct msdos_dir_entry *) realloc(dir->table, (dir->entries + 1) * |
| 435 | + sizeof(struct msdos_dir_entry)); |
| 436 | + |
| 437 | + /* Zero it out to avoid issues */ |
| 438 | + memset(&dir->table[dir->entries], 0, sizeof(struct msdos_dir_entry)); |
| 439 | + } |
| 440 | + dir->entries++; |
| 441 | + } |
| 442 | + } |
| 443 | + |
| 444 | + /* Now, create a new directory entry for the new directory */ |
| 445 | + newdir = (struct dir_entry *) calloc(1, sizeof(struct dir_entry)); |
| 446 | + if (!newdir) goto exit_add_dir; |
| 447 | + |
| 448 | + entry = &dir->table[dir->count++]; |
| 449 | + |
| 450 | + strncpy(entry->name, basename(filename), sizeof(entry->name)); |
| 451 | + |
| 452 | + entry->attr = ATTR_DIR; |
| 453 | + ctime = localtime(&create_time); |
| 454 | + |
| 455 | + entry->time = CT_LE_W((unsigned short)((ctime->tm_sec >> 1) + |
| 456 | + (ctime->tm_min << 5) + (ctime->tm_hour << 11))); |
| 457 | + |
| 458 | + entry->date = CT_LE_W((unsigned short)(ctime->tm_mday + |
| 459 | + ((ctime->tm_mon+1) << 5) + |
| 460 | + ((ctime->tm_year-80) << 9))); |
| 461 | + |
| 462 | + entry->ctime_ms = 0; |
| 463 | + entry->ctime = entry->time; |
| 464 | + entry->cdate = entry->date; |
| 465 | + entry->adate = entry->date; |
| 466 | + |
| 467 | + /* Now, read the directory */ |
| 468 | + |
| 469 | + while((dentry = readdir(rddir))) { |
| 470 | + struct stat st; |
| 471 | + char *buffer; |
| 472 | + |
| 473 | + if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) |
| 474 | + continue; |
| 475 | + |
| 476 | + /* DOS wouldn't like a typical unix . (dot) file, so we skip those too */ |
| 477 | + if (dentry->d_name[0] == '.') continue; |
| 478 | + |
| 479 | + buffer = malloc(strlen(filename) + strlen(dentry->d_name) + 3); |
| 480 | + if (!buffer) continue; |
| 481 | + |
| 482 | + sprintf(buffer, "%s/%s", filename, dentry->d_name); |
| 483 | + if (!stat(buffer, &st)) { |
| 484 | + if (S_ISDIR(st.st_mode)) |
| 485 | + add_directory(buffer, newdir); |
| 486 | + else if (S_ISREG(st.st_mode)) { |
| 487 | + unsigned char attrib = check_attrib_file(filename, dentry->d_name); |
| 488 | + add_file(buffer, newdir, attrib); |
| 489 | + } |
| 490 | + } |
| 491 | + |
| 492 | + free(buffer); |
| 493 | + } |
| 494 | + |
| 495 | + /* Now that the entire directory has been written, go ahead and write the directory |
| 496 | + entry as well */ |
| 497 | + |
| 498 | + entry->start = CT_LE_W(last_cluster_written); |
| 499 | + entry->starthi = CT_LE_W((last_cluster_written & 0xFFFF0000) >> 16); |
| 500 | + entry->size = newdir->count * sizeof(struct msdos_dir_entry); |
| 501 | + |
| 502 | + remain = entry->size; |
| 503 | + data = (char *) newdir->table; |
| 504 | + |
| 505 | + while(remain) { |
| 506 | + int size = |
| 507 | + remain > bs.cluster_size * sector_size ? bs.cluster_size * sector_size : remain; |
| 508 | + |
| 509 | + int pos = (start_data_sector * sector_size) + size_root_dir; |
| 510 | + pos += (last_cluster_written - 2) * bs.cluster_size * sector_size; |
| 511 | + |
| 512 | + seekto(pos, "add_dir"); |
| 513 | + writebuf(data, size, "add_dir"); |
| 514 | + |
| 515 | + remain -= size; |
| 516 | + data += size; |
| 517 | + |
| 518 | + mark_FAT_cluster(last_cluster_written, remain ? last_cluster_written + 1 : FAT_EOF); |
| 519 | + last_cluster_written++; |
| 520 | + } |
| 521 | + |
| 522 | + exit_add_dir: |
| 523 | + if (rddir) closedir(rddir); |
| 524 | + if (newdir->table) free(newdir->table); |
| 525 | + if (newdir) free(newdir); |
| 526 | +} |
| 527 | + |
| 528 | +/* Given a directory, add all the files and directories to the root directory of the |
| 529 | + image. |
| 530 | +*/ |
| 531 | + |
| 532 | +static void add_root_directory(char *dirname) |
| 533 | +{ |
| 534 | + DIR *dir = opendir(dirname); |
| 535 | + struct dirent *entry = 0; |
| 536 | + struct dir_entry *newdir = 0; |
| 537 | + |
| 538 | + if (!dir) { |
| 539 | + printf("Error - directory %s does not exist\n", dirname); |
| 540 | + return; |
| 541 | + } |
| 542 | + |
| 543 | + /* Create the root directory structure - this is a bit different then |
| 544 | + above, because the table already exists, we just refer to it. */ |
| 545 | + |
| 546 | + newdir = (struct dir_entry *) calloc(1,sizeof(struct dir_entry)); |
| 547 | + |
| 548 | + if (!newdir) { |
| 549 | + closedir(dir); |
| 550 | + return; |
| 551 | + } |
| 552 | + |
| 553 | + newdir->entries = root_dir_entries; |
| 554 | + newdir->root = 1; |
| 555 | + newdir->count = 0; |
| 556 | + newdir->table = root_dir; |
| 557 | + |
| 558 | + while((entry = readdir(dir))) { |
| 559 | + struct stat st; |
| 560 | + char *buffer; |
| 561 | + |
| 562 | + if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) |
| 563 | + continue; |
| 564 | + |
| 565 | + /* DOS wouldn't like a typical unix . (dot) file, so we skip those too */ |
| 566 | + if (entry->d_name[0] == '.') continue; |
| 567 | + |
| 568 | + buffer = malloc(strlen(dirname) + strlen(entry->d_name) + 3); |
| 569 | + if (!buffer) continue; |
| 570 | + |
| 571 | + sprintf(buffer, "%s/%s", dirname, entry->d_name); |
| 572 | + if (!stat(buffer, &st)) { |
| 573 | + if (S_ISDIR(st.st_mode)) |
| 574 | + add_directory(buffer, newdir); |
| 575 | + else if (S_ISREG(st.st_mode)) { |
| 576 | + unsigned char attrib = check_attrib_file(dirname, entry->d_name); |
| 577 | + add_file(buffer, newdir, attrib); |
| 578 | + } |
| 579 | + } |
| 580 | + |
| 581 | + free(buffer); |
| 582 | + } |
| 583 | + |
| 584 | + closedir(dir); |
| 585 | + if (newdir) free(newdir); |
| 586 | +} |
| 587 | |
| 588 | /* Report the command usage and return a failure error code */ |
| 589 | |
| 590 | @@ -1423,7 +1903,7 @@ |
| 591 | [-m boot-msg-file] [-n volume-name] [-i volume-id] [-B bootcode]\n\ |
| 592 | [-s sectors-per-cluster] [-S logical-sector-size] [-f number-of-FATs]\n\ |
| 593 | [-h hidden-sectors] [-F fat-size] [-r root-dir-entries] [-R reserved-sectors]\n\ |
| 594 | - /dev/name [blocks]\n"); |
| 595 | + [-d directory] /dev/name [blocks]\n"); |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | @@ -1463,6 +1943,8 @@ |
| 600 | int c; |
| 601 | char *tmp; |
| 602 | char *listfile = NULL; |
| 603 | + char *dirname = NULL; |
| 604 | + |
| 605 | FILE *msgfile; |
| 606 | struct stat statbuf; |
| 607 | int i = 0, pos, ch; |
| 608 | @@ -1483,7 +1965,7 @@ |
| 609 | printf ("%s " VERSION " (" VERSION_DATE ")\n", |
| 610 | program_name); |
| 611 | |
| 612 | - while ((c = getopt (argc, argv, "AbcCf:F:Ii:l:m:n:r:R:s:S:v:B:")) != EOF) |
| 613 | + while ((c = getopt (argc, argv, "AbcCd:f:F:Ii:l:m:n:r:R:s:S:v:B:")) != EOF) |
| 614 | /* Scan the command line for options */ |
| 615 | switch (c) |
| 616 | { |
| 617 | @@ -1508,6 +1990,10 @@ |
| 618 | create = TRUE; |
| 619 | break; |
| 620 | |
| 621 | + case 'd': |
| 622 | + dirname = optarg; |
| 623 | + break; |
| 624 | + |
| 625 | case 'f': /* f : Choose number of FATs */ |
| 626 | nr_fats = (int) strtol (optarg, &tmp, 0); |
| 627 | if (*tmp || nr_fats < 1 || nr_fats > 4) |
| 628 | @@ -1811,8 +2297,10 @@ |
| 629 | else if (listfile) |
| 630 | get_list_blocks (listfile); |
| 631 | |
| 632 | - write_tables (); /* Write the file system tables away! */ |
| 633 | |
| 634 | + if (dirname) add_root_directory(dirname); |
| 635 | + |
| 636 | + write_tables (); /* Write the file system tables away! */ |
| 637 | exit (0); /* Terminate with no errors! */ |
| 638 | } |
| 639 | |