diff options
| author | 2008-09-01 15:04:31 -0700 | |
|---|---|---|
| committer | 2008-09-01 15:04:31 -0700 | |
| commit | 2b05e48cb4a90dfc94ff584124f08e431398bb1a (patch) | |
| tree | e0c8255e2cd5592a31295ac8ce89d8846feb7043 /src/ifuse.c | |
| parent | 7ac3d681889a6a8f9987837ace5465f2967cfff9 (diff) | |
| download | libimobiledevice-2b05e48cb4a90dfc94ff584124f08e431398bb1a.tar.gz libimobiledevice-2b05e48cb4a90dfc94ff584124f08e431398bb1a.tar.bz2 | |
Enforce a modified kr style.
Use "make indent" from now on before committing.
Diffstat (limited to 'src/ifuse.c')
| -rw-r--r-- | src/ifuse.c | 187 |
1 files changed, 107 insertions, 80 deletions
diff --git a/src/ifuse.c b/src/ifuse.c index c266879..ad34eb5 100644 --- a/src/ifuse.c +++ b/src/ifuse.c | |||
| @@ -42,7 +42,8 @@ iphone_lckd_client_t control = NULL; | |||
| 42 | 42 | ||
| 43 | int debug = 0; | 43 | int debug = 0; |
| 44 | 44 | ||
| 45 | static int ifuse_getattr(const char *path, struct stat *stbuf) { | 45 | static int ifuse_getattr(const char *path, struct stat *stbuf) |
| 46 | { | ||
| 46 | int res = 0; | 47 | int res = 0; |
| 47 | 48 | ||
| 48 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 49 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| @@ -54,31 +55,32 @@ static int ifuse_getattr(const char *path, struct stat *stbuf) { | |||
| 54 | return res; | 55 | return res; |
| 55 | } | 56 | } |
| 56 | 57 | ||
| 57 | static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, | 58 | static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) |
| 58 | off_t offset, struct fuse_file_info *fi) { | 59 | { |
| 59 | int i; | 60 | int i; |
| 60 | char **dirs = NULL; | 61 | char **dirs = NULL; |
| 61 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 62 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 62 | 63 | ||
| 63 | iphone_afc_get_dir_list(afc, path, &dirs); | 64 | iphone_afc_get_dir_list(afc, path, &dirs); |
| 64 | 65 | ||
| 65 | if(!dirs) | 66 | if (!dirs) |
| 66 | return -ENOENT; | 67 | return -ENOENT; |
| 67 | 68 | ||
| 68 | for (i = 0; dirs[i]; i++) { | 69 | for (i = 0; dirs[i]; i++) { |
| 69 | filler(buf, dirs[i], NULL, 0); | 70 | filler(buf, dirs[i], NULL, 0); |
| 70 | } | 71 | } |
| 71 | 72 | ||
| 72 | free_dictionary(dirs); | 73 | free_dictionary(dirs); |
| 73 | 74 | ||
| 74 | return 0; | 75 | return 0; |
| 75 | } | 76 | } |
| 76 | 77 | ||
| 77 | static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) { | 78 | static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) |
| 79 | { | ||
| 78 | // exactly the same as open but using a different mode | 80 | // exactly the same as open but using a different mode |
| 79 | iphone_afc_file_t file = NULL; | 81 | iphone_afc_file_t file = NULL; |
| 80 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 82 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 81 | 83 | ||
| 82 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_WRITE, &file); | 84 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_WRITE, &file); |
| 83 | fh_index++; | 85 | fh_index++; |
| 84 | fi->fh = fh_index; | 86 | fi->fh = fh_index; |
| @@ -86,11 +88,12 @@ static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi | |||
| 86 | return 0; | 88 | return 0; |
| 87 | } | 89 | } |
| 88 | 90 | ||
| 89 | static int ifuse_open(const char *path, struct fuse_file_info *fi) { | 91 | static int ifuse_open(const char *path, struct fuse_file_info *fi) |
| 92 | { | ||
| 90 | iphone_afc_file_t file = NULL; | 93 | iphone_afc_file_t file = NULL; |
| 91 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 94 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 92 | uint32_t mode = 0; | 95 | uint32_t mode = 0; |
| 93 | 96 | ||
| 94 | if ((fi->flags & 3) == O_RDWR || (fi->flags & 3) == O_WRONLY) { | 97 | if ((fi->flags & 3) == O_RDWR || (fi->flags & 3) == O_WRONLY) { |
| 95 | mode = IPHONE_AFC_FILE_READ; | 98 | mode = IPHONE_AFC_FILE_READ; |
| 96 | } else if ((fi->flags & 3) == O_RDONLY) { | 99 | } else if ((fi->flags & 3) == O_RDONLY) { |
| @@ -98,9 +101,9 @@ static int ifuse_open(const char *path, struct fuse_file_info *fi) { | |||
| 98 | } else { | 101 | } else { |
| 99 | mode = IPHONE_AFC_FILE_READ; | 102 | mode = IPHONE_AFC_FILE_READ; |
| 100 | } | 103 | } |
| 101 | 104 | ||
| 102 | iphone_afc_open_file(afc, path, mode, &file); | 105 | iphone_afc_open_file(afc, path, mode, &file); |
| 103 | 106 | ||
| 104 | fh_index++; | 107 | fh_index++; |
| 105 | fi->fh = fh_index; | 108 | fi->fh = fh_index; |
| 106 | g_hash_table_insert(file_handles, &fh_index, file); | 109 | g_hash_table_insert(file_handles, &fh_index, file); |
| @@ -108,8 +111,8 @@ static int ifuse_open(const char *path, struct fuse_file_info *fi) { | |||
| 108 | return 0; | 111 | return 0; |
| 109 | } | 112 | } |
| 110 | 113 | ||
| 111 | static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, | 114 | static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) |
| 112 | struct fuse_file_info *fi) { | 115 | { |
| 113 | int bytes = 0; | 116 | int bytes = 0; |
| 114 | iphone_afc_file_t file; | 117 | iphone_afc_file_t file; |
| 115 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 118 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| @@ -118,7 +121,7 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, | |||
| 118 | return 0; | 121 | return 0; |
| 119 | 122 | ||
| 120 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | 123 | file = g_hash_table_lookup(file_handles, &(fi->fh)); |
| 121 | if (!file){ | 124 | if (!file) { |
| 122 | return -ENOENT; | 125 | return -ENOENT; |
| 123 | } | 126 | } |
| 124 | 127 | ||
| @@ -127,171 +130,195 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, | |||
| 127 | return bytes; | 130 | return bytes; |
| 128 | } | 131 | } |
| 129 | 132 | ||
| 130 | static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { | 133 | static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) |
| 134 | { | ||
| 131 | int bytes = 0; | 135 | int bytes = 0; |
| 132 | iphone_afc_file_t file = NULL; | 136 | iphone_afc_file_t file = NULL; |
| 133 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 137 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 134 | 138 | ||
| 135 | if (size == 0) return 0; | 139 | if (size == 0) |
| 136 | 140 | return 0; | |
| 141 | |||
| 137 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | 142 | file = g_hash_table_lookup(file_handles, &(fi->fh)); |
| 138 | if (!file) return -ENOENT; | 143 | if (!file) |
| 139 | 144 | return -ENOENT; | |
| 145 | |||
| 140 | if (IPHONE_E_SUCCESS == iphone_afc_seek_file(afc, file, offset)) | 146 | if (IPHONE_E_SUCCESS == iphone_afc_seek_file(afc, file, offset)) |
| 141 | iphone_afc_write_file(afc, file, buf, size, &bytes); | 147 | iphone_afc_write_file(afc, file, buf, size, &bytes); |
| 142 | return bytes; | 148 | return bytes; |
| 143 | } | 149 | } |
| 144 | 150 | ||
| 145 | static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) { | 151 | static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) |
| 152 | { | ||
| 146 | return 0; | 153 | return 0; |
| 147 | } | 154 | } |
| 148 | 155 | ||
| 149 | static int ifuse_release(const char *path, struct fuse_file_info *fi){ | 156 | static int ifuse_release(const char *path, struct fuse_file_info *fi) |
| 157 | { | ||
| 150 | iphone_afc_file_t file = NULL; | 158 | iphone_afc_file_t file = NULL; |
| 151 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 159 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 152 | 160 | ||
| 153 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | 161 | file = g_hash_table_lookup(file_handles, &(fi->fh)); |
| 154 | if (!file){ | 162 | if (!file) { |
| 155 | return -ENOENT; | 163 | return -ENOENT; |
| 156 | } | 164 | } |
| 157 | iphone_afc_close_file(afc, file); | 165 | iphone_afc_close_file(afc, file); |
| 158 | 166 | ||
| 159 | g_hash_table_remove(file_handles, &(fi->fh)); | 167 | g_hash_table_remove(file_handles, &(fi->fh)); |
| 160 | 168 | ||
| 161 | return 0; | 169 | return 0; |
| 162 | } | 170 | } |
| 163 | 171 | ||
| 164 | void *ifuse_init(struct fuse_conn_info *conn) { | 172 | void *ifuse_init(struct fuse_conn_info *conn) |
| 173 | { | ||
| 165 | int port = 0; | 174 | int port = 0; |
| 166 | iphone_afc_client_t afc = NULL; | 175 | iphone_afc_client_t afc = NULL; |
| 167 | 176 | ||
| 168 | conn->async_read = 0; | 177 | conn->async_read = 0; |
| 169 | 178 | ||
| 170 | file_handles = g_hash_table_new(g_int_hash, g_int_equal); | 179 | file_handles = g_hash_table_new(g_int_hash, g_int_equal); |
| 171 | 180 | ||
| 172 | iphone_get_device(&phone); | 181 | iphone_get_device(&phone); |
| 173 | if (!phone){ | 182 | if (!phone) { |
| 174 | fprintf(stderr, "No iPhone found, is it connected?\n"); | 183 | fprintf(stderr, "No iPhone found, is it connected?\n"); |
| 175 | return NULL; | 184 | return NULL; |
| 176 | } | 185 | } |
| 177 | 186 | ||
| 178 | 187 | ||
| 179 | if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { | 188 | if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) { |
| 180 | iphone_free_device(phone); | 189 | iphone_free_device(phone); |
| 181 | fprintf(stderr, "Something went wrong in the lockdownd client.\n"); | 190 | fprintf(stderr, "Something went wrong in the lockdownd client.\n"); |
| 182 | return NULL; | 191 | return NULL; |
| 183 | } | 192 | } |
| 184 | 193 | ||
| 185 | if (IPHONE_E_SUCCESS == iphone_lckd_start_service(control, "com.apple.afc", &port) && !port) { | 194 | if (IPHONE_E_SUCCESS == iphone_lckd_start_service(control, "com.apple.afc", &port) && !port) { |
| 186 | iphone_lckd_free_client(control); | 195 | iphone_lckd_free_client(control); |
| 187 | iphone_free_device(phone); | 196 | iphone_free_device(phone); |
| 188 | fprintf(stderr, "Something went wrong when starting AFC."); | 197 | fprintf(stderr, "Something went wrong when starting AFC."); |
| 189 | return NULL; | 198 | return NULL; |
| 190 | } | 199 | } |
| 191 | 200 | ||
| 192 | iphone_afc_new_client(phone, 3432, port, &afc); | 201 | iphone_afc_new_client(phone, 3432, port, &afc); |
| 193 | 202 | ||
| 194 | return afc; | 203 | return afc; |
| 195 | } | 204 | } |
| 196 | 205 | ||
| 197 | void ifuse_cleanup(void *data) { | 206 | void ifuse_cleanup(void *data) |
| 198 | iphone_afc_client_t afc = (iphone_afc_client_t )data; | 207 | { |
| 208 | iphone_afc_client_t afc = (iphone_afc_client_t) data; | ||
| 199 | 209 | ||
| 200 | iphone_afc_free_client(afc); | 210 | iphone_afc_free_client(afc); |
| 201 | iphone_lckd_free_client(control); | 211 | iphone_lckd_free_client(control); |
| 202 | iphone_free_device(phone); | 212 | iphone_free_device(phone); |
| 203 | } | 213 | } |
| 204 | 214 | ||
| 205 | int ifuse_flush(const char *path, struct fuse_file_info *fi) { | 215 | int ifuse_flush(const char *path, struct fuse_file_info *fi) |
| 216 | { | ||
| 206 | return 0; | 217 | return 0; |
| 207 | } | 218 | } |
| 208 | 219 | ||
| 209 | int ifuse_statfs(const char *path, struct statvfs *stats) { | 220 | int ifuse_statfs(const char *path, struct statvfs *stats) |
| 221 | { | ||
| 210 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 222 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 211 | char **info_raw = NULL; | 223 | char **info_raw = NULL; |
| 212 | uint32_t totalspace = 0, freespace = 0, blocksize = 0, i = 0; | 224 | uint32_t totalspace = 0, freespace = 0, blocksize = 0, i = 0; |
| 213 | 225 | ||
| 214 | iphone_afc_get_devinfo(afc, &info_raw); | 226 | iphone_afc_get_devinfo(afc, &info_raw); |
| 215 | if (!info_raw) return -ENOENT; | 227 | if (!info_raw) |
| 216 | 228 | return -ENOENT; | |
| 229 | |||
| 217 | for (i = 0; info_raw[i]; i++) { | 230 | for (i = 0; info_raw[i]; i++) { |
| 218 | if (!strcmp(info_raw[i], "FSTotalBytes")) { | 231 | if (!strcmp(info_raw[i], "FSTotalBytes")) { |
| 219 | totalspace = atoi(info_raw[i+1]); | 232 | totalspace = atoi(info_raw[i + 1]); |
| 220 | } else if (!strcmp(info_raw[i], "FSFreeBytes")) { | 233 | } else if (!strcmp(info_raw[i], "FSFreeBytes")) { |
| 221 | freespace = atoi(info_raw[i+1]); | 234 | freespace = atoi(info_raw[i + 1]); |
| 222 | } else if (!strcmp(info_raw[i], "FSBlockSize")) { | 235 | } else if (!strcmp(info_raw[i], "FSBlockSize")) { |
| 223 | blocksize = atoi(info_raw[i+1]); | 236 | blocksize = atoi(info_raw[i + 1]); |
| 224 | } | 237 | } |
| 225 | } | 238 | } |
| 226 | free_dictionary(info_raw); | 239 | free_dictionary(info_raw); |
| 227 | 240 | ||
| 228 | // Now to fill the struct. | 241 | // Now to fill the struct. |
| 229 | stats->f_bsize = stats->f_frsize = blocksize; | 242 | stats->f_bsize = stats->f_frsize = blocksize; |
| 230 | stats->f_blocks = totalspace / blocksize; // gets the blocks by dividing bytes by blocksize | 243 | stats->f_blocks = totalspace / blocksize; // gets the blocks by dividing bytes by blocksize |
| 231 | stats->f_bfree = stats->f_bavail = freespace / blocksize; // all bytes are free to everyone, I guess. | 244 | stats->f_bfree = stats->f_bavail = freespace / blocksize; // all bytes are free to everyone, I guess. |
| 232 | stats->f_namemax = 255; // blah | 245 | stats->f_namemax = 255; // blah |
| 233 | stats->f_files = stats->f_ffree = 1000000000; // make up any old thing, I guess | 246 | stats->f_files = stats->f_ffree = 1000000000; // make up any old thing, I guess |
| 234 | return 0; | 247 | return 0; |
| 235 | } | 248 | } |
| 236 | 249 | ||
| 237 | int ifuse_truncate(const char *path, off_t size) { | 250 | int ifuse_truncate(const char *path, off_t size) |
| 251 | { | ||
| 238 | int result = 0; | 252 | int result = 0; |
| 239 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 253 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 240 | iphone_afc_file_t tfile = NULL; | 254 | iphone_afc_file_t tfile = NULL; |
| 241 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_READ, &tfile); | 255 | iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_READ, &tfile); |
| 242 | if (!tfile) return -1; | 256 | if (!tfile) |
| 243 | 257 | return -1; | |
| 258 | |||
| 244 | result = iphone_afc_truncate_file(afc, tfile, size); | 259 | result = iphone_afc_truncate_file(afc, tfile, size); |
| 245 | iphone_afc_close_file(afc, tfile); | 260 | iphone_afc_close_file(afc, tfile); |
| 246 | return result; | 261 | return result; |
| 247 | } | 262 | } |
| 248 | 263 | ||
| 249 | int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) { | 264 | int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) |
| 265 | { | ||
| 250 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 266 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 251 | iphone_afc_file_t file = g_hash_table_lookup(file_handles, &fi->fh); | 267 | iphone_afc_file_t file = g_hash_table_lookup(file_handles, &fi->fh); |
| 252 | if (!file) return -ENOENT; | 268 | if (!file) |
| 253 | 269 | return -ENOENT; | |
| 270 | |||
| 254 | return iphone_afc_truncate_file(afc, file, size); | 271 | return iphone_afc_truncate_file(afc, file, size); |
| 255 | } | 272 | } |
| 256 | 273 | ||
| 257 | int ifuse_unlink(const char *path) { | 274 | int ifuse_unlink(const char *path) |
| 275 | { | ||
| 258 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 276 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 259 | if (IPHONE_E_SUCCESS == iphone_afc_delete_file(afc, path)) return 0; | 277 | if (IPHONE_E_SUCCESS == iphone_afc_delete_file(afc, path)) |
| 260 | else return -1; | 278 | return 0; |
| 279 | else | ||
| 280 | return -1; | ||
| 261 | } | 281 | } |
| 262 | 282 | ||
| 263 | int ifuse_rename(const char *from, const char *to) { | 283 | int ifuse_rename(const char *from, const char *to) |
| 284 | { | ||
| 264 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 285 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 265 | if (IPHONE_E_SUCCESS == iphone_afc_rename_file(afc, from, to)) return 0; | 286 | if (IPHONE_E_SUCCESS == iphone_afc_rename_file(afc, from, to)) |
| 266 | else return -1; | 287 | return 0; |
| 288 | else | ||
| 289 | return -1; | ||
| 267 | } | 290 | } |
| 268 | 291 | ||
| 269 | int ifuse_mkdir(const char *dir, mode_t ignored) { | 292 | int ifuse_mkdir(const char *dir, mode_t ignored) |
| 293 | { | ||
| 270 | iphone_afc_client_t afc = fuse_get_context()->private_data; | 294 | iphone_afc_client_t afc = fuse_get_context()->private_data; |
| 271 | if (IPHONE_E_SUCCESS == iphone_afc_mkdir(afc, dir)) return 0; | 295 | if (IPHONE_E_SUCCESS == iphone_afc_mkdir(afc, dir)) |
| 272 | else return -1; | 296 | return 0; |
| 297 | else | ||
| 298 | return -1; | ||
| 273 | } | 299 | } |
| 274 | 300 | ||
| 275 | static struct fuse_operations ifuse_oper = { | 301 | static struct fuse_operations ifuse_oper = { |
| 276 | .getattr = ifuse_getattr, | 302 | .getattr = ifuse_getattr, |
| 277 | .statfs = ifuse_statfs, | 303 | .statfs = ifuse_statfs, |
| 278 | .readdir = ifuse_readdir, | 304 | .readdir = ifuse_readdir, |
| 279 | .mkdir = ifuse_mkdir, | 305 | .mkdir = ifuse_mkdir, |
| 280 | .rmdir = ifuse_unlink, // AFC uses the same op for both. | 306 | .rmdir = ifuse_unlink, // AFC uses the same op for both. |
| 281 | .create = ifuse_create, | 307 | .create = ifuse_create, |
| 282 | .open = ifuse_open, | 308 | .open = ifuse_open, |
| 283 | .read = ifuse_read, | 309 | .read = ifuse_read, |
| 284 | .write = ifuse_write, | 310 | .write = ifuse_write, |
| 285 | .truncate = ifuse_truncate, | 311 | .truncate = ifuse_truncate, |
| 286 | .ftruncate = ifuse_ftruncate, | 312 | .ftruncate = ifuse_ftruncate, |
| 287 | .unlink = ifuse_unlink, | 313 | .unlink = ifuse_unlink, |
| 288 | .rename = ifuse_rename, | 314 | .rename = ifuse_rename, |
| 289 | .fsync = ifuse_fsync, | 315 | .fsync = ifuse_fsync, |
| 290 | .release = ifuse_release, | 316 | .release = ifuse_release, |
| 291 | .init = ifuse_init, | 317 | .init = ifuse_init, |
| 292 | .destroy = ifuse_cleanup | 318 | .destroy = ifuse_cleanup |
| 293 | }; | 319 | }; |
| 294 | 320 | ||
| 295 | int main(int argc, char *argv[]) { | 321 | int main(int argc, char *argv[]) |
| 322 | { | ||
| 296 | return fuse_main(argc, argv, &ifuse_oper, NULL); | 323 | return fuse_main(argc, argv, &ifuse_oper, NULL); |
| 297 | } | 324 | } |
