---
name: "mtp_libmtp.c"
section: "3"
platform: "linux"
package: "libmtp-doc"
version: "1.1.22-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mtp_libmtp.c"
---

# mtp\_libmtp.c(3)

## NAME

libmtp -

## SYNOPSIS

`#include 'config.h'`\
 `#include 'libmtp.h'`\
 `#include 'unicode.h'`\
 `#include 'ptp.h'`\
 `#include 'libusb-glue.h'`\
 `#include 'device-flags.h'`\
 `#include 'playlist-spl.h'`\
 `#include 'util.h'`\
 `#include 'mtpz.h'`\
 `#include <stdarg.h>`\
 `#include <stdlib.h>`\
 `#include <limits.h>`\
 `#include <unistd.h>`\
 `#include <string.h>`\
 `#include <sys/types.h>`\
 `#include <sys/stat.h>`\
 `#include <fcntl.h>`\
 `#include <time.h>`\
 `#include <errno.h>`\

### Data Structures

struct **filemap\_struct**\
 struct **propertymap\_struct**\
 struct **event\_cb\_data\_struct**\
 struct **\_MTPDataHandler**\

### Typedefs

typedef struct **filemap\_struct** **filemap\_t**\
 typedef struct **propertymap\_struct** **propertymap\_t**\
 typedef struct **event\_cb\_data\_struct** **event\_cb\_data\_t**\
 typedef struct **\_MTPDataHandler** **MTPDataHandler**\

### Functions

