internal static AFCError copyToDisk(IntPtr afcClient, string filePath, string savePath) { IntPtr infoPtr; AFCError returnCode = afc_get_file_info(afcClient, filePath, out infoPtr); if (returnCode != AFCError.AFC_E_SUCCESS) { return(returnCode); } List <string> infoList = LibiMobileDevice.PtrToStringList(infoPtr, 0); long fileSize = Convert.ToInt64(infoList[infoList.FindIndex(x => x == "st_size") + 1]); ulong fileHandle; returnCode = afc_file_open(afcClient, filePath, FileOpenMode.AFC_FOPEN_RDONLY, out fileHandle); if (returnCode != AFCError.AFC_E_SUCCESS) { return(returnCode); } FileStream fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write); const int bufferSize = 4194304; for (int i = 0; i < fileSize / bufferSize + 1; i++) { uint bytesRead; long remainder = fileSize - i * bufferSize; int currBufferSize = remainder >= bufferSize ? bufferSize : (int)remainder; byte[] currBuffer = new byte[currBufferSize]; if ((returnCode = afc_file_read(afcClient, fileHandle, currBuffer, Convert.ToUInt32(currBufferSize), out bytesRead)) != AFCError.AFC_E_SUCCESS) { afc_file_close(afcClient, fileHandle); return(returnCode); } fileStream.Write(currBuffer, 0, currBufferSize); } fileStream.Close(); returnCode = afc_file_close(afcClient, fileHandle); return(returnCode); }
static AFCError saveDirectoryTree(IntPtr afcClient, string directoryPath, StreamWriter streamWriter, out string lastDirectory) { lastDirectory = directoryPath; AFCError returnCode; IntPtr directoryListPtr; returnCode = AFC.afc_read_directory(afcClient, directoryPath, out directoryListPtr); if (returnCode == AFCError.AFC_E_READ_ERROR) { return(AFCError.AFC_E_SUCCESS); } else if (returnCode != AFCError.AFC_E_SUCCESS) { return(returnCode); } List <string> directoryList = LibiMobileDevice.PtrToStringList(directoryListPtr, 2); afc_dictionary_free(directoryListPtr); if (directoryPath == "/") { directoryPath = ""; } int tabNumber = directoryPath.Count(x => x == '/'); directoryList.Sort(); foreach (string currDirectory in directoryList) { if (currDirectory == "Photos.sqlite") { photoDatabasePath = directoryPath + "/" + currDirectory; } streamWriter.WriteLine(String.Concat(Enumerable.Repeat("\t", tabNumber)) + currDirectory); if ((returnCode = saveDirectoryTree(afcClient, directoryPath + "/" + currDirectory, streamWriter, out lastDirectory)) != AFCError.AFC_E_SUCCESS) { break; } } return(returnCode); }