davbkup.c

来自「FDI Intel开发的FLASH文件系统,功能很强大」· C语言 代码 · 共 620 行 · 第 1/2 页

C
620
字号
  ###
  ### RETURNS:
  ###   When this function passes with no errors a value of 0 is
  ###   returned otherwise, it returns a status of type ERR_CODE.
  ###*/

ERR_CODE BKUP_CreateBackupObject( SEARCH_SearchInfoPtr aSearchInfoPtr,
                                  FDI_Handle aBkupHandle,
                                  BOOLEAN restart)
{
   ERR_CODE status = ERR_NONE;
   HDR_FixedHeader bkupHeader;
   FDI_Handle bkupHeaderHandle = FDI_InvalidObjAddress;
   BOOLEAN convertingFreeChunk = FALSE;
   BOOLEAN completingPartialBackup = FALSE;
   SEARCH_SearchInfo searchInfo;
   SEARCH_CompareInfo compareInfo;

   /* create a fixed header for the backup object */
   bkupHeader.Attr1 = aSearchInfoPtr->HeaderPtr->FHdr.Attr1;
   bkupHeader.Attr2 = aSearchInfoPtr->HeaderPtr->FHdr.Attr2;
   FHDR_SetHeaderIndexOffset(&bkupHeader, ((aBkupHandle-FDI_FirstObjectAddress)/FDI_PageSize));

   /** ==================================================================== **/
   /** Check for an free chunk at this address.  If found, we are           **/
   /**   converting a free chunk, and not using the free pool               **/
   /** ==================================================================== **/
   SEARCH_Header2SearchInfo((&searchInfo), (&FDI_SearchHeader));             
   compareInfo.CompareValue = aBkupHandle;                   
   compareInfo.NamePtr      = 0;
   compareInfo.NameSize     = 0;
   status = SEARCH_GetNextHeader(&searchInfo, SEARCH_ByObjectHeaderAddress, &compareInfo, FALSE); 
   if (status == ERR_NONE)
   {
      /* Found an existing object, but is it a free chunk? */
      if (FHDR_GetAllocationStatus(&(searchInfo.HeaderPtr->FHdr)) == HDR_ALLOC_EMPTY)
      {
         convertingFreeChunk = TRUE;
         /* set the allocation status for this free chunk to absorbing */
         status = FHDR_AbsorbingHeaderInFlash(searchInfo.CurrObj.HeaderAddress,
                                              &(searchInfo.HeaderPtr->FHdr));
         CleanupAndExitOnError(status);
      }
      else
      {
         /* backup object already exists, partially or fully */
         completingPartialBackup = TRUE;
         bkupHeaderHandle = searchInfo.CurrObj.HeaderAddress;
         bkupHeader.Attr1 = searchInfo.HeaderPtr->FHdr.Attr1;
         bkupHeader.Attr2 = searchInfo.HeaderPtr->FHdr.Attr2;
      }
   }

   /* Create the new backup fixed header */
   if (!completingPartialBackup)
   {
      FDI_LastHeaderEntry = bkupHeaderHandle = FDI_NextFreeHeaderEntry;                    
      FDI_NextFreeHeaderEntry -= FHDR_CalcHeaderSize();
      status = FHDR_CreateFixedHeaderBackup(
                   bkupHeaderHandle,
                   &bkupHeader);
      CleanupAndExitOnError(status);
   }

   if (convertingFreeChunk == TRUE)
   {
      /* Create the new free chunk header if any leftover space */
      if (FHDR_GetSize(&bkupHeader) < FHDR_GetSize(&(searchInfo.HeaderPtr->FHdr)))
      {
         FDI_LastHeaderEntry = FDI_NextFreeHeaderEntry;                    
         FDI_NextFreeHeaderEntry -= FHDR_CalcHeaderSize();
         status = FHDR_CreateFreeChunkHeader(
            FDI_LastHeaderEntry,
            FHDR_GetHeaderIndexOffset(&bkupHeader) + FHDR_GetSize(&bkupHeader), 
            FHDR_GetSize(&(searchInfo.HeaderPtr->FHdr)) - FHDR_GetSize(&bkupHeader));
         CleanupAndExitOnError(status);
      }
   }

   /* copy the object header and object data to the new location */
   status = FLASH_CopyFlash (aSearchInfoPtr->CurrObj.ObjectAddress,
                             aBkupHandle,
                             aSearchInfoPtr->CurrObj.ObjectSize);
   CleanupAndExitOnError(status);

   /* validate the backup header in flash */
   status = FHDR_ValidateHeaderInFlash(bkupHeaderHandle, &bkupHeader);
   CleanupAndExitOnError(status);

   if (convertingFreeChunk == TRUE)
   {
      /* finish absorbing the original free chunk header */
      status = FHDR_AbsorbedHeaderInFlash(searchInfo.CurrObj.HeaderAddress,
                                          &(searchInfo.HeaderPtr->FHdr));
      CleanupAndExitOnError(status);
   }

   /* update memory stats */
   status = MEM_CalcMemoryStatistics(restart);

   return status;
}