void **LIBMTP\_Set\_Debug** (int level)\
 void **LIBMTP\_Init** (void)\
 char const \* **LIBMTP\_Get\_Filetype\_Description** (**LIBMTP\_filetype\_t** intype)\
 char const \* **LIBMTP\_Get\_Property\_Description** (**LIBMTP\_property\_t** inproperty)\
 int **LIBMTP\_Get\_Allowed\_Property\_Values** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_property\_t** const property, **LIBMTP\_filetype\_t** const filetype, **LIBMTP\_allowed\_values\_t** \*allowed\_vals)\
 void **LIBMTP\_destroy\_allowed\_values\_t** (**LIBMTP\_allowed\_values\_t** \*allowed\_vals)\
 int **LIBMTP\_Is\_Property\_Supported** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_property\_t** const property, **LIBMTP\_filetype\_t** const filetype)\
 char \* **LIBMTP\_Get\_String\_From\_Object** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const object\_id, **LIBMTP\_property\_t** const attribute\_id)\
 uint64\_t **LIBMTP\_Get\_u64\_From\_Object** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const object\_id, **LIBMTP\_property\_t** const attribute\_id, uint64\_t const value\_default)\
 uint32\_t **LIBMTP\_Get\_u32\_From\_Object** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const object\_id, **LIBMTP\_property\_t** const attribute\_id, uint32\_t const value\_default)\
 uint16\_t **LIBMTP\_Get\_u16\_From\_Object** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const object\_id, **LIBMTP\_property\_t** const attribute\_id, uint16\_t const value\_default)\
 uint8\_t **LIBMTP\_Get\_u8\_From\_Object** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const object\_id, **LIBMTP\_property\_t** const attribute\_id, uint8\_t const value\_default)\
 int **LIBMTP\_Set\_Object\_String** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const object\_id, **LIBMTP\_property\_t** const attribute\_id, char const \*const string)\
 int **LIBMTP\_Set\_Object\_u32** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const object\_id, **LIBMTP\_property\_t** const attribute\_id, uint32\_t const value)\
 int **LIBMTP\_Set\_Object\_u16** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const object\_id, **LIBMTP\_property\_t** const attribute\_id, uint16\_t const value)\
 int **LIBMTP\_Set\_Object\_u8** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const object\_id, **LIBMTP\_property\_t** const attribute\_id, uint8\_t const value)\
 **LIBMTP\_mtpdevice\_t** \* **LIBMTP\_Get\_Device** (int device\_nr)\
 **LIBMTP\_mtpdevice\_t** \* **LIBMTP\_Get\_First\_Device** (void)\
 **LIBMTP\_mtpdevice\_t** \* **LIBMTP\_Get\_Device\_By\_SerialNumber** (char \*serial\_number)\
 **LIBMTP\_mtpdevice\_t** \* **LIBMTP\_Get\_Device\_By\_ID** (char \*device\_id)\
 **LIBMTP\_mtpdevice\_t** \* **LIBMTP\_Open\_Raw\_Device\_Uncached** (**LIBMTP\_raw\_device\_t** \*rawdevice)\
 **LIBMTP\_mtpdevice\_t** \* **LIBMTP\_Open\_Raw\_Device** (**LIBMTP\_raw\_device\_t** \*rawdevice)\
 int **LIBMTP\_Read\_Event** (**LIBMTP\_mtpdevice\_t** \*device, LIBMTP\_event\_t \*event, uint32\_t \*out1)\
 int **LIBMTP\_Read\_Event\_Async** (**LIBMTP\_mtpdevice\_t** \*device, LIBMTP\_event\_cb\_fn cb, void \*user\_data)\
 uint32\_t **LIBMTP\_Number\_Devices\_In\_List** (**LIBMTP\_mtpdevice\_t** \*device\_list)\
 **LIBMTP\_error\_number\_t** **LIBMTP\_Get\_Connected\_Devices** (**LIBMTP\_mtpdevice\_t** \*\*device\_list)\
 void **LIBMTP\_Release\_Device\_List** (**LIBMTP\_mtpdevice\_t** \*device)\
 void **LIBMTP\_Release\_Device** (**LIBMTP\_mtpdevice\_t** \*device)\
 **LIBMTP\_error\_t** \* **LIBMTP\_Get\_Errorstack** (**LIBMTP\_mtpdevice\_t** \*device)\
 void **LIBMTP\_Clear\_Errorstack** (**LIBMTP\_mtpdevice\_t** \*device)\
 void **LIBMTP\_Dump\_Errorstack** (**LIBMTP\_mtpdevice\_t** \*device)\
 void **LIBMTP\_Dump\_Device\_Info** (**LIBMTP\_mtpdevice\_t** \*device)\
 int **LIBMTP\_Reset\_Device** (**LIBMTP\_mtpdevice\_t** \*device)\
 char \* **LIBMTP\_Get\_Manufacturername** (**LIBMTP\_mtpdevice\_t** \*device)\
 char \* **LIBMTP\_Get\_Modelname** (**LIBMTP\_mtpdevice\_t** \*device)\
 char \* **LIBMTP\_Get\_Serialnumber** (**LIBMTP\_mtpdevice\_t** \*device)\
 char \* **LIBMTP\_Get\_Deviceversion** (**LIBMTP\_mtpdevice\_t** \*device)\
 char \* **LIBMTP\_Get\_Friendlyname** (**LIBMTP\_mtpdevice\_t** \*device)\
 int **LIBMTP\_Set\_Friendlyname** (**LIBMTP\_mtpdevice\_t** \*device, char const \*const friendlyname)\
 char \* **LIBMTP\_Get\_Syncpartner** (**LIBMTP\_mtpdevice\_t** \*device)\
 int **LIBMTP\_Set\_Syncpartner** (**LIBMTP\_mtpdevice\_t** \*device, char const \*const syncpartner)\
 int **LIBMTP\_Get\_Batterylevel** (**LIBMTP\_mtpdevice\_t** \*device, uint8\_t \*const maximum\_level, uint8\_t \*const current\_level)\
 int **LIBMTP\_Format\_Storage** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_devicestorage\_t** \*storage)\
 int **LIBMTP\_Get\_Secure\_Time** (**LIBMTP\_mtpdevice\_t** \*device, char \*\*const sectime)\
 int **LIBMTP\_Get\_Device\_Certificate** (**LIBMTP\_mtpdevice\_t** \*device, char \*\*const devcert)\
 int **LIBMTP\_Get\_Supported\_Filetypes** (**LIBMTP\_mtpdevice\_t** \*device, uint16\_t \*\*const filetypes, uint16\_t \*const length)\
 int **LIBMTP\_Check\_Capability** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_devicecap\_t** cap)\
 int **LIBMTP\_Get\_Storage** (**LIBMTP\_mtpdevice\_t** \*device, int const sortby)\
 **LIBMTP\_file\_t** \* **LIBMTP\_new\_file\_t** (void)\
 void **LIBMTP\_destroy\_file\_t** (**LIBMTP\_file\_t** \*file)\
 **LIBMTP\_file\_t** \* **LIBMTP\_Get\_Filemetadata** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const fileid)\
 **LIBMTP\_file\_t** \* **LIBMTP\_Get\_Filelisting** (**LIBMTP\_mtpdevice\_t** \*device)\
 **LIBMTP\_file\_t** \* **LIBMTP\_Get\_Filelisting\_With\_Callback** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 **LIBMTP\_file\_t** \* **LIBMTP\_Get\_Files\_And\_Folders** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const storage, uint32\_t const parent)\
 int **LIBMTP\_Get\_Children** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const storage, uint32\_t const parent, uint32\_t \*\*out)\
 **LIBMTP\_track\_t** \* **LIBMTP\_new\_track\_t** (void)\
 void **LIBMTP\_destroy\_track\_t** (**LIBMTP\_track\_t** \*track)\
 **LIBMTP\_track\_t** \* **LIBMTP\_Get\_Tracklisting** (**LIBMTP\_mtpdevice\_t** \*device)\
 **LIBMTP\_track\_t** \* **LIBMTP\_Get\_Tracklisting\_With\_Callback** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 **LIBMTP\_track\_t** \* **LIBMTP\_Get\_Tracklisting\_With\_Callback\_For\_Storage** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const storage\_id, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 **LIBMTP\_track\_t** \* **LIBMTP\_Get\_Trackmetadata** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const trackid)\
 int **LIBMTP\_Get\_File\_To\_File** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, char const \*const path, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Get\_File\_To\_File\_Descriptor** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, int const fd, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Get\_File\_To\_Handler** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, **MTPDataPutFunc** put\_func, void \*priv, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Get\_Track\_To\_File** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, char const \*const path, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Get\_Track\_To\_File\_Descriptor** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, int const fd, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Get\_Track\_To\_Handler** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, **MTPDataPutFunc** put\_func, void \*priv, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Send\_Track\_From\_File** (**LIBMTP\_mtpdevice\_t** \*device, char const \*const path, **LIBMTP\_track\_t** \*const metadata, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Send\_Track\_From\_File\_Descriptor** (**LIBMTP\_mtpdevice\_t** \*device, int const fd, **LIBMTP\_track\_t** \*const metadata, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Send\_Track\_From\_Handler** (**LIBMTP\_mtpdevice\_t** \*device, **MTPDataGetFunc** get\_func, void \*priv, **LIBMTP\_track\_t** \*const metadata, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Send\_File\_From\_File** (**LIBMTP\_mtpdevice\_t** \*device, char const \*const path, **LIBMTP\_file\_t** \*const filedata, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Send\_File\_From\_File\_Descriptor** (**LIBMTP\_mtpdevice\_t** \*device, int const fd, **LIBMTP\_file\_t** \*const filedata, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Send\_File\_From\_Handler** (**LIBMTP\_mtpdevice\_t** \*device, **MTPDataGetFunc** get\_func, void \*priv, **LIBMTP\_file\_t** \*const filedata, **LIBMTP\_progressfunc\_t** const callback, void const \*const data)\
 int **LIBMTP\_Update\_Track\_Metadata** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_track\_t** const \*const metadata)\
 int **LIBMTP\_Delete\_Object** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t object\_id)\
 int **LIBMTP\_Move\_Object** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t object\_id, uint32\_t storage\_id, uint32\_t parent\_id)\
 int **LIBMTP\_Copy\_Object** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t object\_id, uint32\_t storage\_id, uint32\_t parent\_id)\
 int **LIBMTP\_Set\_File\_Name** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_file\_t** \*file, const char \*newname)\
 int **LIBMTP\_Set\_Folder\_Name** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_folder\_t** \*folder, const char \*newname)\
 int **LIBMTP\_Set\_Track\_Name** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_track\_t** \*track, const char \*newname)\
 int **LIBMTP\_Set\_Playlist\_Name** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_playlist\_t** \*playlist, const char \*newname)\
 int **LIBMTP\_Set\_Album\_Name** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_album\_t** \*album, const char \*newname)\
 int **LIBMTP\_Set\_Object\_Filename** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t object\_id, char \*newname)\
 int **LIBMTP\_Track\_Exists** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id)\
 **LIBMTP\_folder\_t** \* **LIBMTP\_new\_folder\_t** (void)\
 void **LIBMTP\_destroy\_folder\_t** (**LIBMTP\_folder\_t** \*folder)\
 **LIBMTP\_folder\_t** \* **LIBMTP\_Find\_Folder** (**LIBMTP\_folder\_t** \*folderlist, uint32\_t id)\
 **LIBMTP\_folder\_t** \* **LIBMTP\_Get\_Folder\_List\_For\_Storage** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const storage)\
 **LIBMTP\_folder\_t** \* **LIBMTP\_Get\_Folder\_List** (**LIBMTP\_mtpdevice\_t** \*device)\
 uint32\_t **LIBMTP\_Create\_Folder** (**LIBMTP\_mtpdevice\_t** \*device, char \*name, uint32\_t parent\_id, uint32\_t storage\_id)\
 **LIBMTP\_playlist\_t** \* **LIBMTP\_new\_playlist\_t** (void)\
 void **LIBMTP\_destroy\_playlist\_t** (**LIBMTP\_playlist\_t** \*playlist)\
 **LIBMTP\_playlist\_t** \* **LIBMTP\_Get\_Playlist\_List** (**LIBMTP\_mtpdevice\_t** \*device)\
 **LIBMTP\_playlist\_t** \* **LIBMTP\_Get\_Playlist** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const plid)\
 int **LIBMTP\_Create\_New\_Playlist** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_playlist\_t** \*const metadata)\
 int **LIBMTP\_Update\_Playlist** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_playlist\_t** \*const metadata)\
 **LIBMTP\_album\_t** \* **LIBMTP\_new\_album\_t** (void)\
 void **LIBMTP\_destroy\_album\_t** (**LIBMTP\_album\_t** \*album)\
 **LIBMTP\_album\_t** \* **LIBMTP\_Get\_Album\_List** (**LIBMTP\_mtpdevice\_t** \*device)\
 **LIBMTP\_album\_t** \* **LIBMTP\_Get\_Album\_List\_For\_Storage** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const storage\_id)\
 **LIBMTP\_album\_t** \* **LIBMTP\_Get\_Album** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const albid)\
 int **LIBMTP\_Create\_New\_Album** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_album\_t** \*const metadata)\
 **LIBMTP\_filesampledata\_t** \* **LIBMTP\_new\_filesampledata\_t** (void)\
 void **LIBMTP\_destroy\_filesampledata\_t** (**LIBMTP\_filesampledata\_t** \*sample)\
 int **LIBMTP\_Get\_Representative\_Sample\_Format** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_filetype\_t** const filetype, **LIBMTP\_filesampledata\_t** \*\*sample)\
 int **LIBMTP\_Send\_Representative\_Sample** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, **LIBMTP\_filesampledata\_t** \*sampledata)\
 int **LIBMTP\_Get\_Representative\_Sample** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, **LIBMTP\_filesampledata\_t** \*sampledata)\
 int **LIBMTP\_Get\_Thumbnail** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, unsigned char \*\*data, unsigned int \*size)\
 int **LIBMTP\_GetPartialObject** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, uint64\_t offset, uint32\_t maxbytes, unsigned char \*\*data, unsigned int \*size)\
 int **LIBMTP\_SendPartialObject** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, uint64\_t offset, unsigned char \*data, unsigned int size)\
 int **LIBMTP\_BeginEditObject** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id)\
 int **LIBMTP\_EndEditObject** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id)\
 int **LIBMTP\_TruncateObject** (**LIBMTP\_mtpdevice\_t** \*device, uint32\_t const id, uint64\_t offset)\
 int **LIBMTP\_Update\_Album** (**LIBMTP\_mtpdevice\_t** \*device, **LIBMTP\_album\_t** const \*const metadata)\
 void **ptp\_nikon\_getptpipguid** (unsigned char \*guid)\
 int **LIBMTP\_Custom\_Operation** (**LIBMTP\_mtpdevice\_t** \*device, uint16\_t code, int n\_param,...)\
 void **LIBMTP\_FreeMemory** (void \*mem)\

