joliet.c

来自「一款功能很强的光盘镜象制作工具」· C语言 代码 · 共 1,087 行 · 第 1/2 页

C
1,087
字号
  }
  return 0;
} /* generate_path_tables(... */

static void FDECL2(generate_one_joliet_directory, struct directory *, dpnt, FILE *, outfile)
{
     unsigned int			  dir_index;
     char				* directory_buffer;
     int				  new_reclen;
     struct directory_entry		* s_entry;
     struct directory_entry		* s_entry1;
     struct iso_directory_record	  jrec;
     unsigned int			  total_size;
     int				  cvt_len;
     struct directory			* finddir;
     
     total_size = (dpnt->jsize + (SECTOR_SIZE - 1)) &  ~(SECTOR_SIZE - 1);
     directory_buffer = (char *) e_malloc(total_size);
     memset(directory_buffer, 0, total_size);
     dir_index = 0;
     
     s_entry = dpnt->jcontents;
     while(s_entry) 
     {
	     if(s_entry->de_flags & INHIBIT_JOLIET_ENTRY) {
		s_entry = s_entry->jnext;
		continue;
	     }
		
	     /*
	      * If this entry was a directory that was relocated, we have a bit
	      * of trouble here.  We need to dig out the real thing and put it
	      * back here.  In the Joliet tree, there is no relocated rock
	      * ridge, as there are no depth limits to a directory tree.
	      */
	     if( (s_entry->de_flags & RELOCATED_DIRECTORY) != 0 )
	     {
		 for(s_entry1 = reloc_dir->contents; s_entry1; s_entry1 = s_entry1->next)
		 {
		     if( s_entry1->parent_rec == s_entry )
		     {
			 break;
		     }
		 }
		 if( s_entry1 == NULL )
		 {
		     /*
		      * We got trouble.
		      */
		     fprintf(stderr, "Unable to locate relocated directory\n");
		     exit(1);
		 }
	     }
	     else
	     {
		 s_entry1 = s_entry;
	     }
	      
	     /* 
	      * We do not allow directory entries to cross sector boundaries.  
	      * Simply pad, and then start the next entry at the next sector 
	      */
	     new_reclen = s_entry1->jreclen;
	     if( (dir_index & (SECTOR_SIZE - 1)) + new_reclen >= SECTOR_SIZE )
	     {
		 dir_index = (dir_index + (SECTOR_SIZE - 1)) & 
		     ~(SECTOR_SIZE - 1);
	     }
	     
	     memcpy(&jrec, &s_entry1->isorec, sizeof(struct iso_directory_record) -
		    sizeof(s_entry1->isorec.name));
	     
	     cvt_len = joliet_strlen(s_entry1->name);
	     
	     /*
	      * Fix the record length - this was the non-Joliet version we
	      * were seeing.
	      */
	     jrec.name_len[0] = cvt_len;
	     jrec.length[0] = s_entry1->jreclen;
	     
	     /*
	      * If this is a directory, fix the correct size and extent
	      * number.
	      */
	     if( (jrec.flags[0] & 2) != 0 )
	     {
		 if(strcmp(s_entry1->name,".") == 0) 
		 {
		     jrec.name_len[0] = 1;
		     set_733((char *) jrec.extent, dpnt->jextent);
		     set_733((char *) jrec.size, ROUND_UP(dpnt->jsize));
		 }
		 else if(strcmp(s_entry1->name,"..") == 0) 
		 {
		     jrec.name_len[0] = 1;
		     if( dpnt->parent == reloc_dir )
		     {
			 set_733((char *) jrec.extent, dpnt->self->parent_rec->filedir->jextent);
			 set_733((char *) jrec.size, ROUND_UP(dpnt->self->parent_rec->filedir->jsize));
		     }
		     else

		     {
			 set_733((char *) jrec.extent, dpnt->parent->jextent);
			 set_733((char *) jrec.size, ROUND_UP(dpnt->parent->jsize));
		     }
		 }
		 else
		 {
		     if( (s_entry->de_flags & RELOCATED_DIRECTORY) != 0 )
		     {
			 finddir = reloc_dir->subdir;
		     }
		     else
		     {
			 finddir = dpnt->subdir;
		     }
		     while(1==1)
		     {
			 if(finddir->self == s_entry1) break;
			 finddir = finddir->next;
			 if(!finddir) 
			 {
			     fprintf(stderr,"Fatal goof - unable to find directory location\n"); exit(1);
			 }
		     }
		     set_733((char *) jrec.extent, finddir->jextent);
		     set_733((char *) jrec.size, ROUND_UP(finddir->jsize));
		 }
	     }
	     
	     memcpy(directory_buffer + dir_index, &jrec, 
		    sizeof(struct iso_directory_record) -
		    sizeof(s_entry1->isorec.name));
	     
	     
	     dir_index += sizeof(struct iso_directory_record) - 
		 sizeof (s_entry1->isorec.name);
	     
	     /*
	      * Finally dump the Unicode version of the filename.
	      * Note - . and .. are the same as with non-Joliet discs.
	      */
	     if( (jrec.flags[0] & 2) != 0 
		 && strcmp(s_entry1->name, ".") == 0 )
	     {
		 directory_buffer[dir_index++] = 0;
	     }
	     else if( (jrec.flags[0] & 2) != 0 
		      && strcmp(s_entry1->name, "..") == 0 )
	     {
		 directory_buffer[dir_index++] = 1;
	     }
	     else
	     {
		 convert_to_unicode((u_char *)directory_buffer + dir_index,
				    cvt_len,
				    s_entry1->name);
		 dir_index += cvt_len;
	     }
	     
	     if(dir_index & 1)
	     {
		 directory_buffer[dir_index++] = 0;
	     }

	     s_entry = s_entry->jnext;
     }
     
     if(dpnt->jsize != dir_index)
     {
	 fprintf(stderr,"Unexpected joliet directory length %d %d %s\n",dpnt->jsize, 
		 dir_index, dpnt->de_name);
     }
     
     xfwrite(directory_buffer, 1, total_size, outfile);
     last_extent_written += total_size >> 11;
     free(directory_buffer);
} /* generate_one_joliet_directory(... */

