// Reads a file from the iPod and stores it on the PC filesystem. // // Returns: // MDERR_OK when successful // MDERR_AFC_NOT_FOUND if the file couldn't be found (or is a broken link) // MDERR_AFC_ACCESS_DENIED if access denied (e.g. directory, dir. link) // -1 if the file could not be entirely read // // TODO: cleaner return error codes // int CiPoTApi::FileRead(char *remotePath, char *localPath) { t_iPodFileInfo info; t_iPodExtraInfo extra; t_MachError ret; unsigned char *buffer; FILE *fLocal; unsigned long total = 0; unsigned int len; afc_file_ref handle; CMacPath MacPath; char linkPath[MAX_PATH]; MacPath.SetWindowsPath(remotePath); ret = GetFileInfo(remotePath, &info, &extra); if (ret != MDERR_OK) return ret; // Resolve symlinks if (extra.iType == IPOD_IFLNK) { GetLinkTargetPath(MacPath.GetBuffer(), extra.LinkTarget, linkPath); MacPath.SetWindowsPath(linkPath); } ret = AFCFileRefOpen(m_iPodConnection, MacPath.GetBuffer(), AFC_FILEMODE_READ, 0, &handle); if (ret != MDERR_OK) return ret; buffer = new unsigned char[FILEREAD_BUFFER_SIZE]; if (buffer) { fLocal = fopen(localPath, "wb"); if (fLocal) { while (total < info.findData.nFileSizeLow) { len = (unsigned int)min(info.findData.nFileSizeLow - total, (unsigned long)FILEREAD_BUFFER_SIZE); ret = AFCFileRefRead(m_iPodConnection, handle, buffer, &len); if (ret != MDERR_OK || !len) break; if (fwrite(buffer, 1, len, fLocal) != len) break; total += len; if (m_ProgressCallBack(remotePath, localPath, (int)(100.0*total/info.findData.nFileSizeLow))) break; } fclose(fLocal); } delete[] buffer; } AFCFileRefClose(m_iPodConnection, handle); return (total == info.findData.nFileSizeLow) ? MDERR_OK : -1; }
static void device_notification_callback(am_device_notification_callback_info *info, void *thing) { if (info->msg != ADNCI_MSG_CONNECTED) return; puts("Opened device connection."); am_device *dev = info->dev; AMDeviceConnect(dev); assert(AMDeviceIsPaired(dev)); assert(AMDeviceValidatePairing(dev) == 0); assert(AMDeviceStartSession(dev) == 0); struct afc_connection *afc; service_conn_t afc_conn; assert(AMDeviceStartService(dev, CFSTR("com.apple.afc2"), &afc_conn, NULL) == 0); assert(AFCConnectionOpen(afc_conn, 0, &afc) == 0); char cachepath[63]; const char *caches[3] = {"dyld_shared_cache_armv7s", "dyld_shared_cache_armv7", "dyld_shared_cache_armv6"}; struct afc_dictionary *dict; unsigned int cache_index; char *fullpath; size_t cachesize; for (cache_index=0; cache_index<3; cache_index++) { strcpy(cachepath, "/System/Library/Caches/com.apple.dyld/"); fullpath = strcat(cachepath, caches[cache_index]); if (AFCFileInfoOpen(afc, fullpath, &dict) == 0) { char *key, *value; while (1) { assert(AFCKeyValueRead(dict, &key, &value) == 0); if (key == NULL) break; if (strcmp(key, "st_size") == 0) { cachesize = strtol(value, NULL, 0); break; } } printf("Found cache %s with size %lu\n", fullpath, cachesize); assert(AFCKeyValueClose(dict) == 0); goto _label_hasfile; } } fprintf(stderr, "Could not find cache file.\n"); exit(2); _label_hasfile:; afc_file_ref cache; assert(AFCFileRefOpen(afc, fullpath, 1, &cache) == 0); if (is_cwd) { strcat(outputfile, "/"); strcat(outputfile, caches[cache_index]); gen_path = 1; } puts(outputfile); FILE *output = fopen(outputfile, "w"); assert(output != NULL); printf("Writing cache to %s\n", outputfile); size_t total_bytes = 0; char buffer[65536]; while (1) { unsigned int length = 65536; assert(AFCFileRefRead(afc, cache, buffer, &length) == 0); fwrite(buffer, sizeof(char), length, output); total_bytes += length; float progress = (float)total_bytes/cachesize*100; printf("Progress: %f%%\n\033[F\033[J", progress); if (length < sizeof(buffer)) break; } printf("Successfully wrote cache to %s\n", outputfile); assert(AFCFileRefClose(afc, cache) == 0); CFRunLoopStop(CFRunLoopGetCurrent()); }