znsoft
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2023-10-25
  • 粉丝300
  • 关注6
  • 积分910分
  • 威望14796点
  • 贡献值7点
  • 好评度2410点
  • 原创分5分
  • 专家分100分
  • 社区居民
  • 最爱沙发
  • 社区明星
阅读:3729回复:14

实现文件的重定向访问,转自ms

楼主#
更多 发布于:2007-08-24 13:19
  

View products that this article applies to.
Article ID : 319447
Last Review : February 12, 2007
Revision : 2.3
This article was previously published under Q319447
On This Page

SUMMARY

MORE INFORMATION

Sample Code
SUMMARY
The IO manager offers a convenient way to reparse a file object. Typically, this method is used in file system filter drivers to redirect a file-open or file-creation operation to another file.
Back to the top

MORE INFORMATION
To redirect a file-open or file-creation operation to another file, a file system filter driver does the following: 1. In the handler of IRP_MJ_CREATE, obtains the file name (FileName field) from the FILE_OBJECT.
2. Replaces this name with the full name of the destination file.

This full name includes the name of the volume device object (for example, Device\HardDiskVolume0\Directory\MyFile.txt). You can substitute your own buffer to the existing FileName.Buffer present in the FILE_OBJECT. In this case, allocate your buffer from NonPaged pool memory, free the original FileName.Buffer by using ExFreePool, and then replace FileName.Buffer with your buffer.
3. Sets the status field of the IoStatus block to STATUS_REPARSE, and then sets the Information field to IO_REPARSE.
4. Completes the request.
5. Returns STATUS_REPARSE.
The IO Manager then triggers another file-open operation and sends an IRP_MJ_CREATE, taking into account the particular file name.

The destination file can be local or on a remote computer. To redirect the file-open operation to a remote file, use the following syntax for the file name: • "\??\UNC\HostName\Share\File"

-or-
• "\Device\Mup\HostName\Share\File"

-or-
• "\Device\LanmanagerRedirector\HostName\Share\File" (assuming you are targeting a file on CIFS/SMB/LanManager)
The fact that the first create-file operation is performed relative to another file object does not matter. Do not modify the RelatedFileObject field of the FILE_OBJECT. To perform the reparse operation, the IO Manager considers only the FileName field and not the RelatedFileObject. Additionally, the IO Manager frees the RelatedFileObject, as appropriate, when it handles the STATUS_REPARSE status returned by the filter. Therefore, it is not the responsibility of the filter to free that file object.

There is a fixed limit concerning the number of nested reparse operations that the IO Manager can perform. This limit has been introduced to avoid infinite loops. The maximum number of nested reparse operations the system can perform is 32.

This reparsing method performed by the IO Manager has to be disassociated from reparse points. Reparse points have been introduced in NTFS, starting with Microsoft Windows 2000. Reparse points permit you to store information together with a file.
Back to the top

