惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
博客园 - 【当耐特】
H
Help Net Security
腾讯CDC
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
Last Week in AI
Last Week in AI
Jina AI
Jina AI
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
Y
Y Combinator Blog
C
Check Point Blog
博客园 - 司徒正美
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
MTP File Transfer in Rust on macOS — Why I Wrote My Own S...
hiyoyo · 2026-06-20 · via DEV Community
Cover image for MTP File Transfer in Rust on macOS — Why I Wrote My Own Stack

hiyoyo

All tests run on an 8-year-old MacBook Air. All results from shipping 7 Mac apps as a solo developer. No sponsored opinion.

HiyokoMTP transfers files between Android and Mac via MTP. The obvious approach — use libmtp — doesn't work well on macOS. So I wrote a custom MTP implementation. Here's why, and what that looks like.


The libmtp problem on macOS

libmtp is the standard C library for MTP. On Linux, it works well. On macOS, it conflicts with IOKit's USB ownership model.

macOS claims USB devices at the system level. libmtp tries to claim them again at the library level. The result: connection failures, device not found errors, crashes. Unreliable enough to be unusable for a shipping product.


The alternative: nusb

nusb is a pure Rust USB library that works with macOS's IOKit properly. Instead of using libmtp, I implement the MTP protocol directly over USB using nusb.

[dependencies]
nusb = "0.1"


MTP basics in Rust

MTP runs over USB bulk transfer endpoints. The protocol is request-response: send an operation request, receive a response, transfer data.

use nusb::transfer::RequestBuffer;

async fn send_mtp_operation(
    interface: &nusb::Interface,
    op_code: u16,
    params: &[u32],
) -> Result<Vec<u8>, AppError> {
    // Build MTP container
    let container = build_operation_container(op_code, params);

    // Send on bulk-out endpoint
    interface.bulk_out(BULK_OUT_EP, container).await?;

    // Receive response on bulk-in endpoint
    let response = interface
        .bulk_in(BULK_IN_EP, RequestBuffer::new(512))
        .await?;

    parse_mtp_response(&response)
}


File transfer

Large file transfers use chunked bulk reads:

async fn download_file(
    interface: &nusb::Interface,
    object_handle: u32,
    output: &mut impl Write,
) -> Result<(), AppError> {
    // Request file data
    send_mtp_operation(interface, MTP_OP_GET_OBJECT, &[object_handle]).await?;

    // Read data container header
    let header = read_data_container_header(interface).await?;
    let total_size = header.data_length;
    let mut received = 0usize;

    // Stream data in chunks
    while received < total_size {
        let chunk = interface
            .bulk_in(BULK_IN_EP, RequestBuffer::new(65536))
            .await?;
        output.write_all(&chunk)?;
        received += chunk.len();
    }

    Ok(())
}


What this enables

A custom MTP stack means full control over the protocol. Smart resume (check partial transfers, skip completed files), parallel transfers (multiple MTP sessions), conflict detection — all buildable because the stack is yours.

libmtp gives you a C API. A custom stack gives you whatever you need.


Is it worth it?

For a Mac-focused app: yes. The libmtp reliability issues on macOS make it a non-starter. The custom stack took time to build but is stable and fast.

For a cross-platform app where Linux support matters: libmtp on Linux is fine. Use it there, custom on macOS.


TL;DR: libmtp conflicts with macOS's IOKit USB ownership model — don't use it on Mac. Instead, use nusb (pure Rust) and implement the MTP protocol directly over bulk USB transfers. More work upfront, but you get full control: smart resume, parallel sessions, conflict detection.


If this was useful, a ❤️ helps more than you'd think — thanks!

HiyokoMTP | X → @hiyoyok