From 187659d7d30e183a4ef8f54703408f45af3c1253 Mon Sep 17 00:00:00 2001
From: Nikias Bassen <nikias@gmx.li>
Date: Fri, 31 Jul 2009 03:30:21 +0200
Subject: [PATCH 01/14] Try locking the lockfile multiple times until we get a lock.

When the MobileMusicPlayer app is running and the sync is started, it
will be closed down. The lockfile is locked by the music player and
it takes 1-2 seconds for MobileMusicPlayer to terminate. So we'll wait
a maximum of 10 seconds to get the file lock.
---
 src/itdbprep/libitdbprep.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/src/itdbprep/libitdbprep.c b/src/itdbprep/libitdbprep.c
index 8325a9a..4166dde 100644
--- a/src/itdbprep/libitdbprep.c
+++ b/src/itdbprep/libitdbprep.c
@@ -1402,7 +1402,15 @@ int itdbprep_start_sync(const char *uuid, itdbprep_t *prepdata)
     }
 
     /* lock /com.apple.itunes.lock_sync */
-    afcerror = afc_file_lock(pdata_loc->afc, pdata_loc->lockfile, AFC_LOCK_EX);
+    int retries = 0;
+    do {
+	afcerror = afc_file_lock(pdata_loc->afc, pdata_loc->lockfile, AFC_LOCK_EX);
+	if (afcerror == AFC_E_SUCCESS) {
+	    break;
+	}
+	sleep(1);
+    } while (retries++ < 10);
+
     if (afcerror != AFC_E_SUCCESS) {
 	fprintf(stderr, "%s: ERROR: could not lock lockfile (AFC error code %d)\n", __func__, afcerror);
 	afc_file_close(pdata_loc->afc, pdata_loc->lockfile);
-- 
1.6.4.4