/*#################################################################
  ### BKUP_FindOrigObjForBackupObj
  ###
  ### DESCRIPTION:
  ###   This function finds the object that corresponds to the
  ###   backup object. We search the entire system to insure that
  ###   we have only one corresponding object.
  ###
  ### PARAMETERS:
  ###   aBkupObjHandle -- A backup object to match.
  ###   aOrigObjHandle -- A corresponding object (not the backup )
  ###
  ### RETURNS:
  ###   When this function passes with no errors a value of 0 is
  ###   returned otherwise, it returns a status of type ERR_CODE.
  ###*/
ERR_CODE BKUP_FindOrigObjForBackupObj(FDI_Handle aBkupObjHandle, FDI_Handle* aOrigObjHandle)
{
   ERR_CODE status;

   SEARCH_SearchInfo  result;
   SEARCH_CompareInfo compare_info;
   HDR_Header         hdrhdr;
   HDR_ObjectHeader   objHeader;

   *aOrigObjHandle = 0;

   /* Read the backup leader */
   status = OHDR_ReadObjectHeader(aBkupObjHandle, &objHeader, TRUE);
   if(status != 0)
   {
      return status;
   }

   compare_info.NamePtr       = objHeader.Name;
   compare_info.NameSize      = objHeader.NameSize;
   compare_info.CompareValue  = objHeader.Type;
   compare_info.CompareValue2 = 0;

   /* Find a page object that matches the backup object*/
   result.CurrObj.HeaderAddress = 0;
   result.HeaderPtr             = &hdrhdr;
   status = SEARCH_GetNextHeader(&result, SEARCH_ByNameType, &compare_info, FALSE);
   while (status != ERR_NO_MORE_ENTRIES)
   {
      /* This is a valid object? */
      if(FHDR_IsUserChunk(&result.HeaderPtr->FHdr) == TRUE)
      {
         /* Yes. Make sure this is not the backup */
         if(FHDR_GetReallocationStatus(&result.HeaderPtr->FHdr) != HDR_REALLOC_BACKUP_COMPLETE)
         {
            /* Did we already find a master? */
            if(*aOrigObjHandle != 0)
            {
               /* Yes. We have a single backup with two master objects */
               return ERR_STATE;
            }
            *aOrigObjHandle = result.CurrObj.ObjectAddress;
         }
      }
      status = SEARCH_GetNextHeader(&result, SEARCH_ByNameType, &compare_info, FALSE);
   } /* while */

   /* If needed, reclassify the error code */
   if(status != ERR_NONE )
   {
      if(status == ERR_NO_MORE_ENTRIES)
      {
         status = ERR_NONE;
      }
      ExitOnError(status);
   }

   return status;
}


/*#################################################################
  ### BKUP_CalcValidBackupsInSystem
  ###
  ### DESCRIPTION:
  ###   This function searches the entire system for all the backups
  ###   it can find and returns a count. Only the last backup handle
  ###   found is returned.
  ###
  ### PARAMETERS:
  ###   aBackupCountPtr  -- The number of backups found.
  ###   aBackupHandlePtr -- The handle to the backup header.
  ###
  ### RETURNS:
  ###   When this function passes with no errors a value of 0 is
  ###   returned otherwise, it returns a status of type ERR_CODE.
  ###*/
