Skip to content

Commit

Permalink
Attempt at listing paths from an offset.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinodc committed May 12, 2015
1 parent c09a267 commit b75db1a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
5 changes: 3 additions & 2 deletions python2/smb/SMBConnection.py
Expand Up @@ -154,7 +154,7 @@ def eb(failure):

def listPath(self, service_name, path,
search = SMB_FILE_ATTRIBUTE_READONLY | SMB_FILE_ATTRIBUTE_HIDDEN | SMB_FILE_ATTRIBUTE_SYSTEM | SMB_FILE_ATTRIBUTE_DIRECTORY | SMB_FILE_ATTRIBUTE_ARCHIVE,
pattern = '*', timeout = 30):
pattern = '*', index = 0, timeout = 30):
"""
Retrieve a directory listing of files/folders at *path*
Expand All @@ -163,6 +163,7 @@ def listPath(self, service_name, path,
:param integer search: integer value made up from a bitwise-OR of *SMB_FILE_ATTRIBUTE_xxx* bits (see smb_constants.py).
The default *search* value will query for all read-only, hidden, system, archive files and directories.
:param string/unicode pattern: the filter to apply to the results before returning to the client.
:param integer index: File Index Byte Offset to begin enumeration.
:return: A list of :doc:`smb.base.SharedFile<smb_SharedFile>` instances.
"""
if not self.sock:
Expand All @@ -180,7 +181,7 @@ def eb(failure):

self.is_busy = True
try:
self._listPath(service_name, path, cb, eb, search = search, pattern = pattern, timeout = timeout)
self._listPath(service_name, path, cb, eb, search = search, pattern = pattern, index = index, timeout = timeout)
while self.is_busy:
self._pollForNetBIOSPacket(timeout)
finally:
Expand Down
3 changes: 2 additions & 1 deletion python2/smb/SMBProtocol.py
Expand Up @@ -174,7 +174,7 @@ def listShares(self, timeout = 30):

def listPath(self, service_name, path,
search = SMB_FILE_ATTRIBUTE_READONLY | SMB_FILE_ATTRIBUTE_HIDDEN | SMB_FILE_ATTRIBUTE_SYSTEM | SMB_FILE_ATTRIBUTE_DIRECTORY | SMB_FILE_ATTRIBUTE_ARCHIVE,
pattern = '*', timeout = 30):
pattern = '*', index = 0, timeout = 30):
"""
Retrieve a directory listing of files/folders at *path*
Expand All @@ -183,6 +183,7 @@ def listPath(self, service_name, path,
:param integer search: integer value made up from a bitwise-OR of *SMB_FILE_ATTRIBUTE_xxx* bits (see smb_constants.py).
The default *search* value will query for all read-only, hidden, system, archive files and directories.
:param string/unicode pattern: the filter to apply to the results before returning to the client.
:param integer index: File Index Byte Offset to begin enumeration.
:param integer/float timeout: Number of seconds that pysmb will wait before raising *SMBTimeout* via the returned *Deferred* instance's *errback* method.
:return: A *twisted.internet.defer.Deferred* instance. The callback function will be called with a list of :doc:`smb.base.SharedFile<smb_SharedFile>` instances.
"""
Expand Down
10 changes: 7 additions & 3 deletions python2/smb/base.py
Expand Up @@ -537,7 +537,7 @@ def connectCB(connect_message, **kwargs):
else:
connectSrvSvc(self.connected_trees[path])

def _listPath_SMB2(self, service_name, path, callback, errback, search, pattern, timeout = 30):
def _listPath_SMB2(self, service_name, path, callback, errback, search, pattern, index, timeout = 30):
if not self.has_authenticated:
raise NotReadyError('SMB connection not authenticated')

Expand Down Expand Up @@ -581,9 +581,13 @@ def createCB(create_message, **kwargs):
errback(OperationFailure('Failed to list %s on %s: Unable to open directory' % ( path, service_name ), messages_history))

def sendQuery(tid, fid, data_buf):
flags = 0
if index > 0:
flags |= 0x04 # SMB2_INDEX_SPECIFIED
m = SMB2Message(SMB2QueryDirectoryRequest(fid, pattern,
findex = index,
info_class = 0x03, # FileBothDirectoryInformation
flags = 0,
flags = flags,
output_buf_len = self.max_transact_size))
m.tid = tid
self._sendSMBMessage(m)
Expand Down Expand Up @@ -1776,7 +1780,7 @@ def connectCB(connect_message, **kwargs):
self.pending_requests[m.mid] = _PendingRequest(m.mid, expiry_time, connectCB, errback, path = path)
messages_history.append(m)

def _listPath_SMB1(self, service_name, path, callback, errback, search, pattern, timeout = 30):
def _listPath_SMB1(self, service_name, path, callback, errback, search, pattern, index, timeout = 30):
if not self.has_authenticated:
raise NotReadyError('SMB connection not authenticated')

Expand Down
5 changes: 3 additions & 2 deletions python2/smb/smb2_structs.py
Expand Up @@ -665,9 +665,10 @@ class SMB2QueryDirectoryRequest(Structure):
STRUCTURE_FORMAT = "<HBBI16sHHI"
STRUCTURE_SIZE = struct.calcsize(STRUCTURE_FORMAT)

