home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / disk / mkisofs-1.00.5.lha / mkisofs / tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-29  |  25.8 KB  |  918 lines

  1. /*
  2.  * File tree.c - scan directory  tree and build memory structures for iso9660
  3.  * filesystem
  4.  
  5.    Written by Eric Youngdale (1993).
  6.  
  7.    Copyright 1993 Yggdrasil Computing, Incorporated
  8.  
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 2, or (at your option)
  12.    any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <errno.h>
  27.  
  28. #ifndef VMS
  29. #ifndef AMIGA
  30. #include <unistd.h>
  31. #ifndef __QNX__
  32. #include <sys/sysmacros.h>
  33. #endif
  34. #endif
  35. #endif
  36.  
  37. #include "mkisofs.h"
  38. #include "iso9660.h"
  39.  
  40. #ifndef AMIGA
  41. #include <sys/stat.h>
  42. #endif
  43.  
  44. #ifdef AMIGA
  45. #include <exec/memory.h>
  46. #ifdef LATTICE
  47. #include <proto/exec.h>
  48. #endif
  49. #if defined(__GNUC__) || defined(AZTEC_C)
  50. #include <clib/exec_protos.h>
  51. #endif
  52. #endif
  53.  
  54. #include "exclude.h"
  55.  
  56. #ifdef VMS
  57. #define S_ISLNK(m)    (0)
  58. #define S_ISSOCK(m)    (0)
  59. #define S_ISFIFO(m)    (0)
  60. #else
  61. #ifndef S_ISLNK
  62. #define S_ISLNK(m)    (((m) & S_IFMT) == S_IFLNK)
  63. #endif
  64. #ifndef S_ISSOCK
  65. #define S_ISSOCK(m)    (((m) & S_IFMT) == S_IFSOCK)
  66. #endif
  67. #endif
  68.  
  69. #ifdef __svr4__
  70. extern char * strdup(const char *);
  71. #endif
  72.  
  73. static unsigned char symlink_buff[256];
  74.  
  75. extern int verbose;
  76.  
  77. struct stat fstatbuf = {0,};  /* We use this for the artificial entries we create */
  78.  
  79. struct stat root_statbuf = {0, };  /* Stat buffer for root directory */
  80.  
  81. struct directory * reloc_dir = NULL;
  82.  
  83. unsigned long pr_dir_count = 0;
  84. unsigned long pr_file_count = 0;
  85.  
  86. void FDECL1(sort_n_finish, struct directory *, this_dir)
  87. {
  88.   struct directory_entry  *s_entry, *s_entry1;
  89.   time_t current_time;
  90.   struct directory_entry * table;
  91.   int count;
  92.   int new_reclen;
  93.   char *  c;
  94.   int tablesize = 0;
  95.   char newname[15],  rootname[15];
  96.  
  97.   /* Here we can take the opportunity to toss duplicate entries from the
  98.      directory.  */
  99.  
  100.   table = NULL;
  101.  
  102.   if(fstatbuf.st_ctime == 0){
  103.       time (¤t_time);
  104.       fstatbuf.st_uid = 0;
  105.       fstatbuf.st_gid = 0;
  106.       fstatbuf.st_ctime = current_time;
  107.       fstatbuf.st_mtime = current_time;
  108.       fstatbuf.st_atime = current_time;
  109.   };
  110.  
  111.   flush_file_hash();
  112.   s_entry = this_dir->contents;
  113.   while(s_entry){
  114.       
  115.       /* First assume no conflict, and handle this case */
  116.       
  117.       if(!(s_entry1 = find_file_hash(s_entry->isorec.iso_name))){
  118.           add_file_hash(s_entry);
  119.           s_entry = s_entry->next;
  120.           continue;
  121.       };
  122.       
  123.       if (!convert_filenames) {
  124.         fprintf (stderr, "Duplicate filename: %s\n", s_entry->isorec.iso_name);
  125.         fprintf (stderr, "Filenames must be shorter than 30 characters!\n");
  126.         exit (1);
  127.       }
  128.  
  129.           if (s_entry->isorec.name_len[0] == 1 &&
  130.           s_entry->isorec.iso_name[0] < 2) {
  131.         fprintf (stderr, "Fatal error: duplicate dot or dotdot entry.\n");
  132.         exit (1);
  133.       }
  134.  
  135.       if(s_entry1 == s_entry){
  136.           fprintf(stderr,"Fatal goof\n");
  137.           exit(1);
  138.       };
  139.       /* OK, handle the conflicts.  Try substitute names until we come
  140.          up with a winner */
  141.       strcpy(rootname, s_entry->isorec.iso_name);
  142.       c  = strchr(rootname, '.');
  143.       if (c) *c = 0;
  144.       count = 0;
  145.       while(count < 1000){
  146.           sprintf(newname,"%s.%3.3d%s", rootname,  count,
  147.               (s_entry->isorec.flags[0] == 2 ? "" : ";1"));
  148.           if(!find_file_hash(newname)) break;
  149.           count++;
  150.       };
  151.       if(count >= 1000){
  152.           fprintf(stderr,"Unable to  generate unique  name for file %s\n", s_entry->name);
  153.           exit(1);
  154.       };
  155.       
  156.       /* OK, now we have a good replacement name.  Now decide which one
  157.          of these two beasts should get the name changed */
  158.       
  159.       if(s_entry->priority < s_entry1->priority) {
  160.           fprintf(stderr,"Using %s for  %s/%s (%s)\n", newname,  this_dir->whole_name, s_entry->name, s_entry1->name);
  161.           s_entry->isorec.name_len[0] =  strlen(newname);
  162.           new_reclen = ISO_REC_SIZE + strlen(newname);
  163.           if(use_RockRidge) {
  164.               if (new_reclen & 1) new_reclen++;  /* Pad to an even byte */
  165.               new_reclen += s_entry->rr_attr_size;
  166.           };
  167.           if (new_reclen & 1) new_reclen++;  /* Pad to an even byte */
  168.           s_entry->isorec.length[0] = new_reclen;
  169.           s_entry->isorec.iso_name = strdup (newname);
  170.       } else {
  171.           delete_file_hash(s_entry1);
  172.           fprintf(stderr,"Using %s for  %s/%s (%s)\n", newname,  this_dir->whole_name, s_entry1->name, s_entry->name);
  173.           s_entry1->isorec.name_len[0] =  strlen(newname);
  174.           new_reclen = ISO_REC_SIZE + strlen(newname);
  175.           if(use_RockRidge) {
  176.               if (new_reclen & 1) new_reclen++;  /* Pad to an even byte */
  177.               new_reclen += s_entry1->rr_attr_size;
  178.           };
  179.           if (new_reclen & 1) new_reclen++;  /* Pad to an even byte */
  180.           s_entry1->isorec.length[0] = new_reclen;
  181.           s_entry1->isorec.iso_name = strdup (newname);
  182.           add_file_hash(s_entry1);
  183.       };
  184.       add_file_hash(s_entry);
  185.       s_entry = s_entry->next;
  186.   };
  187.  
  188.   if(generate_tables && !find_file_hash("TRANS.TBL;1") && (reloc_dir != this_dir)){
  189.       /* First we need to figure out how big this table is */
  190.       for (s_entry = this_dir->contents; s_entry; s_entry = s_entry->next){
  191.           if(strcmp(s_entry->name, ".") == 0  ||
  192.              strcmp(s_entry->name, "..") == 0) continue; 
  193.           if(s_entry->table) tablesize += 35 + strlen(s_entry->table);
  194.       };
  195.           table = (struct directory_entry *) 
  196.         malloc(sizeof (struct directory_entry));
  197.      memset(table, 0, sizeof(struct directory_entry));
  198.     table->table = NULL;
  199.     table->next = this_dir->contents;
  200.     this_dir->contents = table;
  201.  
  202.     table->filedir = root;
  203.     table->isorec.flags[0] = 0;
  204.     table->priority  = 32768;
  205.     iso9660_date(table->isorec.date, current_time);
  206. #ifdef AMIGA
  207.     table->is_a_table = 1;
  208. #else
  209.     table->inode = TABLE_INODE;
  210.     table->dev = UNCACHED_DEVICE;
  211. #endif
  212.     set_723(table->isorec.volume_sequence_number, 1);
  213.     set_733(table->isorec.size, tablesize);
  214.     table->size = tablesize;
  215.     table->filedir = this_dir;
  216.     table->name = strdup("<translation table>");
  217.     table->table = (char *) malloc(ROUND_UP(tablesize));
  218.     memset(table->table, 0, ROUND_UP(tablesize));
  219.     iso9660_file_length  ("TRANS.TBL;1", table, 1);
  220.         
  221.     if(use_RockRidge){
  222.         fstatbuf.st_mode = 0444 | S_IFREG;
  223.         fstatbuf.st_nlink = 1;
  224.         generate_rock_ridge_attributes("",
  225.                            "TRANS.TBL", table,
  226.                            &fstatbuf, &fstatbuf, 0);
  227.     };
  228.   };
  229.  
  230.   for(s_entry = this_dir->contents; s_entry; s_entry = s_entry->next){
  231.       new_reclen = strlen (s_entry->isorec.iso_name);
  232.       
  233.       if(s_entry->isorec.flags[0] ==  2){
  234.           if (strcmp(s_entry->name,".") && strcmp(s_entry->name,"..")) {
  235.                /* path_table_size += new_reclen + sizeof(struct iso_path_table) - 1; */
  236.               path_table_size += new_reclen + 8;
  237.               if (new_reclen & 1) path_table_size++;
  238.           } else {
  239.               new_reclen = 1;
  240.               if (this_dir == root && strlen(s_entry->name) == 1)
  241.                 /* path_table_size += sizeof(struct iso_path_table); */
  242.                 path_table_size += 9;
  243.           }
  244.       };
  245.       if(path_table_size & 1) path_table_size++;  /* For odd lengths we pad */
  246.       s_entry->isorec.name_len[0] = new_reclen;
  247.  
  248.       new_reclen += ISO_REC_SIZE;
  249.       
  250.       if (new_reclen & 1)    
  251.           new_reclen++;
  252.       if(use_RockRidge){
  253.           new_reclen += s_entry->rr_attr_size;
  254.  
  255.           if (new_reclen & 1)    
  256.               new_reclen++;
  257.       };
  258.       if(new_reclen > 0xff) {
  259.           fprintf(stderr,"Fatal error - RR overflow for file %s\n",
  260.               s_entry->name);
  261.           exit(1);
  262.       };
  263.       s_entry->isorec.length[0] = new_reclen;
  264.   };
  265.  
  266.   sort_directory(&this_dir->contents);
  267.  
  268.   if(table){
  269.       char buffer[1024];
  270.       count = 0;
  271.       for (s_entry = this_dir->contents; s_entry; s_entry = s_entry->next){
  272.           if(s_entry == table) continue;
  273.           if(!s_entry->table) continue;
  274.           if(strcmp(s_entry->name, ".") == 0  ||
  275.              strcmp(s_entry->name, "..") == 0) continue;
  276.  
  277.           sprintf(buffer,"%c %-34s%s",s_entry->table[0],  
  278.               s_entry->isorec.iso_name, s_entry->table+1);
  279.           memcpy(table->table + count, buffer, strlen(buffer));
  280.           count +=  strlen(buffer);
  281.           free(s_entry->table);
  282.           s_entry->table = NULL;
  283.     };
  284.     if(count !=  tablesize) {
  285.          fprintf(stderr,"Translation table size mismatch %d %d\n",
  286.             count, tablesize);
  287.         exit(1);
  288.     };
  289.   };
  290.  
  291.   /* Now go through the directory and figure out how large this one will be.
  292.      Do not split a directory entry across a sector boundary */
  293.   
  294.   s_entry = this_dir->contents;
  295.   while(s_entry){
  296.       new_reclen = s_entry->isorec.length[0];
  297.       if ((this_dir->size & (SECTOR_SIZE - 1)) + new_reclen >= SECTOR_SIZE)
  298.           this_dir->size = (this_dir->size + (SECTOR_SIZE - 1)) & 
  299.               ~(SECTOR_SIZE - 1);
  300.       this_dir->size += new_reclen;
  301.       s_entry = s_entry->next;
  302.   }
  303. }
  304.  
  305. static void generate_reloc_directory(void)
  306. {
  307.     int new_reclen;
  308.     time_t current_time;
  309.     struct directory_entry  *s_entry;
  310.  
  311.     /* Create an  entry for our internal tree */
  312.     time (¤t_time);
  313.     reloc_dir = (struct directory *) 
  314.         malloc(sizeof(struct directory));
  315.     memset(reloc_dir, 0, sizeof(struct directory));
  316.     reloc_dir->parent = root;
  317.     reloc_dir->next = root->subdir;
  318.     root->subdir = reloc_dir;
  319.     reloc_dir->depth = 1;
  320.     reloc_dir->whole_name = strdup("./rr_moved");
  321.     reloc_dir->de_name =  strdup("rr_moved");
  322.     reloc_dir->extent = 0;
  323.     
  324.     new_reclen  = strlen(reloc_dir->de_name);
  325.     
  326.     /* Now create an actual directory  entry */
  327.     s_entry = (struct directory_entry *) 
  328.         malloc(sizeof (struct directory_entry));
  329.     memset(s_entry, 0, sizeof(struct directory_entry));
  330.     s_entry->next = root->contents;
  331.     reloc_dir->self = s_entry;
  332.  
  333.     root->contents = s_entry;
  334.     root->contents->name = strdup(reloc_dir->de_name);
  335.     root->contents->filedir = root;
  336.     root->contents->isorec.flags[0] = 2;
  337.     root->contents->priority  = 32768;
  338.     iso9660_date(root->contents->isorec.date, current_time);
  339. #ifdef AMIGA
  340.     root->contents->is_a_table = 0;
  341. #else
  342.     root->contents->inode = UNCACHED_INODE;
  343.     root->contents->dev = UNCACHED_DEVICE;
  344. #endif
  345.     set_723(root->contents->isorec.volume_sequence_number, 1);
  346.     iso9660_file_length (reloc_dir->de_name, root->contents, 1);
  347.  
  348.     if(use_RockRidge){
  349.         fstatbuf.st_mode = 0555 | S_IFDIR;
  350.         fstatbuf.st_nlink = 2;
  351.         generate_rock_ridge_attributes("",
  352.                            "rr_moved", s_entry,
  353.                            &fstatbuf, &fstatbuf, 0);
  354.     };
  355.     
  356.     /* Now create the . and .. entries in rr_moved */
  357.     /* Now create an actual directory  entry */
  358.     s_entry = (struct directory_entry *) 
  359.         malloc(sizeof (struct directory_entry));
  360.     memcpy(s_entry, root->contents, 
  361.            sizeof(struct directory_entry));
  362.     s_entry->name = strdup(".");
  363.     iso9660_file_length (".", s_entry, 1);
  364.  
  365. #ifdef AMIGA
  366.     s_entry->is_a_table = 0;
  367. #endif
  368.  
  369.     s_entry->filedir = reloc_dir;
  370.     reloc_dir->contents = s_entry;
  371.  
  372.     if(use_RockRidge){
  373.         fstatbuf.st_mode = 0555 | S_IFDIR;
  374.         fstatbuf.st_nlink = 2;
  375.         generate_rock_ridge_attributes("",
  376.                            ".", s_entry,
  377.                            &fstatbuf, &fstatbuf, 0);
  378.     };
  379.     
  380.     s_entry = (struct directory_entry *) 
  381.         malloc(sizeof (struct directory_entry));
  382.     memcpy(s_entry, root->contents, 
  383.            sizeof(struct directory_entry));
  384. #ifdef AMIGA
  385.     s_entry->is_a_table = 0;
  386. #endif
  387.     s_entry->name = strdup("..");
  388.     iso9660_file_length ("..", s_entry, 1);
  389.     s_entry->filedir = root;
  390.     reloc_dir->contents->next = s_entry;
  391.     reloc_dir->contents->next->next = NULL;
  392.     if(use_RockRidge){
  393.         fstatbuf.st_mode = 0555 | S_IFDIR;
  394.         fstatbuf.st_nlink = 2;
  395.         generate_rock_ridge_attributes("",
  396.                            "..", s_entry,
  397.                            &root_statbuf, &root_statbuf, 0);
  398.     };
  399. }
  400.  
  401. static void FDECL1(increment_nlink, struct directory_entry *, s_entry){
  402.   char * pnt;
  403.   int len, nlink;
  404.  
  405.   pnt = s_entry->rr_attributes;
  406.   len = s_entry->rr_attr_size;
  407.   while(len){
  408.     if(pnt[0] == 'P' && pnt[1] == 'X') {
  409.       nlink =  get_733(pnt+12);
  410.       set_733(pnt+12, nlink+1);
  411.       break;
  412.     };
  413.     len -= pnt[2];
  414.     pnt += pnt[2];
  415.   };
  416. }
  417.  
  418. void finish_cl_pl_entries(){
  419.   struct directory_entry  *s_entry, *s_entry1;
  420.   struct directory *  d_entry;
  421.  
  422.   s_entry = reloc_dir->contents;
  423.    s_entry  = s_entry->next->next;  /* Skip past . and .. */
  424.   for(; s_entry; s_entry = s_entry->next){
  425.       d_entry = reloc_dir->subdir;
  426.       while(d_entry){
  427.           if(d_entry->self == s_entry) break;
  428.           d_entry = d_entry->next;
  429.       };
  430.       if(!d_entry){
  431.           fprintf(stderr,"Unable to locate directory parent\n");
  432.           exit(1);
  433.       };
  434.  
  435.       /* First fix the PL pointer in the directory in the rr_reloc dir */
  436.       s_entry1 = d_entry->contents->next;
  437.       set_733(s_entry1->rr_attributes +  s_entry1->rr_attr_size - 8,
  438.           s_entry->filedir->extent);
  439.  
  440.       /* Now fix the CL pointer */
  441.       s_entry1 = s_entry->parent_rec;
  442.  
  443.       set_733(s_entry1->rr_attributes +  s_entry1->rr_attr_size - 8,
  444.           d_entry->extent);
  445.  
  446.       s_entry->filedir = reloc_dir;  /* Now we can fix this */
  447.   }
  448.   /* Next we need to modify the NLINK terms in the assorted root directory records
  449.      to account for the presence of the RR_MOVED directory */
  450.  
  451.   increment_nlink(root->self);
  452.   increment_nlink(root->self->next);
  453.   d_entry = root->subdir;
  454.   while(d_entry){
  455.     increment_nlink(d_entry->contents->next);
  456.     d_entry = d_entry->next;
  457.   };
  458. }
  459. /*
  460.  * This function scans the directory tree, looking for files, and it makes
  461.  * note of everything that is found.  We also begin to construct the ISO9660
  462.  * directory entries, so that we can determine how large each directory is.
  463.  */
  464.  
  465. int
  466. FDECL2(scan_directory_tree,char *, path, struct directory_entry *, de){
  467.   DIR * current_dir;
  468.   char whole_path[1024];
  469. #ifdef AMIGA
  470.   char amiga_path[1024];
  471. #endif
  472.   struct dirent * d_entry;
  473.   struct directory_entry  *s_entry, *s_entry1;
  474.   struct directory * this_dir, *next_brother, *parent;
  475.   struct stat statbuf, lstatbuf;
  476.   int status;
  477.   char * cpnt;
  478. #ifdef AMIGA
  479.   char * cpnt2;
  480. #endif
  481.   int deep_flag;
  482.  
  483.   current_dir = opendir(path);
  484.   if(!current_dir) {
  485.       fprintf(stderr,"Unable to open directory %s\n", path);
  486.       de->isorec.flags[0] &= ~2; /* Mark as not a directory */
  487.       return 0;
  488.   };
  489.  
  490.   parent = de->filedir;
  491.   /* Set up the struct for the current directory, and insert it into the
  492.      tree */
  493.  
  494.   this_dir = (struct directory *) malloc(sizeof(struct directory));
  495.   this_dir->next = NULL;
  496.   this_dir->subdir = NULL;
  497.   this_dir->self = de;
  498.   this_dir->contents = NULL;
  499.   this_dir->whole_name = strdup(path);
  500.   cpnt = strrchr(this_dir->whole_name, '/');
  501. #ifdef AMIGA
  502.   if ((cpnt2 = strchr (this_dir->whole_name, ':')) && cpnt < cpnt2)
  503.     cpnt = cpnt2;
  504. #endif
  505.   if(cpnt)
  506.     this_dir->de_name = cpnt + 1;
  507.   else
  508.     this_dir->de_name = strdup (path);
  509.   this_dir->size = 0;
  510.   this_dir->extent = 0;
  511.  
  512.   if (verbose)
  513.     fprintf (stderr, "%s\n", path);
  514.  
  515.   if(!parent || parent == root){
  516.     if (!root) {
  517.       root = this_dir;  /* First time through for root directory only */
  518.       root->depth = 0;
  519.       root->parent = root;
  520.     } else {
  521.       this_dir->depth = 1;
  522.       if(!root->subdir)
  523.     root->subdir = this_dir;
  524.       else {
  525.     next_brother = root->subdir;
  526.     while(next_brother->next) next_brother = next_brother->next;
  527.     next_brother->next = this_dir;
  528.       };
  529.       this_dir->parent = parent;
  530.     };
  531.   } else {
  532.       /* Come through here for  normal traversal of  tree */
  533. #ifdef DEBUG
  534.       fprintf(stderr,"%s(%d) ", path, this_dir->depth);
  535. #endif
  536.       if(!inhibit_relocation && parent->depth >  6) {
  537.           fprintf(stderr,"Directories too deep  %s\n", path);
  538.           exit(1);
  539.       };
  540.  
  541.       this_dir->parent = parent; 
  542.       this_dir->depth = parent->depth + 1;
  543.  
  544.       if(!parent->subdir)
  545.           parent->subdir = this_dir;
  546.       else {
  547.           next_brother = parent->subdir;
  548.           while(next_brother->next) next_brother = next_brother->next;
  549.           next_brother->next = this_dir;
  550.       };
  551.   };
  552.  
  553. /* Now we scan the directory itself, and look at what is inside of it. */
  554.  
  555.   while(1==1){
  556.  
  557.     d_entry = readdir(current_dir);
  558.     if(!d_entry) break;
  559.  
  560.     /* OK, got a valid entry */
  561.  
  562.     /* If we do not want all files, then pitch the backups. */
  563.     if(!all_files){
  564.         if(strchr(d_entry->d_name,'~')) continue;
  565.         if(strchr(d_entry->d_name,'#')) continue;
  566.     };
  567.  
  568.     if(strlen(path)+strlen(d_entry->d_name) + 2 > sizeof(whole_path)){
  569.       fprintf(stderr, "Overflow of stat buffer\n");
  570.       exit(1);
  571.     };
  572.  
  573.     /* Generate the complete ASCII path for this file */
  574.     strcpy(whole_path, path);
  575. #ifdef AMIGA
  576.     if(whole_path[strlen(whole_path)-1] != '/' &&
  577.        whole_path[strlen(whole_path)-1] != ':') strcat(whole_path, "/");
  578. #else
  579.     if(whole_path[strlen(whole_path)-1] != '/') strcat(whole_path, "/");
  580. #endif
  581.     strcat(whole_path, d_entry->d_name);
  582.  
  583.     /* Should we exclude this file? */
  584.     if (is_excluded(whole_path)) {
  585.       if (verbose) {
  586.         fprintf(stderr, "Excluded: %s\n",whole_path);
  587.       }
  588.       continue;
  589.     }
  590.  
  591.     if (verbose >= 2)  fprintf(stderr, "%s\n",whole_path);
  592.  
  593. #ifdef AMIGA
  594.     strcpy (amiga_path, whole_path);
  595. #ifndef __GNUC__
  596.     remove_dot_files (amiga_path);
  597. #endif
  598.  
  599.     status = stat(amiga_path, &statbuf);
  600.     lstatbuf = statbuf;
  601. #else
  602.     status = stat(whole_path, &statbuf);
  603.     lstat(whole_path, &lstatbuf);
  604. #endif
  605.  
  606.     if(this_dir == root && strcmp(d_entry->d_name, ".") == 0)
  607.       root_statbuf = statbuf;  /* Save this for later on */
  608.  
  609.     /* We do this to make sure that the root entries are consistent */
  610.     if(this_dir == root && strcmp(d_entry->d_name, "..") == 0) {
  611.       statbuf = root_statbuf;
  612.       lstatbuf = root_statbuf;
  613.     };
  614.  
  615. #ifndef AMIGA
  616.     if(S_ISLNK(lstatbuf.st_mode)){
  617.  
  618.         /* Here we decide how to handle the symbolic links.  Here we
  619.            handle the general case - if we are not following links or there is an
  620.            error, then we must change something.  If RR is in use, it is easy, we
  621.            let RR describe the file.  If not, then we punt the file. */
  622.  
  623.         if((status || !follow_links)){
  624.             if(use_RockRidge){
  625.                 status = 0;
  626.                 statbuf.st_size = 0;
  627.                 statbuf.st_ino = UNCACHED_INODE;
  628.                 statbuf.st_dev = UNCACHED_DEVICE;
  629.                 statbuf.st_mode = (statbuf.st_mode & ~S_IFMT) | S_IFREG;
  630.             } else {
  631.                 if(follow_links) fprintf(stderr,
  632.                     "Unable to stat file %s - ignoring and continuing.\n",
  633.                     whole_path);
  634.                 else fprintf(stderr,
  635.                     "Symlink %s ignored - continuing.\n",
  636.                     whole_path);
  637.                 continue;  /* Non Rock Ridge discs - ignore all symlinks */
  638.             };
  639.         }
  640.         
  641.         /* Here we handle a different kind of case.  Here we have a symlink,
  642.            but we want to follow symlinks.  If we run across a directory loop,
  643.            then we need to pretend that we are not following symlinks for this file.
  644.            If this is the first time we have seen this, then make this seem
  645.            as if there was no symlink there in the first place */
  646.                       
  647.         else if(strcmp(d_entry->d_name, ".") && 
  648.            strcmp(d_entry->d_name, "..")) {
  649.             if(find_directory_hash(statbuf.st_dev, statbuf.st_ino)){
  650.                 fprintf(stderr, "Infinite loop detected (%s)\n", whole_path);
  651.                 if(!use_RockRidge) continue;
  652.                 statbuf.st_size = 0;
  653.                 statbuf.st_ino = UNCACHED_INODE;
  654.                 statbuf.st_dev = UNCACHED_DEVICE;
  655.                 statbuf.st_mode = (statbuf.st_mode & ~S_IFMT) | S_IFREG;
  656.             } else {
  657.                 lstatbuf = statbuf;
  658.                 add_directory_hash(statbuf.st_dev, statbuf.st_ino);
  659.             };
  660.         };        
  661.     };
  662. #endif
  663.  
  664. #ifdef AMIGA
  665.     if(S_ISREG(lstatbuf.st_mode) && !(S_IRUSR & lstatbuf.st_mode)) {
  666. #else
  667.     if(S_ISREG(lstatbuf.st_mode) && (status = access(whole_path, R_OK))){
  668. #endif
  669.       fprintf(stderr, "File %s is not readable (errno = %d) - ignoring\n", 
  670.           whole_path, errno);
  671.       continue;
  672.     }
  673.  
  674. #ifndef AMIGA
  675.     /* Add this so that we can detect directory loops with hard links.
  676.      If we are set up to follow symlinks, then we skip this checking. */
  677.     if(!follow_links && S_ISDIR(lstatbuf.st_mode) && strcmp(d_entry->d_name, ".") && 
  678.        strcmp(d_entry->d_name, "..")) {
  679.         if(find_directory_hash(statbuf.st_dev, statbuf.st_ino)) {
  680.             fprintf(stderr,"Directory loop - fatal goof (%s %x %d).\n",
  681.                 whole_path, statbuf.st_dev, statbuf.st_ino);
  682.             exit(1);
  683.         };
  684.         add_directory_hash(statbuf.st_dev, statbuf.st_ino);
  685.     };
  686. #endif
  687.  
  688.     if (!S_ISCHR(lstatbuf.st_mode) && !S_ISBLK(lstatbuf.st_mode) &&
  689. #ifndef AMIGA
  690.     !S_ISFIFO(lstatbuf.st_mode) && !S_ISSOCK(lstatbuf.st_mode) &&
  691. #endif
  692.     !S_ISLNK(lstatbuf.st_mode) && !S_ISREG(lstatbuf.st_mode) &&
  693.     !S_ISDIR(lstatbuf.st_mode)) {
  694.       fprintf(stderr,"Unknown file type %s - ignoring and continuing.\n",
  695.           whole_path);
  696.       continue;
  697.     };
  698.  
  699.     /* Who knows what trash this is - ignore and continue */
  700.  
  701.     if(status) {
  702.         fprintf(stderr,
  703.             "Unable to stat file %s - ignoring and continuing.\n",
  704.             whole_path);
  705.         continue; 
  706.     };
  707.  
  708.     s_entry = (struct directory_entry *) 
  709.       malloc_forever(sizeof (struct directory_entry));
  710.     s_entry->next = this_dir->contents;
  711.     this_dir->contents = s_entry;
  712.     deep_flag = 0;
  713.     s_entry->table = NULL;
  714.  
  715.     s_entry->name = strdup(d_entry->d_name);
  716.  
  717.     s_entry->filedir = this_dir;
  718.     s_entry->isorec.flags[0] = 0;
  719.     s_entry->isorec.ext_attr_length[0] = 0;
  720.     iso9660_date(s_entry->isorec.date, statbuf.st_ctime);
  721.     s_entry->isorec.file_unit_size[0] = 0;
  722.     s_entry->isorec.interleave[0] = 0;
  723. #ifdef AMIGA
  724.     s_entry->is_a_table = 0;
  725. #endif
  726.     if(parent && parent ==  reloc_dir && strcmp(d_entry->d_name,  "..") == 0){
  727. #ifndef AMIGA
  728.         s_entry->inode = UNCACHED_INODE;
  729.         s_entry->dev = UNCACHED_DEVICE;
  730. #endif
  731.         deep_flag  = NEED_PL;
  732.     } else  {
  733. #ifndef AMIGA
  734.         s_entry->inode = statbuf.st_ino;
  735.         s_entry->dev = statbuf.st_dev;
  736. #endif
  737.     };
  738.     set_723(s_entry->isorec.volume_sequence_number, 1);
  739.     iso9660_file_length(d_entry->d_name, s_entry, S_ISDIR(statbuf.st_mode));
  740.     s_entry->rr_attr_size = 0;
  741.     s_entry->rr_attributes = NULL;
  742.  
  743.     /* Directories are assigned sizes later on */
  744.     if (!S_ISDIR(statbuf.st_mode)) {
  745.     set_733(s_entry->isorec.size, statbuf.st_size); 
  746.  
  747.     if (S_ISCHR(lstatbuf.st_mode) || S_ISBLK(lstatbuf.st_mode) ||
  748. #ifndef AMIGA
  749.         S_ISFIFO(lstatbuf.st_mode) || S_ISSOCK(lstatbuf.st_mode) ||
  750. #endif
  751.         S_ISLNK(lstatbuf.st_mode))
  752.       s_entry->size = 0; 
  753.     else
  754.       s_entry->size = statbuf.st_size; 
  755.     pr_file_count++;
  756.     } else {
  757.       s_entry->isorec.flags[0] = 2;
  758.       if (strcmp(d_entry->d_name,".") && strcmp(d_entry->d_name,".."))
  759.         pr_dir_count++;
  760.     }
  761.  
  762.     if (!inhibit_relocation &&
  763.         strcmp(d_entry->d_name,".") && strcmp(d_entry->d_name,"..") && 
  764.     S_ISDIR(statbuf.st_mode) && this_dir->depth > 6){
  765.           if(!reloc_dir) generate_reloc_directory();
  766.  
  767.           s_entry1 = (struct directory_entry *) 
  768.               malloc(sizeof (struct directory_entry));
  769.           memcpy(s_entry1, this_dir->contents, 
  770.              sizeof(struct directory_entry));
  771.           s_entry1->table = NULL;
  772.           s_entry1->name = strdup(this_dir->contents->name);
  773.           s_entry1->next = reloc_dir->contents;
  774.           reloc_dir->contents = s_entry1;
  775.           s_entry1->priority  =  32768;
  776.           s_entry1->parent_rec = this_dir->contents;
  777.  
  778.           deep_flag = NEED_RE;
  779.  
  780.           if(use_RockRidge) {
  781.               generate_rock_ridge_attributes(whole_path,
  782.                              d_entry->d_name, s_entry1,
  783.                              &statbuf, &lstatbuf, deep_flag);
  784.           };
  785.  
  786.           deep_flag = 0;
  787.  
  788.           /* We need to set this temporarily so that the parent to this is correctly
  789.              determined. */
  790.           s_entry1->filedir = reloc_dir;
  791.           scan_directory_tree(whole_path, s_entry1);
  792.           s_entry1->filedir = this_dir;
  793.  
  794.           statbuf.st_size = 0;
  795.           statbuf.st_mode &= 0777;
  796.           set_733(s_entry->isorec.size, 0);
  797.           s_entry->size = 0;
  798.           s_entry->isorec.flags[0] = 0;
  799. #ifdef AMIGA
  800.           s_entry->is_a_table = 0;
  801. #else
  802.           s_entry->inode = UNCACHED_INODE;
  803. #endif
  804.           deep_flag = NEED_CL;
  805.       };
  806.  
  807.     if(generate_tables && strcmp(s_entry->name, ".") && strcmp(s_entry->name, "..")) {
  808.         char  buffer[2048];
  809.         switch(lstatbuf.st_mode & S_IFMT){
  810.         case S_IFDIR:
  811.             sprintf(buffer,"D\t%s\n",
  812.                 s_entry->name);
  813.             break;
  814. #ifndef AMIGA
  815. #ifndef VMS
  816.         case S_IFBLK:
  817.             sprintf(buffer,"B\t%s\t%d %d\n",
  818.                 s_entry->name,
  819.                 major(statbuf.st_rdev), minor(statbuf.st_rdev));
  820.             break;
  821.         case S_IFIFO:
  822.             sprintf(buffer,"P\t%s\n",
  823.                 s_entry->name);
  824.             break;
  825.         case S_IFCHR:
  826.             sprintf(buffer,"C\t%s\t%d %d\n",
  827.                 s_entry->name,
  828.                 major(statbuf.st_rdev), minor(statbuf.st_rdev));
  829.             break;
  830.         case S_IFLNK:
  831.             readlink(whole_path, symlink_buff, sizeof(symlink_buff));
  832.             sprintf(buffer,"L\t%s\t%s\n",
  833.                 s_entry->name, symlink_buff);
  834.             break;
  835.         case S_IFSOCK:
  836.             sprintf(buffer,"S\t%s\n",
  837.                 s_entry->name);
  838.             break;
  839. #endif
  840. #endif
  841.         case S_IFREG:
  842.         default:
  843.             sprintf(buffer,"F\t%s\n",
  844.                 s_entry->name);
  845.             break;
  846.         };
  847.         s_entry->table = strdup(buffer);
  848.     };
  849.     
  850.     if(S_ISDIR(statbuf.st_mode)){
  851.             int dflag;
  852.         if (strcmp(d_entry->d_name,".") && strcmp(d_entry->d_name,"..")) {
  853.             /* shall filenames in this directory be converted? */
  854.         if (is_included_conv (whole_path)) {
  855.           convert_filenames = 1;
  856.           dflag = scan_directory_tree(whole_path, s_entry);
  857.           convert_filenames = 0;
  858.         } else
  859.           dflag = scan_directory_tree(whole_path, s_entry);
  860.           
  861.             /* If unable to scan directory, mark this as a non-directory */
  862.             if(!dflag)
  863.           lstatbuf.st_mode = (lstatbuf.st_mode & ~S_IFMT) | S_IFREG;
  864.           }
  865.       };
  866.  
  867.   if(use_RockRidge && this_dir == root && strcmp(s_entry->name, ".")  == 0)
  868.       deep_flag |= NEED_CE | NEED_SP;  /* For extension record */
  869.  
  870.     /* Now figure out how much room this file will take in the directory */
  871.  
  872.     if(use_RockRidge) {
  873.         generate_rock_ridge_attributes(whole_path,
  874.                        d_entry->d_name, s_entry,
  875.                        &statbuf, &lstatbuf, deep_flag);
  876.         
  877.     }
  878.   }
  879.   closedir(current_dir);
  880.   sort_n_finish(this_dir);
  881.  
  882. #ifdef AMIGA
  883.     if (progress_indicator) {
  884.       printf ("Scanning tree; %lu directories, %lu files; free mem: %lu   \r",
  885.                 pr_dir_count, pr_file_count, AvailMem (MEMF_ANY));
  886.       fflush (stdout);
  887.     }
  888. #endif
  889.  
  890.   return 1;
  891. }
  892.  
  893.  
  894. void FDECL2(generate_iso9660_directories, struct directory *, node, FILE*, outfile){
  895.   struct directory * dpnt;
  896.  
  897.   dpnt = node;
  898.  
  899.   while (dpnt){
  900.     generate_one_directory(dpnt, outfile);
  901.     if(dpnt->subdir) generate_iso9660_directories(dpnt->subdir, outfile);
  902.     dpnt = dpnt->next;
  903.   };
  904. }
  905.  
  906. void FDECL1(dump_tree, struct directory *, node){
  907.   struct directory * dpnt;
  908.  
  909.   dpnt = node;
  910.  
  911.   while (dpnt){
  912.     fprintf(stderr,"%4d %5d %s\n",dpnt->extent, dpnt->size, dpnt->de_name);
  913.     if(dpnt->subdir) dump_tree(dpnt->subdir);
  914.     dpnt = dpnt->next;
  915.   };
  916. }
  917.  
  918.