void qwrite(afc_connection * afc, const char *from, const char *to) { printf("Sending %s -> %s... ", from, to); afc_file_ref ref; int fd = open(from, O_RDONLY); assert(fd != -1); size_t size = (size_t) lseek(fd, 0, SEEK_END); void *buf = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); assert(buf != MAP_FAILED); AFCFileRefOpen(afc, to, 3, &ref); AFCFileRefWrite(afc, ref, buf, size); AFCFileRefClose(afc, ref); printf("done.\n"); close(fd); }
// Writes a file from the PC filesystem and stores it on the iPod. // // 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 written // // TODO: cleaner return error codes // int CiPoTApi::FileWrite(char *remotePath, char *localPath) { struct _stat info; t_MachError ret; unsigned char *buffer; FILE *fLocal; unsigned long len, total = 0; afc_file_ref handle; CMacPath MacPath; MacPath.SetWindowsPath(remotePath); if (_stat(localPath, &info)) return MDERR_AFC_NOT_FOUND; ret = AFCFileRefOpen(m_iPodConnection, MacPath.GetBuffer(), AFC_FILEMODE_WRITE, 0, &handle); if (ret != MDERR_OK) return ret; buffer = new unsigned char[FILEREAD_BUFFER_SIZE]; if (buffer) { fLocal = fopen(localPath, "rb"); if (fLocal) { while (total < (unsigned long)info.st_size) { len = min(info.st_size - total, FILEREAD_BUFFER_SIZE); len = fread(buffer, 1, len, fLocal); if (!len) break; ret = AFCFileRefWrite(m_iPodConnection, handle, buffer, len); if (ret != MDERR_OK) break; total += len; if (m_ProgressCallBack(remotePath, localPath, (int)(100.0*total/info.st_size))) break; } fclose(fLocal); } delete[] buffer; } AFCFileRefClose(m_iPodConnection, handle); return (total == info.st_size) ? MDERR_OK : -1; }
void upload_file(AMDeviceRef device) { service_conn_t houseFd = start_house_arrest_service(device); afc_file_ref file_ref; afc_connection afc_conn; afc_connection* afc_conn_p = &afc_conn; AFCConnectionOpen(houseFd, 0, &afc_conn_p); // read_dir(houseFd, NULL, "/"); if (!target_filename) { target_filename = get_filename_from_path(doc_file_path); } char *target_path = malloc(sizeof("/Documents/") + strlen(target_filename) + 1); strcat(target_path, "/Documents/"); strcat(target_path, target_filename); size_t file_size; void* file_content = read_file_to_memory(doc_file_path, &file_size); if (!file_content) { PRINT("Could not open file: %s\n", doc_file_path); exit(-1); } assert(AFCFileRefOpen(afc_conn_p, target_path, 3, &file_ref) == 0); assert(AFCFileRefWrite(afc_conn_p, file_ref, file_content, file_size) == 0); assert(AFCFileRefClose(afc_conn_p, file_ref) == 0); assert(AFCConnectionClose(afc_conn_p) == 0); free(target_path); free(file_content); }
static uint32 FileRefWrite(AFCCommConnection* Connection, uint64 Handle, void* Data, uint32 Length) { return AFCFileRefWrite(Connection, Handle, Data, Length); }