davsearch.c

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

C
1,358
字号
	         /* this object exists in page space above the current object */
            /* and it's closer than any previous objects found */
            prevOffset = candidateOffset;
            *aPrevHeaderHandlePtr = currHeaderHandle;
         }
      }
     currHeaderHandle = SEARCH_CalcNextHdrAddr(currHeaderHandle);
   } while ((status == ERR_NONE) && (FHDR_GetHeaderStatus(&currHeader) != HDR_HEADER_UNUSED));

   if (*aPrevHeaderHandlePtr == FDI_NextFreeHeaderEntry)
   {
      status = ERR_NO_MORE_ENTRIES;
   }

   return status;
}

/*#################################################################
  ### SEARCH_CalcNextInternalObjectHdrAddr
  ### 
  ### DESCRIPTION:
  ###   This function will take the current header handle and use it to
  ###   calculate the address of the header of the next object in page
  ###   space.
  ### 
  ### PARAMETERS:
  ###   aCurrentHeaderHandle - Address of the current header.
  ###   aNextHeaderHandlePtr - Ptr to address of the next header.
  ### 
  ### RETURNS:
  ###   Error status.
  ###*/
ERR_CODE SEARCH_CalcNextInternalObjectHdrAddr(FDI_Handle    aCurrentHeaderHandle, 
                                              FDI_HandlePtr aNextHeaderHandlePtr)
{
ERR_CODE        status = ERR_NONE;

HDR_FixedHeader currHeader;
UINT32          currOffset = 0;
FDI_Handle      currHeaderHandle = aCurrentHeaderHandle;
UINT32          nextOffset = HDR_INDEX_OFFSET_UNUSED;
UINT32          candidateOffset;

   /* set the initial value for currHeaderHandle if not valid */
   if (currHeaderHandle > FDI_ParaSpaceAddressTop || currHeaderHandle < FDI_ParaSpaceAddressBottom)
   {
      /* current header handle is not valid; set up to find first user object in page space */
      status = SEARCH_CalcFirstUserObjectHdrAddr(&currHeaderHandle);
      ExitOnError(status);
      /* if invalid current object hdr handle was given, return handle to first object header */
      *aNextHeaderHandlePtr = currHeaderHandle;
      return status;
   }

   /* set the initial value for the next header to the first free header entry */
   *aNextHeaderHandlePtr = FDI_NextFreeHeaderEntry;

   /* Read the current header */
   status = FHDR_ReadFixedHeader(currHeaderHandle, &currHeader, FALSE);
   ExitOnError(status);
   if (
      (FHDR_GetHeaderStatus(&currHeader) == HDR_HEADER_VALID) && 
      (FHDR_GetAbsorbStatus(&currHeader) != HDR_ABSORBED) && 
      (FHDR_IsPageObject(&currHeader))
      )
   {
      currOffset = FHDR_GetHeaderIndexOffset(&currHeader);
   }
   else
   {
      /* can't compute the next user object header given an internal object header */
      status = ERR_NOT_DONE;
   }
   ExitOnError(status);

   currHeaderHandle = DAV_MEM_FirstHeaderAddr;

   do
   {
      status = FHDR_ReadFixedHeader(currHeaderHandle, &currHeader, FALSE);
      ExitOnError(status);
      /* if the header points to a real internal object, dirty chunk or free chunk */
      if (
            (FHDR_GetHeaderStatus(&currHeader) == HDR_HEADER_VALID) && 
            (FHDR_GetAbsorbStatus(&currHeader) != HDR_ABSORBED) && 
            (FHDR_IsParaObject(&currHeader))
         )
      {
	      /* compare the offset in this header with the current header */
	      /* and with the header offset of the closest object found so far */
         candidateOffset = FHDR_GetHeaderIndexOffset(&currHeader);
         if ((candidateOffset > currOffset) && (candidateOffset < nextOffset))
	      {
	         /* this object exists in para space above the current object */
            /* and it's closer than any previous objects found */
            nextOffset = candidateOffset;
            *aNextHeaderHandlePtr = currHeaderHandle;
         }
      }
     currHeaderHandle = SEARCH_CalcNextHdrAddr(currHeaderHandle);
   } while ((status == ERR_NONE) && (FHDR_GetHeaderStatus(&currHeader) != HDR_HEADER_UNUSED));

   if (*aNextHeaderHandlePtr == FDI_NextFreeHeaderEntry)
   {
      status = ERR_NO_MORE_ENTRIES;
   }

   return status;
}

/*#################################################################
  ### SEARCH_Header2SearchInfo
  ###
  ### DESCRIPTION:
  ###   This function initializes search info structure for 
  ###   the first search.
  ###
  ### PARAMETERS:
  ###   aSrchInfoPtr - search info structure to initialize.
  ###   aHeaderPtr    - header that is used in the search info
  ###                   structure.
  ###
  ### RETURNS:
  ###   None.
  ###*/
