// Finds the first file of a given directory. Must be used in conjunction with // the FindNext and FindClose methods. FindClose needn't be called if the call // to FindFirst returned false. // // Returns: // true if a file was found // false if no file was found, or if there was an error // // Example: // // CiPoTApi iPodApi; // t_iPodFileInfo info; // bool bFile; // t_iPodError status = iPodApi.OpenSession(); // // if (status == IPOD_ERR_OK) { // bFile = iPodApi.FindFirst(remotePath, &info); // while (bFile) { // printf("%s\n", info.findData.cFileName); // bFile = iPodApi.FindNext(&info); // } // iPodApi.FindClose(&info); // bool CiPoTApi::FindFirst(char *remotePath, t_iPodFileInfo *pInfo) { t_AFCDirectory *pHandle; afc_error_t ret; char *pEntry; CMacPath MacPath; pInfo->pHandle = NULL; pInfo->remotePath = NULL; MacPath.SetWindowsPath(remotePath); ret = AFCDirectoryOpen(m_iPodConnection, MacPath.GetBuffer(), &pHandle); if (ret) return false; ret = AFCDirectoryRead(m_iPodConnection, pHandle, &pEntry); if (ret || pEntry == NULL) { // Broken link? AFCDirectoryClose(m_iPodConnection, pHandle); return false; } pInfo->remotePath = strdup(remotePath); pInfo->pHandle = pHandle; // Now, a special case with the Applications folder pInfo->appFolder = bTranslateApps && ( !strcmp(remotePath, _T("\\User\\Applications")) || !strcmp(remotePath, _T("\\var\\mobile\\Applications")) || !strcmp(remotePath, _T("\\private\\var\\mobile\\Applications"))); MacPath.SetString(pEntry); MacPath.GetWindowsPath(pInfo->findData.cFileName); GetFileInfo(0, pInfo); return true; }
// Finds the next file in the directory. Used in conjunction with FindFirst // and FindClose. // // Returns: // true if a file was found // false if no more file was found, or if there was an error // bool CiPoTApi::FindNext(t_iPodFileInfo *pInfo) { char *pEntry; afc_error_t ret; CMacPath MacPath; ret = AFCDirectoryRead(m_iPodConnection, pInfo->pHandle, &pEntry); if (ret || pEntry == NULL) return false; MacPath.SetString(pEntry); if (pInfo->appFolder && MacPath.Compare(_T(".."))) do { t_AFCDirectory *pAppHandle; char *pAppEntry = 0; CMacPath FullPath; FullPath.SetWindowsPath(pInfo->remotePath); FullPath.Append(_T("/")); FullPath.Append(MacPath); // MessageBox(0, FullPath.GetBuffer(), MacPath.GetBuffer(), 0); ret = AFCDirectoryOpen(m_iPodConnection, FullPath.GetBuffer(), &pAppHandle); if (ret) pAppHandle = 0; while (!ret) { ret = AFCDirectoryRead(m_iPodConnection, pAppHandle, &pAppEntry); if (ret || !pAppEntry) break; //MessageBox(0, pAppEntry, FullPath.GetBuffer(), 0); int len = lstrlen(pAppEntry); if (len > 4 && !lstrcmp(pAppEntry + len - 4, _T(".app"))) break; } //MessageBox(0, pAppEntry, FullPath.GetBuffer(), 0); if (!ret && pAppEntry) { MacPath.Append(_T("/")); MacPath.Append(pAppEntry); } if (pAppHandle) AFCDirectoryClose(m_iPodConnection, pAppHandle); } while (0); MacPath.GetWindowsPath(pInfo->findData.cFileName); GetFileInfo(0, pInfo); return true; }
/** Debug function, retrieves all files in a directory and prints them to stdout */ BOOL GetFiles(iPhone* iphone, char *path) { if(iphone->connected == FALSE){ iPhone_SetLastError("GetFiles() failed, device not connected."); return FALSE; } //If we aren't connected, we can't get files. void *hDir = NULL; if(AFCDirectoryOpen(iphone->hAFC, path, &hDir) != 0){ iPhone_SetLastError("AFCDirectoryOpen failed"); return FALSE; } char *buffer = NULL; while(1) { AFCDirectoryRead(iphone->hAFC, hDir, &buffer); if(buffer == NULL) break; printf("%s\n", buffer); } AFCDirectoryClose(iphone->hAFC, hDir); return TRUE; }
void read_dir(service_conn_t afcFd, afc_connection* afc_conn_p, const char* dir) { char *dir_ent; afc_connection afc_conn; if (!afc_conn_p) { afc_conn_p = &afc_conn; AFCConnectionOpen(afcFd, 0, &afc_conn_p); } printf("%s\n", dir); afc_dictionary afc_dict; afc_dictionary* afc_dict_p = &afc_dict; AFCFileInfoOpen(afc_conn_p, dir, &afc_dict_p); afc_directory afc_dir; afc_directory* afc_dir_p = &afc_dir; afc_error_t err = AFCDirectoryOpen(afc_conn_p, dir, &afc_dir_p); if (err != 0) { // Couldn't open dir - was probably a file return; } while(true) { err = AFCDirectoryRead(afc_conn_p, afc_dir_p, &dir_ent); if (!dir_ent) break; if (strcmp(dir_ent, ".") == 0 || strcmp(dir_ent, "..") == 0) continue; char* dir_joined = malloc(strlen(dir) + strlen(dir_ent) + 2); strcpy(dir_joined, dir); if (dir_joined[strlen(dir)-1] != '/') strcat(dir_joined, "/"); strcat(dir_joined, dir_ent); read_dir(afcFd, afc_conn_p, dir_joined); free(dir_joined); } AFCDirectoryClose(afc_conn_p, afc_dir_p); }