ERR_CODE BKUP_CalcValidBackupsInSystem(UINT16* aBackupCountPtr, FDI_Handle* aHdrHandle, FDI_Handle* aObjHandle)
{
   ERR_CODE status;

   SEARCH_SearchInfo  result;
   SEARCH_CompareInfo compare_info;
   HDR_Header         hdrhdr;

   *aBackupCountPtr = 0;
   *aHdrHandle      = 0;
   *aObjHandle      = 0;

   /* Find all headers that are backups */
   result.CurrObj.HeaderAddress = 0;
   result.CurrObj.ObjectAddress = 0;
   result.CurrObj.ObjectSize    = 0;
   result.HeaderPtr             = &hdrhdr;
   status = SEARCH_GetNextHeader(&result, SEARCH_ByNextValidPageObject, &compare_info, FALSE);
   while (status != ERR_NO_MORE_ENTRIES)
   {
      /* Did we get something? */
      if(status == ERR_NONE)
      {
         /* Yes. Is this a backup? */
         if(FHDR_GetReallocationStatus(&result.HeaderPtr->FHdr) == HDR_REALLOC_BACKUP_COMPLETE)
         {
            /* Yes. Increment the counter and recored the header address */
            *aBackupCountPtr = *aBackupCountPtr + 1;
            *aHdrHandle      = result.CurrObj.HeaderAddress;
            *aObjHandle      = result.CurrObj.ObjectAddress;
         }
      }

      status = SEARCH_GetNextHeader(&result, SEARCH_ByNextValidPageObject, &compare_info, FALSE);
   } /* while */

   /* If needed, reclassify the error code */
   if(status != ERR_NONE )
   {
      if(status == ERR_NO_MORE_ENTRIES)
      {
         status = ERR_NONE;
      }
      ExitOnError(status);
   }

   return status;
}

/*#################################################################
  ### BKUP_CalcWipBackupsInSystem
  ###
  ### DESCRIPTION:
  ###   This function searches the entire system for all the backups
  ###   it can find (VALID, WIP) and returns a count. Only the last 
  ###   backup handle found is returned.
  ###
  ### PARAMETERS:
  ###   aBackupCountPtr  -- The number of backups found.
  ###   aBackupHandlePtr -- The handle to the backup header.
  ###
  ### RETURNS:
  ###   When this function passes with no errors a value of 0 is
  ###   returned otherwise, it returns a status of type ERR_CODE.
  ###*/
ERR_CODE BKUP_CalcWipBackupsInSystem(UINT16* aBackupCountPtr, FDI_Handle* aBackupHandlePtr)
{
   ERR_CODE status;

   SEARCH_SearchInfo  result;
   SEARCH_CompareInfo compare_info;
   HDR_Header         hdrhdr;

   *aBackupCountPtr  = 0;
   *aBackupHandlePtr = 0;

   /* Find all headers that are backups */
   result.CurrObj.HeaderAddress = 0;
   result.CurrObj.ObjectAddress = 0;
   result.CurrObj.ObjectSize    = 0;
   result.HeaderPtr             = &hdrhdr;
   status = SEARCH_GetNextHeader(&result, SEARCH_ByNextPageObject, &compare_info, FALSE);
   while (status != ERR_NO_MORE_ENTRIES)
   {
      /* Did we get something? */
      if(status == ERR_NONE)
      {
         /* Yes. Is this a backup? */
         if(FHDR_GetReallocationStatus(&result.HeaderPtr->FHdr) == HDR_REALLOC_BACKUP_COMPLETE)
         {
            /* Yes. Increment the counter and recored the header address */
            *aBackupCountPtr  = *aBackupCountPtr + 1;
            *aBackupHandlePtr = result.CurrObj.HeaderAddress;
         }
      }

      status = SEARCH_GetNextHeader(&result, SEARCH_ByNextPageObject, &compare_info, FALSE);
   } /* while */

   /* If needed, reclassify the error code */
   if(status != ERR_NONE )
   {
      if(status == ERR_NO_MORE_ENTRIES)
      {
         status = ERR_NONE;
      }
      ExitOnError(status);
   }

   return status;
}

#endif /* DIRECT_ACCESS_VOLUME */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?