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