⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pathsup.c

📁 winddk src目录下的文件系统驱动源码压缩!
💻 C
📖 第 1 页 / 共 2 页
字号:
    Fcb - This is the Fcb for the Path Table.

    BaseOffset - Offset of the first sector to map.  This will be on a
        sector boundary.

    PathContext - Enumeration context to update in this routine.

Return Value:

    None.

--*/

{
    ULONG CurrentLength;
    ULONG SectorSize;
    ULONG DataOffset;
    ULONG PassCount;
    PVOID Sector;

    PAGED_CODE();

    //
    //  Map the new block and set the enumeration context to this
    //  point.  Allocate an auxilary buffer if necessary.
    //

    CurrentLength = 2 * SECTOR_SIZE;

    if (CurrentLength >= (ULONG) (Fcb->FileSize.QuadPart - BaseOffset)) {

        CurrentLength = (ULONG) (Fcb->FileSize.QuadPart - BaseOffset);

        //
        //  We know this is the last data block for this
        //  path table.
        //

        PathContext->LastDataBlock = TRUE;
    }

    //
    //  Set context values.
    //

    PathContext->BaseOffset = (ULONG) BaseOffset;
    PathContext->DataLength = CurrentLength;

    //
    //  Drop the previous sector's mapping
    //

    CdUnpinData( IrpContext, &PathContext->Bcb );

    //
    //  Check if spanning a view section.  The following must
    //  be true before we take this step.
    //
    //      Data length is more than one sector.
    //      Starting offset must be one sector before the
    //          cache manager VACB boundary.
    //

    if ((CurrentLength > SECTOR_SIZE) &&
        (FlagOn( ((ULONG) BaseOffset), VACB_MAPPING_MASK ) == LAST_VACB_SECTOR_OFFSET )) {

        //
        //  Map each sector individually and store into an auxilary
        //  buffer.
        //

        SectorSize = SECTOR_SIZE;
        DataOffset = 0;
        PassCount = 2;

        PathContext->Data = FsRtlAllocatePoolWithTag( CdPagedPool,
                                                      CurrentLength,
                                                      TAG_SPANNING_PATH_TABLE );
        PathContext->AllocatedData = TRUE;

        while (PassCount--) {

            CcMapData( Fcb->FileObject,
                       (PLARGE_INTEGER) &BaseOffset,
                       SectorSize,
                       TRUE,
                       &PathContext->Bcb,
                       &Sector );

            RtlCopyMemory( Add2Ptr( PathContext->Data, DataOffset, PVOID ),
                           Sector,
                           SectorSize );

            CdUnpinData( IrpContext, &PathContext->Bcb );

            BaseOffset += SECTOR_SIZE;
            SectorSize = CurrentLength - SECTOR_SIZE;
            DataOffset = SECTOR_SIZE;
        }

    //
    //  Otherwise we can just map the data into the cache.
    //

    } else {

        //
        //  There is a slight chance that we have allocated an
        //  auxilary buffer on the previous sector.
        //

        if (PathContext->AllocatedData) {

            CdFreePool( &PathContext->Data );
            PathContext->AllocatedData = FALSE;
        }

        CcMapData( Fcb->FileObject,
                   (PLARGE_INTEGER) &BaseOffset,
                   CurrentLength,
                   TRUE,
                   &PathContext->Bcb,
                   &PathContext->Data );
    }

    return;
}


//
//  Local support routine
//

BOOLEAN
CdUpdatePathEntryFromRawPathEntry (
    IN PIRP_CONTEXT IrpContext,
    IN ULONG Ordinal,
    IN BOOLEAN VerifyBounds,
    IN PPATH_ENUM_CONTEXT PathContext,
    OUT PPATH_ENTRY PathEntry
    )

/*++

Routine Description:

    This routine is called to update the in-memory Path Entry from the on-disk
    path entry.  We also do a careful check of the bounds if requested and we
    are in the last data block of the path table.

Arguments:

    Ordinal - Ordinal number for this directory.

    VerifyBounds - Check that the current raw Path Entry actually fits
        within the data block.

    PathContext - Current path table enumeration context.

    PathEntry - Pointer to the in-memory path entry structure.

Return Value:

    TRUE  if updated ok,  
    FALSE if we've hit the end of the pathtable - zero name length && PT size is a multiple
          of blocksize.  This is a workaround for some Video CDs.  Win 9x works around this.

    This routine may raise.

--*/

