home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / useful / dist / disk / cdrom / amicdrom / rock.c < prev    next >
C/C++ Source or Header  |  1994-02-05  |  9KB  |  340 lines

  1. /* rock.c:
  2.  *
  3.  * Support for the Rock Ridge filing system.
  4.  *
  5.  * ----------------------------------------------------------------------
  6.  * This code is (C) Copyright 1993,1994 by Frank Munkert.
  7.  * All rights reserved.
  8.  * This software may be freely distributed and redistributed for
  9.  * non-commercial purposes, provided this notice is included.
  10.  * ----------------------------------------------------------------------
  11.  * History:
  12.  * 
  13.  * 05-Feb-94   fmu   Added support for relocated directories.
  14.  * 16-Oct-93   fmu   Adapted to new VOLUME structure.
  15.  */
  16.  
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #include <exec/memory.h>
  21. #include <clib/exec_protos.h>
  22. #ifdef AZTEC_C
  23. #include <pragmas/exec_lib.h>
  24. #endif
  25. #ifdef LATTICE
  26. #include <pragmas/exec_pragmas.h>
  27. #endif
  28. #if defined(_DCC) && defined(REGISTERED)
  29. #include <pragmas/exec_pragmas.h>
  30. extern struct Library *SysBase;
  31. #endif
  32.  
  33. #include "rock.h"
  34.  
  35. #define VOL(vol,tag) (((t_iso_vol_info *)(vol->vol_info))->tag)
  36. #define OBJ(obj,tag) (((t_iso_obj_info *)(obj->obj_info))->tag)
  37.  
  38. /* Check whether the given volume uses the Rock Ridge Interchange Protocol.
  39.  * The protocol is identified by the sequence
  40.  *            'S' 'P' 7 1 0xbe 0xef
  41.  * in the system use field of the (00) directory in the root directory of
  42.  * the volume.
  43.  *
  44.  * Returns 1 iff the RR protocol is used; 0 otherwise.
  45.  * If the RR protocol is used, *p_skip will be set to the skip length
  46.  * specified in the SP system use field.
  47.  */
  48.  
  49. t_bool Uses_Rock_Ridge_Protocol (VOLUME *p_volume, int *p_skip)
  50. {
  51.   unsigned long loc = VOL(p_volume,pvd).root.extent_loc_m;
  52.   directory_record *dir;
  53.   int system_use_pos;
  54.   unsigned char *sys;
  55.  
  56.   if (!(dir = Get_Directory_Record (p_volume, loc, 0)))
  57.     return 0;
  58.   
  59.   system_use_pos = 33 + dir->file_id_length;
  60.   if (system_use_pos & 1)
  61.     system_use_pos++;
  62.  
  63.   if (system_use_pos >= dir->length)
  64.     return 0;
  65.  
  66.   sys = (unsigned char *) dir + system_use_pos;
  67.   if (sys[0] == 'S' && sys[1] == 'P' && sys[2] == 7 &&
  68.       sys[3] == 1 && sys[4] == 0xbe && sys[5] == 0xef) {
  69.     *p_skip = sys[6];
  70.     return 1;
  71.   } else
  72.     return 0;
  73. }
  74.  
  75. /* Searches for the system use field with name p_name in the directory record
  76.  * p_dir and fills the buffer p_buf (with length p_buf_len) with the information
  77.  * contained in the system use field.
  78.  *
  79.  * p_index is the ordinal number of the system use field (if more than one
  80.  * system use field with the same name is recorded.) 0=first occurrence,
  81.  * 1=second occurrence, and so on.
  82.  *
  83.  * 1 is returned if the system use field has been found; otherwise 0
  84.  * is returned.
  85.  */
  86.  
  87. int Get_System_Use_Field (VOLUME *p_volume, directory_record *p_dir,
  88.               char *p_name, char *p_buf, int p_buf_len,
  89.               int p_index)
  90. {
  91.   int system_use_pos;
  92.   int slen, len;
  93.   unsigned long length = p_dir->length;
  94.   unsigned char *buf = (unsigned char *) p_dir;
  95.  
  96.   system_use_pos = 33 + p_dir->file_id_length;
  97.   if (system_use_pos & 1)
  98.     system_use_pos++;
  99.   system_use_pos += VOL(p_volume,skip);
  100.  
  101.   /* the system use field must be at least 4 bytes long */
  102.   while (system_use_pos + 3 < length) {
  103.     slen = buf[system_use_pos+2];
  104.     if (buf[system_use_pos] == p_name[0] &&
  105.         buf[system_use_pos+1] == p_name[1]) {
  106.       if (p_index)
  107.         p_index--;
  108.       else {
  109.         len = (slen < p_buf_len) ? slen : p_buf_len;
  110.         memcpy (p_buf, buf + system_use_pos, len);
  111.         return 1;
  112.       }
  113.     }
  114.     /* look for continuation area: */
  115.     if (buf[system_use_pos] == 'C' &&
  116.         buf[system_use_pos+1] == 'E') {
  117.       unsigned long newloc, offset;
  118.       memcpy (&newloc, buf + system_use_pos + 8, 4);
  119.       memcpy (&offset, buf + system_use_pos + 16, 4);
  120.       memcpy (&length, buf + system_use_pos + 24, 4);
  121.       if (!Read_Sector (p_volume->cd, newloc))
  122.         return 0;
  123.       buf = p_volume->cd->buffer;
  124.       system_use_pos = offset;
  125.       continue;
  126.     }
  127.     
  128.     /* look for system use field terminator: */
  129.     if (buf[system_use_pos] == 'S' &&
  130.         buf[system_use_pos+1] == 'T')
  131.       return 0;
  132.  
  133.     system_use_pos += slen;
  134.   }
  135.   return 0;
  136. }
  137.  
  138. /* Determines the Rock Ridge file name of the CDROM object p_obj.
  139.  * The file name will be stored in the buffer p_buf (with length p_buf_len).
  140.  * The file name will NOT be null-terminated. The number of characters in
  141.  * the file name is returned. If there is no Rock Ridge file name for
  142.  * p_obj, then -1 is returned.
  143.  */
  144.  
  145. int Get_RR_File_Name (VOLUME *p_volume, directory_record *p_dir,
  146.               char *p_buf, int p_buf_len)
  147. {
  148.   __unaligned struct nm_system_use_field {
  149.     char      id[2];
  150.     unsigned char length;
  151.     unsigned char version;
  152.     unsigned char flags;
  153.     char          name[210];
  154.   } nm;
  155.   int len, slen;
  156.   int index = 0;
  157.   int total = 0;
  158.  
  159.   for (;;) {
  160.     if (!Get_System_Use_Field (p_volume, p_dir, "NM",
  161.                      (char *) &nm, sizeof (nm), index))
  162.       return -1;
  163.  
  164.     slen = nm.length-5;
  165.     len = (p_buf_len < slen) ? p_buf_len : slen;
  166.     if (len)
  167.       memcpy (p_buf, nm.name, len);
  168.  
  169.     total += len;
  170.     if (!(nm.flags & 1))
  171.       return total;
  172.  
  173.     p_buf += len;
  174.     p_buf_len -= len;
  175.     index++;
  176.   }
  177. }
  178.  
  179. /* Returns 1 if the PX system use field indicates a symbolic link.
  180.  */
  181.  
  182. int Is_A_Symbolic_Link (VOLUME *p_volume, directory_record *p_dir)
  183. {
  184.   __unaligned struct px_system_use_field {
  185.     char      id[2];
  186.     unsigned char length;
  187.     unsigned char version;
  188.     unsigned long mode_i;
  189.     unsigned long mode_m;
  190.     unsigned long links_i;
  191.     unsigned long links_m;
  192.     unsigned long user_id_i;
  193.     unsigned long user_id_m;
  194.     unsigned long group_id_i;
  195.     unsigned long group_id_m;
  196.   } px;
  197.  
  198.   if (!Get_System_Use_Field (p_volume, p_dir, "PX", (char *) &px, sizeof (px), 0))
  199.     return 0;
  200.  
  201.   /* 0120000 is the POSIX code for symbolic links:
  202.    */
  203.   return (px.mode_m & 0770000) == 0120000;
  204. }
  205.  
  206. /* Read content of SL system use field.
  207.  * A full path name (starting with ":" or "sys:") will always be returned.
  208.  */
  209.  
  210. t_bool Get_Link_Name (CDROM_OBJ *p_obj, char *p_buf, int p_buf_len)
  211. {
  212.   unsigned char buf[256];
  213.   char out[530];
  214.   int index = 0;
  215.   int len;
  216.   int offs;
  217.   char c;
  218.  
  219.   out[0] = 0;
  220.   for (;; ) {
  221.     if (!Get_System_Use_Field (p_obj->volume, OBJ(p_obj,dir), "SL", (char *) buf,
  222.                      sizeof (buf), index)) {
  223.       return (index == 0) ? 0 : 1;
  224.     }
  225.  
  226.     offs = 5;
  227.     for (;;) {
  228.  
  229.       if (strlen (out) > 256)
  230.         return 0;
  231.  
  232.       if (index == 0 && offs == 5) {
  233.         /* handle the first component record: */
  234.  
  235.         if (buf[5] & 4) /* parent directory */ {
  236.           CDROM_OBJ *parent1 = Find_Parent (p_obj);
  237.       CDROM_OBJ *parent2;
  238.       char fullpath[256];
  239.       if (!parent1)
  240.         return 0;
  241.           parent2 = Find_Parent (parent1);
  242.       if (!parent2)
  243.         return 0;
  244.       if (!Full_Path_Name (parent2, fullpath, sizeof (fullpath)))
  245.         return 0;
  246.       Close_Object (parent1);
  247.       Close_Object (parent2);
  248.       strcat (out, fullpath);
  249.       if (out[1] != 0)
  250.         strcat (out, "/");
  251.         } else if (buf[5] & 8) /* root */
  252.           strcat (out, "sys:");
  253.         else if (buf[5] & 16) /* volume root */
  254.           strcat (out, ":");
  255.         else { /* current directory */
  256.           CDROM_OBJ *parent = Find_Parent (p_obj);
  257.       char fullpath[256];
  258.       if (!parent)
  259.         return 0;
  260.       if (!Full_Path_Name (parent, fullpath, sizeof (fullpath)))
  261.         return 0;
  262.       Close_Object (parent);
  263.       strcat (out, fullpath);
  264.       if (out[1] != 0)
  265.         strcat (out, "/");
  266.         }
  267.       }
  268.  
  269.       if (out[0] && (c = out[strlen(out)-1]) != ':' && c != '/')
  270.         strcat (out, "/");
  271.  
  272.       if (buf[offs] & 32) /* host name */
  273.         strcat (out, "AMIGA");
  274.  
  275.       len = strlen (out);
  276.       memcpy (out + len, buf + offs + 2, buf[offs+1]);
  277.       out[len + buf[offs+1]] = 0;
  278.  
  279.       offs += 2 + buf[offs+1];
  280.       if (offs >= buf[2])
  281.         break;
  282.     }
  283.     if (!(buf[4] & 1)) /* continue flag */
  284.       break;
  285.     index++;
  286.   }
  287.   
  288.   strncpy (p_buf, out, p_buf_len - 1);
  289.   p_buf[p_buf_len-1] = 0;
  290.   return 1;
  291. }
  292.  
  293. /* Check whether a system use field is present: */
  294.  
  295. int Has_System_Use_Field (VOLUME *p_volume, directory_record *p_dir,
  296.               char *p_name)
  297. {
  298.   return Get_System_Use_Field (p_volume, p_dir, p_name, NULL, 0, 0);
  299. }
  300.  
  301. /* Return content of "CL" system use field, or -1, if no such system use field
  302.  * is present.
  303.  */
  304.  
  305. long RR_Child_Link (VOLUME *p_vol