diff options
| -rw-r--r-- | src/Makefile.am | 10 | ||||
| -rw-r--r-- | src/ifuse.c | 351 |
2 files changed, 3 insertions, 358 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 9a5df62..34f6c1c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | INCLUDES = -I$(top_srcdir)/include | 1 | INCLUDES = -I$(top_srcdir)/include |
| 2 | 2 | ||
| 3 | AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libfuse_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) -g | 3 | AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) -g |
| 4 | AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libfuse_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) | 4 | AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) |
| 5 | 5 | ||
| 6 | bin_PROGRAMS = iphoneclient ifuse libiphone-initconf lckd-client | 6 | bin_PROGRAMS = iphoneclient libiphone-initconf lckd-client |
| 7 | iphoneclient_SOURCES = main.c | 7 | iphoneclient_SOURCES = main.c |
| 8 | iphoneclient_LDADD = libiphone.la | 8 | iphoneclient_LDADD = libiphone.la |
| 9 | 9 | ||
| @@ -11,9 +11,6 @@ libiphone_initconf_SOURCES = initconf.c userpref.c lockdown.c plist.c usbmux.c i | |||
| 11 | libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS) | 11 | libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS) |
| 12 | libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS) | 12 | libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS) |
| 13 | 13 | ||
| 14 | ifuse_SOURCES = ifuse.c | ||
| 15 | ifuse_LDADD = libiphone.la | ||
| 16 | |||
| 17 | lckd_client_SOURCES = lckdclient.c | 14 | lckd_client_SOURCES = lckdclient.c |
| 18 | lckd_client_CFLAGS = $(AM_CFLAGS) | 15 | lckd_client_CFLAGS = $(AM_CFLAGS) |
| 19 | lckd_client_LDFLAGS = -lreadline $(AM_LDFLAGS) | 16 | lckd_client_LDFLAGS = -lreadline $(AM_LDFLAGS) |
| @@ -21,4 +18,3 @@ lckd_client_LDADD = libiphone.la | |||
| 21 | 18 | ||
| 22 | lib_LTLIBRARIES = libiphone.la | 19 | lib_LTLIBRARIES = libiphone.la |
| 23 | libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c userpref.c | 20 | libiphone_la_SOURCES = usbmux.c iphone.c plist.c lockdown.c AFC.c userpref.c |
| 24 | |||
diff --git a/src/ifuse.c b/src/ifuse.c deleted file mode 100644 index aef24bd..0000000 --- a/src/ifuse.c +++ /dev/null | |||
| @@ -1,351 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * ifuse.c | ||
| 3 | * A Fuse filesystem which exposes the iPhone's filesystem. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2008 Matt Colyer All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #define FUSE_USE_VERSION 26 | ||
| 23 | |||
| 24 | #include <fuse.h> | ||
| 25 | #include <stdio.h> | ||
| 26 | #include <string.h> | ||
| 27 | #include <errno.h> | ||
| 28 | #include <fcntl.h> | ||
| 29 | #include <usb.h> | ||
| 30 | #include <libxml/parser.h> | ||
| 31 | #include <libxml/tree.h> | ||
| 32 | #include <glib.h> | ||
| 33 | #include <unistd.h> | ||
| 34 | |||
| 35 | #include <libiphone/libiphone.h> | ||
| 36 | |||
| 37 | GHashTable *file_handles; | ||
| 38 | int fh_index = 0; | ||
| 39 | |||
| 40 | iphone_device_t phone = NULL; | ||
| 41 | iphone_lckd_client_t control = NULL; | ||
| 42 | |||
| 43 | int debug = 0; | ||
| 44 | |||
| 45 | static int ifuse_getattr(const char *path, struct stat *stbuf) | ||
| 46 | { | ||
| 47 | int res = 0; | ||
| 48 | |||
| 49 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 50 | iphone_error_t ret = iphone_afc_get_file_attr(afc, path, stbuf); | ||
| 51 | |||
| 52 | if (ret != IPHONE_E_SUCCESS) | ||
| 53 | res = -ENOENT; | ||
| 54 | |||
| 55 | return res; | ||
| 56 | } | ||
| 57 | |||
| 58 | static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) | ||
| 59 | { | ||
| 60 | int i; | ||
| 61 | char **dirs = NULL; | ||
| 62 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 63 | |||
| 64 | iphone_afc_get_dir_list(afc, path, &dirs); | ||
| 65 | |||
| 66 | if (!dirs) | ||
| 67 | return -ENOENT; | ||
| 68 | |||
| 69 | for (i = 0; dirs[i]; i++) { | ||
| 70 | filler(buf, dirs[i], NULL, 0); | ||
| 71 | } | ||
| 72 | |||
| 73 | free_dictionary(dirs); | ||
| 74 | |||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | |||
| 78 | static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) | ||
| 79 | { | ||
| 80 | // exactly the same as open but using a different mode | ||
| 81 | iphone_afc_file_t file = NULL; | ||
| 82 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 83 | |||
| 84 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_WRITE, &file); | ||
| 85 | fh_index++; | ||
| 86 | fi->fh = fh_index; | ||
| 87 | g_hash_table_insert(file_handles, &fh_index, file); | ||
| 88 | return 0; | ||
| 89 | } | ||
| 90 | |||
| 91 | static int ifuse_open(const char *path, struct fuse_file_info *fi) | ||
| 92 | { | ||
| 93 | iphone_afc_file_t file = NULL; | ||
| 94 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 95 | uint32_t mode = 0; | ||
| 96 | |||
| 97 | if ((fi->flags & 3) == O_RDWR || (fi->flags & 3) == O_WRONLY) { | ||
| 98 | mode = IPHONE_AFC_FILE_READ; | ||
| 99 | } else if ((fi->flags & 3) == O_RDONLY) { | ||
| 100 | mode = IPHONE_AFC_FILE_READ; | ||
| 101 | } else { | ||
| 102 | mode = IPHONE_AFC_FILE_READ; | ||
| 103 | } | ||
| 104 | |||
| 105 | iphone_afc_open_file(afc, path, mode, &file); | ||
| 106 | |||
| 107 | fh_index++; | ||
| 108 | fi->fh = fh_index; | ||
| 109 | g_hash_table_insert(file_handles, &fh_index, file); | ||
| 110 | |||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | |||
| 114 | static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) | ||
| 115 | { | ||
| 116 | int bytes = 0; | ||
| 117 | iphone_afc_file_t file; | ||
| 118 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 119 | |||
| 120 | if (size == 0) | ||
| 121 | return 0; | ||
| 122 | |||
| 123 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | ||
| 124 | if (!file) { | ||
| 125 | return -ENOENT; | ||
| 126 | } | ||
| 127 | |||
| 128 | if (IPHONE_E_SUCCESS == iphone_afc_seek_file(afc, file, offset)) | ||
| 129 | iphone_afc_read_file(afc, file, buf, size, &bytes); | ||
| 130 | return bytes; | ||
| 131 | } | ||
| 132 | |||
| 133 | static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) | ||
| 134 | { | ||
| 135 | int bytes = 0; | ||
| 136 | iphone_afc_file_t file = NULL; | ||
| 137 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 138 | |||
| 139 | if (size == 0) | ||
| 140 | return 0; | ||
| 141 | |||
| 142 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | ||
| 143 | if (!file) | ||
| 144 | return -ENOENT; | ||
| 145 | |||
| 146 | if (IPHONE_E_SUCCESS == iphone_afc_seek_file(afc, file, offset)) | ||
| 147 | iphone_afc_write_file(afc, file, buf, size, &bytes); | ||
| 148 | return bytes; | ||
| 149 | } | ||
| 150 | |||
| 151 | static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) | ||
| 152 | { | ||
| 153 | return 0; | ||
| 154 | } | ||
| 155 | |||
| 156 | static int ifuse_release(const char *path, struct fuse_file_info *fi) | ||
| 157 | { | ||
| 158 | iphone_afc_file_t file = NULL; | ||
| 159 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 160 | |||
| 161 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | ||
| 162 | if (!file) { | ||
| 163 | return -ENOENT; | ||
| 164 | } | ||
| 165 | iphone_afc_close_file(afc, file); | ||
| 166 | |||
| 167 | g_hash_table_remove(file_handles, &(fi->fh)); | ||
| 168 | |||
| 169 | return 0; | ||
| 170 | } | ||
| 171 | |||
| 172 | void *ifuse_init_with_service(struct fuse_conn_info *conn, const char *service_name) | ||
| 173 | { | ||
| 174 | int port = 0; | ||
| 175 | iphone_afc_client_t afc = NULL; | ||
| 176 | |||
| 177 | conn->async_read = 0; | ||
| 178 | |||
| 179 | file_handles = g_hash_table_new(g_int_hash, g_int_equal); | ||
| 180 | |||
| 181 | iphone_get_device(&phone); | ||
| 182 | if (!phone) { | ||
| 183 | fprintf(stderr, "No iPhone found, is it connected?\n"); | ||
| 184 | return NULL; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { | ||
| 189 | iphone_free_device(phone); | ||
| 190 | fprintf(stderr, "Something went wrong in the lockdownd client.\n"); | ||
| 191 | return NULL; | ||
| 192 | } | ||
| 193 | |||
| 194 | if (IPHONE_E_SUCCESS == iphone_lckd_start_service(control, service_name, &port) && !port) { | ||
| 195 | iphone_lckd_free_client(control); | ||
| 196 | iphone_free_device(phone); | ||
| 197 | fprintf(stderr, "Something went wrong when starting AFC."); | ||
| 198 | return NULL; | ||
| 199 | } | ||
| 200 | |||
| 201 | iphone_afc_new_client(phone, 3432, port, &afc); | ||
| 202 | |||
| 203 | return afc; | ||
| 204 | } | ||
| 205 | |||
| 206 | void ifuse_cleanup(void *data) | ||
| 207 | { | ||
| 208 | iphone_afc_client_t afc = (iphone_afc_client_t) data; | ||
| 209 | |||
| 210 | iphone_afc_free_client(afc); | ||
| 211 | iphone_lckd_free_client(control); | ||
| 212 | iphone_free_device(phone); | ||
| 213 | } | ||
| 214 | |||
| 215 | int ifuse_flush(const char *path, struct fuse_file_info *fi) | ||
| 216 | { | ||
| 217 | return 0; | ||
| 218 | } | ||
| 219 | |||
| 220 | int ifuse_statfs(const char *path, struct statvfs *stats) | ||
| 221 | { | ||
| 222 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 223 | char **info_raw = NULL; | ||
| 224 | uint32_t totalspace = 0, freespace = 0, blocksize = 0, i = 0; | ||
| 225 | |||
| 226 | iphone_afc_get_devinfo(afc, &info_raw); | ||
| 227 | if (!info_raw) | ||
| 228 | return -ENOENT; | ||
| 229 | |||
| 230 | for (i = 0; info_raw[i]; i++) { | ||
| 231 | if (!strcmp(info_raw[i], "FSTotalBytes")) { | ||
| 232 | totalspace = atoi(info_raw[i + 1]); | ||
| 233 | } else if (!strcmp(info_raw[i], "FSFreeBytes")) { | ||
| 234 | freespace = atoi(info_raw[i + 1]); | ||
| 235 | } else if (!strcmp(info_raw[i], "FSBlockSize")) { | ||
| 236 | blocksize = atoi(info_raw[i + 1]); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | free_dictionary(info_raw); | ||
| 240 | |||
| 241 | // Now to fill the struct. | ||
| 242 | stats->f_bsize = stats->f_frsize = blocksize; | ||
| 243 | stats->f_blocks = totalspace / blocksize; // gets the blocks by dividing bytes by blocksize | ||
| 244 | stats->f_bfree = stats->f_bavail = freespace / blocksize; // all bytes are free to everyone, I guess. | ||
| 245 | stats->f_namemax = 255; // blah | ||
| 246 | stats->f_files = stats->f_ffree = 1000000000; // make up any old thing, I guess | ||
| 247 | return 0; | ||
| 248 | } | ||
| 249 | |||
| 250 | int ifuse_truncate(const char *path, off_t size) | ||
| 251 | { | ||
| 252 | int result = 0; | ||
| 253 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 254 | iphone_afc_file_t tfile = NULL; | ||
| 255 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_READ, &tfile); | ||
| 256 | if (!tfile) | ||
| 257 | return -1; | ||
| 258 | |||
| 259 | result = iphone_afc_truncate_file(afc, tfile, size); | ||
| 260 | iphone_afc_close_file(afc, tfile); | ||
| 261 | return result; | ||
| 262 | } | ||
| 263 | |||
| 264 | int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) | ||
| 265 | { | ||
| 266 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 267 | iphone_afc_file_t file = g_hash_table_lookup(file_handles, &fi->fh); | ||
| 268 | if (!file) | ||
| 269 | return -ENOENT; | ||
| 270 | |||
| 271 | return iphone_afc_truncate_file(afc, file, size); | ||
| 272 | } | ||
| 273 | |||
| 274 | int ifuse_unlink(const char *path) | ||
| 275 | { | ||
| 276 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 277 | if (IPHONE_E_SUCCESS == iphone_afc_delete_file(afc, path)) | ||
| 278 | return 0; | ||
| 279 | else | ||
| 280 | return -1; | ||
| 281 | } | ||
| 282 | |||
| 283 | int ifuse_rename(const char *from, const char *to) | ||
| 284 | { | ||
| 285 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 286 | if (IPHONE_E_SUCCESS == iphone_afc_rename_file(afc, from, to)) | ||
| 287 | return 0; | ||
| 288 | else | ||
| 289 | return -1; | ||
| 290 | } | ||
| 291 | |||
| 292 | int ifuse_mkdir(const char *dir, mode_t ignored) | ||
| 293 | { | ||
| 294 | iphone_afc_client_t afc = fuse_get_context()->private_data; | ||
| 295 | if (IPHONE_E_SUCCESS == iphone_afc_mkdir(afc, dir)) | ||
| 296 | return 0; | ||
| 297 | else | ||
| 298 | return -1; | ||
| 299 | } | ||
| 300 | |||
| 301 | void *ifuse_init_normal(struct fuse_conn_info *conn) | ||
| 302 | { | ||
| 303 | return ifuse_init_with_service(conn, "com.apple.afc"); | ||
| 304 | } | ||
| 305 | |||
| 306 | void *ifuse_init_jailbroken(struct fuse_conn_info *conn) | ||
| 307 | { | ||
| 308 | return ifuse_init_with_service(conn, "com.apple.afc2"); | ||
| 309 | } | ||
| 310 | |||
| 311 | static struct fuse_operations ifuse_oper = { | ||
| 312 | .getattr = ifuse_getattr, | ||
| 313 | .statfs = ifuse_statfs, | ||
| 314 | .readdir = ifuse_readdir, | ||
| 315 | .mkdir = ifuse_mkdir, | ||
| 316 | .rmdir = ifuse_unlink, // AFC uses the same op for both. | ||
| 317 | .create = ifuse_create, | ||
| 318 | .open = ifuse_open, | ||
| 319 | .read = ifuse_read, | ||
| 320 | .write = ifuse_write, | ||
| 321 | .truncate = ifuse_truncate, | ||
| 322 | .ftruncate = ifuse_ftruncate, | ||
| 323 | .unlink = ifuse_unlink, | ||
| 324 | .rename = ifuse_rename, | ||
| 325 | .fsync = ifuse_fsync, | ||
| 326 | .release = ifuse_release, | ||
| 327 | .init = ifuse_init_normal, | ||
| 328 | .destroy = ifuse_cleanup | ||
| 329 | }; | ||
| 330 | |||
| 331 | int main(int argc, char *argv[]) | ||
| 332 | { | ||
| 333 | char **ammended_argv; | ||
| 334 | int i, j; | ||
| 335 | |||
| 336 | // Parse extra options | ||
| 337 | if (argc > 2 && (ammended_argv = malloc((argc + 1) * sizeof(*ammended_argv)))) { | ||
| 338 | for (i = j = 0; ammended_argv[j] = argv[i], i < argc; i++) { | ||
| 339 | // Try to use the (jailbroken) com.apple.afc2 if requested by the user | ||
| 340 | if (argv[i] && (!strcmp("--root", argv[i]) || !strcmp("--afc2", argv[i]))) { | ||
| 341 | ifuse_oper.init = ifuse_init_jailbroken; | ||
| 342 | continue; | ||
| 343 | } | ||
| 344 | j++; | ||
| 345 | } | ||
| 346 | argv = ammended_argv; | ||
| 347 | argc = j; | ||
| 348 | } | ||
| 349 | |||
| 350 | return fuse_main(argc, argv, &ifuse_oper, NULL); | ||
| 351 | } | ||
