| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
""" |
| 20 |
The main code for the pycdlib-explorer tool, which can open, read, write, |
| 21 |
and otherwise manipulate ISOs in an interactive way. |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import print_function |
| 25 |
|
| 26 |
import cmd |
| 27 |
import collections |
| 28 |
import os |
| 29 |
import shlex |
| 30 |
import sys |
| 31 |
|
| 32 |
import pycdlib |
| 33 |
|
| 34 |
|
| 35 |
class PyCdlibCmdLoop(cmd.Cmd): |
| 36 |
"""The main class dealing with the pycdlib-explorer command loop.""" |
| 37 |
PRINT_MODES = ('iso9660', 'rr', 'joliet', 'udf') |
| 38 |
|
| 39 |
def __init__(self, iso): |
| 40 |
cmd.Cmd.__init__(self) |
| 41 |
self.iso = iso |
| 42 |
self.cwds = {} |
| 43 |
for mode in self.PRINT_MODES: |
| 44 |
self.cwds[mode] = '/' |
| 45 |
self.print_mode = 'iso9660' |
| 46 |
self.encoding = 'utf-8' |
| 47 |
self.pathname = 'iso_path' |
| 48 |
|
| 49 |
prompt = '(pycdlib) ' |
| 50 |
|
| 51 |
def help_exit(self): |
| 52 |
"""The help method for the 'exit' command.""" |
| 53 |
print('> exit') |
| 54 |
print('Exit the program.') |
| 55 |
|
| 56 |
def do_exit(self, line): |
| 57 |
"""The command to quit the program.""" |
| 58 |
if line: |
| 59 |
print('No parameters allowed for exit') |
| 60 |
return False |
| 61 |
return True |
| 62 |
|
| 63 |
def help_quit(self): |
| 64 |
"""The help method for the 'quit' command.""" |
| 65 |
print('> quit') |
| 66 |
print('Exit the program.') |
| 67 |
|
| 68 |
def do_quit(self, line): |
| 69 |
"""The other command to quit the program.""" |
| 70 |
if line: |
| 71 |
print('No parameters allowed for quit') |
| 72 |
return False |
| 73 |
return True |
| 74 |
|
| 75 |
def do_EOF(self, line): |
| 76 |
"""Handel EOF on the terminal.""" |
| 77 |
print() |
| 78 |
return True |
| 79 |
|
| 80 |
def help_print_mode(self): |
| 81 |
"""The help method for the 'print_mode' command.""" |
| 82 |
print('> print_mode [iso9660|rr|joliet|udf]') |
| 83 |
print("Change which 'mode' of filenames are printed out. There are four main\n" |
| 84 |
'modes: ISO9660 (iso9660, the default), Rock Ridge (rr), Joliet (joliet), and\n' |
| 85 |
'UDF (udf). The original iso9660 mode only allows filenames of 8 characters,\n' |
| 86 |
'plus 3 for the extension. The Rock Ridge extensions allow much longer\n' |
| 87 |
'filenames and much deeper directory structures. The Joliet extensions also\n' |
| 88 |
'allow longer filenames and deeper directory structures, but in an entirely\n' |
| 89 |
'different namespace (though in most circumstances, the Joliet namespace will\n' |
| 90 |
'mirror the ISO9660/Rock Ridge namespace). The UDF Bridge extensions add an\n' |
| 91 |
'entirely parallel UDF namespace to the ISO as well. Any given ISO will always\n' |
| 92 |
'have ISO9660 mode, but may have any combination of Rock Ridge, Joliet, and UDF\n' |
| 93 |
'(including none of them). Running this command with no arguments prints out\n' |
| 94 |
"the current mode. Passing 'iso9660' as an argument sets it to the original\n" |
| 95 |
"ISO9660 mode. Passing 'rr' as an argument sets it to Rock Ridge mode. Passing\n" |
| 96 |
"'joliet' as an argument sets it to Joliet mode. Passing 'udf' as an argument\n" |
| 97 |
'sets it to UDF mode.') |
| 98 |
|
| 99 |
def do_print_mode(self, line): |
| 100 |
""" |
| 101 |
The command to change whether the explorer is printing in iso9660, |
| 102 |
joliet, rr, or udf (see help for more details). |
| 103 |
""" |
| 104 |
split = shlex.split(line) |
| 105 |
splitlen = len(split) |
| 106 |
if splitlen == 0: |
| 107 |
print(self.print_mode) |
| 108 |
return False |
| 109 |
if splitlen != 1: |
| 110 |
print('Only a single parameter allowed for print_mode') |
| 111 |
return False |
| 112 |
|
| 113 |
if split[0] not in self.PRINT_MODES: |
| 114 |
print("Parameter for print_mode must be one of '" + "', ".join(self.PRINT_MODES) + "'") |
| 115 |
return False |
| 116 |
|
| 117 |
if split[0] == 'rr' and not self.iso.has_rock_ridge(): |
| 118 |
print('Can only enable Rock Ridge names for Rock Ridge ISOs') |
| 119 |
return False |
| 120 |
|
| 121 |
if split[0] == 'joliet' and not self.iso.has_joliet(): |
| 122 |
print('Can only enable Joliet names for Joliet ISOs') |
| 123 |
return False |
| 124 |
|
| 125 |
if split[0] == 'udf' and not self.iso.has_udf(): |
| 126 |
print('Can only enable UDF names for UDF ISOs') |
| 127 |
return False |
| 128 |
|
| 129 |
self.print_mode = split[0] |
| 130 |
|
| 131 |
pathstart = split[0] |
| 132 |
if split[0] == 'iso9660': |
| 133 |
pathstart = 'iso' |
| 134 |
|
| 135 |
self.pathname = pathstart + '_path' |
| 136 |
|
| 137 |
self.encoding = 'utf-8' |
| 138 |
if split[0] == 'joliet': |
| 139 |
self.encoding = 'utf-16_be' |
| 140 |
|
| 141 |
return False |
| 142 |
|
| 143 |
def help_ls(self): |
| 144 |
"""The help method for the 'ls' command.""" |
| 145 |
print('> ls') |
| 146 |
print('Show the contents of the current working directory. The format of the output is:\n') |
| 147 |
print('TYPE(F=file, D=directory) NAME SIZE') |
| 148 |
|
| 149 |
def do_ls(self, line): |
| 150 |
"""The command to list the contents of a directory.""" |
| 151 |
if line: |
| 152 |
print('No parameters allowed for ls') |
| 153 |
return False |
| 154 |
|
| 155 |
diriter = self.iso.list_children(**{self.pathname: self.cwds[self.print_mode]}) |
| 156 |
|
| 157 |
for child in diriter: |
| 158 |
if child is None: |
| 159 |
prefix = 'D' |
| 160 |
name = '..' |
| 161 |
else: |
| 162 |
prefix = 'F' |
| 163 |
if child.is_dir(): |
| 164 |
prefix = 'D' |
| 165 |
name = child.file_identifier().decode(self.encoding) |
| 166 |
if self.print_mode == 'rr': |
| 167 |
name = '' |
| 168 |
if child.is_dot(): |
| 169 |
name = '.' |
| 170 |
elif child.is_dotdot(): |
| 171 |
name = '..' |
| 172 |
else: |
| 173 |
if child.rock_ridge is not None and child.rock_ridge.name() != '': |
| 174 |
name = child.rock_ridge.name().decode('utf-8') |
| 175 |
if child.rock_ridge.is_symlink(): |
| 176 |
name += ' -> %s' % (child.rock_ridge.symlink_path()) |
| 177 |
prefix += 'S' |
| 178 |
|
| 179 |
size = '' |
| 180 |
if child is not None and child.is_file(): |
| 181 |
if isinstance(child, pycdlib.udf.UDFFileEntry): |
| 182 |
size = child.info_len |
| 183 |
else: |
| 184 |
size = child.data_length |
| 185 |
|
| 186 |
print('%2s %s %s' % (prefix, name, size)) |
| 187 |
|
| 188 |
return False |
| 189 |
|
| 190 |
def help_cd(self): |
| 191 |
"""The help method for the 'cd' command.""" |
| 192 |
print('> cd <iso_dir>') |
| 193 |
print('Change directory to <iso_dir> on the ISO.') |
| 194 |
|
| 195 |
def do_cd(self, line): |
| 196 |
""" |
| 197 |
The command to change the current working directory. |
| 198 |
""" |
| 199 |
split = shlex.split(line) |
| 200 |
if len(split) != 1: |
| 201 |
print('The cd command supports one and only one parameter') |
| 202 |
return False |
| 203 |
|
| 204 |
directory = split[0] |
| 205 |
|
| 206 |
if directory[0] != '/': |
| 207 |
tmp = self.cwds[self.print_mode] + '/' + directory |
| 208 |
directory = pycdlib.utils.normpath(tmp).decode('ascii') |
| 209 |
|
| 210 |
rec = self.iso.get_record(**{self.pathname: directory}) |
| 211 |
|
| 212 |
if not rec.is_dir(): |
| 213 |
print('Entry %s is not a directory' % (directory)) |
| 214 |
return False |
| 215 |
self.cwds[self.print_mode] = directory |
| 216 |
|
| 217 |
return False |
| 218 |
|
| 219 |
def help_get(self): |
| 220 |
"""The help method for the 'get' command.""" |
| 221 |
print('> get <iso_file> <out_file>') |
| 222 |
print('Get the contents of <iso_file> from the ISO and write them to <out_file>.') |
| 223 |
|
| 224 |
def do_get(self, line): |
| 225 |
""" |
| 226 |
The command to extract a file from the ISO. |
| 227 |
""" |
| 228 |
split = shlex.split(line) |
| 229 |
if len(split) != 2: |
| 230 |
print('The get command must be passed two parameters.') |
| 231 |
return False |
| 232 |
|
| 233 |
iso_file = split[0] |
| 234 |
outfile = split[1] |
| 235 |
|
| 236 |
if iso_file[0] != '/': |
| 237 |
tmp = self.cwds[self.print_mode] + '/' + iso_file |
| 238 |
iso_file = pycdlib.utils.normpath(tmp).decode('ascii') |
| 239 |
|
| 240 |
self.iso.get_file_from_iso(outfile, **{self.pathname: iso_file}) |
| 241 |
|
| 242 |
return False |
| 243 |
|
| 244 |
def help_cwd(self): |
| 245 |
"""The help method for the 'cwd' command.""" |
| 246 |
print('> cwd') |
| 247 |
print('Show the current working directory.') |
| 248 |
|
| 249 |
def do_cwd(self, line): |
| 250 |
"""The command to find out what the current working directory is.""" |
| 251 |
if line: |
| 252 |
print('No parameters allowed for cwd') |
| 253 |
return False |
| 254 |
|
| 255 |
print(self.cwds[self.print_mode]) |
| 256 |
|
| 257 |
return False |
| 258 |
|
| 259 |
def help_tree(self): |
| 260 |
"""The help method for the 'tree' command.""" |
| 261 |
print('> tree') |
| 262 |
print("Print all files and subdirectories below the current directory (similar to the Unix 'tree' command).") |
| 263 |
|
| 264 |
def do_tree(self, line): |
| 265 |
""" |
| 266 |
The command to print out all of the files and directories below the |
| 267 |
current working directory in a convenient tree-like form. |
| 268 |
""" |
| 269 |
if line: |
| 270 |
print('No parameters allowed for tree') |
| 271 |
return False |
| 272 |
|
| 273 |
utf8_corner = '└──' |
| 274 |
utf8_middlebar = '├──' |
| 275 |
utf8_vertical_line = '│' |
| 276 |
|
| 277 |
entry = self.iso.get_record(**{self.pathname: self.cwds[self.print_mode]}) |
| 278 |
|
| 279 |
dirs = collections.deque([(entry, [])]) |
| 280 |
while dirs: |
| 281 |
dir_record, lasts = dirs.popleft() |
| 282 |
prefix = '' |
| 283 |
if lasts: |
| 284 |
for last in lasts[:-1]: |
| 285 |
if last: |
| 286 |
prefix += ' ' |
| 287 |
else: |
| 288 |
prefix += utf8_vertical_line + ' ' |
| 289 |
|
| 290 |
if lasts[-1]: |
| 291 |
prefix += utf8_corner + ' ' |
| 292 |
else: |
| 293 |
prefix += utf8_middlebar + ' ' |
| 294 |
|
| 295 |
name = dir_record.file_identifier() |
| 296 |
if self.print_mode == 'rr': |
| 297 |
if dir_record.rock_ridge is not None and dir_record.rock_ridge.name() != '': |
| 298 |
name = dir_record.rock_ridge.name() |
| 299 |
|
| 300 |
print('%s%s' % (prefix, name.decode('utf-8'))) |
| 301 |
|
| 302 |
if not dir_record.is_dir(): |
| 303 |
continue |
| 304 |
|
| 305 |
tmp = collections.deque() |
| 306 |
new_lasts = lasts + [False] |
| 307 |
if self.print_mode == 'udf': |
| 308 |
for d in dir_record.fi_descs: |
| 309 |
child = d.file_entry |
| 310 |
if child is None: |
| 311 |
continue |
| 312 |
tmp.appendleft((child, new_lasts)) |
| 313 |
else: |
| 314 |
for child in dir_record.children: |
| 315 |
if child.is_dot() or child.is_dotdot(): |
| 316 |
continue |
| 317 |
tmp.appendleft((child, new_lasts)) |
| 318 |
if tmp: |
| 319 |
tmp.popleft() |
| 320 |
tmp.appendleft((child, lasts + [True])) |
| 321 |
dirs.extendleft(tmp) |
| 322 |
|
| 323 |
return False |
| 324 |
|
| 325 |
def help_write(self): |
| 326 |
"""The help method for the 'write' command.""" |
| 327 |
print('> write <out_file>') |
| 328 |
print('Write the current ISO contents to <out_file>.') |
| 329 |
|
| 330 |
def do_write(self, line): |
| 331 |
""" |
| 332 |
The command to write the ISO out to a new file. |
| 333 |
""" |
| 334 |
split = shlex.split(line) |
| 335 |
if len(split) != 1: |
| 336 |
print('The write command supports one and only one parameter.') |
| 337 |
return False |
| 338 |
|
| 339 |
out_name = split[0] |
| 340 |
|
| 341 |
self.iso.write(out_name) |
| 342 |
|
| 343 |
return False |
| 344 |
|
| 345 |
def help_add_file(self): |
| 346 |
"""The help method for the 'add_file' command.""" |
| 347 |
print('> add_file <iso_path> <src_filename> [rr_name=<rr_name>] [joliet_path=<joliet_path>]') |
| 348 |
print('Add the contents of <src_filename> to the ISO at the location specified in <iso_path>.') |
| 349 |
print('If the ISO is a Rock Ridge ISO, <rr_name> must be specified; otherwise, it must not be.') |
| 350 |
print('If the ISO is not a Joliet ISO, <joliet_path> must not be specified. If the ISO is a') |
| 351 |
print('Joliet ISO, <joliet_path> is optional, but highly recommended to supply.') |
| 352 |
|
| 353 |
def do_add_file(self, line): |
| 354 |
""" |
| 355 |
The command to add a new file to the ISO from the local filesystem. |
| 356 |
""" |
| 357 |
split = shlex.split(line) |
| 358 |
|
| 359 |
if len(split) < 2 or len(split) > 4: |
| 360 |
self.help_add_file() |
| 361 |
return False |
| 362 |
|
| 363 |
iso_path = split[0] |
| 364 |
src_path = split[1] |
| 365 |
rr_name = None |
| 366 |
joliet_path = None |
| 367 |
|
| 368 |
for arg in split[2:]: |
| 369 |
keyval = arg.split('=') |
| 370 |
if len(keyval) != 2: |
| 371 |
print('Invalid key/val pair, must be rr_name=<rr_name> or joliet_path=<joliet_path>') |
| 372 |
return False |
| 373 |
|
| 374 |
key = keyval[0] |
| 375 |
val = keyval[1] |
| 376 |
|
| 377 |
if key == 'rr_name': |
| 378 |
rr_name = val |
| 379 |
elif key == 'joliet_path': |
| 380 |
joliet_path = val |
| 381 |
else: |
| 382 |
print('Unknown key, must be rr_name=<rr_name> or joliet_path=<joliet_path>') |
| 383 |
return False |
| 384 |
|
| 385 |
if self.iso.has_rock_ridge() and rr_name is None: |
| 386 |
print('The ISO is Rock Ridge, so a <rr_name> parameter must be specified.') |
| 387 |
return False |
| 388 |
|
| 389 |
if iso_path[0] != '/': |
| 390 |
tmp = self.cwds['iso9660'] + '/' + iso_path |
| 391 |
iso_path = pycdlib.utils.normpath(tmp).decode('ascii') |
| 392 |
|
| 393 |
self.iso.add_file(src_path, iso_path, rr_name=rr_name, joliet_path=joliet_path) |
| 394 |
|
| 395 |
return False |
| 396 |
|
| 397 |
def help_modify_file_in_place(self): |
| 398 |
''' |
| 399 |
The help method for the 'modify_file_in_place' command. |
| 400 |
''' |
| 401 |
print('> modify_file_in_place <iso_path> <src_filename>') |
| 402 |
print('Replace the contents of the file within the ISO specified in <iso_path> with that of <src_filename>.') |
| 403 |
print('Warning: This command modifies the opened ISO file in-place.') |
| 404 |
print('You must start %s with the `--read-write` flag to enable this.' % (sys.argv[0])) |
| 405 |
print('<src_filename> must occupy the same number of extents as the original file.') |
| 406 |
|
| 407 |
def do_modify_file_in_place(self, line): |
| 408 |
''' |
| 409 |
The command to replace an existing file in the ISO from the local filesystem. |
| 410 |
''' |
| 411 |
split = shlex.split(line) |
| 412 |
|
| 413 |
if len(split) != 2: |
| 414 |
self.help_modify_file_in_place() |
| 415 |
return False |
| 416 |
|
| 417 |
iso_path = split[0] |
| 418 |
src_path = split[1] |
| 419 |
|
| 420 |
if iso_path[0] != '/': |
| 421 |
tmp = self.cwds['iso9660'] + '/' + iso_path |
| 422 |
iso_path = pycdlib.utils.normpath(tmp).decode('ascii') |
| 423 |
|
| 424 |
with open(src_path, 'rb') as src_fp: |
| 425 |
self.iso.modify_file_in_place(src_fp, os.stat(src_path).st_size, iso_path) |
| 426 |
|
| 427 |
return False |
| 428 |
|
| 429 |
def help_rm_file(self): |
| 430 |
"""The help method for the 'rm_file' command.""" |
| 431 |
print('> rm_file <iso_path>') |
| 432 |
print('Remove the contents of <iso_path> from the ISO.') |
| 433 |
|
| 434 |
def do_rm_file(self, line): |
| 435 |
""" |
| 436 |
The command to remove a file from the ISO. |
| 437 |
""" |
| 438 |
split = shlex.split(line) |
| 439 |
if len(split) != 1: |
| 440 |
print('The rm_file command takes one and only one parameter (iso path).') |
| 441 |
return False |
| 442 |
|
| 443 |
iso_path = split[0] |
| 444 |
|
| 445 |
if iso_path[0] != '/': |
| 446 |
tmp = self.cwds['iso9660'] + '/' + iso_path |
| 447 |
iso_path = pycdlib.utils.normpath(tmp).decode('ascii') |
| 448 |
|
| 449 |
self.iso.rm_file(iso_path) |
| 450 |
|
| 451 |
return False |
| 452 |
|
| 453 |
def help_mkdir(self): |
| 454 |
"""The help method for the 'mkdir' command.""" |
| 455 |
print('> mkdir <iso_path> [rr_name=<rr_name>] [joliet_path=<joliet_path>]') |
| 456 |
print('Make a new directory called <iso_path>.') |
| 457 |
print('If the ISO is a Rock Ridge ISO, <rr_name> must be specified; otherwise, it must not be.') |
| 458 |
print('If the ISO is not a Joliet ISO, <joliet_path> must not be specified. If the ISO is a') |
| 459 |
print('Joliet ISO, <joliet_path> is optional, but highly recommended to supply.') |
| 460 |
|
| 461 |
def do_mkdir(self, line): |
| 462 |
""" |
| 463 |
The command to make a new directory on the ISO. |
| 464 |
""" |
| 465 |
split = shlex.split(line) |
| 466 |
|
| 467 |
if not split or len(split) > 3: |
| 468 |
self.help_mkdir() |
| 469 |
return False |
| 470 |
|
| 471 |
iso_path = split[0] |
| 472 |
rr_name = None |
| 473 |
joliet_path = None |
| 474 |
|
| 475 |
for arg in split[1:]: |
| 476 |
keyval = arg.split('=') |
| 477 |
if len(keyval) != 2: |
| 478 |
print('Invalid key/val pair, must be rr_name=<rr_name> or joliet_path=<joliet_path>') |
| 479 |
return False |
| 480 |
|
| 481 |
key = keyval[0] |
| 482 |
val = keyval[1] |
| 483 |
|
| 484 |
if key == 'rr_name': |
| 485 |
rr_name = val |
| 486 |
elif key == 'joliet_path': |
| 487 |
joliet_path = val |
| 488 |
else: |
| 489 |
print('Unknown key, must be rr_name=<rr_name> or joliet_path=<joliet_path>') |
| 490 |
return False |
| 491 |
|
| 492 |
if self.iso.has_rock_ridge() and rr_name is None: |
| 493 |
print('The ISO is Rock Ridge, so a <rr_name> parameter must be specified.') |
| 494 |
return False |
| 495 |
|
| 496 |
if iso_path[0] != '/': |
| 497 |
tmp = self.cwds['iso9660'] + '/' + iso_path |
| 498 |
iso_path = pycdlib.utils.normpath(tmp).decode('ascii') |
| 499 |
|
| 500 |
self.iso.add_directory(iso_path, rr_name=rr_name, joliet_path=joliet_path) |
| 501 |
|
| 502 |
return False |
| 503 |
|
| 504 |
def help_rmdir(self): |
| 505 |
"""The help method for the 'rmdir' command.""" |
| 506 |
print('> rmdir <iso_path>') |
| 507 |
print('Remove the directory at <iso_path>. Note that the directory must be empty for the command to succeed.') |
| 508 |
|
| 509 |
def do_rmdir(self, line): |
| 510 |
""" |
| 511 |
The command to remove a directory from the ISO. |
| 512 |
""" |
| 513 |
split = shlex.split(line) |
| 514 |
if len(split) != 1: |
| 515 |
print('The rmdir command takes one and only one parameter (iso path).') |
| 516 |
return False |
| 517 |
|
| 518 |
iso_path = split[0] |
| 519 |
|
| 520 |
if iso_path[0] != '/': |
| 521 |
tmp = self.cwds['iso9660'] + '/' + iso_path |
| 522 |
iso_path = pycdlib.utils.normpath(tmp).decode('ascii') |
| 523 |
|
| 524 |
self.iso.rm_directory(iso_path) |
| 525 |
|
| 526 |
return False |
| 527 |
|
| 528 |
|
| 529 |
def main(): |
| 530 |
""" |
| 531 |
The main function. |
| 532 |
""" |
| 533 |
|
| 534 |
if len(sys.argv) != 2: |
| 535 |
print('Usage: %s <isofile>' % (sys.argv[0])) |
| 536 |
sys.exit(1) |
| 537 |
|
| 538 |
iso = pycdlib.PyCdlib() |
| 539 |
with open(sys.argv[1], 'r+b') as fp: |
| 540 |
iso.open_fp(fp) |
| 541 |
|
| 542 |
done = False |
| 543 |
cmdloop = PyCdlibCmdLoop(iso) |
| 544 |
while not done: |
| 545 |
try: |
| 546 |
cmdloop.cmdloop() |
| 547 |
done = True |
| 548 |
except Exception as e: |
| 549 |
print(e) |
| 550 |
|
| 551 |
iso.close() |
| 552 |
|
| 553 |
|
| 554 |
if __name__ == '__main__': |
| 555 |
main() |