Sample Code
NTSTATUS
SfCreate (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
    PIO_STACK_LOCATION        IrpSp;
    PUNICODE_STRING        FileName;
    PVOID            FileNameBuffer;
    UNICODE_STRING        NewFileName;
    BOOLEAN            bRedirectFileOpen = FALSE;

    //
    //  If the device being opened is the primary device object instead of a
    //  filter device object, just indicate that the operation worked.
    //

    if (DeviceObject == FsDeviceObject)
    {
        //
        //  Allow users to open the device that represents our driver.
        //

        Irp->IoStatus.Status = STATUS_SUCCESS;
        Irp->IoStatus.Information = FILE_OPENED;

        IoCompleteRequest( Irp, IO_NO_INCREMENT );
        return STATUS_SUCCESS;
    }
    
    IrpSp = IoGetCurrentIrpStackLocation(Irp);

    //
    // At this point, you must determine whether you want to redirect
    // the file open/create for this particular file.
    // Beware that the file name from the FILE_OBJECT in the current
    // IRP stack location is not always the file name with the full
    // path, nor the long file name or even a name. The way the file is
     // opened (with full path, relatively to another file, with short
    // or long file name, by ID, ...) affects this name.
    //
    // TODO: Put your code here to check whether you have to redirect the operation.
    // If so, set bRedirectFileOpen to TRUE and initialize the NewFileName
    // UNICODE_STRING to the full file name of the destination file.
    //

    if ( bRedirectFileOpen )
    {
        FileName = &(IrpSp->FileObject->FileName);

        FileNameBuffer = ExAllocatePool( NonPagedPool, NewFileName.MaximumLength );
        if (!FileNameBuffer)
        {
            //
            // Not enough resources. Complete the IRP with the appropriate status.
            //

            Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
            Irp->IoStatus.Information = 0;

            IoCompleteRequest( Irp, IO_NO_INCREMENT );
            return STATUS_INSUFFICIENT_RESOURCES;
        }

        ExFreePool( FileName->Buffer );
        FileName->Buffer = FileNameBuffer;
        FileName->MaximumLength = NewFileName.MaximumLength;

        RtlCopyUnicodeString( FileName, &NewFileName );

        //
        // Instruct the IO Manager to reparse this file.
        //

        Irp->IoStatus.Status = STATUS_REPARSE;
        Irp->IoStatus.Information = IO_REPARSE;

        IoCompleteRequest( Irp, IO_NO_INCREMENT );
        return STATUS_REPARSE;
    }
    else
    {
        //
        // Pass the request "as is" down the device stack.
        //

        //
        // The next driver will get the IO_STACK_LOCATION
        // that you received.
        //

        IoSkipCurrentIrpStackLocation( Irp );
    
            //
        // Call the appropriate file system driver with the request.
        //
        // TODO: Replace AttachedToDeviceObject by the device
        // object pointer your device object is attached to (the
        // lower device object in the stack).
        // Typically, this device object pointer is saved by your
        // filter in your device extension.
        //

        return IoCallDriver( AttachedToDeviceObject, Irp );
    }
}
                
Back to the top


--------------------------------------------------------------------------------

APPLIES TO
• Microsoft Win32 Device Driver Kit for Windows 2000
• Microsoft Windows XP Driver Development Kit

Back to the top

Keywords:  kbhowto kbifs kbkmode KB319447

Back to the top

 
Provide feedback on this article
 
Did this article help you solve your problem?
 Yes
 
 No
 
 Partially
 
 I do not know yet
 
 Strongly Agree        Strongly Disagree
 
 9 8 7 6 5 4 3 2 1
The article is easy to understand          
The article is accurate          
Additional Comments:
 
 
To protect your privacy, do not include contact information in your feedback.
 

 

http://www.zndev.com 免费源码交换网 ----------------------------- 软件创造价值,驱动提供力量! 淡泊以明志,宁静以致远。 ---------------------------------- 勤用搜索,多查资料,先搜再问。
Dragon_Rider
驱动牛犊
驱动牛犊
  • 注册日期2003-06-22
  • 最后登录2007-09-28
  • 粉丝0
  • 关注0
  • 积分260分
  • 威望26点
  • 贡献值0点
  • 好评度26点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2007-08-24 14:36
非常感谢!
xiabl
驱动牛犊
驱动牛犊
  • 注册日期2005-10-24
  • 最后登录2010-05-20
  • 粉丝0
  • 关注0
  • 积分221分
  • 威望77点
  • 贡献值0点
  • 好评度71点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2007-08-28 16:34
一直都是这么用的, 呵呵!
小桥流水人家
looksail
荣誉会员
荣誉会员
  • 注册日期2005-05-22
  • 最后登录2014-03-15
  • 粉丝2
  • 关注0
  • 积分1016分
  • 威望991点
  • 贡献值0点
  • 好评度239点
  • 原创分0分
  • 专家分0分
地板#
发布于:2007-08-28 17:48
实现文件重定向的用处是什么 ?
提问归提问,还是只能靠自己
yuanyuan
驱动大牛
驱动大牛
  • 注册日期2003-01-15
  • 最后登录2010-08-04
  • 粉丝0
  • 关注0
  • 积分1025分
  • 威望300点
  • 贡献值0点
  • 好评度232点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2007-08-30 11:44
谢谢啊,呵呵
wanghui219
禁止发言
禁止发言
  • 注册日期2007-08-28
  • 最后登录2019-07-29
  • 粉丝4
  • 关注3
  • 积分101166分
  • 威望505351点
  • 贡献值0点
  • 好评度137点
  • 原创分0分
  • 专家分4分
  • 社区居民
