Fixing my iPad’s photo order with Samba

Since upgrading to iOS 5.0 the photos on my iPad and iPhone have been in the “wrong” order and after spending a few hours googling, reading this thread on the Apple forums and researching I now understand why and how to work round it.

Summary

iTunes when syncing to iOS 5.0/5.1 doesn’t use the exif date, file timestamps or filenames to determine the order of photos in an album, instead the order is based on the order of files returned by the QueryDirectory syscall. You can work round this by presorting the list of files returned by Samba using the VFS module dirsort.

Background

My photos are shared from a HP Microserver running Ubuntu, normally I access and edit my photos under Linux via iSCSI but I have an automatically updating LVM snapshot of my master photo archive exported to Windows by Samba for iTunes to sync my iDevices.

The exact details here don’t really matter, the keys points are that I have my photos stored on Linux and exported via Samba to iTunes running on Windows 7.

The problem

In iOS 4.x synced photos in the photos app where displayed in exif date order, in iOS 5.0/5.1 they are displayed it what seems to be a random order. This order isn’t affected by any of the time stamps on the files (ctime/mtime/atime for those who know unix), the names of the files or the exif timestamp embedded in the files.

Debugging

After trying all the tricks I could think of fiddling with timestamps and names of the files under Linux I tried copying the files on my Windows machines local NTFS drive and resyncing the iPad and it worked!

So the problem wasn’t in the files or the file names it had to be somewhere between Windows and Samba, next I fired up process monitor from sysinternals to watch exactly what iTunes was doing. After a bit tinkering with the filters on process monitor I finally found something that matched the “random” order of photos on my iPad, I had found the problem!

The cause

When iTunes builds the list of files to sync onto a iDevice it uses the order of files returned by the QueryDirectory syscall, when this syscall is used to list a directory under NTFS it returns the list of files in the directory sorted in alphabetical order, but Samba returns it unsorted in disk order. As you can see from this screenshot the list of files is unsorted, and it matches perfectly the apparently random order of photos on my iPad in the album.

The solution

Some further googling led me to discover that other people have had problems with Windows applications that assume directory listings are returned sorted, and someone has written a Samba VFS module that works round the problem. The module is called dirsort and is included in modern versions of Samba so all you need to work round the problem is add the following line to /etc/samba/smb.conf for your photo’s share definition.

vfs objects = dirsort

After making this change and restarting Samba this is output of the QueryDirectory syscall for the same directory.

This does assume that your photos are named in the order you want to show them, I fix this by using the following exiftool command.

exiftool "-FileName<CreateDate" -d "%Y%m%d_%H%M%S.%%e" DIR