void SEARCH_Header2SearchInfo(SEARCH_SearchInfoPtr aSrchInfoPtr,  /* formerly HDR_InitSearchInfo */
                              HDR_HeaderPtr        aHeaderPtr)
{
   aSrchInfoPtr->HeaderPtr = aHeaderPtr;
   aSrchInfoPtr->HeaderPtr->OHdr.NameSize = 0;
   aSrchInfoPtr->CurrObj.ObjectSize = 0;
   aSrchInfoPtr->CurrObj.HeaderAddress = 0;
   aSrchInfoPtr->CurrObj.ObjectAddress = FDI_InvalidObjAddress;
}

/*#################################################################
  ### SEARCH_FixedHeader2SearchInfo
  ###
  ### DESCRIPTION:
  ###   This function initializes search info structure for 
  ###   the first search.
  ###
  ### PARAMETERS:
  ###   aSrchInfoPtr - search info structure to initialize.
  ###   aHeaderPtr    - fixed header that is used in the search info
  ###                   structure.
  ###
  ### RETURNS:
  ###   None.
  ###*/
void SEARCH_FixedHeader2SearchInfo(SEARCH_SearchInfo2Ptr aSrchInfoPtr, /* formerly HDR_InitSearchInfo2 */
                                   HDR_FixedHeaderPtr    aHeaderPtr)
{
   aSrchInfoPtr->HeaderPtr->FHdr.Attr1 = aHeaderPtr->Attr1;
   aSrchInfoPtr->HeaderPtr->FHdr.Attr2 = aHeaderPtr->Attr2;
   aSrchInfoPtr->HeaderPtr->OHdr.NameSize = 0;
}


/*#################################################################
  ### SEARCH_SearchInfo2ObjectInfo
  ###
  ### DESCRIPTION:
  ###   This function copies all the information from search_info 
  ###   structure to the appropriate fields in the obj_info structure.
  ###
  ### PARAMETERS:
  ###   anObjInfoPtr -    IN:  Object info structure with all valid 
  ###                           header information.
  ###                     OUT: Will contain translated search info
  ###                           and header information.
  ###
  ###   aSrchInfoPtr - IN:  Must contain a pointer to a valid
  ###                           search info structure that contains
  ###                           physical object information to be
  ###                           translated.
  ###                     OUT: unchanged.
  ###
  ### RETURNS:
  ###   none.
  ###*/
void SEARCH_SearchInfo2ObjectInfo(FDI_ObjectInfoPtr    anObjInfoPtr,
                                  SEARCH_SearchInfoPtr aSrchInfoPtr)
{
   UINT32 ii;

   /* Copy the name field from the header to the search info struct. */
   anObjInfoPtr->NameSize = aSrchInfoPtr->HeaderPtr->OHdr.NameSize;
   for (ii = 0; ii < anObjInfoPtr->NameSize; ii++)
   {
      anObjInfoPtr->Name[ii] = aSrchInfoPtr->HeaderPtr->OHdr.Name[ii];
   }
   
   anObjInfoPtr->ObjectType = aSrchInfoPtr->HeaderPtr->OHdr.Type;
   anObjInfoPtr->SecurityKey = aSrchInfoPtr->HeaderPtr->OHdr.SecureId;
   anObjInfoPtr->ObjectAddress = aSrchInfoPtr->CurrObj.ObjectAddress + OHDR_CalcHeaderSize();
   
   /* Store the size of the object data in the object info pointer in pages. */
   /* Since fixed header stores cumulative size of */
   /* object leader header w/name and object data, */
   /* need to subtract leader header size */
   anObjInfoPtr->ObjectSize = 
            (FHDR_CalcObjectSize(&(aSrchInfoPtr->HeaderPtr->FHdr)) - OHDR_CalcHeaderSize()) / FDI_PageSize;
   
   /* Ability to detect WIP vs. Valid object */           
   if (FHDR_GetAllocationStatus(&(aSrchInfoPtr->HeaderPtr->FHdr)) == HDR_ALLOC_VALID)
   {
      anObjInfoPtr->WriteStatus = FDI_WriteStatusValid;
   }
   else
   {
      /* DAV Interface will not allow user to see invalid  */
      /* objects, therefore, it must be WIP.               */
      anObjInfoPtr->WriteStatus = FDI_WriteStatusWIP;
   }
}

