home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / the25.zip / thesrc251.zip / os2eas.c < prev    next >
C/C++ Source or Header  |  1998-04-20  |  19KB  |  450 lines

  1. /***********************************************************************/
  2. /* OS2EAS.C - OS/2 Extended Attribute Preservation Routines            */
  3. /***********************************************************************/
  4. /*
  5.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  6.  * Copyright (C) 1991-1997 Mark Hessling
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  29.  * This software is going to be maintained and enhanced as deemed
  30.  * necessary by the community.
  31.  *
  32.  * Mark Hessling                 Email:             M.Hessling@qut.edu.au
  33.  * PO Box 203                    Phone:                    +617 3802 0800
  34.  * Bellara                       http://www.gu.edu.au/gext/the/markh.html
  35.  * QLD 4507                      **** Maintainer PDCurses & REXX/SQL ****
  36.  * Australia                     ************* Author of THE ************
  37.  *
  38.  *======================================================================
  39.  * This code based heavily on sample code provided as part of the IBM 
  40.  * OS/2 CSet/2 1.0 compiler.
  41.  *======================================================================
  42.  *
  43.  */
  44.  
  45. /*
  46. $Id: os2eas.c 2.1 1995/06/24 16:30:41 MH Rel MH $
  47. */
  48. #ifdef __EMX__                                /* EMX has its own routines, which   */
  49.                                       /* work in both read and protected   */
  50.                                       /* mode.                             */
  51. #include <the.h>
  52. #include <sys/ead.h>
  53.  
  54. static _ead CurrentFileEAs = NULL;
  55.  
  56. /****************************************************************\
  57.  *                                                            *
  58.  *  Name:    ReadEAs(filename)                                        *
  59.  *                                                            *
  60.  *  Purpose: Call EMX to Query a file's EA names and values.    *
  61.  *                                                            *
  62.  *  Usage:                                                    *
  63.  *                                                            *
  64.  *  Method : NOTE:  This routine does NOT prevent other       *
  65.  *         processes from accessing the file's EAs while it is*
  66.  *         reading them in, or while the program is editing   *
  67.  *         them.                                              *
  68.  *                                                            *
  69.  *  Returns: 0          if it successfully reads in the EAs. Upon exit*
  70.  *         , CurrentFileEAs holds the EAs for the current     *
  71.  *         file. Any EAs previously saved will be destroyed.  *
  72.  *                                                            *
  73. \****************************************************************/
  74. bool ReadEAs(CHARTYPE *filename)
  75. {
  76.    if (CurrentFileEAs != NULL)                /* should not happen                 */
  77.    {
  78.       _ead_destroy(CurrentFileEAs);
  79.       CurrentFileEAs = NULL;
  80.    }
  81.  
  82.    if ((CurrentFileEAs = _ead_create()) == NULL)
  83.     return (1);
  84.    if (_ead_read(CurrentFileEAs, filename, 0, 0) != 0)
  85.    {
  86.       _ead_destroy(CurrentFileEAs);
  87.       CurrentFileEAs = NULL;
  88.       return (1);
  89.    }
  90.    return(0);
  91. }
  92.  
  93. /****************************************************************\
  94.  *                                                            *
  95.  *  Name:    WriteEAs(filename)                               *
  96.  *                                                            *
  97.  *  Purpose: Writes all EAs of CurrentFileEAs to the file     *
  98.  *         and destroys the contents of CurrentFileEAs.       *
  99.  *         CurrentFileEAs may be empty.                       *
  100.  *         You should never nest ReadEAs und WriteEAs.        *
  101.  *                                                            *
  102.  *  Returns: 0          if successful,  1     otherwise               *
  103.  *                                                            *
  104. \****************************************************************/
  105. bool WriteEAs(CHARTYPE *filename)
  106. {
  107.    bool retval;
  108.  
  109.    if (CurrentFileEAs == NULL)
  110.       return (1);
  111.    if (_ead_write(CurrentFileEAs, filename, 0, 0) < 0)
  112.       retval = 1;
  113.      else
  114.       retval = 0;
  115.  
  116.   _ead_destroy(CurrentFileEAs);
  117.   CurrentFileEAs = NULL;
  118.   return (retval);
  119. }
  120. #else                                 /* not EMX                           */
  121. #define INCL_DOSPROCESS     
  122. #include <the.h>
  123.  
  124. /* The HoldFEA is used to hold individual EAs.  The member names correspond
  125.    directly to those of the FEA structure.  Note however, that both szName
  126.    and aValue are pointers to the values.  An additional field, next, is
  127.    used to link the HoldFEA's together to form a linked list. */
  128.  
  129. struct _HoldFEA
  130. {
  131.  ULONG oNextEntryOffset;
  132.  BYTE fEA;       
  133.  BYTE cbName;
  134.  USHORT cbValue;
  135.  CHAR *szName;
  136.  CHAR *aValue;
  137.  struct _HoldFEA *next;
  138. };
  139. typedef struct _HoldFEA HOLDFEA;
  140.  
  141. static VOID  Free_FEAList(HOLDFEA *);
  142. static VOID  GetMem(PVOID *pvMessage, ULONG ulSize);
  143.  
  144. /*--------------------------------------------------------------*\
  145.  *  Other constants                                             *
  146. \*--------------------------------------------------------------*/
  147. #define MAX_GEA            500L  /* Max size for a GEA List                 */
  148. #define Ref_ASCIIZ            1  /* Reference type for DosEnumAttribute     */
  149.  
  150. /* definition of level specifiers. required for File Info */
  151.  
  152. #define GetInfoLevel1         1            /* Get info from SFT             */
  153. #define GetInfoLevel2         2            /* Get size of FEAlist           */
  154. #define GetInfoLevel3         3            /* Get FEAlist given the GEAlist */
  155. #define GetInfoLevel4         4            /* Get whole FEAlist             */
  156. #define GetInfoLevel5         5            /* Get FSDname                   */
  157.  
  158. #define SetInfoLevel1         1            /* Set info in SFT               */
  159. #define SetInfoLevel2         2            /* Set FEAlist                   */
  160.  
  161.  
  162. /*--------------------------------------------------------------*\
  163.  *  Global variables                                            *
  164. \*--------------------------------------------------------------*/
  165. CHAR    *pAlloc,*szEditBuf;
  166. HOLDFEA *pHoldFEA;                         /* Global EA linked-list pointer */
  167.  
  168. /****************************************************************\
  169.  *                                                              *
  170.  *  Name:    ReadEAs(filename)                                  *
  171.  *                                                              *
  172.  *  Purpose: Call DOS to Query a file's EA names and values.    *
  173.  *                                                              *
  174.  *  Usage:                                                      *
  175.  *                                                              *
  176.  *  Method : Routine does NOT do full memory error trapping.    *
  177.  *           NOTE:  This routine does NOT prevent other         *
  178.  *           processes from accessing the file's EAs while it is*
  179.  *           reading them in, or while the program is editing   *
  180.  *           them.                                              *
  181.  *                                                              *
  182.  *  Returns: 0    if it successfully reads in the EAs. Upon exit*
  183.  *           , globalp HoldFEA points to a linked list of the   *
  184.  *           EAs for the current file.                          *
  185.  *                                                              *
  186. \****************************************************************/
  187. bool ReadEAs(CHARTYPE *filename)
  188. {
  189.  CHAR *pAllocc=NULL; /* Holds the FEA struct returned by DosEnumAttribute */
  190.                      /*  used to create the GEA2LIST for DosQueryPathInfo */
  191.  CHAR *pBigAlloc=NULL; /* Temp buffer to hold each EA as it is read in    */
  192.  USHORT cbBigAlloc=0;  /* Size of buffer                                  */
  193.  
  194.  ULONG ulEntryNum = 1; /* count of current EA to read (1-relative)        */
  195.  ULONG ulEnumCnt;      /* Number of EAs for Enum to return, always 1      */
  196.  
  197.  HOLDFEA *pLastIn=0;    /* Points to last EA added, so new EA can link    */
  198.  HOLDFEA *pNewFEA=NULL; /* Struct to build the new EA in                  */
  199.  
  200.  FEA2 *pFEA;           /* Used to read from Enum's return buffer          */
  201.  GEA2LIST *pGEAList;/*Ptr used to set up buffer for DosQueryPathInfo call */
  202.  EAOP2  eaopGet;       /* Used to call DosQueryPathInfo                   */
  203.                                  /* Allocate enough room for any GEA List */
  204.  GetMem((PPVOID)&pAllocc, MAX_GEA);
  205.  pFEA = (FEA2 *) pAllocc;               /* pFEA always uses pAlloc buffer */
  206.  
  207.  pHoldFEA = '\0';            /* Reset the pointer for the EA linked list  */
  208.  
  209.  while(1)                /* Loop continues until there are no more EAs */
  210.    {
  211.     ulEnumCnt = 1;                /* Only want to get one EA at a time */
  212.     if(DosEnumAttribute(Ref_ASCIIZ,         /* Read into pAlloc Buffer */
  213.                         (CHAR *)filename,   /* Note that this does not */
  214.                         ulEntryNum,         /* get the aValue field,   */
  215.                         pAllocc,            /* so DosQueryPathInfo must*/
  216.                         MAX_GEA,            /* be called to get it.    */
  217.                         &ulEnumCnt,
  218.                         (LONG) GetInfoLevel1))
  219.       break;                           /* There was some sort of error */
  220.  
  221.     if(ulEnumCnt != 1)                   /* All the EAs have been read */
  222.        break;
  223.  
  224.     ulEntryNum++;
  225.  
  226.     GetMem((PPVOID)&pNewFEA, sizeof(HOLDFEA));
  227.  
  228.     if (pNewFEA == '\0')                              /* Out of memory */
  229.       {
  230.        DosFreeMem(pAllocc);
  231.        Free_FEAList(pHoldFEA);
  232.        return (1);
  233.       }
  234.  
  235.     pNewFEA->cbName = pFEA->cbName;   /* Fill in the HoldFEA structure */
  236.     pNewFEA->cbValue= pFEA->cbValue;
  237.     pNewFEA->fEA    = pFEA->fEA;
  238.     pNewFEA->next = '\0';
  239.  
  240.     GetMem((PPVOID)&pNewFEA->szName, pFEA->cbName +1); /* Allocate for 2 arrays */
  241.     GetMem((PPVOID)&pNewFEA->aValue, pFEA->cbValue);
  242.  
  243.     if (!pNewFEA->szName || !pNewFEA->aValue)          /* Out of memory */
  244.       {
  245.        if(pNewFEA->szName)
  246.           DosFreeMem(pNewFEA->szName);
  247.        if(pNewFEA->aValue)
  248.           DosFreeMem(pNewFEA->aValue);
  249.  
  250.        DosFreeMem(pAllocc);
  251.        DosFreeMem(pNewFEA);
  252.  
  253.        Free_FEAList(pHoldFEA);
  254.        return (1);
  255.       }
  256.     strcpy(pNewFEA->szName,pFEA->szName);           /* Copy in EA Name */
  257.  
  258.     cbBigAlloc = sizeof(FEA2LIST) + pNewFEA->cbName+1 +
  259.                             pNewFEA->cbValue;
  260.     GetMem((PPVOID)&pBigAlloc, cbBigAlloc);
  261.     if (pBigAlloc == '\0')
  262.       {
  263.        DosFreeMem(pNewFEA->szName);
  264.        DosFreeMem(pNewFEA->aValue);
  265.        DosFreeMem(pAllocc);
  266.        DosFreeMem(pNewFEA);
  267.        Free_FEAList(pHoldFEA);
  268.        return (1);
  269.       }
  270.  
  271.     pGEAList = (GEA2LIST *) pAllocc;       /* Set up GEAList structure */
  272.  
  273.     pGEAList->cbList = sizeof(GEA2LIST) + pNewFEA->cbName; /* +1 for NULL */
  274.     pGEAList->list[0].oNextEntryOffset = 0L;
  275.     pGEAList->list[0].cbName = pNewFEA->cbName;
  276.     strcpy(pGEAList->list[0].szName,pNewFEA->szName);
  277.  
  278.     eaopGet.fpGEA2List = (GEA2LIST FAR *) pAllocc;
  279.     eaopGet.fpFEA2List = (FEA2LIST FAR *) pBigAlloc;
  280.  
  281.     eaopGet.fpFEA2List->cbList = cbBigAlloc;
  282.  
  283.     DosQueryPathInfo((CHAR *)filename,     /* Get the complete EA info */
  284.                      FIL_QUERYEASFROMLIST,
  285.                     (PVOID) &eaopGet,
  286.                      sizeof(EAOP2));
  287.     memcpy(pNewFEA->aValue,               /* Copy the value to HoldFEA */
  288.            pBigAlloc+sizeof(FEA2LIST)+pNewFEA->cbName,
  289.            pNewFEA->cbValue);
  290.  
  291.     DosFreeMem(pBigAlloc);                /* Release the temp Enum buffer */
  292.  
  293. #if 0
  294.     if(!CheckEAIntegrity(pNewFEA->aValue,pNewFEA->cbValue))  /* Bad EA */
  295.       {
  296.        FreeMem(pNewFEA->szName);
  297.        FreeMem(pNewFEA->aValue);
  298.        FreeMem(pNewFEA);
  299.        continue;                   /* Don't add this EA to linked list */
  300.       }
  301. #endif
  302.  
  303.     if(pHoldFEA == '\0')                  /* If first EA, set pHoldFEA */
  304.        pHoldFEA = pNewFEA;
  305.     else                              /* Otherwise, add to end of list */
  306.        pLastIn->next = pNewFEA;
  307.  
  308.     pLastIn = pNewFEA;                   /* Update the end of the list */
  309.    }
  310.  DosFreeMem(pAllocc);               /* Free up the GEA buf for DosEnum */
  311.  return (0);
  312. }
  313.  
  314. /****************************************************************\
  315.  *                                                              *
  316.  *  Name:    WriteEAs(filename)                                 *
  317.  *                                                              *
  318.  *  Purpose: This routine updates the EAs on disk to reflect    *
  319.  *           their current condition in memory.  First, all EAs *
  320.  *           in the delete list are removed from the disk, then *
  321.  *           all EAs in the pHoldFEA list are written to disk.  *
  322.  *  Usage  :                                                    *
  323.  *  Method : This routine is not bulletproof as it does not get *
  324.  *           exclusive access to the file EAs, nor does it      *
  325.  *           handle out of disk space sort of errors. Also,     *
  326.  *           memory fetches are not fully error trapped.        *
  327.  *                                                              *
  328.  *  Returns: 0    if successful,  1     otherwise               *
  329.  *                                                              *
  330. \****************************************************************/
  331. bool WriteEAs(CHARTYPE *filename)
  332. {
  333.  HOLDFEA    *pHFEA=pHoldFEA;
  334.  EAOP2      eaopWrite;
  335.  CHAR       aBuf[MAX_GEA],*aPtr=NULL;
  336.  FEA2       *pFEA = (FEA2 *) &aBuf[sizeof(ULONG)];
  337.  USHORT     usMemNeeded=0, usRet=0;
  338.  ULONG      *pulPtr=(ULONG *)aBuf;/* Initally points to top of FEALIST */
  339.  
  340.  eaopWrite.fpFEA2List=(FEA2LIST FAR *)aBuf; /* Setup fields that won't */
  341.  pFEA->fEA     = 0;                         /* change for the delete   */
  342.  pFEA->cbValue = 0;                         /* calls to DosSetPathInfo */
  343.  
  344.  while(pHFEA)                               /* Go through each HoldFEA */
  345.    {
  346.     usMemNeeded = sizeof(FEA2LIST) + pHFEA->cbName+1 + pHFEA->cbValue;
  347.     GetMem((PPVOID)&aPtr, usMemNeeded);
  348.     if (aPtr == NULL) /* GetMem failed to allocate */
  349.       {
  350.        Free_FEAList(pHoldFEA);     
  351.        return(1);
  352.       }
  353.  
  354.     eaopWrite.fpFEA2List = (FEA2LIST FAR *) aPtr;/* Fill in eaop struct */
  355.     eaopWrite.fpFEA2List->cbList = usMemNeeded;
  356.  
  357.     eaopWrite.fpFEA2List->list[0].fEA     = pHFEA->fEA;
  358.     eaopWrite.fpFEA2List->list[0].cbName  = pHFEA->cbName;
  359.     eaopWrite.fpFEA2List->list[0].cbValue = pHFEA->cbValue;
  360.  
  361.     strcpy(eaopWrite.fpFEA2List->list[0].szName, pHFEA->szName);
  362.     memcpy(eaopWrite.fpFEA2List->list[0].szName + pHFEA->cbName+1,
  363.            pHFEA->aValue, pHFEA->cbValue);
  364.  
  365.     usRet = DosSetPathInfo((CHAR *)filename,       /* Write out the EA */
  366.                            FIL_QUERYEASIZE,
  367.                            (PVOID) &eaopWrite,
  368.                            sizeof(EAOP2),
  369.                            DSPI_WRTTHRU);
  370.     if (usRet)
  371.       {
  372.        DosFreeMem(aPtr);                 /* Free up the FEALIST struct */
  373.        Free_FEAList(pHoldFEA);     
  374.        return(1);
  375.       }
  376.     DosFreeMem(aPtr);                    /* Free up the FEALIST struct */
  377.  
  378.     pHFEA = pHFEA->next;                           /* point to next EA */
  379.    }
  380.  Free_FEAList(pHoldFEA);     /* free up all memory associated with EAs */
  381.  return(0);
  382. }
  383.  
  384. /****************************************************************\
  385.  *                                                              *
  386.  *  Name:    Free_FEAList(pFEA)                                 *
  387.  *                                                              *
  388.  *  Purpose: This routine frees up the current list of EAs by   *
  389.  *           deallocating the space used by the szName and      *
  390.  *           aValue fields, then deallocating the HoldFEA struct*
  391.  *           Next, it deletes the EAName space.                 *
  392.  *  Usage :                                                     *
  393.  *  Method:  Note that EAS always passes in pHoldFEA.           *
  394.  *                    which is unnecessary since they are global*
  395.  *           pointers;however, this is done to make the routine *
  396.  *           more flexible by allowing multiple linked lists to *
  397.  *           exist.                                             *
  398.  *                                                              *
  399.  *  Returns: VOID.  The two linked lists passed in are cleaned  *
  400.  *           out though.                                        *
  401.  *                                                              *
  402. \****************************************************************/
  403. static VOID Free_FEAList(HOLDFEA *pFEA)
  404. {
  405.  HOLDFEA *next;    /* Holds the next field since we free the structure */
  406.                    /* before reading the current next field            */
  407.  
  408.  while(pFEA)
  409.    {
  410.     next = pFEA->next;
  411.     if(pFEA->szName)                         /* Free if non-NULL name  */
  412.        DosFreeMem(pFEA->szName);
  413.     if(pFEA->aValue)                         /* Free if non-NULL value */
  414.        DosFreeMem(pFEA->aValue);
  415.  
  416.     DosFreeMem(pFEA);                           /* Free HoldFEA struct */
  417.     pFEA = next;
  418.    }
  419.  return;
  420. }
  421.  
  422. /****************************************************************\
  423.  *                                                              *
  424.  *  Name:    GetMem (ppv, cb)                                   *
  425.  *                                                              *
  426.  *  Purpose: This routine returns a pointer to a available*     *
  427.  *           memory space.                                      *
  428.  *                                                              *
  429.  *  Usage  :                                                    *
  430.  *  Method : Routine should be bullet proof as it does its own  *
  431.  *           error checking. It assumes that hwnd points to the *
  432.  *           correct window with the name listbox in it.        *
  433.  *                                                              *
  434.  *  Returns: VOID, and allocated memory.                        *
  435.  *                                                              *
  436. \****************************************************************/
  437. static VOID GetMem (PVOID *ppv, ULONG cb)
  438. {
  439.  BOOL f;
  440.  
  441.  f =(BOOL) DosAllocMem(ppv, cb, fPERM|PAG_COMMIT);
  442.  if (f)
  443.    {
  444.     *ppv = NULL;
  445.     return;
  446.    }
  447.  return;
  448. }
  449. #endif
  450.