ntfs 数据流

前几天看到了一片关于ntfs数据流的老文章,依此写了一个枚举文件中命名数据流的小软件,现在把源代码贴出来。

以下内容为程序代码:

// ntfs.cpp : Defines the entry point for the console application.
//

// 1.0.0.0: First version.
// 1.0.0.1: All error info output to stderr.
char* VERSION_INFO = "1.0.0.1";

//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>

void PrintUsage()
{
        printf("usage: nsinfo [-q][-p][-d] file/n/
  -q: query streams/n/
  -p: print stream/n/
  -d: delete stream/n/
  -v: version info/n"[img]/images/wink.gif[/img];
}

void QueryStreamInfo(_TCHAR* pszFilePath)
{
        WIN32_STREAM_ID wsid;
        ZeroMemory(&wsid, sizeof(WIN32_STREAM_ID));

        HANDLE hfile = CreateFile(pszFilePath/*"i://stream.txt"*/,
                                  GENERIC_READ,
                                  0,
                                  NULL,
                                  OPEN_EXISTING,
                                  0,
                                  0);

        if (hfile == INVALID_HANDLE_VALUE)
        {
                fprintf(stderr, "Cound not open file %s./n", pszFilePath);
                return;
        }

        DWORD dwStreamHeaderSize = (DWORD)((LPBYTE)&wsid.cStreamName - (LPBYTE)&wsid + wsid.dwStreamNameSize);

        LPBYTE lpContext = NULL;
        DWORD dwRead = 0;
        UINT unCounter = 0;
        BOOL bContinue = TRUE;

        while(bContinue)
        {
                bContinue = BackupRead(hfile, (LPBYTE)&wsid, dwStreamHeaderSize, &dwRead, FALSE, FALSE, (LPVOID*)&lpContext);
                if (!bContinue)
                {
                        break;
                }
                WCHAR szStreamname[MAX_PATH] = L"/0";
                bContinue = BackupRead(hfile,
                        (LPBYTE)szStreamname,
                        wsid.dwStreamNameSize,
                        &dwRead,
                        FALSE,
                        FALSE,
                        (LPVOID*)&lpContext);
                if (!bContinue || dwRead == 0)
                {
                        break;
                }
                wprintf(L"%s/n", szStreamname);
                unCounter++;

                DWORD dw1, dw2;
                bContinue = BackupSeek(hfile, wsid.Size.LowPart, wsid.Size.HighPart, &dw1, &dw2, (LPVOID*)&lpContext);
        }
        if (unCounter == 1)
        {
                printf("There is only %d named streams in %s./n", unCounter, pszFilePath);
        } else {
                printf("There are %d named streams in %s./n", unCounter, pszFilePath);
        }
}

void ReadStreamInfo(_TCHAR* pszFilePath)
{
        HANDLE hfile = CreateFile(pszFilePath/*"i://stream.txt"*/,
                                  GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);

        if (hfile == INVALID_HANDLE_VALUE)
        {
                fprintf(stderr, "Cound not open file %s./n", pszFilePath);
                return;
        }
        DWORD dwReaded = 1;
        BOOL bReault = TRUE;
        while (bReault && dwReaded != 0)
        {
                BYTE szBuffer[512] = "/0";
                bReault = ReadFile(hfile, szBuffer, 510, &dwReaded, NULL);
                printf("%s", szBuffer);
        }
}

int _tmain(int argc, _TCHAR* argv[])
{
        _TCHAR *pszFilePath = NULL;
        BOOL bDeleteStream = FALSE;
        BOOL bQueryStream = FALSE;
        BOOL bReadStream = FALSE;
       
        // do with the params
        if (argc == 1)
        {
                PrintUsage();
        }
        if (argc == 2)  //default enable query option.
        {
                bQueryStream = TRUE;
        }

        for (int i = 1; i < argc; i++)
        {
                if (argv[i][0] == '-')
                {
                        switch(argv[i][1]) {
                        case 'd':
                                bDeleteStream = TRUE;
                         break;
                        case 'p':
                                bReadStream = TRUE;
                                break;
                        case 'q':
                                bQueryStream = TRUE;
                                break;
                        case 'v':
                                printf("nsinfo: show named streams under ntfs(win2k, or above)./nVersion: %s/n",
                                       VERSION_INFO);
                                printf("Author: WilliamX/nEmail: sam.feng@sohu.com/n"[img]/images/wink.gif[/img];
                                break;
                        default:
                                break;
                        }
                } else {
                       pszFilePath = argv[i];
                }
        }
        // do with the params

        if (pszFilePath == NULL)
        {
                return 0;
        }
        // delete file or stream
        if (bDeleteStream)
        {
                if (DeleteFile(pszFilePath))
                {
                        printf("Delete %s OK./n", pszFilePath);
                } else {
                        printf("Delete %s Failed./n", pszFilePath);
                }
        }

        if (bQueryStream)
        {
                QueryStreamInfo(pszFilePath);
        }

        if (bReadStream)
        {
                ReadStreamInfo(pszFilePath);
        }
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
摘要:NTFS是Microsoft公司开发的一种有着良好安全性和稳定性的高性能文件系统,NTFS的文件或文件夹中附加多个额外的数据流,但是其访问一直没有很好的解决办法,本文使用VB2003实现NTFS文件附加数据流的读写类,提供.Net框架下NTFS文件附加数据流的完整解决方案。 关键词:VB.Net NTFS 数据流 类 在项目中选择添加引用->浏览->选择“JWBStreamOP.dll”文件->确定,即可成功引用。 4.1 类的声明: Dim myStreamOP As New ClassJWBStreamOP(“NTFS文件完整路径”) 4.2 属性: 该类共有3个只读属性 属性名 返回值类型 备注 FileName String 只读,在成功声明后使用 Ready Boolean 只读,该类可操作时为True Ver String 只读,类版本、版权信息 4.3 方法 该类共有6个方法: 4.3.1 OpenNTFSStream(ByVal sStreamName As String) As System.IO.FileStream 打开指定文件(声明时指定)的指定数据流,返回值为指定数据流FileStream接口。 参数列表 类型 传递方式 参数说明 sStreamName String Byval 流文件名 4.3.2 GetNTFSStreamSize(ByVal sStreamName As String) As Long 获取指定数据流的大小,返回实际大小,执行失败返回-1 参数列表 类型 传递方式 参数说明 sStreamName String Byval 流文件名 4.3.3 AddNTFSStream(ByVal toHidName As String, ByRef percentDone As Double) As Boolean 添加附加数据流,返回执行结果。 参数列表 类型 传递方式 参数说明 toHidName String ByVal 待添加的文件路径 percentDone Double ByRef 传递一个完成百分比的参数 4.3.4 SaveNTFSStream(ByVal sStreamName As String, ByVal outFileName As String, ByRef percentDone As Double) As Boolean将指定的数据流保存为文件,返回执行结果。 参数列表 类型 传递方式 参数说明 sStreamName String ByVal 流文件名 outFileName String ByVal 保存文件路径 percentDone Double ByRef 传递一个完成百分比的参数 4.3.5 ReadNTFSStreamsName() As String() 获取文件的所有附加数据流名称,返回名称数组。 4.3.6 DeleteNTFSStream(ByVal sStreamName As String) As Boolean 删除指定数据流,返回执行结果。 参数列表 类型 传递方式 参数说明 sStreamName String Byval 流文件名

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值