summaryrefslogtreecommitdiffstats
path: root/src/ifuse.c
blob: 7672bb9e62f9f833a33ffb2cbcf8ec543b3b83b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* 
 * ifuse.c
 * A Fuse filesystem which exposes the iPhone's filesystem.
 *
 * Copyright (c) 2008 Matt Colyer All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 */

#define FUSE_USE_VERSION  26

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <usb.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <glib.h>
#include <unistd.h>

#include "usbmux.h"
#include "iphone.h"
#include "plist.h"
#include "lockdown.h"
#include "AFC.h"
#include "userpref.h"

GHashTable *file_handles;
int fh_index = 0;

iPhone *phone = NULL;
lockdownd_client *control = NULL;

int debug = 0;

static int ifuse_getattr(const char *path, struct stat *stbuf) {
	int res = 0;
	AFCFile *file;
	AFClient *afc = fuse_get_context()->private_data;

	memset(stbuf, 0, sizeof(struct stat));
	file = afc_get_file_info(afc, path);
	if (!file){
		res = -ENOENT;
	} else {
	  	stbuf->st_mode = file->type | (S_ISDIR(file->type) ? 0755 : 0644);
		stbuf->st_size = file->size;
		stbuf->st_blksize = 2048; // FIXME: Is this the actual block size used on the iPhone?
		stbuf->st_blocks = file->blocks;
		stbuf->st_uid = getuid();
		stbuf->st_gid = getgid();

		afc_close_file(afc,file);
		free(file);
	}

	return res;
}

static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
			 off_t offset, struct fuse_file_info *fi) {
	int i;
	char **dirs;
	AFClient *afc = fuse_get_context()->private_data;

	dirs = afc_get_dir_list(afc, path);

	if(!dirs)
		return -ENOENT;

	for (i = 0; dirs[i]; i++) {
		filler(buf, dirs[i], NULL, 0);
	}
	
	free_dictionary(dirs);

	return 0;
}

static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) {
	// exactly the same as open but using a different mode
	AFCFile *file;
	AFClient *afc = fuse_get_context()->private_data;
	
	file = afc_open_file(afc, path, AFC_FILE_WRITE);
	fh_index++;
	fi->fh = fh_index;
	g_hash_table_insert(file_handles, &fh_index, file);
	return 0;
}

static int ifuse_open(const char *path, struct fuse_file_info *fi) {
	AFCFile *file;
	AFClient *afc = fuse_get_context()->private_data;
	uint32 mode = 0;
	
	if ((fi->flags & 3) == O_RDWR || (fi->flags & 3) == O_WRONLY) {
		mode = AFC_FILE_READ;
	} else if ((fi->flags & 3) == O_RDONLY) {
		mode = AFC_FILE_READ;
	} else {
		mode = AFC_FILE_READ;
	}
	
	file = afc_open_file(afc, path, mode);
	
	fh_index++;
	fi->fh = fh_index;
	g_hash_table_insert(file_handles, &fh_index, file);

	return 0;
}

static int ifuse_read(const char *path, char *buf, size_t size, off_t offset,
			struct fuse_file_info *fi) {
	int bytes;
	AFCFile *file;
	AFClient *afc = fuse_get_context()->private_data;

	if (size == 0)
		return 0;

	file = g_hash_table_lookup(file_handles, &(fi->fh));
	if (!file){
		return -ENOENT;
	}

	bytes = afc_seek_file(afc, file, offset);
	bytes = afc_read_file(afc, file, buf, size);
	return bytes;
}

static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	int bytes = 0;
	AFCFile *file = NULL;
	AFClient *afc = fuse_get_context()->private_data;
	
	if (size == 0) return 0;
	
	file = g_hash_table_lookup(file_handles, &(fi->fh));
	if (!file) return -ENOENT;
	
	bytes = afc_seek_file(afc, file, offset);
	bytes = afc_write_file(afc, file, buf, size);
	return bytes;
}

static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) {
	return 0;
}

static int ifuse_release(const char *path, struct fuse_file_info *fi){
	AFCFile *file;
	AFClient *afc = fuse_get_context()->private_data;
	
	file = g_hash_table_lookup(file_handles, &(fi->fh));
	if (!file){
		return -ENOENT;
	}
	afc_close_file(afc, file);
	
	free(file);
	g_hash_table_remove(file_handles, &(fi->fh));

	return 0;
}

