diff options
Diffstat (limited to 'src/ifuse.c')
| -rw-r--r-- | src/ifuse.c | 131 |
1 files changed, 125 insertions, 6 deletions
diff --git a/src/ifuse.c b/src/ifuse.c index 3d921fd..9fc1ad8 100644 --- a/src/ifuse.c +++ b/src/ifuse.c | |||
| @@ -41,7 +41,7 @@ | |||
| 41 | GHashTable *file_handles; | 41 | GHashTable *file_handles; |
| 42 | int fh_index = 0; | 42 | int fh_index = 0; |
| 43 | 43 | ||
| 44 | int debug = 1; | 44 | int debug = 0; |
| 45 | 45 | ||
| 46 | static int ifuse_getattr(const char *path, struct stat *stbuf) { | 46 | static int ifuse_getattr(const char *path, struct stat *stbuf) { |
| 47 | int res = 0; | 47 | int res = 0; |
| @@ -53,7 +53,8 @@ static int ifuse_getattr(const char *path, struct stat *stbuf) { | |||
| 53 | if (!file){ | 53 | if (!file){ |
| 54 | res = -ENOENT; | 54 | res = -ENOENT; |
| 55 | } else { | 55 | } else { |
| 56 | stbuf->st_mode = file->type | 0444; | 56 | //stbuf->st_mode = file->type | 0444; // testing write access too now |
| 57 | stbuf->st_mode = file->type | 0644; // but we don't want anything on the iPhone executable, like, ever | ||
| 57 | stbuf->st_size = file->size; | 58 | stbuf->st_size = file->size; |
| 58 | //stbuf->st_nlink = 2; | 59 | //stbuf->st_nlink = 2; |
| 59 | } | 60 | } |
| @@ -81,14 +82,34 @@ static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, | |||
| 81 | return 0; | 82 | return 0; |
| 82 | } | 83 | } |
| 83 | 84 | ||
| 84 | static int ifuse_open(const char *path, struct fuse_file_info *fi) { | 85 | static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) { |
| 86 | // exactly the same as open but using a different mode | ||
| 85 | AFCFile *file; | 87 | AFCFile *file; |
| 86 | AFClient *afc = fuse_get_context()->private_data; | 88 | AFClient *afc = fuse_get_context()->private_data; |
| 89 | |||
| 90 | file = afc_open_file(afc, path, AFC_FILE_WRITE); | ||
| 91 | fh_index++; | ||
| 92 | fi->fh = fh_index; | ||
| 93 | g_hash_table_insert(file_handles, &fh_index, file); | ||
| 94 | return 0; | ||
| 95 | } | ||
| 87 | 96 | ||
| 88 | if((fi->flags & 3) != O_RDONLY) | 97 | static int ifuse_open(const char *path, struct fuse_file_info *fi) { |
| 89 | return -EACCES; | 98 | AFCFile *file; |
| 99 | AFClient *afc = fuse_get_context()->private_data; | ||
| 100 | uint32 mode = 0; | ||
| 101 | /*if((fi->flags & 3) != O_RDONLY) | ||
| 102 | return -EACCES;*/ // trying to test write access here | ||
| 103 | |||
| 104 | if ((fi->flags & 3) == O_RDWR || (fi->flags & 3) == O_WRONLY) { | ||
| 105 | mode = AFC_FILE_READ; | ||
| 106 | } else if ((fi->flags & 3) == O_RDONLY) { | ||
| 107 | mode = AFC_FILE_READ; | ||
| 108 | } else { | ||
| 109 | mode = AFC_FILE_READ; | ||
| 110 | } | ||
| 90 | 111 | ||
| 91 | file = afc_open_file(afc, path, AFC_FILE_READ); | 112 | file = afc_open_file(afc, path, mode); |
| 92 | 113 | ||
| 93 | fh_index++; | 114 | fh_index++; |
| 94 | fi->fh = fh_index; | 115 | fi->fh = fh_index; |
| @@ -111,10 +132,30 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, | |||
| 111 | return -ENOENT; | 132 | return -ENOENT; |
| 112 | } | 133 | } |
| 113 | 134 | ||
| 135 | bytes = afc_seek_file(afc, file, offset); | ||
| 114 | bytes = afc_read_file(afc, file, buf, size); | 136 | bytes = afc_read_file(afc, file, buf, size); |
| 115 | return bytes; | 137 | return bytes; |
| 116 | } | 138 | } |
| 117 | 139 | ||
| 140 | static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { | ||
| 141 | int bytes = 0; | ||
| 142 | AFCFile *file = NULL; | ||
| 143 | AFClient *afc = fuse_get_context()->private_data; | ||
| 144 | |||
| 145 | if (size == 0) return 0; | ||
| 146 | |||
| 147 | file = g_hash_table_lookup(file_handles, &(fi->fh)); | ||
| 148 | if (!file) return -ENOENT; | ||
| 149 | |||
| 150 | bytes = afc_seek_file(afc, file, offset); | ||
| 151 | bytes = afc_write_file(afc, file, buf, size); | ||
| 152 | return bytes; | ||
| 153 | } | ||
| 154 | |||
| 155 | static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) { | ||
| 156 | return 0; | ||
| 157 | } | ||
| 158 | |||
| 118 | static int ifuse_release(const char *path, struct fuse_file_info *fi){ | 159 | static int ifuse_release(const char *path, struct fuse_file_info *fi){ |
| 119 | AFCFile *file; | 160 | AFCFile *file; |
| 120 | AFClient *afc = fuse_get_context()->private_data; | 161 | AFClient *afc = fuse_get_context()->private_data; |
| @@ -181,11 +222,89 @@ void ifuse_cleanup(void *data) { | |||
| 181 | } | 222 | } |
| 182 | } | 223 | } |
| 183 | 224 | ||
| 225 | int ifuse_flush(const char *path, struct fuse_file_info *fi) { | ||
| 226 | return 0; | ||
| 227 | } | ||
| 228 | |||
| 229 | int ifuse_statfs(const char *path, struct statvfs *stats) { | ||
| 230 | AFClient *afc = fuse_get_context()->private_data; | ||
| 231 | char **info_raw = afc_get_devinfo(afc); | ||
| 232 | uint32 totalspace = 0, freespace = 0, blocksize = 0, i = 0; | ||
| 233 | |||
| 234 | if (!info_raw) return -ENOENT; | ||
| 235 | |||
| 236 | for (i = 0; strcmp(info_raw[i], ""); i++) { | ||
| 237 | if (!strcmp(info_raw[i], "FSTotalBytes")) { | ||
| 238 | totalspace = atoi(info_raw[i+1]); | ||
| 239 | } else if (!strcmp(info_raw[i], "FSFreeBytes")) { | ||
| 240 | freespace = atoi(info_raw[i+1]); | ||
| 241 | } else if (!strcmp(info_raw[i], "FSBlockSize")) { | ||
| 242 | blocksize = atoi(info_raw[i+1]); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | // Now to fill the struct. | ||
| 247 | stats->f_bsize = stats->f_frsize = blocksize; | ||
| 248 | stats->f_blocks = totalspace / blocksize; // gets the blocks by dividing bytes by blocksize | ||
| 249 | stats->f_bfree = stats->f_bavail = freespace / blocksize; // all bytes are free to everyone, I guess. | ||
| 250 | stats->f_namemax = 255; // blah | ||
| 251 | stats->f_files = stats->f_ffree = 1000000000; // make up any old thing, I guess | ||
| 252 | return 0; | ||
| 253 | } | ||
| 254 | |||
| 255 | int ifuse_truncate(const char *path, off_t size) { | ||
| 256 | int result = 0; | ||
| 257 | AFClient *afc = fuse_get_context()->private_data; | ||
| 258 | AFCFile *tfile = afc_open_file(afc, path, AFC_FILE_READ); | ||
| 259 | if (!tfile) return -1; | ||
| 260 | |||
| 261 | result = afc_truncate_file(afc, tfile, size); | ||
| 262 | afc_close_file(afc, tfile); | ||
| 263 | return result; | ||
| 264 | } | ||
| 265 | |||
| 266 | int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) { | ||
| 267 | int result = 0; | ||
| 268 | AFClient *afc = fuse_get_context()->private_data; | ||
| 269 | AFCFile *file = g_hash_table_lookup(file_handles, &fi->fh); | ||
| 270 | if (!file) return -ENOENT; | ||
| 271 | |||
| 272 | return afc_truncate_file(afc, file, size); | ||
| 273 | } | ||
| 274 | |||
| 275 | int ifuse_unlink(const char *path) { | ||
| 276 | AFClient *afc = fuse_get_context()->private_data; | ||
| 277 | if (afc_delete_file(afc, path)) return 0; | ||
| 278 | else return -1; | ||
| 279 | } | ||
| 280 | |||
| 281 | int ifuse_rename(const char *from, const char *to) { | ||
| 282 | AFClient *afc = fuse_get_context()->private_data; | ||
| 283 | if (afc_rename_file(afc, from, to)) return 0; | ||
| 284 | else return -1; | ||
| 285 | } | ||
| 286 | |||
| 287 | int ifuse_mkdir(const char *dir, mode_t ignored) { | ||
| 288 | AFClient *afc = fuse_get_context()->private_data; | ||
| 289 | if (afc_mkdir(afc, dir)) return 0; | ||
| 290 | else return -1; | ||
| 291 | } | ||
| 292 | |||
| 184 | static struct fuse_operations ifuse_oper = { | 293 | static struct fuse_operations ifuse_oper = { |
| 185 | .getattr = ifuse_getattr, | 294 | .getattr = ifuse_getattr, |
| 295 | .statfs = ifuse_statfs, | ||
| 186 | .readdir = ifuse_readdir, | 296 | .readdir = ifuse_readdir, |
| 297 | .mkdir = ifuse_mkdir, | ||
| 298 | .rmdir = ifuse_unlink, // AFC uses the same op for both. | ||
| 299 | .create = ifuse_create, | ||
| 187 | .open = ifuse_open, | 300 | .open = ifuse_open, |
| 188 | .read = ifuse_read, | 301 | .read = ifuse_read, |
| 302 | .write = ifuse_write, | ||
| 303 | .truncate = ifuse_truncate, | ||
| 304 | .ftruncate = ifuse_ftruncate, | ||
| 305 | .unlink = ifuse_unlink, | ||
| 306 | .rename = ifuse_rename, | ||
| 307 | .fsync = ifuse_fsync, | ||
| 189 | .release = ifuse_release, | 308 | .release = ifuse_release, |
| 190 | .init = ifuse_init, | 309 | .init = ifuse_init, |
| 191 | .destroy = ifuse_cleanup | 310 | .destroy = ifuse_cleanup |