5楼#
发布于:2007-09-07 01:18
用户被禁言,该主题自动屏蔽!
znsoft
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2023-10-25
  • 粉丝300
  • 关注6
  • 积分910分
  • 威望14796点
  • 贡献值7点
  • 好评度2410点
  • 原创分5分
  • 专家分100分
  • 社区居民
  • 最爱沙发
  • 社区明星
6楼#
发布于:2008-11-01 21:28
这个很简单的阿
http://www.zndev.com 免费源码交换网 ----------------------------- 软件创造价值,驱动提供力量! 淡泊以明志,宁静以致远。 ---------------------------------- 勤用搜索,多查资料,先搜再问。
jyss1644
驱动牛犊
驱动牛犊
  • 注册日期2007-08-15
  • 最后登录2011-04-11
  • 粉丝0
  • 关注0
  • 积分20分
  • 威望180点
  • 贡献值0点
  • 好评度6点
  • 原创分0分
  • 专家分0分
7楼#
发布于:2008-11-02 14:02
CNKI上还看到有人拿这个东西申请自然科学基金呢~
lovehhy
驱动小牛
驱动小牛
  • 注册日期2007-09-17
  • 最后登录2010-09-17
  • 粉丝0
  • 关注0
  • 积分1028分
  • 威望244点
  • 贡献值0点
  • 好评度146点
  • 原创分0分
  • 专家分0分
8楼#
发布于:2008-11-03 01:08
引用第7楼jyss1644于2008-11-02 14:02发表的  :
CNKI上还看到有人拿这个东西申请自然科学基金呢~


重定向的概念可以用到很多领域,同步,备份,沙盘,虚拟化,存储,防毒.....
还有做跨网络的重定向,跨操作系统的重定向,这个确实是一个值得研究的课题。
chrysanth
驱动牛犊
驱动牛犊
  • 注册日期2007-05-02
  • 最后登录2010-02-02
  • 粉丝0
  • 关注0
  • 积分9分
  • 威望129点
  • 贡献值0点
  • 好评度61点
  • 原创分0分
  • 专家分0分
9楼#
发布于:2008-11-04 09:54
好咚咚
uljtg
驱动牛犊
驱动牛犊
  • 注册日期2006-03-23
  • 最后登录2011-12-03
  • 粉丝0
  • 关注0
  • 积分19分
  • 威望210点
  • 贡献值0点
  • 好评度46点
  • 原创分0分
  • 专家分0分
10楼#
发布于:2008-11-30 20:30
这东西好啊
cradiator
驱动牛犊
驱动牛犊
  • 注册日期2008-10-04
  • 最后登录2010-08-17
  • 粉丝0
  • 关注0
  • 积分12分
  • 威望121点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
11楼#
发布于:2008-12-03 14:46
这个好,简单实用,适合我这个菜鸟
shenhui
驱动小牛
驱动小牛
  • 注册日期2006-05-11
  • 最后登录2023-02-10
  • 粉丝14
  • 关注11
  • 积分142分
  • 威望1314点
  • 贡献值1点
  • 好评度146点
  • 原创分0分
  • 专家分1分
  • 社区居民
12楼#
发布于:2009-09-14 15:49
这个方法是不是只能使用到NTFS上面呢? FAT32好像没有重解析点这个概念。。。
作一名真实,诚实,优秀的科技工作者!
ytfsse
驱动牛犊
驱动牛犊
  • 注册日期2009-10-15
  • 最后登录2011-01-26
  • 粉丝0
  • 关注0
  • 积分27分
  • 威望241点
  • 贡献值1点
  • 好评度0点
  • 原创分0分
  • 专家分0分
13楼#
发布于:2009-12-03 12:49
这个方法是不是只能使用到NTFS上面呢? FAT32好像没有重解析点这个概念。。。
mr6698
驱动牛犊
驱动牛犊
  • 注册日期2008-03-26
  • 最后登录2017-09-27
  • 粉丝3
  • 关注0
  • 积分21分
  • 威望200点
  • 贡献值0点
  • 好评度26点
  • 原创分0分
  • 专家分1分
14楼#
发布于:2009-12-11 09:22
太感谢了!
游客

返回顶部