fatlister/fatlister_wrapper.py

141 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
"""List file and directory names in a disk image."""
#===============================================================================
#
# FILE:
# ./fatlister_wrapper.py
#
# BASIC USAGE:
# $ ./fatlister_wrapper.py [-h|--help] [-o|--offset OFFSET] [-f|--file FILENAME] image
# OR
# $ python fatlister_wrapper.py [-h|--help] [-o|--offset OFFSET] [-f|--file FILENAME] image
#
# OPTIONS:
# -h,
# --help show help message and exit
# -o OFFSET,
# --offset OFFSET offset in sectors (default=0)
# -f FILENAME, write output to file (default=unset)
# --file FILENAME if unset output goes to stdout
# image raw image file (esp. dd)
#
# EXIT STATES:
# 0 = success
# 1 = Python version not tested/supported
# 2 = image file does not exist
# 33 = file is not readable
# 3 = offset is not a positive integer
# 22 = output file already exist
# 23 = output directory is not writable
# 4 = wrong or missing data in dictionary
# 5 = empty image file
# 6 = image file smaller than offset
# 7 = invalid vbr signatures
#
# REQUIREMENTS:
# python2.7
#
# NOTES:
# Tested on:
# - ArchLinux (64-Bit) + python 2.7.13
# - Raspbian GNU/Linux 8.0 (32-Bit) + python 2.7.9
# - macOS (10.12.3) + python 2.7.10
# - Bash on Ubuntu on Windows 10 (64-Bit) + python 2.7.6
# with:
# - raw images
# - filesystem(s) in partition(s)
# - filesystem without a partition
# - no partition/no filesystem (empty file)
# - invalid filesystem
# - FAT12, FAT16 and FAT32
#
# WARRANTY:
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See the LICENSE file for more details.
#
# HISTORY:
# See the CHANGES file for more details.
#
#===============================================================================
#=== MODULES ===================================================================
import argparse
import sys
import fatlister
#=== INFO ======================================================================
"""@author: Patrick Neumann
@contact: patrick@neumannsland.de
@copyright: Copyright (C) 2017, Patrick Neumann
@license: GNU General Public License 3.0
@date: 2017-05-08
@version: 1.0.0
@status: Development
"""
#=== CHECKS ====================================================================
"""Only tested with Python versions 3.6.x!"""
if sys.version_info < (3, 6, 0):
sys.stderr.write("Sorry, your Python version was not tested but may work?\n")
sys.exit(1)
#=== FUNCTIONS =================================================================
# This is a private module function only for the other module functions.
# Unfortunately, the underscore does not prevent other developers from using
# it directly from outside this module. :-(
# sphinx-apidoc does ignore it. :-)
def _getargs():
"""Get command line arguments.
Parses the command line arguments and return them.
(Don't forget to check them in the next steps?)
Returns
-------
tuple
A tuple containing the to the image file,
the offset in bytes in the image file calculated from sectors
and the output file.
The default offset is 0.
For example:
( "image.dd", 1048576, "/dev/stdout" )
"""
parser = argparse.ArgumentParser()
parser.add_argument("image",
type=str,
help="raw image file (esp.: image.dd)")
parser.add_argument("-o",
"--offset",
type=int,
default=0,
help="offset in sectors (default=0)")
parser.add_argument("-f",
"--file",
type=str,
default="",
help="output file (default=/dev/stdout)")
args = parser.parse_args()
return (args.image, args.offset * 512, args.file)
#=== MAIN ======================================================================
if __name__ == '__main__':
(IMAGE, OFFSET, FILE) = _getargs()
OBJECT = fatlister.Fatlister(IMAGE, OFFSET, FILE)
sys.exit(0)