/ManagediDeviceBackup2/ManagedDeviceBackup2.h

https://github.com/outbred/libimobiledevice-win32 · C Header · 263 lines · 204 code · 33 blank · 26 comment · 4 complexity · 86158c897701713ce5cad59c7b101e95 MD5 · raw file

  1. #include "iDeviceBackup2.h"
  2. #include <libimobiledevice/screenshotr.h>
  3. using namespace System;
  4. using namespace System::Runtime::InteropServices;
  5. using namespace System::IO;
  6. struct timeval_t {
  7. long tv_sec; /* seconds */
  8. long tv_usec; /* and microseconds */
  9. };
  10. //typedef timeval_t timeval;
  11. #define FORMAT_KEY_VALUE 1
  12. #define FORMAT_XML 2
  13. static const char *domains[] = {
  14. "com.apple.disk_usage",
  15. "com.apple.disk_usage.factory",
  16. "com.apple.mobile.battery",
  17. /* FIXME: For some reason lockdownd segfaults on this, works sometimes though
  18. "com.apple.mobile.debug",. */
  19. "com.apple.iqagent",
  20. "com.apple.purplebuddy",
  21. "com.apple.PurpleBuddy",
  22. "com.apple.mobile.chaperone",
  23. "com.apple.mobile.third_party_termination",
  24. "com.apple.mobile.lockdownd",
  25. "com.apple.mobile.lockdown_cache",
  26. "com.apple.xcode.developerdomain",
  27. "com.apple.international",
  28. "com.apple.mobile.data_sync",
  29. "com.apple.mobile.tethered_sync",
  30. "com.apple.mobile.mobile_application_usage",
  31. "com.apple.mobile.backup",
  32. "com.apple.mobile.nikita",
  33. "com.apple.mobile.restriction",
  34. "com.apple.mobile.user_preferences",
  35. "com.apple.mobile.sync_data_class",
  36. "com.apple.mobile.software_behavior",
  37. "com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands",
  38. "com.apple.mobile.iTunes.accessories",
  39. "com.apple.mobile.internal", /**< iOS 4.0+ */
  40. "com.apple.mobile.wireless_lockdown", /**< iOS 4.0+ */
  41. "com.apple.fairplay",
  42. "com.apple.iTunes",
  43. "com.apple.mobile.iTunes.store",
  44. "com.apple.mobile.iTunes",
  45. NULL
  46. };
  47. static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  48. static const char base64_pad = '=';
  49. namespace BackupWrapper {
  50. [Serializable]
  51. public ref class NoDeviceFoundException : public Exception
  52. {
  53. public:
  54. NoDeviceFoundException() : Exception("No device detected. Plugged in and turned on?") {}
  55. NoDeviceFoundException(Exception^ ex) : Exception("No device detected. Plugged in and turned on?", ex) {}
  56. };
  57. [Serializable]
  58. public ref class DeviceErrorException : public Exception
  59. {
  60. public:
  61. DeviceErrorException() : Exception("Error communicating with device.") {}
  62. DeviceErrorException(Exception^ ex) : Exception("Error communicating with device.", ex) {}
  63. };
  64. [Serializable]
  65. public ref class BadPasswordException : public Exception
  66. {
  67. public:
  68. BadPasswordException() : Exception("The password(s) supplied are invalid.") {}
  69. BadPasswordException(Exception^ ex) : Exception("The password(s) supplied are invalid.", ex) {}
  70. };
  71. [Serializable]
  72. public ref class NoInfoPlistFoundException : public Exception
  73. {
  74. public:
  75. NoInfoPlistFoundException() : Exception("There was no info.plist at the specified directory. Are you sure that's a backup location for the connected device?") {}
  76. NoInfoPlistFoundException(Exception^ ex) : Exception("There was no info.plist at the specified directory. Are you sure that's a backup location for the connected device?", ex) {}
  77. };
  78. [Serializable]
  79. public ref class NoManifestPlistFoundException : public Exception
  80. {
  81. public:
  82. NoManifestPlistFoundException() : Exception("There was no manifest.plist at the specified directory. Are you sure that's a backup location for the connected device?") {}
  83. NoManifestPlistFoundException(Exception^ ex) : Exception("There was no manifest.plist at the specified directory. Are you sure that's a backup location for the connected device?", ex) {}
  84. };
  85. [Serializable]
  86. public ref class UnableToStartServiceException : public Exception
  87. {
  88. public:
  89. UnableToStartServiceException(String^ message) : Exception(String::Format("Unable to start service {0}.", message)) {}
  90. UnableToStartServiceException(String^ message, Exception^ ex) : Exception(String::Format("Unable to start service {0}.", message), ex) {}
  91. };
  92. [Serializable]
  93. public ref class UnableToPerformBackupProtocalExchangeException : public Exception
  94. {
  95. public:
  96. UnableToPerformBackupProtocalExchangeException() : Exception("Unable to perform backup protocol version exchange") {}
  97. UnableToPerformBackupProtocalExchangeException(Exception^ ex) : Exception("Unable to perform backup protocol version exchange", ex) {}
  98. };
  99. [Serializable]
  100. public ref class ErrorCommunicatingWithDeviceException : public Exception
  101. {
  102. public:
  103. ErrorCommunicatingWithDeviceException() : Exception("Unable to communicate with the device") {}
  104. ErrorCommunicatingWithDeviceException(Exception^ ex) : Exception("Unable to communicate with the device", ex) {}
  105. };
  106. [Serializable]
  107. public ref class BackupDeviceException : public Exception
  108. {
  109. public:
  110. BackupDeviceException(String^ message) : Exception(message) {}
  111. BackupDeviceException(String^ message, Exception^ ex) : Exception(message, ex) {}
  112. };
  113. [Serializable]
  114. public ref class RestoreDeviceException : public Exception
  115. {
  116. public:
  117. RestoreDeviceException(String^ message) : Exception(message) {}
  118. RestoreDeviceException(String^ message, Exception^ ex) : Exception(message, ex) {}
  119. };
  120. public delegate int CancelTaskCallBack(const char *notification, void *userdata);
  121. ///<summary>
  122. ///Wrapper around all the unamanged code that does all the hard work of backup/restore from/to an iOS device
  123. ///</summary>
  124. public ref class ManagedDeviceBackup2
  125. {
  126. protected:
  127. !ManagedDeviceBackup2() {
  128. Cleanup();
  129. }
  130. public:
  131. ManagedDeviceBackup2() {
  132. Initialize();
  133. }
  134. ~ManagedDeviceBackup2() {
  135. Cleanup();
  136. }
  137. /*
  138. <MBStatus: version=2.4, date=2013-07-29 20:17:10 +0000, backupState=new, snapshotState=finished, fullBackup=0>
  139. <MBProperties: version=8.0, systemDomainsVersion=3.0, date=2013-07-29 20:16:58 +0000, encrypted=0, passcodeSet=0, lockdownKey
  140. s={ProductType: "iPod2,1", BuildVersion: "8C148", DeviceName: "MPETESTING", UniqueDeviceID: "253ad710236d7228eb42fbbb5c3fc184
  141. 4716384e", com.apple.Accessibility: {}, com.apple.MobileDeviceCrashCopy: {}, ProductVersion: "4.2.1", com.apple.mobile.data_s
  142. ync: {}, com.apple.TerminalFlashr: {}, com.apple.mobile.iTunes.accessories: {}}>
  143. <MBDatabaseIndex: version=2.0, count=113>
  144. <MBDatabase: version=5.0>
  145. Count Size (B) Domain name
  146. 45 1523726 HomeDomain
  147. 2 18985 KeychainDomain
  148. 8 70463 MediaDomain
  149. 2 41002 RootDomain
  150. 6 3034 SystemPreferencesDomain
  151. 2 28738 WirelessDomain
  152. 65 1685948 Total
  153. */
  154. String^ GetInfoFromLastBackup(String^ backupDirectory);
  155. String^ UnbackBackup(String^ backupDirectory, [System::Runtime::InteropServices::Optional] String^ udid);
  156. System::Collections::Generic::List<String^>^ GetConnectedDevicesUdids();
  157. bool HasConnectedDevice();
  158. void Backup(
  159. [System::Runtime::InteropServices::Optional]
  160. String^ uUid,
  161. [System::Runtime::InteropServices::Optional]
  162. String^ password,
  163. [System::Runtime::InteropServices::Optional]
  164. String^ backupDirectoryRoot,
  165. [System::Runtime::InteropServices::Optional]
  166. Action<Double, Double>^ progressCallback);
  167. void Restore(
  168. String^ backupDirectory,
  169. [System::Runtime::InteropServices::Optional]
  170. String^ sourceUuid,
  171. [System::Runtime::InteropServices::Optional]
  172. String^ password,
  173. [System::Runtime::InteropServices::Optional]
  174. bool^ copyFirst,
  175. [System::Runtime::InteropServices::Optional]
  176. bool^ rebootWhenDone,
  177. [System::Runtime::InteropServices::Optional]
  178. bool^ removeItemsNotRestored,
  179. [System::Runtime::InteropServices::Optional]
  180. bool^ restoreSystemFiles,
  181. [System::Runtime::InteropServices::Optional]
  182. Action<Double, Double>^ progressCallback,
  183. [System::Runtime::InteropServices::Optional]
  184. bool^ restoreSettings);
  185. Boolean^ ChangePassword(String^ currentPassword, String^ newPassword) { return false; }
  186. bool^ GetInfoForConnectedDevice(String^ udid, String^ locationForPlist, bool^ simple);
  187. bool GetScreenshotofDevice(String^ backupDirectory, String^ udid, [Out] String^% filePath );
  188. private:
  189. delegate void ProgressCallbackWrapper(uint64_t current, uint64_t total);
  190. int Errors;
  191. cmd_mode RequestedOperation;
  192. void Cleanup() {
  193. if(_deviceBackup) {
  194. Errors = _deviceBackup->GetErrorFlags();
  195. RequestedOperation = _deviceBackup->GetRequestedOperation();
  196. delete _deviceBackup;
  197. _deviceBackup = NULL;
  198. }
  199. cancelTask = nullptr;
  200. _progressCallback = nullptr;
  201. }
  202. void Initialize() {
  203. Errors = 0;
  204. _progressCallback = nullptr;
  205. progressWrapper = gcnew ProgressCallbackWrapper(this, &ManagedDeviceBackup2::ReportProgress);
  206. }
  207. progress_t GetProgressCallback() {
  208. if(_deviceBackup) {
  209. IntPtr ip = Marshal::GetFunctionPointerForDelegate(progressWrapper);
  210. return static_cast<progress_t>(ip.ToPointer());
  211. }
  212. return NULL;
  213. }
  214. void ReportProgress(uint64_t current, uint64_t total) {
  215. if(_progressCallback != nullptr) {
  216. _progressCallback(static_cast<double>(current), static_cast<double>(total));
  217. }
  218. }
  219. void DecodeErrors();
  220. CancelTaskCallBack^ cancelTask;
  221. ProgressCallbackWrapper^ progressWrapper;
  222. Action<Double, Double>^ _progressCallback;
  223. iDeviceBackup2* _deviceBackup;
  224. // BEGIN ideviceinfo support
  225. char *base64encode(const unsigned char *buf, size_t size);
  226. int is_domain_known(char *domain);
  227. // END
  228. };
  229. }