home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / disk / cdrom / amicdrom / source.lha / volumes.c < prev   
C/C++ Source or Header  |  1994-09-08  |  8KB  |  333 lines

  1. /* volumes.c:
  2.  *
  3.  * Volume management.
  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.  * 22-May-94   fmu   Performance improvement for lock+filehandle processing.
  14.  */
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #include "cdrom.h"
  20. #include "device.h"
  21. #include "devsupp.h"
  22. #include "generic.h"
  23.  
  24. extern char *g_vol_name;
  25. extern VOLUME *g_volume;
  26. extern CDROM_OBJ *g_top_level_obj;
  27. extern DEVLIST *DevList;
  28.  
  29. typedef struct lock_node {
  30.   LOCK *lock;
  31.   char *vol_name;
  32.   t_path_list pathlist;
  33.   struct lock_node *next;
  34. } t_lock_node;
  35.  
  36. t_lock_node *g_lock_list = NULL;
  37.  
  38. /*  Associate p_lock with the pathname of the locked object on the current
  39.  *  volume.
  40.  */
  41.  
  42. void Register_Lock (LOCK *p_lock)
  43. {
  44.   t_lock_node *new;
  45.   BUG(char pathname[300];)
  46.  
  47. #ifndef NDEBUG
  48.   if (!Full_Path_Name ((CDROM_OBJ*) p_lock->fl_Key, pathname, sizeof (pathname))) {
  49.     dbprintf ("[Cannot install lock / cannot determine pathname]");
  50.     return;
  51.   }
  52. #endif
  53.  
  54.   new = (t_lock_node*) AllocMem (sizeof (t_lock_node), MEMF_PUBLIC);
  55.   if (!new) {
  56.     BUG(dbprintf ("[Cannot install lock on '%s']", pathname);)
  57.     return;
  58.   }
  59.  
  60.   new->pathlist = Copy_Path_List (((CDROM_OBJ*) p_lock->fl_Key)->pathlist, FALSE);
  61.  
  62.   new->vol_name = (char*) AllocMem (strlen (g_vol_name+1) + 1,
  63.                       MEMF_PUBLIC);
  64.   if (!new->vol_name) {
  65.     BUG(dbprintf ("[Cannot install lock on '%s']", pathname);)
  66.     FreeMem (new, sizeof (t_lock_node));
  67.     return;
  68.   }
  69.   strcpy (new->vol_name, g_vol_name+1);
  70.  
  71.   new->lock = p_lock;
  72.   new->next = g_lock_list;
  73.   g_lock_list = new;
  74.   
  75.   BUG(dbprintf ("[Installing lock on '%s']", pathname);)
  76. }
  77.  
  78. /*  Remove the entry for p_lock in the list g_lock_list.
  79.  */
  80.  
  81. void Unregister_Lock (LOCK *p_lock)
  82. {
  83.   t_lock_node *ptr, *old;
  84.   BUG(char pathname[300];)
  85.  
  86.   for (ptr=g_lock_list, old = NULL; ptr; old = ptr, ptr = ptr->next)
  87.     if (ptr->lock == p_lock) {
  88. #ifndef NDEBUG
  89.       if (!Path_Name_From_Path_List (ptr->pathlist, pathname,
  90.                                      sizeof (pathname))) {
  91.         dbprintf ("[cannot determine pathname]");
  92.         return;
  93.       }
  94.       dbprintf ("[Removing lock from '%s']", pathname);
  95. #endif  
  96.       if (old)
  97.         old->next = ptr->next;
  98.       else
  99.         g_lock_list = ptr->next;
  100.       Free_Path_List (ptr->pathlist);
  101.       FreeMem (ptr->vol_name, strlen (ptr->vol_name) + 1);
  102.       FreeMem (ptr, sizeof (t_lock_node));
  103.       return;
  104.     }
  105.   BUG(dbprintf ("[Lock cannot be removed %08lx]", (unsigned long) p_lock);)
  106. }
  107.  
  108. /*  Update the fl_Key values for all locks that have been
  109.  *  registered for a volume with the name g_vol_name.
  110.  *  The fl_Key value is a pointer to a CDROM_OBJ object which
  111.  *  respresents the locked file or directory.
  112.  *
  113.  *  Returns the number of locks on the volume.
  114.  */
  115.  
  116. int Reinstall_Locks (void)
  117. {
  118.   t_lock_node *ptr;
  119.   CDROM_OBJ* obj;
  120.   int result = 0;
  121.   char pathname[300];
  122.  
  123.   for (ptr=g_lock_list; ptr; ptr = ptr->next) {
  124.     if (strcmp (g_vol_name+1, ptr->vol_name) == 0) {
  125.       result++;
  126.       if (!Path_Name_From_Path_List (ptr->pathlist, pathname, sizeof (pathname))) {
  127.         BUG(dbprintf ("[cannot determine pathname]");)
  128.         break;
  129.       }
  130.       BUG(dbprintf ("[Reinstalling lock on '%s'", pathname);)
  131.       if (ptr->pathlist != NULL) {
  132.         obj = Open_Object (g_top_level_obj, pathname);
  133.         if (obj) {
  134.           BUG(dbprintf ("]\n");)
  135.         } else {
  136.           BUG(dbprintf ("(FAILED) ]\n");)
  137.       continue;
  138.         }
  139.         ptr->lock->fl_Key = (LONG) obj;
  140.       } else {
  141.         ptr->lock->fl_Key = (LONG) 0;
  142.         BUG(dbprintf ("]\n");)
  143.       }
  144.     }
  145.   }
  146.   return result;
  147. }
  148.  
  149. typedef struct fh_node {
  150.   char *vol_name;
  151.   BUG(char *pathname;)
  152.   CDROM_OBJ* obj;
  153.   DEVLIST* devlist;
  154.   struct fh_node *next;
  155. } t_fh_node;
  156.  
  157. t_fh_node *g_fh_list = NULL;
  158.  
  159. /*  Associate p_obj with the pathname of the associated disk object on the current
  160.  *  volume.
  161.  */
  162.  
  163. void Register_File_Handle (CDROM_OBJ *p_obj)
  164. {
  165.   t_fh_node *new;
  166.   BUG(static char pathname[300];)
  167.  
  168. #ifndef NDEBUG
  169.   if (!Full_Path_Name (p_obj, pathname, sizeof (pathname))) {
  170.     BUG(dbprintf ("[Cannot install file handle / cannot determine pathname]");)
  171.     return;
  172.   }
  173. #endif
  174.  
  175.   new = (t_fh_node*) AllocMem (sizeof (t_fh_node), MEMF_PUBLIC);
  176.   if (!new) {
  177.     BUG(dbprintf ("[Cannot install file handle on '%s']", pathname);)
  178.     return;
  179.   }
  180.  
  181.   new->vol_name = (char*) AllocMem (strlen (g_vol_name+1) + 1,
  182.                       MEMF_PUBLIC);
  183.   if (!new->vol_name) {
  184.     BUG(dbprintf ("[Cannot install file handle on '%s']", pathname);)
  185.     FreeMem (new, sizeof (t_fh_node));
  186.     return;
  187.   }
  188.   strcpy (new->vol_name, g_vol_name+1);
  189.  
  190. #ifndef NDEBUG
  191.   new->pathname = (char*) AllocMem (strlen (pathname) + 1,
  192.                       MEMF_PUBLIC);
  193.   if (!new->pathname) {
  194.     BUG(dbprintf ("[Cannot install file handle on '%s']", pathname);)
  195.     FreeMem (new->vol_name, strlen (new->vol_name) + 1);
  196.     FreeMem (new, sizeof (t_fh_node));
  197.     return;
  198.   }
  199.   strcpy (new->pathname, pathname);
  200. #endif
  201.  
  202.   new->obj = p_obj;
  203.   new->devlist = DevList;
  204.   new->next = g_fh_list;
  205.   g_fh_list = new;
  206.   
  207.   BUG(dbprintf ("[Installing file handle on '%s']", pathname);)
  208. }
  209.  
  210. /*  Remove the entry for the file handle p_obj in the list g_fh_list.
  211.  */
  212.  
  213. void Unregister_File_Handle (CDROM_OBJ *p_obj)
  214. {
  215.   t_fh_node *ptr, *old;
  216.   
  217.   for (ptr=g_fh_list, old = NULL; ptr; old = ptr, ptr = ptr->next)
  218.     if (ptr->obj == p_obj && strcmp (g_vol_name+1, ptr->vol_name) == 0) {
  219.       if (old)
  220.         old->next = ptr->next;
  221.       else
  222.         g_fh_list = ptr->next;
  223.       BUG(dbprintf ("[Removing file handle from '%s']", ptr->pathname);)
  224.       BUG(FreeMem (ptr->pathname, strlen (ptr->pathname) + 1);)
  225.       FreeMem (ptr->vol_name, strlen (ptr->vol_name) + 1);
  226.       FreeMem (ptr, sizeof (t_fh_node));
  227.       return;
  228.     }
  229.   BUG(dbprintf ("[File handle cannot be removed]");)
  230. }
  231.  
  232. /*  Update the volume pointer for all CDROM_OBJs which are used as
  233.  *  filehandles for the current volume.
  234.  *
  235.  *  Returns the number of file handles on the volume.
  236.  */
  237.  
  238. int Reinstall_File_Handles (void)
  239. {
  240.   t_fh_node *ptr;
  241.   int result = 0;
  242.  
  243.   for (ptr=g_fh_list; ptr; ptr = ptr->next) {
  244.     if (strcmp (g_vol_name+1, ptr->vol_name) == 0) {
  245.       result++;
  246.       BUG(dbprintf ("[Reinstalling file handle on '%s']\n", ptr->pathname);)
  247.       ptr->obj->volume = g_volume;
  248.     }
  249.   }
  250.   return result;
  251. }
  252.  
  253. DEVLIST *Find_Dev_List (CDROM_OBJ *p_obj)
  254. {
  255.   t_fh_node *ptr;
  256.  
  257.   for (ptr=g_fh_list; ptr; ptr = ptr->next) {
  258.     if (ptr->obj == p_obj) {
  259.       return ptr->devlist;
  260.     }
  261.   }
  262.   BUG(dbprintf("\n[ERROR: file handle not found!]\n");)
  263.   return NULL;
  264. }
  265.  
  266. typedef struct vol_reg_node {
  267.   char *name;
  268.   DEVLIST *volume;
  269.   struct vol_reg_node *next;
  270. } t_vol_reg_node;
  271.  
  272. t_vol_reg_node *g_volume_list = NULL;
  273.  
  274. /*  Register a volume node as owned by this handler.
  275.  */
  276.  
  277. void Register_Volume_Node (DEVLIST *p_volume)
  278. {
  279.   t_vol_reg_node *new;
  280.   int len;
  281.   
  282.   new = (t_vol_reg_node*) AllocMem (sizeof (t_vol_reg_node), MEMF_PUBLIC);
  283.   if (!new)
  284.     return;
  285.  
  286.   new->volume = p_volume;
  287.   len = strlen ((char*) (BTOC (p_volume->dl_Name)) + 1);
  288.   new->name = (char*) AllocMem (len + 1, MEMF_PUBLIC);
  289.   if (!new) {
  290.     FreeMem (new, sizeof (t_vol_reg_node));
  291.     return;
  292.   }
  293.   memcpy (new->name, (char*) (BTOC (p_volume->dl_Name)) + 1, len);
  294.   new->name[len] = 0;
  295.   new->next = g_volume_list;
  296.   g_volume_list = new;
  297. }
  298.  
  299. /*  Remove the registration for the volume node.
  300.  */
  301.  
  302. void Unregister_Volume_Node (DEVLIST *p_volume)
  303. {
  304.   t_vol_reg_node *ptr, *old;
  305.  
  306.   for (ptr=g_volume_list, old=NULL; ptr; old=ptr, ptr=ptr->next) {
  307.     if (p_volume == ptr->volume) {
  308.       if (old)
  309.         old->next = ptr->next;
  310.       else
  311.         g_volume_list = ptr->next;
  312.       FreeMem (ptr->name, strlen (ptr->name) + 1);
  313.       FreeMem (ptr, sizeof (t_vol_reg_node));
  314.       return;
  315.     }
  316.   }
  317.   BUG(dbprintf("ERROR: cannot unregister volume node!\n");)
  318. }
  319.  
  320. /*  Find a volume node with a matching name.
  321.  */
  322.  
  323. DEVLIST *Find_Volume_Node (char *p_name)
  324. {
  325.   t_vol_reg_node *ptr;
  326.  
  327.   for (ptr=g_volume_list; ptr; ptr=ptr->next) {
  328.     if (Stricmp (ptr->name, p_name) == 0)
  329.       return ptr->volume;
  330.   }
  331.   return NULL;
  332. }
  333.