Skip to content

Instantly share code, notes, and snippets.

@joachimmetz
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joachimmetz/c7b96c1cb5e72120514c to your computer and use it in GitHub Desktop.
Save joachimmetz/c7b96c1cb5e72120514c to your computer and use it in GitHub Desktop.
Script to convert fsntfsinfo output into a find-like output.
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2014, Joachim Metz <joachim.metz@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script to convert fsntfsinfo output into a find-like output."""
from __future__ import print_function
import argparse
import sys
def Main():
"""The main program function.
Returns:
A boolean containing True if successful or False if not.
"""
args_parser = argparse.ArgumentParser(description=(
u'Converts fsntfsinfo output into a find-like output.'))
args_parser.add_argument(
u'source', nargs=u'?', action=u'store', metavar=u'fsntfsinfo.txt',
default=None, help=(u'file containing the output of fsntfsinfo.'))
options = args_parser.parse_args()
if not options.source:
print(u'Source value is missing.')
print(u'')
args_parser.print_help()
print(u'')
return False
with open(options.source, 'rb') as file_object:
path_segments = []
in_file_hierarchy = False
for line in file_object.readlines():
line = line.decode(u'utf8')
line = line.rstrip()
if not in_file_hierarchy:
if line.startswith(u'File hierarchy information:'):
in_file_hierarchy = True
else:
if not line:
continue
number_of_leading_spaces = 0;
for character in line:
if character != u' ':
break
number_of_leading_spaces += 1
while True:
if len(path_segments) <= number_of_leading_spaces:
break
_ = path_segments.pop()
filename = line.lstrip()
path_segments.append(filename)
path = u'/'.join(path_segments)
if len(path_segments) == 1:
path = u'{0:s}/'.format(path)
print(path.encode(u'utf8'))
return True
if __name__ == '__main__':
if not Main():
sys.exit(1)
else:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment