March 01, 2021, 09:39:01 PM

Author Topic: [bb] NTFS Alternate Data Streams by grable [ 1+ years ago ]  (Read 568 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : NTFS Alternate Data Streams
Author : grable
Posted : 1+ years ago

Description : FOR BLITZMAX & WIN32

What is NTFS Alternate Data Streams?
   
   They are streams attached to files/directories, but not part of the original content.
   
   General syntax for streams are "FILENAME:STREAMNAME"
   or the longer "FILENAME:STREAMNAME:$DATA".
   
   Most file apis handle the FILE:STREAM syntax,
   including BlitzMax's TStream.
   
   Note that there is no indication that a file has any streams
   attached to it in explorer, not even the size is incremented.
   
   Handling the file outside explorer/cli
   (eg sending over network, archiving, etc)
   makes the file loose its streams.      
   
Requirements:
   Windows 2000 and above
   NTFS Volumes

Example:
Code: [Select]
Framework BRL.Blitz

Import BRl.StandardIO
Import BRL.Stream

Import "NTFSStreams.bmx"

SuperStrict


Const TEST_FILE:String = "foo"


' create the file and its streams
Local stream:TStream = WriteStream( TEST_FILE)
stream.WriteLine( "this is foo")
stream.Close()

stream = WriteStream( TEST_FILE + ":bar")
stream.WriteLine( "this is bar")
stream.Close()

stream = WriteStream( TEST_FILE + ":baz")
stream.WriteLine( "this is baz")
stream.Close()

CopyFileStream( TEST_FILE + ":bar", TEST_FILE + ":booz")

' enumerate the streams and their contents
Local fs:TFindStream = FindFirstStream( TEST_FILE, False)
If fs Then
Local stream:TStream
Repeat
Print "--------------------------------------"
Print "foo" + fs.Name + " =>"
stream = ReadStream( TEST_FILE + fs.Name)
If stream Then
While Not stream.Eof()
Print stream.ReadLine()
Wend
stream.Close()
EndIf
Until Not fs.FindNext()
fs.Close()
EndIf


Code :
Code: BlitzBasic
  1. Rem
  2.         Enumerate/Handle NTFS Alternate Data Streams attached to a file/dir
  3.  
  4.         What is NTFS Alternate Data Streams?
  5.        
  6.                 They are streams attached to files/directories, but not part of the original content.
  7.                
  8.                 General syntax for streams are "FILENAME:STREAMNAME"
  9.                 or the longer "FILENAME:STREAMNAME:$DATA".
  10.                
  11.                 Most file apis handle the FILE:STREAM syntax,
  12.                 including BlitzMax's TStream.
  13.                
  14.                 Note that there is no indication that a file has any streams
  15.                 attached to it in explorer, not even the size is incremented.
  16.                
  17.                 Handling the file outside explorer/cli
  18.                 (eg sending over network, archiving, etc)
  19.                 makes the file loose its streams.              
  20.                
  21.         Requirements:
  22.                 Windows 2000 and above
  23.                 NTFS Volumes
  24.                
  25.         author: grable
  26.         email : grable0@gmail.com
  27. EndRem
  28. SuperStrict
  29.  
  30. Import BRL.Stream
  31.  
  32. ?Win32
  33.  
  34. Private
  35. Extern "Win32"
  36.         Const INVALID_HANDLE_VALUE:Int = -1
  37.  
  38.         Const FILE_SHARE_READ:Int = $00000001  
  39.         Const OPEN_EXISTING:Int = 3    
  40.         Const FILE_FLAG_BACKUP_SEMANTICS:Int = 33554432
  41.        
  42.         Const GENERIC_READ:Int = $80000000
  43.  
  44.         Function BackupRead:Int( hfile:Int, buff:Byte Ptr, count:Int, bytesread:Int Var, abort:Int, processsecurity:Int, context:Byte Ptr Var)
  45.         Function BackupSeek:Int( hfile:Int, lowbytestoseek:Int, highbytestoseek:Int, lowbytesseeked:Int Var, highbytesseeked:Int Var, context:Byte Ptr Var)
  46.         Function CreateFileA:Int( fname$z, access:Int, sharedmode:Int, securityattribs:Byte Ptr, disp:Int, flags:Int, templatefile:Int)
  47.         Function CloseHandle:Int( hfile:Int)
  48.         Function DeleteFileA:Int( fn$z)
  49. EndExtern
  50. Public
  51.  
  52.  
  53. '
  54. ' Stream Types
  55. '
  56. Const STREAM_DATA:Int = 1                       ' original file stream
  57. Const STREAM_EXTERNAL_DATA:Int = 2
  58. Const STREAM_SECURITY_DATA:Int = 3
  59. Const STREAM_ALTERNATE_DATA:Int = 4     ' standard attached streams
  60. Const STREAM_LINK:Int = 5
  61. Const STREAM_PROPERTY_DATA:Int = 6
  62. Const STREAM_OBJECT_ID:Int = 7
  63. Const STREAM_REPARSE_DATA:Int = 8
  64. Const STREAM_SPARSE_DOCK:Int = 9
  65.  
  66. Type TWin32StreamID Abstract
  67.         Field Typ:Int
  68.         Field Attribs:Int
  69.         Field Size:Long
  70.         Field NameSize:Int
  71. EndType
  72.  
  73. '
  74. ' enumerate streams attatched to file/dir
  75. '
  76. Type TFindStream Extends TWin32StreamID
  77.         Field Name:String
  78.         Field StripTags:Int
  79. ' private
  80.         Field Handle:Int
  81.         Field Context:Byte Ptr
  82.        
  83.         Function Create:TFindStream( filename:String, striptags:Int = True)
  84.                 Local fs:TFindStream = New TFindStream
  85.                 fs.StripTags = striptags
  86.                 If Not fs.FindFirst( filename) Then Return Null
  87.                 Return fs
  88.         EndFunction
  89.        
  90.         Method FindFirst:Int( filename:String)
  91.                 Handle = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, Null, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, Null)
  92.                 If Handle = INVALID_HANDLE_VALUE Then Return Null
  93.                 ' skip the real file stream
  94.                 If Not FindNext() Then
  95.                         Close()
  96.                         Return False
  97.                 EndIf
  98.                 ' get next stream
  99.                 If Not FindNext() Then
  100.                         Close()
  101.                         Return False
  102.                 EndIf
  103.                 Return True
  104.         EndMethod
  105.        
  106.         Method FindNext:Int()
  107.                 If Handle = INVALID_HANDLE_VALUE Then Return False     
  108.                 Local read:Int
  109.                 ' read header  
  110.                 If Not BackupRead( Handle, Self, SizeOf(TWin32StreamID), read, False, False, Context) Then Return False
  111.                 If read <> SizeOf(TWin32StreamID) Then Return False
  112.                 ' read name
  113.                 Local streamname:Short[NameSize + 1]
  114.                 If Not BackupRead( Handle, streamname, NameSize, read, False, False, Context) Then Return False
  115.                 If read <> NameSize Then Return False  
  116.                 If StripTags Then
  117.                         Name = String.FromWString( streamname)[1..]
  118.                         Name = Name[..Name.Find( ":")]
  119.                 Else
  120.                         Name = String.FromWString( streamname)                 
  121.                 EndIf
  122.                 ' skip data
  123.                 Local t1:Int, t2:Int
  124.                 BackupSeek( Handle, Int(Size & $FFFFFFFF), Int((Size Shr 32) & $FFFFFFFF), t1, t2, Context)
  125.                 Return True
  126.         EndMethod
  127.        
  128.         Method Close()
  129.                 If Handle = INVALID_HANDLE_VALUE Then Return
  130.                 CloseHandle( Handle)
  131.                 Handle = INVALID_HANDLE_VALUE
  132.         EndMethod
  133. EndType
  134.  
  135.  
  136. '
  137. ' alternate stream enumeration functions
  138. '
  139. Function FindFirstStream:TFindStream( filename:String, striptags:Int = True)
  140.         Return TFindStream.Create( filename, striptags)
  141. EndFunction
  142.  
  143. Function FindNextStream:Int( fs:TFindStream)
  144.         If fs Then Return fs.FindNext()
  145.         Return False
  146. EndFunction
  147.  
  148. Function CloseFindStream( fs:TFindStream)
  149.         If fs Then fs.Close()
  150. EndFunction
  151.  
  152.  
  153. '
  154. ' alternate stream utility functions
  155. '
  156. Function DeleteFileStream:Int( fn:String)
  157.         If fn And fn.Find(":") Then Return DeleteFileA( fn)
  158.         Return False
  159. EndFunction
  160.  
  161. Function CopyFileStream:Int( oldfn:String, newfn:String)
  162.         If oldfn And newfn And oldfn.Find(":") And newfn.Find(":") Then
  163.                 Local in:TStream = ReadStream( oldfn), ok:Int = True
  164.                 If in Then
  165.                         Local out:TStream = WriteStream( newfn)
  166.                         If out Then
  167.                                 Try
  168.                                         CopyStream in, out
  169.                                 Catch ex:TStreamWriteException
  170.                                         ok = False
  171.                                 EndTry
  172.                                 out.Close()
  173.                         EndIf                  
  174.                         in.Close()
  175.                 EndIf          
  176.                 Return ok
  177.         EndIf
  178.         Return False
  179. EndFunction
  180.  
  181. Function RenameFileStream:Int( oldfn:String, newfn:String)
  182.         If CopyFileStream( oldfn, newfn) Then
  183.                 DeleteFileStream( oldfn)
  184.                 Return True
  185.         EndIf
  186.         Return False           
  187. EndFunction
  188.  
  189.  
  190. ?


Comments :


_33(Posted 1+ years ago)

 This is BMX code :(


plash(Posted 1+ years ago)

 <div class="quote"> This is BMX code :(  </div><div class="quote"> FOR BLITZMAX & WIN32 </div>...


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal