Beispiel #1
0
 def GetMachObject(self, path):
     if not os.path.exists(path):
         raise Exception('No such file or directory: ' + path)
     if os.path.isdir(path):
         for root, dirs, files in os.walk(path):
             for fn in files:
                 path = os.path.join(root, fn)
                 if is_platform_file(path):
                     return path
     else:
         if is_platform_file(path):
             return path
     raise Exception('No mach object found')
def _gatherNodes(threadPool: concurrent.futures.Executor, targets: list,
                 records: list, ignoreSystem: bool, verbosity: int) -> dict:
    nodes = queue.Queue()
    nodeCache = {}  # "path@arch" : node
    cacheLock = threading.Lock()

    __worker = functools.partial(_worker,
                                 nodes=nodes,
                                 cache=nodeCache,
                                 lock=cacheLock,
                                 records=records,
                                 ignoreSystem=ignoreSystem,
                                 verbosity=verbosity)

    for target in targets:
        rootFiles = _listAllFiles(target["files"])
        for path in rootFiles:
            if util.is_platform_file(path):  # ignoring all non-macho files
                nodes.put({
                    "path": path,
                    "root": True,
                    "package": target["package"]
                })

    futures = []
    for i in range(threadPool._max_workers):
        futures.append(threadPool.submit(__worker))

    nodes.join()

    [future.result() for future in futures]  # raising exceptions if any

    return nodeCache
Beispiel #3
0
def iter_platform_files(path, is_platform_file=macholib.util.is_platform_file):
    """
    Iterate over all of the platform files in a directory
    """
    for root, _dirs, files in os.walk(path):
        for fn in files:
            fn = os.path.join(root, fn)
            if is_platform_file(fn):
                yield fn
Beispiel #4
0
def check_file(fp, path, callback):
    if not os.path.exists(path):
        print >>sys.stderr, '%s: %s: No such file or directory' % (sys.argv[0], path)
        return 1

    try:
        is_plat = is_platform_file(path)

    except IOError, msg:
        print >>sys.stderr, '%s: %s: %s' % (sys.argv[0], path, msg)
        return 1
Beispiel #5
0
def check_file(fp, path, callback):
    if not os.path.exists(path):
        print >> sys.stderr, '%s: %s: No such file or directory' % (
            sys.argv[0], path)
        return 1

    try:
        is_plat = is_platform_file(path)

    except IOError, msg:
        print >> sys.stderr, '%s: %s: %s' % (sys.argv[0], path, msg)
        return 1
Beispiel #6
0
def check_file(path):
    if not os.path.exists(path):
        print >>sys.stderr, '%s: %s: No such file or directory' % (sys.argv[0], path)
        return 1
    try:
        is_plat = is_platform_file(path)
    except IOError:
        print >>sys.stderr, '%s: %s: Permission denied' % (sys.argv[0], path)
        return 1
    else:
        if is_plat:
            dump_file(path)
    return 0
    def test_shared_main(self):

        saved_stderr = sys.stderr
        saved_argv = sys.argv
        try:
            sys.stderr = StringIO()

            sys.argv = ["macho_tool"]

            self.assertEqual(_cmdline.main(lambda *args: None), 1)
            self.assertEqual(sys.stderr.getvalue(),
                             "Usage: macho_tool filename...\n")

            names = []

            def record_names(fp, name):
                self.assertEqual(fp, sys.stdout)
                names.append(name)

            sys.stderr = StringIO()
            sys.argv = ["macho_tool", "/bin/sh"]
            self.assertEqual(_cmdline.main(record_names), 0)
            self.assertEqual(sys.stderr.getvalue(), "")
            self.assertEqual(names, ["/bin/sh"])

            names = []
            sys.stderr = StringIO()
            sys.argv = ["macho_tool", "/bin/sh", "/bin/ls"]
            self.assertEqual(_cmdline.main(record_names), 0)
            self.assertEqual(sys.stderr.getvalue(), "")
            self.assertEqual(names, ["/bin/sh", "/bin/ls"])

            names = []
            sys.stderr = StringIO()
            sys.argv = ["macho_tool", "/bin"]
            self.assertEqual(_cmdline.main(record_names), 0)
            self.assertEqual(sys.stderr.getvalue(), "")
            names.sort()
            dn = "/bin"
            real_names = [
                os.path.join(dn, fn) for fn in os.listdir(dn)
                if util.is_platform_file(os.path.join(dn, fn))
            ]
            real_names.sort()

            self.assertEqual(names, real_names)

        finally:
            sys.stderr = saved_stderr
            sys.argv = saved_argv
