python 在python中使用来自OSX 10.11 El Capitan的SharedFileList(.sfl)文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在python中使用来自OSX 10.11 El Capitan的SharedFileList(.sfl)文件相关的知识,希望对你有一定的参考价值。

from Foundation import NSKeyedUnarchiver
from struct import unpack

# This entire function is black magic of the highest order and I'll blog about it later
def extract_share(bookmark_data):
    content_offset, = unpack('I', bookmark_data[12:16])
    first_TOC, = unpack('I', bookmark_data[content_offset:content_offset+4])
    first_TOC += content_offset
    TOC_len, rec_type, level, next_TOC, record_count = unpack('IIIII', bookmark_data[first_TOC:first_TOC+20])
    TOC_cursor = first_TOC + 20
    record_offsets = {}
    for i in range(record_count):
        record_id, offset = unpack('<IQ', bookmark_data[TOC_cursor:TOC_cursor+12])
        record_offsets[record_id] = offset + content_offset
        TOC_cursor += 12
    mount_record = record_offsets.get(0x2050, None)
    # Check to see if we actually had a volMountURL record
    if mount_record is not None:
        mount_length, rec_type = unpack('II', bookmark_data[mount_record:mount_record+8])
        mount_record += 8
        mount_URL = (bookmark_data[mount_record:mount_record+mount_length]).decode('utf-8')
        return mount_URL
    else:
        return None

def get_recentservers(sfl_file_path):
    # Read the com.apple.LSSharedFileList.RecentServers.sfl file (located in ~/Library/Application Support/com.apple.sharedfilelist on 10.11+)
    with open(sfl_file_path, 'rb') as f:
        raw_data = f.read()
    # It's NSKeyedArchiver data - let's decode it!
    recent_servers = NSKeyedUnarchiver.unarchiveObjectWithData_(buffer(raw_data))
    # Build an empty set
    server_URLs = []
    # Add in our discovered server URLs from the SFLListItems and return in 'SFLListItem.order' order
    for x in sorted(recent_servers['items'], lambda y,_: int(y.order())):
        url = extract_share(x.bookmark()[:].tobytes())
        if url is not None:
            server_URLs.append(url)
    return server_URLs

# Example usage:
# get_recentservers('com.apple.LSSharedFileList.RecentServers.sfl')

以上是关于python 在python中使用来自OSX 10.11 El Capitan的SharedFileList(.sfl)文件的主要内容,如果未能解决你的问题,请参考以下文章

恢复 Python/Cocoa 项目以使用默认的 OSX 10.5 Python (2.5)

在 OSX 10.13.2、python 3.5.1、Anaconda3 上安装 vowpal wabbit python3 接口

使用 Python 多处理进行通信的 OSX 和 Linux 之间的性能差异

python tab 补全 Mac OSX 10.7 (Lion)

设置:OS X 10.6.6 + XCode3.2 使用 python 和 cocoa

在 OSX 中,将应用程序名称从“python”更改