def __init__(self, fid, filename, info_class, flags, output_buf_len):
def __init__(self, fid, filename, findex, info_class, flags, output_buf_len):
self.fid = fid
self.filename = filename
self.findex = findex
self.info_class = info_class
self.flags = flags
self.output_buf_len = output_buf_len
Expand All @@ -681,7 +682,7 @@ def prepare(self, message):
33, # Structure size. Must be 33 as mandated by [MS-SMB2] 2.2.33
self.info_class, # FileInformationClass
self.flags, # Flags
0, # FileIndex
self.findex, # FileIndex
self.fid, # FileID
SMB2Message.HEADER_SIZE + self.STRUCTURE_SIZE, # FileNameOffset
len(self.filename)*2,
Expand Down
5 changes: 3 additions & 2 deletions python3/smb/SMBConnection.py
Expand Up @@ -153,7 +153,7 @@ def eb(failure):

def listPath(self, service_name, path,
search = SMB_FILE_ATTRIBUTE_READONLY | SMB_FILE_ATTRIBUTE_HIDDEN | SMB_FILE_ATTRIBUTE_SYSTEM | SMB_FILE_ATTRIBUTE_DIRECTORY | SMB_FILE_ATTRIBUTE_ARCHIVE,
pattern = '*', timeout = 30):
pattern = '*', index = 0, timeout = 30):
"""
Retrieve a directory listing of files/folders at *path*
Expand All @@ -162,6 +162,7 @@ def listPath(self, service_name, path,
:param integer search: integer value made up from a bitwise-OR of *SMB_FILE_ATTRIBUTE_xxx* bits (see smb_constants.py).
The default *search* value will query for all read-only, hidden, system, archive files and directories.
:param string/unicode pattern: the filter to apply to the results before returning to the client.
:param integer index: File Index Byte Offset to begin enumeration.
:return: A list of :doc:`smb.base.SharedFile<smb_SharedFile>` instances.
"""
if not self.sock:
Expand All @@ -179,7 +180,7 @@ def eb(failure):

self.is_busy = True
try:
self._listPath(service_name, path, cb, eb, search = search, pattern = pattern, timeout = timeout)
self._listPath(service_name, path, cb, eb, search = search, pattern = pattern, index = index, timeout = timeout)
while self.is_busy:
self._pollForNetBIOSPacket(timeout)
finally:
Expand Down
3 changes: 2 additions & 1 deletion python3/smb/SMBProtocol.py
Expand Up @@ -175,7 +175,7 @@ def listShares(self, timeout = 30):

def listPath(self, service_name, path,
search = SMB_FILE_ATTRIBUTE_READONLY | SMB_FILE_ATTRIBUTE_HIDDEN | SMB_FILE_ATTRIBUTE_SYSTEM | SMB_FILE_ATTRIBUTE_DIRECTORY | SMB_FILE_ATTRIBUTE_ARCHIVE,
pattern = '*', timeout = 30):
pattern = '*', index = 0, timeout = 30):
"""
Retrieve a directory listing of files/folders at *path*
Expand All @@ -184,6 +184,7 @@ def listPath(self, service_name, path,
:param integer search: integer value made up from a bitwise-OR of *SMB_FILE_ATTRIBUTE_xxx* bits (see smb_constants.py).
The default *search* value will query for all read-only, hidden, system, archive files and directories.
:param string/unicode pattern: the filter to apply to the results before returning to the client.
:param integer index: File Index Byte Offset to begin enumeration.
:param integer/float timeout: Number of seconds that pysmb will wait before raising *SMBTimeout* via the returned *Deferred* instance's *errback* method.
:return: A *twisted.internet.defer.Deferred* instance. The callback function will be called with a list of :doc:`smb.base.SharedFile<smb_SharedFile>` instances.
"""
Expand Down
8 changes: 6 additions & 2 deletions python3/smb/base.py
Expand Up @@ -531,7 +531,7 @@ def connectCB(connect_message, **kwargs):
else:
connectSrvSvc(self.connected_trees[path])

def _listPath_SMB2(self, service_name, path, callback, errback, search, pattern, timeout = 30):
def _listPath_SMB2(self, service_name, path, callback, errback, search, pattern, index, timeout = 30):
if not self.has_authenticated:
raise NotReadyError('SMB connection not authenticated')

Expand Down Expand Up @@ -575,7 +575,11 @@ def createCB(create_message, **kwargs):
errback(OperationFailure('Failed to list %s on %s: Unable to open directory' % ( path, service_name ), messages_history))

def sendQuery(tid, fid, data_buf):
flags = 0
if index > 0:
flags |= 0x04 # SMB2_INDEX_SPECIFIED
m = SMB2Message(SMB2QueryDirectoryRequest(fid, pattern,
findex = index,
info_class = 0x03, # FileBothDirectoryInformation
flags = 0,
output_buf_len = self.max_transact_size))
Expand Down Expand Up @@ -1770,7 +1774,7 @@ def connectCB(connect_message, **kwargs):
self.pending_requests[m.mid] = _PendingRequest(m.mid, expiry_time, connectCB, errback, path = path)
messages_history.append(m)

def _listPath_SMB1(self, service_name, path, callback, errback, search, pattern, timeout = 30):
def _listPath_SMB1(self, service_name, path, callback, errback, search, pattern, index, timeout = 30):
if not self.has_authenticated:
raise NotReadyError('SMB connection not authenticated')

Expand Down

0 comments on commit b75db1a

Please sign in to comment.