forked from ikatson/smb_ctypes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samba.py
executable file
·143 lines (116 loc) · 3.16 KB
/
samba.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# coding: utf-8
import sys
import urlparse
import libsmbclient as l
class SambaError(Exception):
pass
_context = None
def _check_error():
errno = l.get_errno()
if errno:
raise SambaError(l.strerror(errno))
def _hide_auth(smb_url):
parsed = urlparse.urlparse(smb_url)
return urlparse.urlunparse((
parsed.scheme,
parsed.netloc.split('@')[-1],
parsed.path,
parsed.params,
parsed.query,
parsed.fragment,
))
def set_auth_func(auth_func):
global _context
l.smbc_init(l.AUTHFUNC(l.py_auth_func), 1)
_context = l.smbc_new_context()
l.smbc_init_context(_context)
def _smbobj_from_dirent(base_url, dirent):
t = dirent.contents.smbc_type
if t == l.FILE_SHARE:
cls = FileShareObj
elif t == l.DIR:
cls = DirObj
elif t == l.FILE:
cls = FileObj
else:
cls = SmbObj
return cls(
base_url, t,
dirent.contents.comment,
dirent.contents.name
)
class SmbObj(object):
__slots__ = ('type', 'comment', 'name', 'base_url')
def __init__(self, base_url, type_, comment, name):
self.base_url = base_url
self.type = type_
self.comment = comment
self.name = name
def __repr__(self):
if type(self) is SmbObj:
cls = 'SmbObj %s' % l.TYPE_STRINGS[self.type]
else:
cls = type(self).__name__
return '<%s on %s: %s>' % (
cls,
_hide_auth(self.base_url),
self.name
)
@property
def url(self):
return self.base_url + self.name
class FileShareObj(SmbObj):
pass
class DirObj(SmbObj):
pass
class _smbfile(object):
__slots__ = ('fd')
def __init__(self, fd):
self.fd = fd
def fileno(self):
return self.fd
def read(self, count=4096 * 1024):
t = l.create_string_buffer(count)
import time
before = time.time()
bytes_written = l.smbc_read(self.fd, t, count)
print >> sys.stderr, time.time() - before, bytes_written
if bytes_written < 0:
_check_error()
return t.value
class FileObj(SmbObj):
def open(self, mode='r'):
modes = {
'r': l.O_RDONLY,
'w': l.O_WRONLY,
'rw': l.O_RDWR,
}
print self.url
fd = l.smbc_open(self.url, modes[mode], 0644)
if fd < 0:
_check_error()
return _smbfile(fd)
def listdir(smb_url):
dir_ = l.smbc_opendir(smb_url)
if dir_ < 0:
_check_error()
while True:
dirent = l.smbc_readdir(dir_)
if not dirent:
_check_error()
break
obj_ = _smbobj_from_dirent(smb_url, dirent)
if obj_.type == l.DIR and obj_.name in ('.', '..'):
continue
yield obj_
# set_auth_func(l.py_auth_func)
if __name__ == '__main__':
l.smbc_init(l.auth_func, 1)
l.smbc_init_context(l.smbc_new_context())
for i in listdir(sys.argv[1]):
if isinstance(i, FileObj):
f = i.open()
import shutil
import sys
shutil.copyfileobj(f, sys.stdout)