2014-09-23 15:12:19 +02:00
|
|
|
/*
|
2014-09-24 18:33:04 +02:00
|
|
|
* Copyright (C) 2014 Johannes Schauer <j.schauer@email.de>
|
2014-09-23 15:12:19 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License version 2, as published by
|
|
|
|
|
* the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
|
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define FUSE_USE_VERSION 30
|
|
|
|
|
|
2014-09-23 20:53:29 +02:00
|
|
|
#include <fuse/fuse.h>
|
|
|
|
|
#include <fuse/fuse_opt.h>
|
|
|
|
|
#include <stddef.h>
|
2014-09-23 15:12:19 +02:00
|
|
|
#include <stdio.h>
|
2014-09-23 20:53:29 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
2014-09-28 09:37:24 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/types.h>
|
2014-09-23 20:53:29 +02:00
|
|
|
|
|
|
|
|
#include "../mfapi/mfconn.h"
|
2014-09-28 09:37:24 +02:00
|
|
|
#include "../mfapi/apicalls.h"
|
2014-09-25 13:05:17 +02:00
|
|
|
#include "hashtbl.h"
|
2014-09-23 15:12:19 +02:00
|
|
|
|
2014-09-29 10:53:34 +02:00
|
|
|
/* what you can safely assume about requests to your filesystem
|
|
|
|
|
*
|
|
|
|
|
* from: http://sourceforge.net/p/fuse/wiki/FuseInvariants/
|
|
|
|
|
*
|
|
|
|
|
* There are a number of assumptions that one can safely make when
|
|
|
|
|
* implementing a filesystem using fuse. This page should be completed with a
|
|
|
|
|
* set of such assumptions.
|
|
|
|
|
*
|
|
|
|
|
* - All requests are absolute, i.e. all paths begin with "/" and include the
|
|
|
|
|
* complete path to a file or a directory. Symlinks, "." and ".." are
|
|
|
|
|
* already resolved.
|
|
|
|
|
*
|
|
|
|
|
* - For every request you can get except for "Getattr()", "Read()" and
|
|
|
|
|
* "Write()", usually for every path argument (both source and destination
|
|
|
|
|
* for link and rename, but only the source for symlink), you will get a
|
|
|
|
|
* "Getattr()" request just before the callback.
|
|
|
|
|
*
|
|
|
|
|
* For example, suppose I store file names of files in a filesystem also into a
|
|
|
|
|
* database. To keep data in sync, I would like, for each filesystem operation
|
|
|
|
|
* that succeeds, to check if the file exists on the database. I just do this in
|
|
|
|
|
* the "Getattr()" call, since all other calls will be preceded by a getattr.
|
|
|
|
|
*
|
|
|
|
|
* - The value of the "st_dev" attribute in the "Getattr()" call are ignored
|
|
|
|
|
* by fuse and an appropriate anomynous device number is inserted instead.
|
|
|
|
|
*
|
|
|
|
|
* - The arguments for every request are already verified as much as
|
|
|
|
|
* possible. This means that, for example "readdir()" is only called with
|
|
|
|
|
* an existing directory name, "Readlink()" is only called with an existing
|
|
|
|
|
* symlink, "Symlink()" is only called if there isn't already another
|
|
|
|
|
* object with the requested linkname, "read()" and "Write()" are only
|
|
|
|
|
* called if the file has been opened with the correct flags.
|
|
|
|
|
*
|
|
|
|
|
* - The VFS also takes care of avoiding race conditions:
|
|
|
|
|
*
|
|
|
|
|
* - while "Unlink()" is running on a specific file, it cannot be interrupted
|
|
|
|
|
* by a "Chmod()", "Link()" or "Open()" call from a different thread on the
|
|
|
|
|
* same file.
|
|
|
|
|
*
|
|
|
|
|
* - while "Rmdir()" is running, no files can be created in the directory
|
|
|
|
|
* that "Rmdir()" is acting on.
|
|
|
|
|
*
|
|
|
|
|
* - If a request returns invalid values (e.g. in the structure returned by
|
|
|
|
|
* "Getattr()" or in the link target returned by "Symlink()") or if a
|
|
|
|
|
* request appears to have failed (e.g. if a "Create()" request succeds but
|
|
|
|
|
* a subsequent "Getattr()" (that fuse calls automatically) ndicates that
|
|
|
|
|
* no regular file has been created), the syscall returns EIO to the
|
|
|
|
|
* caller.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-09-24 11:28:35 +02:00
|
|
|
enum {
|
2014-09-23 15:12:19 +02:00
|
|
|
KEY_HELP,
|
|
|
|
|
KEY_VERSION,
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-24 11:28:35 +02:00
|
|
|
mfconn *conn;
|
2014-09-26 16:08:30 +02:00
|
|
|
folder_tree *tree;
|
2014-09-23 20:53:29 +02:00
|
|
|
|
2014-09-23 15:12:19 +02:00
|
|
|
struct mediafirefs_user_options {
|
2014-09-24 11:28:35 +02:00
|
|
|
char *username;
|
|
|
|
|
char *password;
|
|
|
|
|
char *configfile;
|
|
|
|
|
char *server;
|
|
|
|
|
int app_id;
|
|
|
|
|
char *api_key;
|
2014-09-23 15:12:19 +02:00
|
|
|
} mediafirefs_user_options = {
|
2014-09-24 11:28:35 +02:00
|
|
|
NULL, NULL, NULL, NULL, -1, NULL};
|
2014-09-23 15:12:19 +02:00
|
|
|
|
|
|
|
|
static struct fuse_opt mediafirefs_opts[] = {
|
|
|
|
|
FUSE_OPT_KEY("-h", KEY_HELP),
|
|
|
|
|
FUSE_OPT_KEY("--help", KEY_HELP),
|
|
|
|
|
FUSE_OPT_KEY("-V", KEY_VERSION),
|
|
|
|
|
FUSE_OPT_KEY("--version", KEY_VERSION),
|
|
|
|
|
{"-c %s", offsetof(struct mediafirefs_user_options, configfile), 0},
|
2014-09-23 20:53:29 +02:00
|
|
|
{"--config=%s", offsetof(struct mediafirefs_user_options, configfile), 0},
|
2014-09-23 15:12:19 +02:00
|
|
|
{"-u %s", offsetof(struct mediafirefs_user_options, username), 0},
|
|
|
|
|
{"--username %s", offsetof(struct mediafirefs_user_options, username), 0},
|
|
|
|
|
{"-p %s", offsetof(struct mediafirefs_user_options, password), 0},
|
|
|
|
|
{"--password %s", offsetof(struct mediafirefs_user_options, password), 0},
|
2014-09-23 20:53:29 +02:00
|
|
|
{"--server %s", offsetof(struct mediafirefs_user_options, server), 0},
|
|
|
|
|
{"-i %d", offsetof(struct mediafirefs_user_options, app_id), 0},
|
|
|
|
|
{"--app-id %d", offsetof(struct mediafirefs_user_options, app_id), 0},
|
|
|
|
|
{"-k %s", offsetof(struct mediafirefs_user_options, api_key), 0},
|
|
|
|
|
{"--api-key %s", offsetof(struct mediafirefs_user_options, api_key), 0},
|
2014-09-23 15:12:19 +02:00
|
|
|
FUSE_OPT_END
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void usage(const char *progname)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Usage %s [options] mountpoint\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"general options:\n"
|
2014-09-23 20:53:29 +02:00
|
|
|
" -o opt[,opt...] mount options\n"
|
|
|
|
|
" -h, --help show this help\n"
|
|
|
|
|
" -V, --version show version information\n"
|
2014-09-23 15:12:19 +02:00
|
|
|
"\n"
|
|
|
|
|
"MediaFire FS options:\n"
|
2014-09-23 20:53:29 +02:00
|
|
|
" -o, --username str username\n"
|
|
|
|
|
" -p, --password str password\n"
|
|
|
|
|
" -c, --config file configuration file\n"
|
|
|
|
|
" --server domain server domain\n"
|
|
|
|
|
" -i, --app-id id App ID\n"
|
|
|
|
|
" -k, --api-key key API Key\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Notice that long options are separated from their arguments by\n"
|
2014-09-24 11:28:35 +02:00
|
|
|
"a space and not an equal sign.\n" "\n", progname);
|
2014-09-23 15:12:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int mediafirefs_getattr(const char *path, struct stat *stbuf)
|
|
|
|
|
{
|
2014-09-29 10:53:34 +02:00
|
|
|
/*
|
|
|
|
|
* since getattr is called before every other call (except for getattr,
|
|
|
|
|
* read and write) wee only call folder_tree_update in the getattr call
|
|
|
|
|
* and not the others
|
|
|
|
|
*/
|
2014-09-27 13:53:44 +02:00
|
|
|
folder_tree_update(tree, conn);
|
2014-09-26 16:08:30 +02:00
|
|
|
return folder_tree_getattr(tree, path, stbuf);
|
2014-09-23 15:12:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int mediafirefs_readdir(const char *path, void *buf,
|
2014-09-24 11:28:35 +02:00
|
|
|
fuse_fill_dir_t filldir, off_t offset,
|
|
|
|
|
struct fuse_file_info *info)
|
2014-09-23 15:12:19 +02:00
|
|
|
{
|
|
|
|
|
(void)offset;
|
|
|
|
|
(void)info;
|
|
|
|
|
|
2014-09-26 16:08:30 +02:00
|
|
|
return folder_tree_readdir(tree, path, buf, filldir);
|
2014-09-23 15:12:19 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-27 20:18:02 +02:00
|
|
|
static void mediafirefs_destroy()
|
|
|
|
|
{
|
|
|
|
|
FILE *fd;
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "storing hashtable\n");
|
|
|
|
|
|
|
|
|
|
fd = fopen("hashtable.dump", "w+");
|
|
|
|
|
|
|
|
|
|
folder_tree_store(tree, fd);
|
|
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
|
|
folder_tree_destroy(tree);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-28 09:37:24 +02:00
|
|
|
static int mediafirefs_mkdir(const char *path, mode_t mode)
|
|
|
|
|
{
|
|
|
|
|
(void)mode;
|
|
|
|
|
|
|
|
|
|
char *dirname;
|
|
|
|
|
int retval;
|
|
|
|
|
char *basename;
|
|
|
|
|
const char *key;
|
|
|
|
|
|
2014-09-29 10:53:34 +02:00
|
|
|
/* we don't need to check whether the path already existed because the
|
|
|
|
|
* getattr call made before this one takes care of that
|
|
|
|
|
*/
|
2014-09-28 09:37:24 +02:00
|
|
|
|
|
|
|
|
/* before calling the remote function we check locally */
|
|
|
|
|
|
|
|
|
|
dirname = strdup(path);
|
|
|
|
|
|
|
|
|
|
/* remove possible trailing slash */
|
|
|
|
|
if (dirname[strlen(dirname) - 1] == '/') {
|
|
|
|
|
dirname[strlen(dirname) - 1] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* split into dirname and basename */
|
|
|
|
|
|
|
|
|
|
basename = rindex(dirname, '/');
|
|
|
|
|
if (basename == NULL) {
|
|
|
|
|
fprintf(stderr, "cannot find slash\n");
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* replace the slash by a terminating zero */
|
|
|
|
|
basename[0] = '\0';
|
|
|
|
|
|
|
|
|
|
/* basename points to the string after the last slash */
|
|
|
|
|
basename++;
|
|
|
|
|
|
|
|
|
|
/* check if the dirname is of zero length now. If yes, then the directory
|
|
|
|
|
* is to be created in the root */
|
|
|
|
|
if (dirname[0] == '\0') {
|
|
|
|
|
key = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
key = folder_tree_path_get_key(tree, dirname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retval = mfconn_api_folder_create(conn, key, basename);
|
|
|
|
|
mfconn_update_secret_key(conn);
|
|
|
|
|
if (retval != 0) {
|
|
|
|
|
fprintf(stderr, "mfconn_api_folder_create unsuccessful\n");
|
|
|
|
|
// FIXME: find better errno in this case
|
|
|
|
|
return -EAGAIN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(dirname);
|
|
|
|
|
|
|
|
|
|
folder_tree_update(tree, conn);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 10:53:34 +02:00
|
|
|
static int mediafirefs_rmdir(const char *path)
|
2014-09-28 09:37:24 +02:00
|
|
|
{
|
|
|
|
|
const char *key;
|
|
|
|
|
int retval;
|
|
|
|
|
|
2014-09-29 10:53:34 +02:00
|
|
|
/* no need to check
|
|
|
|
|
* - if path is directory
|
|
|
|
|
* - if directory is empty
|
|
|
|
|
* - if directory is root
|
|
|
|
|
*
|
|
|
|
|
* because getattr was called before and already made sure
|
|
|
|
|
*/
|
2014-09-28 09:37:24 +02:00
|
|
|
|
2014-09-29 10:53:34 +02:00
|
|
|
key = folder_tree_path_get_key(tree, path);
|
|
|
|
|
if (key == NULL) {
|
|
|
|
|
fprintf(stderr, "key is NULL\n");
|
2014-09-28 09:37:24 +02:00
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retval = mfconn_api_folder_delete(conn, key);
|
|
|
|
|
mfconn_update_secret_key(conn);
|
|
|
|
|
if (retval != 0) {
|
|
|
|
|
fprintf(stderr, "mfconn_api_folder_create unsuccessful\n");
|
|
|
|
|
// FIXME: find better errno in this case
|
|
|
|
|
return -EAGAIN;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 10:53:34 +02:00
|
|
|
/* retrieve remote changes to not get out of sync */
|
2014-09-28 09:37:24 +02:00
|
|
|
folder_tree_update(tree, conn);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 10:53:34 +02:00
|
|
|
static int mediafirefs_open(const char * path, struct fuse_file_info * file_info)
|
|
|
|
|
{
|
|
|
|
|
(void)path;
|
|
|
|
|
|
|
|
|
|
if((file_info->flags & O_ACCMODE) != O_RDONLY) {
|
|
|
|
|
fprintf(stderr, "can only open read-only");
|
|
|
|
|
return -EACCES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check if file has already been downloaded */
|
|
|
|
|
|
|
|
|
|
/* check if downloaded version is the current one */
|
|
|
|
|
|
|
|
|
|
/* download file from remote */
|
|
|
|
|
|
|
|
|
|
/* update local file with patch */
|
|
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int mediafirefs_read(const char * path, char * buf, size_t size, off_t offset, struct fuse_file_info * file_info)
|
|
|
|
|
{
|
|
|
|
|
(void)path;
|
|
|
|
|
(void)buf;
|
|
|
|
|
(void)size;
|
|
|
|
|
(void)offset;
|
|
|
|
|
(void)file_info;
|
|
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-23 15:12:19 +02:00
|
|
|
static struct fuse_operations mediafirefs_oper = {
|
|
|
|
|
.getattr = mediafirefs_getattr,
|
|
|
|
|
.readdir = mediafirefs_readdir,
|
2014-09-27 20:18:02 +02:00
|
|
|
.destroy = mediafirefs_destroy,
|
2014-09-28 09:37:24 +02:00
|
|
|
.mkdir = mediafirefs_mkdir,
|
|
|
|
|
.rmdir = mediafirefs_rmdir,
|
2014-09-29 10:53:34 +02:00
|
|
|
.open = mediafirefs_open,
|
|
|
|
|
.read = mediafirefs_read,
|
2014-09-23 15:12:19 +02:00
|
|
|
/* .create = mediafirefs_create,
|
|
|
|
|
.fsync = mediafirefs_fsync,
|
|
|
|
|
.getxattr = mediafirefs_getxattr,
|
|
|
|
|
.init = mediafirefs_init,
|
|
|
|
|
.listxattr = mediafirefs_listxattr,
|
|
|
|
|
.opendir = mediafirefs_opendir,
|
|
|
|
|
.releasedir = mediafirefs_releasedir,
|
|
|
|
|
.setxattr = mediafirefs_setxattr,
|
|
|
|
|
.statfs = mediafirefs_statfs,
|
|
|
|
|
.truncate = mediafirefs_truncate,
|
|
|
|
|
.unlink = mediafirefs_unlink,
|
|
|
|
|
.utime = mediafirefs_utime,
|
|
|
|
|
.write = mediafirefs_write,*/
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
mediafirefs_opt_proc(void *data, const char *arg, int key,
|
|
|
|
|
struct fuse_args *outargs)
|
|
|
|
|
{
|
2014-09-24 11:28:35 +02:00
|
|
|
(void)data; // unused
|
|
|
|
|
(void)arg; // unused
|
2014-09-23 15:12:19 +02:00
|
|
|
|
2014-09-24 11:28:35 +02:00
|
|
|
if (key == KEY_HELP) {
|
2014-09-23 15:12:19 +02:00
|
|
|
usage(outargs->argv[0]);
|
|
|
|
|
fuse_opt_add_arg(outargs, "-ho");
|
|
|
|
|
fuse_main(outargs->argc, outargs->argv, &mediafirefs_oper, NULL);
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 11:28:35 +02:00
|
|
|
if (key == KEY_VERSION) {
|
2014-09-23 15:12:19 +02:00
|
|
|
fuse_opt_add_arg(outargs, "--version");
|
|
|
|
|
fuse_main(outargs->argc, outargs->argv, &mediafirefs_oper, NULL);
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 11:28:35 +02:00
|
|
|
int main(int argc, char *argv[])
|
2014-09-23 15:12:19 +02:00
|
|
|
{
|
2014-09-25 13:05:17 +02:00
|
|
|
int ret;
|
2014-09-23 15:12:19 +02:00
|
|
|
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
|
2014-09-27 20:18:02 +02:00
|
|
|
FILE *fd;
|
2014-09-24 11:28:35 +02:00
|
|
|
|
|
|
|
|
if (fuse_opt_parse
|
|
|
|
|
(&args, &mediafirefs_user_options, mediafirefs_opts,
|
|
|
|
|
mediafirefs_opt_proc) == -1) {
|
2014-09-23 15:12:19 +02:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2014-09-23 20:53:29 +02:00
|
|
|
|
|
|
|
|
if (mediafirefs_user_options.app_id == -1) {
|
|
|
|
|
mediafirefs_user_options.app_id = 42709;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mediafirefs_user_options.server == NULL) {
|
|
|
|
|
mediafirefs_user_options.server = "www.mediafire.com";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mediafirefs_user_options.username == NULL ||
|
|
|
|
|
mediafirefs_user_options.password == NULL) {
|
|
|
|
|
fprintf(stderr, "You must specify username and pasword\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn = mfconn_create(mediafirefs_user_options.server,
|
2014-09-24 11:28:35 +02:00
|
|
|
mediafirefs_user_options.username,
|
|
|
|
|
mediafirefs_user_options.password,
|
|
|
|
|
mediafirefs_user_options.app_id,
|
|
|
|
|
mediafirefs_user_options.api_key);
|
2014-09-23 20:53:29 +02:00
|
|
|
|
|
|
|
|
if (conn == NULL) {
|
|
|
|
|
fprintf(stderr, "Cannot establish connection\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-27 20:18:02 +02:00
|
|
|
if (access("hashtable.dump", F_OK) != -1) {
|
|
|
|
|
// file exists
|
|
|
|
|
fprintf(stderr, "loading hashtable\n");
|
|
|
|
|
fd = fopen("hashtable.dump", "r");
|
|
|
|
|
|
|
|
|
|
tree = folder_tree_load(fd);
|
|
|
|
|
|
|
|
|
|
fclose(fd);
|
2014-09-25 13:05:17 +02:00
|
|
|
|
2014-09-27 20:18:02 +02:00
|
|
|
folder_tree_update(tree, conn);
|
|
|
|
|
} else {
|
|
|
|
|
// file doesn't exist
|
|
|
|
|
tree = folder_tree_create();
|
|
|
|
|
|
|
|
|
|
ret = folder_tree_rebuild(tree, conn);
|
|
|
|
|
}
|
2014-09-25 13:05:17 +02:00
|
|
|
|
|
|
|
|
//folder_tree_housekeep(tree);
|
|
|
|
|
|
2014-09-28 07:39:13 +02:00
|
|
|
folder_tree_debug(tree);
|
2014-09-25 13:05:17 +02:00
|
|
|
|
2014-09-26 16:08:30 +02:00
|
|
|
return fuse_main(args.argc, args.argv, &mediafirefs_oper, NULL);
|
2014-09-23 15:12:19 +02:00
|
|
|
}
|