-
Notifications
You must be signed in to change notification settings - Fork 21
/
afc_shell.py
173 lines (143 loc) · 4.14 KB
/
afc_shell.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
import os
import sys
import cmd
import plist
from imobiledevice import *
debug_printf = lambda msg: sys.stdout.write(msg)
def lockdown_get_service_client(service_class):
ld = LockdownClient(iDevice())
return ld.get_service_client(service_class)
class CommandShell(cmd.Cmd):
afc = None
_path = None
_dirs = []
def __init__(self, root=True):
cmd.Cmd.__init__(self)
if root:
self.afc = lockdown_get_service_client(Afc2Client)
else:
self.afc = lockdown_get_service_client(AfcClient)
self._set_path("/")
self._dirs = self.afc.read_directory("/")
def _set_path(self, path):
if (path[-1] == '/'):
self._path = path[:-1]
else:
self._path = path
if self._path:
self.prompt = "afc:%s # " % self._path
else:
self.prompt = "afc:/ # "
def _relative_path(self, line):
if line.strip() == '':
new_path = self._path
elif line == "..":
new_path = os.path.dirname(self._path)
elif line[0] == '/':
new_path = line
else:
new_path = "%s/%s" % (self._path, line)
return new_path
def _complete_dir(self, text, line, start_index, end_index):
if text:
return [tmp_dir for tmp_dir in self._dirs if tmp_dir.startswith(text)]
else:
return tmp_dir
def do_pwd(self, line):
"""pwd: return working directory name\n"""
debug_printf("%s\n" % self._path)
def do_cd(self, line):
new_path = self._relative_path(line)
try:
self.afc.get_file_info(new_path)
self._set_path(new_path)
self._dirs = self.afc.read_directory(new_path)
except AfcError, e:
debug_printf("cd %s: %s\n" % (new_path, e))
complete_cd = _complete_dir
def do_ls(self, line):
new_path = self._relative_path(line)
try:
self._dirs = self.afc.read_directory(new_path)
for tmp_dir in self._dirs:
if (tmp_dir == ".") or (tmp_dir == ".."):
continue
debug_printf("%s\n" % tmp_dir)
except AfcError, e:
debug_printf("ls %s: %s\n" % (new_path, e))
complete_ls = _complete_dir
def do_mkdir(self, line):
"""mkdir <path>: make directories\n"""
new_path = self._relative_path(line)
try:
self.afc.make_directory(new_path)
except AfcError, e:
debug_printf("%s\n" % e)
def do_rm(self, line):
"""rm <path>: remove files or directories\n"""
new_path = self._relative_path(line)
try:
self.afc.remove_path(new_path)
except AfcError, e:
debug_printf("%s\n" % e)
def do_rename(self, line):
"""rename <from> <to>: change the name of a file\n"""
f_from, f_to = line.split(" ")[:2]
new_from = self._relative_path(f_from)
new_to = self._relative_path(f_to)
try:
self.afc.rename_path(new_from, new_to)
except AfcError, e:
debug_printf("%s\n" % e)
do_rn = do_rename
def do_sz(self, line):
"""sz <remote> <local>\n"""
f_remote, f_local = line.split(" ")[:2]
new_remote = self._relative_path(f_remote)
try:
with self.afc.open(new_remote, mode="r") as rfile:
rfile.seek(0, 2) # SEEK_END(2)
rfile_size = rfile.tell()
rfile.seek(0, 0) # SEEK_SET(0)
local_file = open(f_local, "w+")
local_file.write(rfile.read(rfile_size))
local_file.close()
debug_printf("Write %d bytes to %s.\n" % (rfile_size, f_local))
except AfcError, e:
debug_printf("%s\n" % e)
complete_sz = _complete_dir
def do_rz(self, line):
"""rz <local> <remote>\n"""
f_local, f_remote = line.split(" ")[:2]
new_remote = self._relative_path(f_remote)
try:
with self.afc.open(new_remote, mode="w+") as rfile:
local_file = open(f_local, "r")
rfile.write(local_file.read())
local_file.close()
debug_printf("Upload to %s success.\n" % (new_remote))
except AfcError, e:
debug_printf("%s\n" % e)
complete_rz = _complete_dir
def do_cat(self, line):
"""cat: concatenate and print files"""
new_path = self._relative_path(line)
try:
with self.afc.open(new_path, mode="r") as rfile:
rfile.seek(0, 2) # SEEK_END(2)
rfile_size = rfile.tell()
rfile.seek(0, 0) # SEEK_SET(0)
debug_printf("%s\n" % rfile.read(rfile_size))
except AfcError, e:
debug_printf("%s\n" % e)
complete_cat = _complete_dir
def do_exit(self, line):
return True
do_quit = do_exit
do_q = do_quit
def main():
shell = CommandShell()
shell.cmdloop()
if __name__ == '__main__':
main()