static int FDECL1(joliet_sort_n_finish, struct directory *, this_dir)
{
  struct directory_entry  * s_entry;
  int			    status = 0;

  /* don't want to skip this directory if it's the reloc_dir at the moment */
  if(this_dir != reloc_dir && this_dir->dir_flags & INHIBIT_JOLIET_ENTRY)
    {
      return 0;
    }

  for(s_entry = this_dir->contents; s_entry; s_entry = s_entry->next)
    {
      /* skip hidden entries */
      if(  (s_entry->de_flags & INHIBIT_JOLIET_ENTRY) != 0 )
	{
	  continue;
	}
	  
      /*
       * First update the path table sizes for directories.
       *
       * Finally, set the length of the directory entry if Joliet is used.
       * The name is longer, but no Rock Ridge is ever used here, so
       * depending upon the options the entry size might turn out to be about
       * the same.  The Unicode name is always a multiple of 2 bytes, so
       * we always add 1 to make it an even number.
       */
      if(s_entry->isorec.flags[0] ==  2)
	{
	  if (strcmp(s_entry->name,".") && strcmp(s_entry->name,"..")) 
	    {
	      jpath_table_size += joliet_strlen(s_entry->name) + sizeof(struct iso_path_table) - 1;
	      if (jpath_table_size & 1) 
		{
		  jpath_table_size++;
		}
	    }
	  else 
	    {
	      if (this_dir == root && strlen(s_entry->name) == 1)
		{
		  jpath_table_size += sizeof(struct iso_path_table);
		  if (jpath_table_size & 1) jpath_table_size++;
		}
	    }
	}

      if (strcmp(s_entry->name,".") && strcmp(s_entry->name,"..")) 
	{
	  s_entry->jreclen = 	sizeof(struct iso_directory_record)
	    - sizeof(s_entry->isorec.name)
	    + joliet_strlen(s_entry->name) 
	    + 1;
	}
      else
	{
	  /*
	   * Special - for '.' and '..' we generate the same records we
	   * did for non-Joliet discs.
	   */
	  s_entry->jreclen = 	sizeof(struct iso_directory_record)
	    - sizeof(s_entry->isorec.name)
	    + 1;
	}


    }

  if( (this_dir->dir_flags & INHIBIT_JOLIET_ENTRY) != 0 )
    {
      return 0;
    }

  this_dir->jcontents = this_dir->contents;
  status = joliet_sort_directory(&this_dir->jcontents);

  /* 
   * Now go through the directory and figure out how large this one will be.
   * Do not split a directory entry across a sector boundary 
   */
  s_entry = this_dir->jcontents;
/*
 * XXX Is it ok to comment this out?
 */
/*XXX JS  this_dir->ce_bytes = 0;*/
  for(s_entry = this_dir->jcontents; s_entry; s_entry = s_entry->jnext)
    {
      int jreclen;

      if( (s_entry->de_flags & INHIBIT_JOLIET_ENTRY) != 0 )
	{
	  continue;
	}

      jreclen = s_entry->jreclen;
      
      if ((this_dir->jsize & (SECTOR_SIZE - 1)) + jreclen >= SECTOR_SIZE)
	{
	  this_dir->jsize = (this_dir->jsize + (SECTOR_SIZE - 1)) & 
	    ~(SECTOR_SIZE - 1);
	}
      this_dir->jsize += jreclen;
    }
  return status;
}

