BackupSeek_streamheaders.py :  » Windows » pyExcelerator » pywin32-214 » win32 » Demos » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » win32 » Demos » BackupSeek_streamheaders.py
## demonstrates using BackupSeek to enumerate data streams for a file
import win32file, win32api, win32con
from win32com import storagecon
import pythoncom, pywintypes
import struct, traceback

stream_types={
    win32con.BACKUP_DATA:"Standard data", 
    win32con.BACKUP_EA_DATA:"Extended attribute data", 
    win32con.BACKUP_SECURITY_DATA:"Security descriptor data", 
    win32con.BACKUP_ALTERNATE_DATA:"Alternative data streams", 
    win32con.BACKUP_LINK:"Hard link information",
    win32con.BACKUP_PROPERTY_DATA:"Property data",
    win32con.BACKUP_OBJECT_ID:"Objects identifiers",
    win32con.BACKUP_REPARSE_DATA:"Reparse points",
    win32con.BACKUP_SPARSE_BLOCK:"Sparse file"
}

tempdir=win32api.GetTempPath()
tempfile=win32api.GetTempFileName(tempdir,'bkr')[0]
print 'Filename:',tempfile

f=open(tempfile,'w')
f.write('some random junk'+'x'*100)
f.close()

f=open(tempfile+':streamdata','w')
f.write('data written to alternate stream'+'y'*100)
f.close()

f=open(tempfile+':anotherstream','w')
f.write('z'*200)
f.close()

## add Summary Information, which is stored as a separate stream
m=storagecon.STGM_READWRITE | storagecon.STGM_SHARE_EXCLUSIVE |storagecon.STGM_DIRECT
pss=pythoncom.StgOpenStorageEx(tempfile, m, storagecon.STGFMT_FILE, 0 , pythoncom.IID_IPropertySetStorage,None)
ps=pss.Create(pythoncom.FMTID_SummaryInformation,pythoncom.IID_IPropertyStorage,0,storagecon.STGM_READWRITE|storagecon.STGM_SHARE_EXCLUSIVE)
ps.WriteMultiple((storagecon.PIDSI_KEYWORDS,storagecon.PIDSI_COMMENTS),('keywords','comments'))
ps=None
pss=None

sa=pywintypes.SECURITY_ATTRIBUTES()
sa.bInheritHandle=False
h=win32file.CreateFile(tempfile, win32con.GENERIC_ALL ,win32con.FILE_SHARE_READ,
    sa, win32con.OPEN_EXISTING, win32file.FILE_FLAG_BACKUP_SEMANTICS , None)


""" stream header:
typedef struct _WIN32_STREAM_ID {
    DWORD dwStreamId;  DWORD dwStreamAttributes;  LARGE_INTEGER Size;
    DWORD dwStreamNameSize;  WCHAR cStreamName[ANYSIZE_ARRAY];
}
"""

win32_stream_id_format="LLQL"
win32_stream_id_size=struct.calcsize(win32_stream_id_format)

def parse_stream_header(h,ctxt,data):
    stream_type, stream_attributes, stream_size, stream_name_size=struct.unpack(win32_stream_id_format,data)
    print '\nType:',stream_type,stream_types[stream_type], 'Attributes:', stream_attributes, 'Size:', stream_size, 'Name len:',stream_name_size
    if stream_name_size>0:
        ## ??? sdk says this size is in characters, but it appears to be number of bytes ???
        bytes_read, stream_name_buf, ctxt=win32file.BackupRead(h, stream_name_size, None, False, True, ctxt)
        stream_name=pywintypes.UnicodeFromRaw(stream_name_buf[:])
    else:
        stream_name='Unnamed'
    print 'Name:'+stream_name
    return ctxt, stream_type, stream_attributes, stream_size, stream_name_size, stream_name

ctxt=0
win32_stream_id_buf=None  ## gets rebound to a writable buffer on first call and reused
while 1:
    bytes_read, win32_stream_id_buf, ctxt=win32file.BackupRead(h, win32_stream_id_size, win32_stream_id_buf, False, True, ctxt)
    if bytes_read==0:
        break
    ctxt, stream_type, stream_attributes, stream_size, stream_name_size, stream_name=\
        parse_stream_header(h, ctxt, win32_stream_id_buf[:])
    if stream_size>0:
        bytes_moved=win32file.BackupSeek(h, stream_size, ctxt)
        print 'Moved: ',bytes_moved

win32file.BackupRead(h, win32_stream_id_size, win32_stream_id_buf, True, True, ctxt)
win32file.CloseHandle(h)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.