void *ifuse_init(struct fuse_conn_info *conn) {
	int port = 0;
	AFClient *afc = NULL;
	
	conn->async_read = 0;

	file_handles = g_hash_table_new(g_int_hash, g_int_equal);

	phone = get_iPhone();
	if (!phone){
		fprintf(stderr, "No iPhone found, is it connected?\n");
		   	return NULL;
	   	}
	
	lockdownd_client *control = new_lockdownd_client(phone);
	if (!lockdownd_hello(control)) {
		fprintf(stderr, "Something went wrong in the lockdownd client.\n");
		return NULL;
	}

	if (!lockdownd_init(phone, &control)) {
		free_iPhone(phone);
		fprintf(stderr, "Something went wrong in the lockdownd client.\n");
		return NULL;
	}
	
	port = lockdownd_start_service(control, "com.apple.afc");
	if (!port) {
		lockdownd_close(control);
		free_iPhone(phone);
		fprintf(stderr, "Something went wrong when starting AFC.");
                return NULL;
	}

	afc = afc_connect(phone, 3432, port);

        return afc;
}

void ifuse_cleanup(void *data) {
	AFClient *afc = (AFClient *)data;

	afc_disconnect(afc);
	lockdownd_close(control);
	free_iPhone(phone);
}

int ifuse_flush(const char *path, struct fuse_file_info *fi) {
	return 0;
}

int ifuse_statfs(const char *path, struct statvfs *stats) {
	AFClient *afc = fuse_get_context()->private_data;
	char **info_raw = afc_get_devinfo(afc);
	uint32 totalspace = 0, freespace = 0, blocksize = 0, i = 0;
	
	if (!info_raw) return -ENOENT;
	
	for (i = 0; info_raw[i]; i++) {
		if (!strcmp(info_raw[i], "FSTotalBytes")) {
			totalspace = atoi(info_raw[i+1]);
		} else if (!strcmp(info_raw[i], "FSFreeBytes")) {
			freespace = atoi(info_raw[i+1]);
		} else if (!strcmp(info_raw[i], "FSBlockSize")) {
			blocksize = atoi(info_raw[i+1]);
		}
	}
	free_dictionary(info_raw);
	
	// Now to fill the struct.
	stats->f_bsize = stats->f_frsize = blocksize;
	stats->f_blocks = totalspace / blocksize; // gets the blocks by dividing bytes by blocksize
	stats->f_bfree = stats->f_bavail = freespace / blocksize; // all bytes are free to everyone, I guess.
	stats->f_namemax = 255; // blah
	stats->f_files = stats->f_ffree = 1000000000; // make up any old thing, I guess
	return 0;
}

int ifuse_truncate(const char *path, off_t size) {
	int result = 0;
	AFClient *afc = fuse_get_context()->private_data;
	AFCFile *tfile = afc_open_file(afc, path, AFC_FILE_READ);
	if (!tfile) return -1;
	
	result = afc_truncate_file(afc, tfile, size);
	afc_close_file(afc, tfile);
	return result;
}

int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) {
	AFClient *afc = fuse_get_context()->private_data;
	AFCFile *file = g_hash_table_lookup(file_handles, &fi->fh);
	if (!file) return -ENOENT;
	
	return afc_truncate_file(afc, file, size);
}

int ifuse_unlink(const char *path) {
	AFClient *afc = fuse_get_context()->private_data;
	if (afc_delete_file(afc, path)) return 0;
	else return -1;
}

int ifuse_rename(const char *from, const char *to) {
	AFClient *afc = fuse_get_context()->private_data;
	if (afc_rename_file(afc, from, to)) return 0;
	else return -1;
}

int ifuse_mkdir(const char *dir, mode_t ignored) {
	AFClient *afc = fuse_get_context()->private_data;
	if (afc_mkdir(afc, dir)) return 0;
	else return -1;
}

static struct fuse_operations ifuse_oper = {
	.getattr	= ifuse_getattr,
	.statfs		= ifuse_statfs,
	.readdir	= ifuse_readdir,
	.mkdir		= ifuse_mkdir,
	.rmdir		= ifuse_unlink, // AFC uses the same op for both.
	.create		= ifuse_create,
	.open		= ifuse_open,
	.read		= ifuse_read,
	.write		= ifuse_write,
	.truncate 	= ifuse_truncate,
	.ftruncate	= ifuse_ftruncate,
	.unlink		= ifuse_unlink,
	.rename		= ifuse_rename,
	.fsync 		= ifuse_fsync,
	.release	= ifuse_release,
	.init		= ifuse_init,
	.destroy	= ifuse_cleanup
};

int main(int argc, char *argv[]) {
	return fuse_main(argc, argv, &ifuse_oper, NULL);
}