mirror of
https://github.com/xorgy/mediafire-fuse
synced 2026-01-13 13:14:29 -08:00
fuse/main.c: eliminate all global variables by using fuse private_data
This commit is contained in:
116
fuse/main.c
116
fuse/main.c
@@ -95,8 +95,13 @@ enum {
|
|||||||
KEY_VERSION,
|
KEY_VERSION,
|
||||||
};
|
};
|
||||||
|
|
||||||
mfconn *conn;
|
struct mediafirefs_context_private {
|
||||||
folder_tree *tree;
|
mfconn *conn;
|
||||||
|
folder_tree *tree;
|
||||||
|
char *configfile;
|
||||||
|
char *dircache;
|
||||||
|
char *filecache;
|
||||||
|
};
|
||||||
|
|
||||||
struct mediafirefs_user_options {
|
struct mediafirefs_user_options {
|
||||||
char *username;
|
char *username;
|
||||||
@@ -142,8 +147,11 @@ static int mediafirefs_getattr(const char *path, struct stat *stbuf)
|
|||||||
* FIXME: only call folder_tree_update if it has not been called for a set
|
* FIXME: only call folder_tree_update if it has not been called for a set
|
||||||
* amount of time
|
* amount of time
|
||||||
*/
|
*/
|
||||||
folder_tree_update(tree, conn);
|
struct mediafirefs_context_private *ctx;
|
||||||
return folder_tree_getattr(tree, conn, path, stbuf);
|
|
||||||
|
ctx = fuse_get_context()->private_data;
|
||||||
|
folder_tree_update(ctx->tree, ctx->conn);
|
||||||
|
return folder_tree_getattr(ctx->tree, ctx->conn, path, stbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int mediafirefs_readdir(const char *path, void *buf,
|
static int mediafirefs_readdir(const char *path, void *buf,
|
||||||
@@ -152,25 +160,30 @@ static int mediafirefs_readdir(const char *path, void *buf,
|
|||||||
{
|
{
|
||||||
(void)offset;
|
(void)offset;
|
||||||
(void)info;
|
(void)info;
|
||||||
|
struct mediafirefs_context_private *ctx;
|
||||||
|
|
||||||
return folder_tree_readdir(tree, conn, path, buf, filldir);
|
ctx = fuse_get_context()->private_data;
|
||||||
|
return folder_tree_readdir(ctx->tree, ctx->conn, path, buf, filldir);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mediafirefs_destroy()
|
static void mediafirefs_destroy()
|
||||||
{
|
{
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
|
struct mediafirefs_context_private *ctx;
|
||||||
|
|
||||||
|
ctx = fuse_get_context()->private_data;
|
||||||
|
|
||||||
fprintf(stderr, "storing hashtable\n");
|
fprintf(stderr, "storing hashtable\n");
|
||||||
|
|
||||||
fd = fopen("hashtable.dump", "w+");
|
fd = fopen("hashtable.dump", "w+");
|
||||||
|
|
||||||
folder_tree_store(tree, fd);
|
folder_tree_store(ctx->tree, fd);
|
||||||
|
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
|
||||||
folder_tree_destroy(tree);
|
folder_tree_destroy(ctx->tree);
|
||||||
|
|
||||||
mfconn_destroy(conn);
|
mfconn_destroy(ctx->conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int mediafirefs_mkdir(const char *path, mode_t mode)
|
static int mediafirefs_mkdir(const char *path, mode_t mode)
|
||||||
@@ -181,6 +194,9 @@ static int mediafirefs_mkdir(const char *path, mode_t mode)
|
|||||||
int retval;
|
int retval;
|
||||||
char *basename;
|
char *basename;
|
||||||
const char *key;
|
const char *key;
|
||||||
|
struct mediafirefs_context_private *ctx;
|
||||||
|
|
||||||
|
ctx = fuse_get_context()->private_data;
|
||||||
|
|
||||||
/* we don't need to check whether the path already existed because the
|
/* we don't need to check whether the path already existed because the
|
||||||
* getattr call made before this one takes care of that
|
* getattr call made before this one takes care of that
|
||||||
@@ -214,11 +230,11 @@ static int mediafirefs_mkdir(const char *path, mode_t mode)
|
|||||||
if (dirname[0] == '\0') {
|
if (dirname[0] == '\0') {
|
||||||
key = NULL;
|
key = NULL;
|
||||||
} else {
|
} else {
|
||||||
key = folder_tree_path_get_key(tree, conn, dirname);
|
key = folder_tree_path_get_key(ctx->tree, ctx->conn, dirname);
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = mfconn_api_folder_create(conn, key, basename);
|
retval = mfconn_api_folder_create(ctx->conn, key, basename);
|
||||||
mfconn_update_secret_key(conn);
|
mfconn_update_secret_key(ctx->conn);
|
||||||
if (retval != 0) {
|
if (retval != 0) {
|
||||||
fprintf(stderr, "mfconn_api_folder_create unsuccessful\n");
|
fprintf(stderr, "mfconn_api_folder_create unsuccessful\n");
|
||||||
// FIXME: find better errno in this case
|
// FIXME: find better errno in this case
|
||||||
@@ -227,7 +243,7 @@ static int mediafirefs_mkdir(const char *path, mode_t mode)
|
|||||||
|
|
||||||
free(dirname);
|
free(dirname);
|
||||||
|
|
||||||
folder_tree_update(tree, conn);
|
folder_tree_update(ctx->tree, ctx->conn);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -236,6 +252,9 @@ static int mediafirefs_rmdir(const char *path)
|
|||||||
{
|
{
|
||||||
const char *key;
|
const char *key;
|
||||||
int retval;
|
int retval;
|
||||||
|
struct mediafirefs_context_private *ctx;
|
||||||
|
|
||||||
|
ctx = fuse_get_context()->private_data;
|
||||||
|
|
||||||
/* no need to check
|
/* no need to check
|
||||||
* - if path is directory
|
* - if path is directory
|
||||||
@@ -245,14 +264,14 @@ static int mediafirefs_rmdir(const char *path)
|
|||||||
* because getattr was called before and already made sure
|
* because getattr was called before and already made sure
|
||||||
*/
|
*/
|
||||||
|
|
||||||
key = folder_tree_path_get_key(tree, conn, path);
|
key = folder_tree_path_get_key(ctx->tree, ctx->conn, path);
|
||||||
if (key == NULL) {
|
if (key == NULL) {
|
||||||
fprintf(stderr, "key is NULL\n");
|
fprintf(stderr, "key is NULL\n");
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = mfconn_api_folder_delete(conn, key);
|
retval = mfconn_api_folder_delete(ctx->conn, key);
|
||||||
mfconn_update_secret_key(conn);
|
mfconn_update_secret_key(ctx->conn);
|
||||||
if (retval != 0) {
|
if (retval != 0) {
|
||||||
fprintf(stderr, "mfconn_api_folder_create unsuccessful\n");
|
fprintf(stderr, "mfconn_api_folder_create unsuccessful\n");
|
||||||
// FIXME: find better errno in this case
|
// FIXME: find better errno in this case
|
||||||
@@ -260,7 +279,7 @@ static int mediafirefs_rmdir(const char *path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* retrieve remote changes to not get out of sync */
|
/* retrieve remote changes to not get out of sync */
|
||||||
folder_tree_update(tree, conn);
|
folder_tree_update(ctx->tree, ctx->conn);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -269,13 +288,16 @@ static int mediafirefs_open(const char *path, struct fuse_file_info *file_info)
|
|||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
struct mediafirefs_openfile *openfile;
|
struct mediafirefs_openfile *openfile;
|
||||||
|
struct mediafirefs_context_private *ctx;
|
||||||
|
|
||||||
|
ctx = fuse_get_context()->private_data;
|
||||||
|
|
||||||
if ((file_info->flags & O_ACCMODE) != O_RDONLY) {
|
if ((file_info->flags & O_ACCMODE) != O_RDONLY) {
|
||||||
fprintf(stderr, "can only open read-only");
|
fprintf(stderr, "can only open read-only");
|
||||||
return -EACCES;
|
return -EACCES;
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = folder_tree_open_file(tree, conn, path);
|
fd = folder_tree_open_file(ctx->tree, ctx->conn, path);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
fprintf(stderr, "folder_tree_file_open unsuccessful\n");
|
fprintf(stderr, "folder_tree_file_open unsuccessful\n");
|
||||||
return fd;
|
return fd;
|
||||||
@@ -497,7 +519,7 @@ static void parse_arguments(int *argc, char ***argv,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void connect_mf(struct mediafirefs_user_options *options,
|
static void connect_mf(struct mediafirefs_user_options *options,
|
||||||
char *dircache, char *filecache)
|
struct mediafirefs_context_private *ctx)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
@@ -514,41 +536,40 @@ static void connect_mf(struct mediafirefs_user_options *options,
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
conn = mfconn_create(options->server,
|
ctx->conn = mfconn_create(options->server, options->username,
|
||||||
options->username,
|
options->password, options->app_id,
|
||||||
options->password, options->app_id, options->api_key);
|
options->api_key);
|
||||||
|
|
||||||
if (conn == NULL) {
|
if (ctx->conn == NULL) {
|
||||||
fprintf(stderr, "Cannot establish connection\n");
|
fprintf(stderr, "Cannot establish connection\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fp = fopen(dircache, "r");
|
fp = fopen(ctx->dircache, "r");
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
// file exists
|
// file exists
|
||||||
fprintf(stderr, "loading hashtable\n");
|
fprintf(stderr, "loading hashtable\n");
|
||||||
|
|
||||||
tree = folder_tree_load(fp, filecache);
|
ctx->tree = folder_tree_load(fp, ctx->filecache);
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
folder_tree_update(tree, conn);
|
folder_tree_update(ctx->tree, ctx->conn);
|
||||||
} else {
|
} else {
|
||||||
// file doesn't exist
|
// file doesn't exist
|
||||||
fprintf(stderr, "creating new hashtable\n");
|
fprintf(stderr, "creating new hashtable\n");
|
||||||
tree = folder_tree_create(filecache);
|
ctx->tree = folder_tree_create(ctx->filecache);
|
||||||
|
|
||||||
folder_tree_rebuild(tree, conn);
|
folder_tree_rebuild(ctx->tree, ctx->conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
//folder_tree_housekeep(tree);
|
//folder_tree_housekeep(tree);
|
||||||
|
|
||||||
fprintf(stderr, "tree before starting fuse:\n");
|
fprintf(stderr, "tree before starting fuse:\n");
|
||||||
folder_tree_debug(tree);
|
folder_tree_debug(ctx->tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setup_conf_and_cache_dir(char **configfile, char **dircache,
|
static void setup_conf_and_cache_dir(struct mediafirefs_context_private *ctx)
|
||||||
char **filecache)
|
|
||||||
{
|
{
|
||||||
const char *homedir;
|
const char *homedir;
|
||||||
const char *configdir;
|
const char *configdir;
|
||||||
@@ -588,20 +609,20 @@ static void setup_conf_and_cache_dir(char **configfile, char **dircache,
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
*configfile = strdup_printf("%s/config", configdir);
|
ctx->configfile = strdup_printf("%s/config", configdir);
|
||||||
/* test if the configuration file can be opened */
|
/* test if the configuration file can be opened */
|
||||||
fd = open(*configfile, O_RDONLY);
|
fd = open(ctx->configfile, O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
free(*configfile);
|
free(ctx->configfile);
|
||||||
*configfile = NULL;
|
ctx->configfile = NULL;
|
||||||
} else {
|
} else {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
*dircache = strdup_printf("%s/directorytree", cachedir);
|
ctx->dircache = strdup_printf("%s/directorytree", cachedir);
|
||||||
|
|
||||||
*filecache = strdup_printf("%s/files", cachedir);
|
ctx->filecache = strdup_printf("%s/files", cachedir);
|
||||||
if (mkdir(*filecache, 0755) != 0 && errno != EEXIST) {
|
if (mkdir(ctx->filecache, 0755) != 0 && errno != EEXIST) {
|
||||||
perror("mkdir");
|
perror("mkdir");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@@ -614,30 +635,31 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
int ret,
|
int ret,
|
||||||
i;
|
i;
|
||||||
char *configfile;
|
struct mediafirefs_context_private *ctx;
|
||||||
char *dircache;
|
|
||||||
char *filecache;
|
|
||||||
|
|
||||||
struct mediafirefs_user_options options = {
|
struct mediafirefs_user_options options = {
|
||||||
NULL, NULL, NULL, NULL, -1, NULL
|
NULL, NULL, NULL, NULL, -1, NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
setup_conf_and_cache_dir(&configfile, &dircache, &filecache);
|
ctx = calloc(1, sizeof(struct mediafirefs_context_private));
|
||||||
|
|
||||||
parse_arguments(&argc, &argv, &options, configfile);
|
setup_conf_and_cache_dir(ctx);
|
||||||
|
|
||||||
connect_mf(&options, dircache, filecache);
|
parse_arguments(&argc, &argv, &options, ctx->configfile);
|
||||||
|
|
||||||
ret = fuse_main(argc, argv, &mediafirefs_oper, NULL);
|
connect_mf(&options, ctx);
|
||||||
|
|
||||||
|
ret = fuse_main(argc, argv, &mediafirefs_oper, ctx);
|
||||||
|
|
||||||
for (i = 0; i < argc; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
free(argv[i]);
|
free(argv[i]);
|
||||||
}
|
}
|
||||||
free(argv);
|
free(argv);
|
||||||
|
|
||||||
free(configfile);
|
free(ctx->configfile);
|
||||||
free(dircache);
|
free(ctx->dircache);
|
||||||
free(filecache);
|
free(ctx->filecache);
|
||||||
|
free(ctx);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user