Beispiel #8
0
def check_file(path):
    if not os.path.exists(path):
        print >> sys.stderr, '%s: %s: No such file or directory' % (
            sys.argv[0], path)
        return 1
    try:
        is_plat = is_platform_file(path)
    except IOError:
        print >> sys.stderr, '%s: %s: Permission denied' % (sys.argv[0], path)
        return 1
    else:
        if is_plat:
            print path
    return 0
    def test_shared_main(self):

        saved_stderr = sys.stderr
        saved_argv = sys.argv
        try:
            sys.stderr = StringIO()

            sys.argv = ['macho_tool']

            self.assertEqual(_cmdline.main(lambda *args: None), 1)
            self.assertEqual(sys.stderr.getvalue(), 'Usage: macho_tool filename...\n')

            names = []
            def record_names(fp, name):
                self.assertEqual(fp, sys.stdout)
                names.append(name)


            sys.stderr = StringIO()
            sys.argv = ['macho_tool', '/bin/sh']
            self.assertEqual(_cmdline.main(record_names), 0)
            self.assertEqual(sys.stderr.getvalue(), '')
            self.assertEqual(names, ['/bin/sh'])

            names = []
            sys.stderr = StringIO()
            sys.argv = ['macho_tool', '/bin/sh', '/bin/ls']
            self.assertEqual(_cmdline.main(record_names), 0)
            self.assertEqual(sys.stderr.getvalue(), '')
            self.assertEqual(names, ['/bin/sh', '/bin/ls'])

            names = []
            sys.stderr = StringIO()
            sys.argv = ['macho_tool', '/bin']
            self.assertEqual(_cmdline.main(record_names), 0)
            self.assertEqual(sys.stderr.getvalue(), '')
            names.sort()
            dn = '/bin'
            real_names = [
                    os.path.join(dn, fn) for fn in os.listdir(dn)
                    if util.is_platform_file(os.path.join(dn, fn)) ]
            real_names.sort()

            self.assertEqual(names, real_names)

        finally:
            sys.stderr = saved_stderr
            sys.argv = saved_argv
Beispiel #10
0
def check_file(fp, path, callback):
    if not os.path.exists(path):
        print('%s: %s: No such file or directory' % (sys.argv[0], path), file=sys.stderr)
        return 1

    try:
        is_plat = is_platform_file(path)

    except IOError as msg:
        print('%s: %s: %s' % (sys.argv[0], path, msg), file=sys.stderr)
        return 1

    else:
        if is_plat:
            callback(fp, path)
    return 0
Beispiel #11
0
def check_file(fp, path, callback):
    if not os.path.exists(path):
        print("%s: %s: No such file or directory" % (gCommand, path), file=sys.stderr)
        return 1

    try:
        is_plat = is_platform_file(path)

    except IOError as msg:
        print("%s: %s: %s" % (gCommand, path, msg), file=sys.stderr)
        return 1

    else:
        if is_plat:
            callback(fp, path)
    return 0
Beispiel #12
0
def check_file(filePath,gCommand,outputPath):
    if not os.path.exists(filePath):
        print("the input file does not exist,please input again")
        sys.exit(1)
        
    if not os.path.exists(outputPath):
        print("the output path does not exist,please input again")
        sys.exit(1)

    try:
        is_macho = is_platform_file(filePath)
        is_fat = utils.isFatBinary(filePath)
        is_InfoPlist = (gCommand == "plist") \
            and (filePath.split("/")[-1] == "Info.plist")

    except IOError as msg:
        print('%s: %s: %s' % (gCommand, path, msg), file=sys.stderr)
        sys.exit(1)

    if not (is_macho or is_fat or is_InfoPlist):
        print("the input file is not macho file or fat binary file or Info.plist,please input again")
        sys.exit(1)
Beispiel #13
0
def _macho_find(path):
    for basename, _dirs, files in os.walk(path):
        for fn in files:
            path = os.path.join(basename, fn)
            if is_platform_file(path):
                yield path