{
    PRAW_PATH_ENTRY RawPathEntry = CdRawPathEntry( IrpContext, PathContext );
    ULONG RemainingDataLength;

    PAGED_CODE();
    
    //
    //  Check for a name length of zero.  This is the first byte of the record,
    //  and there must be at least one byte remaining in the buffer else we 
    //  wouldn't be here (caller would have spotted buffer end).
    //
    
    PathEntry->DirNameLen = CdRawPathIdLen( IrpContext, RawPathEntry );
    
    if (0 == PathEntry->DirNameLen) {

        //
        //  If we are in the last block,  and the path table size (ie last block) is a 
        //  multiple of block size,  then we will consider this the end of the path table
        //  rather than raising an error.  Workaround for NTI Cd Maker video CDs which
        //  round path table length to blocksize multiple.  In all other cases we consider
        //  a zero length name to be corruption.
        //
        
        if ( PathContext->LastDataBlock && 
             (0 == BlockOffset( IrpContext->Vcb, PathContext->DataLength)))  {
        
            return FALSE;
        }
        
        CdRaiseStatus( IrpContext, STATUS_DISK_CORRUPT_ERROR );
    }

    //
    //  Check if we should verify the path entry.  If we are not in the last
    //  data block then there is nothing to check.
    //
    
    if (PathContext->LastDataBlock && VerifyBounds) {

        //
        //  Quick check to see if the maximum size is still available.  This
        //  will handle most cases and we don't need to access any of the
        //  fields.
        //

        RemainingDataLength = PathContext->DataLength - PathContext->DataOffset;

        if (RemainingDataLength < sizeof( RAW_PATH_ENTRY )) {

            //
            //  Make sure the remaining bytes hold the path table entries.
            //  Do the following checks.
            //
            //      - A minimal path table entry will fit (and then check)
            //      - This path table entry (with dir name) will fit.
            //

            if ((RemainingDataLength < MIN_RAW_PATH_ENTRY_LEN) ||
                (RemainingDataLength < (ULONG) (CdRawPathIdLen( IrpContext, RawPathEntry ) + MIN_RAW_PATH_ENTRY_LEN - 1))) {

                CdRaiseStatus( IrpContext, STATUS_DISK_CORRUPT_ERROR );
            }
        }
    }

    //
    //  The ordinal number of this directory is passed in.
    //  Compute the path table offset of this entry.
    //

    PathEntry->Ordinal = Ordinal;
    PathEntry->PathTableOffset = PathContext->BaseOffset + PathContext->DataOffset;

    //
    //  We know we can safely access all of the fields of the raw path table at
    //  this point.
    
    //
    //  Bias the disk offset by the number of logical blocks
    //

    CopyUchar4( &PathEntry->DiskOffset, CdRawPathLoc( IrpContext, RawPathEntry ));

    PathEntry->DiskOffset += CdRawPathXar( IrpContext, RawPathEntry );

    CopyUchar2( &PathEntry->ParentOrdinal, &RawPathEntry->ParentNum );

    PathEntry->PathEntryLength = PathEntry->DirNameLen + MIN_RAW_PATH_ENTRY_LEN - 1;

    //
    //  Align the path entry length on a ushort boundary.
    //

    PathEntry->PathEntryLength = WordAlign( PathEntry->PathEntryLength );

    PathEntry->DirName = RawPathEntry->DirId;

    return TRUE;
}


//
//  Local support routine
//

VOID
CdUpdatePathEntryName (
    IN PIRP_CONTEXT IrpContext,
    IN OUT PPATH_ENTRY PathEntry,
    IN BOOLEAN IgnoreCase
    )

/*++

Routine Description:

    This routine will store the directory name into the CdName in the
    path entry.  If this is a Joliet name then we will make sure we have
    an allocated buffer and need to convert from big endian to little
    endian.  We also correctly update the case name.  If this operation is ignore
    case then we need an auxilary buffer for the name.

    For an Ansi disk we can use the name from the disk for the exact case.  We only
    need to allocate a buffer for the ignore case name.  The on-disk representation of
    a Unicode name is useless for us.  In this case we will need a name buffer for
    both names.  We store a buffer in the PathEntry which can hold two 8.3 unicode
    names.  This means we will almost never need to allocate a buffer in the Ansi case
    (we only need one buffer and already have 48 characters).

Arguments:

    PathEntry - Pointer to a path entry structure.  We have already updated
        this path entry with the values from the raw path entry.

Return Value:

    None.

--*/

{
    ULONG Length;
    NTSTATUS Status;

    PAGED_CODE();

    //
    //  Check if this is a self entry.  We use a fixed string for this.
    //
    //      Self-Entry - Length is 1, value is 0.
    //

    if ((*PathEntry->DirName == 0) &&
        (PathEntry->DirNameLen == 1)) {

        //
        //  There should be no allocated buffers.
        //

        ASSERT( !FlagOn( PathEntry->Flags, PATH_ENTRY_FLAG_ALLOC_BUFFER ));

        //
        //  Now use one of the hard coded directory names.
        //

        PathEntry->CdDirName.FileName = CdUnicodeDirectoryNames[0];

        //
        //  Show that there is no version number.
        //

        PathEntry->CdDirName.VersionString.Length = 0;

        //
        //  The case name is identical.
        //

        PathEntry->CdCaseDirName = PathEntry->CdDirName;

        //
        //  Return now.
        //

        return;
    }

    //
    //  Compute how large a buffer we will need.  If this is an ignore
    //  case operation then we will want a double size buffer.  If the disk is not
    //  a Joliet disk then we might need two bytes for each byte in the name.
    //

    Length = PathEntry->DirNameLen;

    if (IgnoreCase) {

        Length *= 2;
    }

    if (!FlagOn( IrpContext->Vcb->VcbState, VCB_STATE_JOLIET )) {

        Length *= sizeof( WCHAR );
    }

    //
    //  Now decide if we need to allocate a new buffer.  We will if
    //  this name won't fit in the embedded name buffer and it is
    //  larger than the current allocated buffer.  We always use the
    //  allocated buffer if present.
    //
    //  If we haven't allocated a buffer then use the embedded buffer if the data
    //  will fit.  This is the typical case.
    //

    if (!FlagOn( PathEntry->Flags, PATH_ENTRY_FLAG_ALLOC_BUFFER ) &&
        (Length <= sizeof( PathEntry->NameBuffer ))) {

        PathEntry->CdDirName.FileName.MaximumLength = sizeof( PathEntry->NameBuffer );
        PathEntry->CdDirName.FileName.Buffer = PathEntry->NameBuffer;

    } else {

        //
        //  We need to use an allocated buffer.  Check if the current buffer
        //  is large enough.
        //

        if (Length > PathEntry->CdDirName.FileName.MaximumLength) {

            //
            //  Free any allocated buffer.
            //

            if (FlagOn( PathEntry->Flags, PATH_ENTRY_FLAG_ALLOC_BUFFER )) {

                CdFreePool( &PathEntry->CdDirName.FileName.Buffer );
                ClearFlag( PathEntry->Flags, PATH_ENTRY_FLAG_ALLOC_BUFFER );
            }

            PathEntry->CdDirName.FileName.Buffer = FsRtlAllocatePoolWithTag( CdPagedPool,
                                                                             Length,
                                                                             TAG_PATH_ENTRY_NAME );

            SetFlag( PathEntry->Flags, PATH_ENTRY_FLAG_ALLOC_BUFFER );

            PathEntry->CdDirName.FileName.MaximumLength = (USHORT) Length;
        }
    }

    //
    //  We now have a buffer for the name.  We need to either convert the on-disk bigendian
    //  to little endian or covert the name to Unicode.
    //

    if (!FlagOn( IrpContext->Vcb->VcbState, VCB_STATE_JOLIET )) {

        Status = RtlOemToUnicodeN( PathEntry->CdDirName.FileName.Buffer,
                                   PathEntry->CdDirName.FileName.MaximumLength,
                                   &Length,
                                   PathEntry->DirName,
                                   PathEntry->DirNameLen );

        ASSERT( Status == STATUS_SUCCESS );
        PathEntry->CdDirName.FileName.Length = (USHORT) Length;

    } else {

        //
        //  Convert this string to little endian.
        //

        CdConvertBigToLittleEndian( IrpContext,
                                    PathEntry->DirName,
                                    PathEntry->DirNameLen,
                                    (PCHAR) PathEntry->CdDirName.FileName.Buffer );

        PathEntry->CdDirName.FileName.Length = (USHORT) PathEntry->DirNameLen;
    }

    //
    //  There is no version string.
    //

    PathEntry->CdDirName.VersionString.Length =
    PathEntry->CdCaseDirName.VersionString.Length = 0;

    //
    //  If the name string ends with a period then knock off the last
    //  character.
    //

    if (PathEntry->CdDirName.FileName.Buffer[(PathEntry->CdDirName.FileName.Length - sizeof( WCHAR )) / 2] == L'.') {

        //
        //  Shrink the filename length.
        //

        PathEntry->CdDirName.FileName.Length -= sizeof( WCHAR );
    }

    //
    //  Update the case name buffer if necessary.  If this is an exact case
    //  operation then just copy the exact case string.
    //

    if (IgnoreCase) {

        PathEntry->CdCaseDirName.FileName.Buffer = Add2Ptr( PathEntry->CdDirName.FileName.Buffer,
                                                            PathEntry->CdDirName.FileName.MaximumLength / 2,
                                                            PWCHAR);

        PathEntry->CdCaseDirName.FileName.MaximumLength = PathEntry->CdDirName.FileName.MaximumLength / 2;

        CdUpcaseName( IrpContext,
                      &PathEntry->CdDirName,
                      &PathEntry->CdCaseDirName );

    } else {

        PathEntry->CdCaseDirName = PathEntry->CdDirName;
    }

    return;
}


⌨️ 快捷键说明

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