/*#################################################################
  ### SEARCH_GetObjectSize
  ### 
  ### DESCRIPTION:
  ###   This function will load "size" with the size in bytes
  ###   based on the header pointed to by "aHeaderPtr".  THIS
  ###   INCLUDES THE SIZE OF THE OBJECT HEADER IN BYTES.
  ### 
  ### PARAMETERS: 
  ###   aHeaderPtr  - IN:  the header from which to read size fields.
  ###                 OUT: not used.
  ###   size_ptr    - IN:  not used.
  ###                 OUT: Size of the object that is stored in 
  ###                      the header's HiSize and LoSize fields.
  ### 
  ### RETURNS:
  ###   None.
  ###*/
UINT32 SEARCH_GetObjectSize(FDI_ObjectInfoPtr aInfoPtr)
{
   UINT32 result = 0;

   switch(aInfoPtr->ObjectType)
   {
   case FDI_HT_RecoveredHeader: /* A damaged header */
      break;

   case FDI_HT_ReclaimTable:    /* RT */
   case FDI_HT_ObjTrkTable:     /* OTT */
   case FDI_HT_ReAllocateTable: /* RAT */
   case FDI_HT_ConfigHeader:    /* CT */
      break;

   default:
      /* All normal object data is in page space */
      result = 
			OHDR_CalcHeaderSize() + 
			(aInfoPtr->ObjectSize * FDI_PageSize);
      break;
   } /* switch */

   return result;
}

/*#################################################################
  ### SEARCH_GetObjectSize2
  ### 
  ### DESCRIPTION:
  ###   Same as SEARCH_GetObjectSize, but uses the search info
  ###   structure.
  ### 
  ### PARAMETERS: 
  ###   aPtr          - IN:  the header from which to read size fields.
  ###                   OUT: not used.
  ###   aAlignFlag    - IN:  not used.
  ### 
  ### RETURNS:
  ###   None.
  ###*/
UINT32 SEARCH_GetObjectSize2(SEARCH_SearchInfoPtr aPtr)
{
   return FHDR_CalcObjectSize(&(aPtr->HeaderPtr->FHdr));
}


/*#################################################################
  ### SEARCH_ObjectInfo2Header
  ###
  ### DESCRIPTION:
  ###   This function copies all the information from obj_info 
  ###   structure to the appropriate fields in the header structure.
  ###
  ### PARAMETERS:
  ###   aHeaderPtr -   IN:  Must point to a valid header structure.
  ###                  OUT: Object information translated properly.
  ###
  ###   anObjInfoPtr - IN:  Object info structure with all valid 
  ###                        object information.
  ###                  OUT: unchanged.
  ###
  ### RETURNS:
  ###   none.
  ###*/
void SEARCH_ObjectInfo2Header(HDR_HeaderPtr   aHeaderPtr, 
                              FDI_ObjectInfo* anObjInfoPtr)
{
   UINT32 ii;
   UINT32 sizeOfOHdrInPages;

   /* All is well on the memory space front, so create the header. */
   /* Set up Power Loss Recovery Options                           */
   HDR_InitHeaderAttr1((aHeaderPtr->FHdr.Attr1));
   HDR_InitHeaderAttr2((aHeaderPtr->FHdr.Attr2));

   /* Highest level of recovery w/reserves */
   FHDR_SetAllocationStatus(&(aHeaderPtr->FHdr), HDR_ALLOC_WRITE_IN_PROGRESS);
   
   /* Calculate the size of the header (in pages) and place it in the */
   /*  proper fields.  Note that the FDI_ObjectInfo structure is never */
   /*  used to describe an internal object which would have size in bytes. */
   sizeOfOHdrInPages = (OHDR_CalcHeaderSize()%FDI_PageSize) ? OHDR_CalcHeaderSize()/FDI_PageSize : ((OHDR_CalcHeaderSize()/FDI_PageSize) + 1);
   
   FHDR_SetSize(&(aHeaderPtr->FHdr), (anObjInfoPtr->ObjectSize) + sizeOfOHdrInPages);
   
   /* Place the object type in the proper fields */
   FHDR_SetType(&(aHeaderPtr->FHdr), FDI_HT_NormalObject);

   /* Copy data into the object leader header */
   aHeaderPtr->OHdr.Type     = (UINT8)anObjInfoPtr->ObjectType;
   aHeaderPtr->OHdr.NameSize = anObjInfoPtr->NameSize;
   aHeaderPtr->OHdr.SecureId = anObjInfoPtr->SecurityKey;

   /* Copy the name to the aHeaderPtr */
   for (ii = 0; ii < anObjInfoPtr->NameSize; ii++)
   {
      aHeaderPtr->OHdr.Name[ii] = anObjInfoPtr->Name[ii];
   }
}
    
#endif /* DIRECT_ACCESS_VOLUME */

⌨️ 快捷键说明

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