/*
 * Similar to the iso9660 case, except here we perform a full sort based upon the
 * regular name of the file, not the 8.3 version.
 */
static int FDECL2(joliet_compare_dirs, const void *, rr, const void *, ll) 
{
     char * rpnt, *lpnt;
     struct directory_entry ** r, **l;
     
     r = (struct directory_entry **) rr;
     l = (struct directory_entry **) ll;
     rpnt = (*r)->name;
     lpnt = (*l)->name;

     /*
      * If the entries are the same, this is an error.
      */
     if( strcmp(rpnt, lpnt) == 0 )
       {
	 sort_goof++;
       }
     
     /*
      *  Put the '.' and '..' entries on the head of the sorted list.
      *  For normal ASCII, this always happens to be the case, but out of
      *  band characters cause this not to be the case sometimes.
      */
     if( strcmp(rpnt, ".") == 0 ) return -1;
     if( strcmp(lpnt, ".") == 0 ) return  1;

     if( strcmp(rpnt, "..") == 0 ) return -1;
     if( strcmp(lpnt, "..") == 0 ) return  1;

     while(*rpnt && *lpnt) 
     {
	  if(*rpnt == ';' && *lpnt != ';') return -1;
	  if(*rpnt != ';' && *lpnt == ';') return 1;
	  
	  if(*rpnt == ';' && *lpnt == ';') return 0;
	  
	  /*
	   * Extensions are not special here.  Don't treat the dot as something that
	   * must be bumped to the start of the list.
	   */
#if 0
	  if(*rpnt == '.' && *lpnt != '.') return -1;
	  if(*rpnt != '.' && *lpnt == '.') return 1;
#endif
	  
	  if(*rpnt < *lpnt) return -1;
	  if(*rpnt > *lpnt) return 1;
	  rpnt++;  lpnt++;
     }
     if(*rpnt) return 1;
     if(*lpnt) return -1;
     return 0;
}


/* 
 * Function:		sort_directory
 *
 * Purpose:		Sort the directory in the appropriate ISO9660
 *			order.
 *
 * Notes:		Returns 0 if OK, returns > 0 if an error occurred.
 */
static int FDECL1(joliet_sort_directory, struct directory_entry **, sort_dir)
{
     int dcount = 0;
     int i;
     struct directory_entry * s_entry;
     struct directory_entry ** sortlist;
     
     s_entry = *sort_dir;
     while(s_entry)
     {
	  /* skip hidden entries */
	  if (!(s_entry->de_flags & INHIBIT_JOLIET_ENTRY))
	    dcount++;
	  s_entry = s_entry->next;
     }

     /*
      * OK, now we know how many there are.  Build a vector for sorting. 
      */
     sortlist =   (struct directory_entry **) 
	  e_malloc(sizeof(struct directory_entry *) * dcount);

     dcount = 0;
     s_entry = *sort_dir;
     while(s_entry)
     {
	  /* skip hidden entries */
	  if (!(s_entry->de_flags & INHIBIT_JOLIET_ENTRY)) {
	    sortlist[dcount] = s_entry;
	    dcount++;
	  }
	  s_entry = s_entry->next;
     }
  
     sort_goof = 0;
#ifdef	__STDC__
     qsort(sortlist, dcount, sizeof(struct directory_entry *), 
	   (int (*)(const void *, const void *))joliet_compare_dirs);
#else
     qsort(sortlist, dcount, sizeof(struct directory_entry *), 
	   joliet_compare_dirs);
#endif
     
     /* 
      * Now reassemble the linked list in the proper sorted order 
      */
     for(i=0; i<dcount-1; i++)
     {
	  sortlist[i]->jnext = sortlist[i+1];
     }

     sortlist[dcount-1]->jnext = NULL;
     *sort_dir = sortlist[0];
     
     free(sortlist);
     return sort_goof;
}