### Variables

int **use\_mtpz**\
 int **LIBMTP\_debug** = **LIBMTP\_DEBUG\_NONE**\

## Detailed Description

Copyright (C) 2005-2011 Linus Walleij triad@df.lth.se Copyright (C) 2005-2008 Richard A. Low richard@wentnet.com Copyright (C) 2007 Ted Bullock tbullock@canada.com Copyright (C) 2007 Tero Saarni tero.saarni@gmail.com Copyright (C) 2008 Florent Mertens flomertens@gmail.com

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This library 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 GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

This file provides an interface 'glue' to the underlying PTP implementation from libgphoto2. It uses some local code to convert from/to UTF-8 (stored in **unicode.c**/.h) and some small utility functions, mainly for debugging (stored in **util.c**/.h).

The three PTP files (ptp.c, **ptp.h** and **ptp-pack.c**) are plain copied from the libhphoto2 codebase.

The files libusb-glue.c/.h are just what they say: an interface to libusb for the actual, physical USB traffic.

## Typedef Documentation

### typedef struct \_MTPDataHandler MTPDataHandler

These are to wrap the get/put handlers to convert from the MTP types to PTP types in a reliable way

## Function Documentation

### void ptp\_nikon\_getptpipguid (unsigned char \* guid)

Dummy function needed to interface to upstream ptp.c/ptp.h files.

