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