int FDECL1(joliet_sort_tree, struct directory *, node)
{
  struct directory * dpnt;
  int ret = 0;

  dpnt = node;

  while (dpnt){
    ret = joliet_sort_n_finish(dpnt);
    if( ret )
      {
	break;
      }
    if(dpnt->subdir) ret = joliet_sort_tree(dpnt->subdir);
    if( ret )
      {
	break;
      }
    dpnt = dpnt->next;
  }
  return ret;
}

static void FDECL2(generate_joliet_directories, struct directory *, node, FILE*, outfile){
  struct directory * dpnt;

  dpnt = node;

  while (dpnt)
    {
      if( (dpnt->dir_flags & INHIBIT_JOLIET_ENTRY) == 0 )
	{
	  /*
	   * In theory we should never reuse a directory, so this doesn't
	   * make much sense.
	   */
	  if( dpnt->jextent > session_start )
	    {
	      generate_one_joliet_directory(dpnt, outfile);
	    }
	}
      /* skip if hidden - but not for the rr_moved dir */
      if(dpnt->subdir && (!(dpnt->dir_flags & INHIBIT_JOLIET_ENTRY) || dpnt == reloc_dir)) 
	generate_joliet_directories(dpnt->subdir, outfile);
      dpnt = dpnt->next;
    }
}


/*
 * Function to write the EVD for the disc.
 */
static int FDECL1(jpathtab_write, FILE *, outfile)
{
  /*
   * Next we write the path tables 
   */
  xfwrite(jpath_table_l, 1, jpath_blocks << 11, outfile);
  xfwrite(jpath_table_m, 1, jpath_blocks << 11, outfile);
  last_extent_written += 2*jpath_blocks;
  free(jpath_table_l);
  free(jpath_table_m);
  jpath_table_l = NULL;
  jpath_table_m = NULL;
  return 0;
}

static int FDECL1(jdirtree_size, int, starting_extent)
{
  assign_joliet_directory_addresses(root);
  return 0;
}

static int jroot_gen()
{
     jroot_record.length[0] = 1 + sizeof(struct iso_directory_record)
	  - sizeof(jroot_record.name);
     jroot_record.ext_attr_length[0] = 0;
     set_733((char *) jroot_record.extent, root->jextent);
     set_733((char *) jroot_record.size, ROUND_UP(root->jsize));
     iso9660_date(jroot_record.date, root_statbuf.st_mtime);
     jroot_record.flags[0] = 2;
     jroot_record.file_unit_size[0] = 0;
     jroot_record.interleave[0] = 0;
     set_723(jroot_record.volume_sequence_number, volume_sequence_number);
     jroot_record.name_len[0] = 1;
     return 0;
}

static int FDECL1(jdirtree_write, FILE *, outfile)
{
  generate_joliet_directories(root, outfile);
  return 0;
}

/*
 * Function to write the EVD for the disc.
 */
static int FDECL1(jvd_write, FILE *, outfile)
{
  struct iso_primary_descriptor jvol_desc;

  /*
   * Next we write out the boot volume descriptor for the disc 
   */
  jvol_desc = vol_desc;
  get_joliet_vol_desc(&jvol_desc);
  xfwrite(&jvol_desc, 1, 2048, outfile);
  last_extent_written ++;
  return 0;
}

/*
 * Functions to describe padding block at the start of the disc.
 */
static int FDECL1(jpathtab_size, int, starting_extent)
{
  jpath_table[0] = starting_extent;
  jpath_table[1] = 0;
  jpath_table[2] = jpath_table[0] + jpath_blocks;
  jpath_table[3] = 0;
  
  last_extent += 2*jpath_blocks;
  return 0;
}

struct output_fragment joliet_desc    = {NULL, oneblock_size, jroot_gen,jvd_write};
struct output_fragment jpathtable_desc= {NULL, jpathtab_size, generate_joliet_path_tables,     jpathtab_write};
struct output_fragment jdirtree_desc  = {NULL, jdirtree_size, NULL,     jdirtree_write};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?