## Variable Documentation

### int LIBMTP\_debug = LIBMTP\_DEBUG\_NONE

Global debug level We use a flag system to enable a part of logs.

The LIBMTP\_DEBUG environment variable sets the debug flags for any binary that uses libmtp and calls LIBMTP\_Init. The value can be given in decimal (must not start with '0' or it will be interpreted in octal), or in hexadecimal (must start with '0x').

The value '-1' enables all debug flags.

Some of the utilities in examples/ also take a command-line flag '-d' that enables LIBMTP\_DEBUG\_PTP and LIBMTP\_DEBUG\_DATA (same as setting LIBMTP\_DEBUG=9).

Flags (combine by adding the hex values): 0x00 \[0000 0000\] : LIBMTP\_DEBUG\_NONE : no debug (default) 0x01 \[0000 0001\] : LIBMTP\_DEBUG\_PTP : PTP debug 0x02 \[0000 0010\] : LIBMTP\_DEBUG\_PLST : Playlist debug 0x04 \[0000 0100\] : LIBMTP\_DEBUG\_USB : USB debug 0x08 \[0000 1000\] : LIBMTP\_DEBUG\_DATA : USB data debug

(Please keep this list in sync with **libmtp.h**.)

Referenced by **LIBMTP\_Set\_Debug()**.

## Author

Generated automatically by Doxygen for libmtp from the source code.
