static int rm_main(int ac, char *av[]) { gc_error_free GError *local_err = NULL; static struct mega_session *s; tool_init(&ac, &av, "- remove files from mega.nz", entries, TOOL_INIT_AUTH); if (ac < 2) { g_printerr("ERROR: No files specified for removal!\n"); tool_fini(NULL); return 1; } s = tool_start_session(TOOL_SESSION_OPEN); if (!s) { tool_fini(NULL); return 1; } gint i, status = 0; for (i = 1; i < ac; i++) { gc_free gchar *path = tool_convert_filename(av[i], FALSE); if (!mega_session_rm(s, path, &local_err)) { g_printerr("ERROR: Can't remove %s: %s\n", path, local_err->message); g_clear_error(&local_err); status = 1; } } mega_session_save(s, NULL); tool_fini(s); return status; }
int main(int ac, char* av[]) { gc_error_free GError *local_err = NULL; mega_session* s; tool_init(&ac, &av, "- download individual files from mega.nz", entries); if (!strcmp(opt_path, "-")) opt_noprogress = opt_stream = TRUE; if (ac < 2) { g_printerr("ERROR: No files specified for download!\n"); tool_fini(NULL); return 1; } if (opt_stream && ac != 2) { g_printerr("ERROR: Can't stream from multiple files!\n"); tool_fini(NULL); return 1; } s = tool_start_session(); if (!s) { tool_fini(NULL); return 1; } mega_session_watch_status(s, status_callback, NULL); gint i; for (i = 1; i < ac; i++) { gc_free gchar* path = tool_convert_filename(av[i], FALSE); // perform download if (!mega_session_get(s, opt_stream ? NULL : opt_path, path, &local_err)) { if (!opt_noprogress) g_print("\r" ESC_CLREOL "\n"); g_printerr("ERROR: Download failed for '%s': %s\n", path, local_err->message); g_clear_error(&local_err); } else { if (!opt_noprogress) g_print("\r" ESC_CLREOL "Downloaded %s\n", cur_file); } } tool_fini(s); return 0; }
int main(int ac, char* av[]) { GError *local_err = NULL; mega_session* s; tool_init(&ac, &av, "- upload files to mega.co.nz", entries); if (ac < 2) { g_printerr("ERROR: No files specified for upload!\n"); tool_fini(NULL); return 1; } s = tool_start_session(); if (!s) return 1; mega_session_watch_status(s, status_callback, NULL); gint i; for (i = 1; i < ac; i++) { cur_file = g_path_get_basename(av[i]); // perform download if (!mega_session_put(s, opt_path, av[i], &local_err)) { if (!opt_noprogress) g_print("\r" ESC_CLREOL "\n"); g_printerr("ERROR: Upload failed for '%s': %s\n", av[i], local_err->message); g_clear_error(&local_err); } else { if (!opt_noprogress) g_print("\r" ESC_CLREOL "Uploaded %s\n", cur_file); } g_free(cur_file); } mega_session_save(s, NULL); tool_fini(s); return 0; }
int main(int ac, char* av[]) { gc_error_free GError *local_err = NULL; tool_init(&ac, &av, "- create directories at mega.nz", entries); if (ac < 2) { g_printerr("ERROR: No directories specified!\n"); tool_fini(NULL); return 1; } s = tool_start_session(); if (!s) { tool_fini(NULL); return 1; } gint i; for (i = 1; i < ac; i++) { gc_free gchar* path = tool_convert_filename(av[i], FALSE); if (!mega_session_mkdir(s, path, &local_err)) { g_printerr("ERROR: Can't create directory %s: %s\n", path, local_err->message); g_clear_error(&local_err); } } mega_session_save(s, NULL); tool_fini(s); return 0; }
int main(int ac, char* av[]) { gc_error_free GError *local_err = NULL; mega_reg_state* state = NULL; gc_free gchar* signup_key = NULL; mega_session* s; tool_init_bare(&ac, &av, "LINK - register a new mega.nz account", entries); if (opt_verify && opt_register) { g_printerr("ERROR: You must specify either --register or --verify option\n"); return 1; } if (opt_register) { if (!opt_name) { g_printerr("ERROR: You must specify name for your new mega.nz account\n"); return 1; } if (!opt_email) { g_printerr("ERROR: You must specify email for your new mega.nz account\n"); return 1; } if (!opt_password) { g_printerr("ERROR: You must specify password for your new mega.nz account\n"); return 1; } } else if (opt_verify) { if (ac != 2) { g_printerr("ERROR: You must specify signup key and a link from the verification email\n"); return 1; } gc_regex_unref GRegex* r = g_regex_new("^(?:https?://mega(?:\\.co)?\\.nz/#confirm)?([a-z0-9_-]{80,150})$", G_REGEX_CASELESS, 0, NULL); gc_match_info_unref GMatchInfo* m = NULL; g_assert(r != NULL); if (!g_regex_match(r, av[1], 0, &m)) { g_printerr("ERROR: Invalid verification link or key: '%s'\n", av[1]); return 1; } signup_key = g_match_info_fetch(m, 1); state = unserialize_reg_state(opt_verify); if (!state) { g_printerr("ERROR: Failed to decode registration state parameter, make sure you copied it correctly\n"); return 1; } } else { g_printerr("ERROR: You must specify either --register or --verify option\n"); return 1; } s = mega_session_new(); if (opt_register) { if (!mega_session_register(s, opt_email, opt_password, opt_name, &state, &local_err)) { g_printerr("ERROR: Registration failed: %s\n", local_err ? local_err->message : "Unknown error"); goto err; } gc_free gchar* serialized_state = serialize_reg_state(state); if (opt_script) g_print("%s --verify %s @LINK@\n", av[0], serialized_state); else g_print( "Registration email was sent to %s. To complete registration, you must run:\n\n" " %s --verify %s @LINK@\n\n" "(Where @LINK@ is registration link from the 'MEGA Signup' email)\n", opt_email, g_get_prgname(), serialized_state ); } if (opt_verify) { if (!mega_session_register_verify(s, state, signup_key, &local_err)) { g_printerr("ERROR: Verification failed: %s\n", local_err ? local_err->message : "Unknown error"); goto err; } if (!opt_script) g_print("Account registered successfully!\n"); } tool_fini(s); return 0; err: tool_fini(s); return 1; }
int main(int ac, char* av[]) { mega_session* s; gc_error_free GError *local_err = NULL; GSList *l = NULL, *i; gint j; tool_init(&ac, &av, "- list files stored at mega.nz", entries); s = tool_start_session(); if (!s) return 1; // gather nodes if (ac == 1) { l = mega_session_ls_all(s); opt_names = FALSE; } else { if (ac > 2 || opt_recursive) opt_names = FALSE; for (j = 1; j < ac; j++) { gc_free gchar* path = tool_convert_filename(av[j], FALSE); mega_node* n = mega_session_stat(s, path); if (n && (n->type == MEGA_NODE_FILE || !opt_names)) l = g_slist_append(l, n); l = g_slist_concat(l, mega_session_ls(s, path, opt_recursive)); } } l = g_slist_sort(l, (GCompareFunc)compare_node); // export if requested if (opt_export && !mega_session_addlinks(s, l, &local_err)) { g_printerr("ERROR: Can't read links info from mega.nz: %s\n", local_err->message); g_slist_free(l); tool_fini(s); return 1; } if (l && opt_long && opt_header && !opt_export) { g_print("===================================================================================\n"); g_print("%-11s %-11s %-1s %13s %-19s %s\n", "Handle", "Owner", "T", "Size", "Mod. Date", opt_names ? "Filename" : "Path"); g_print("===================================================================================\n"); } for (i = l; i; i = i->next) { mega_node* n = i->data; gc_free gchar* node_path = mega_node_get_path_dup(n); if (opt_export) g_print("%73s ", n->link ? mega_node_get_link(n, TRUE) : ""); if (opt_long) { GDateTime* dt = g_date_time_new_from_unix_local(n->timestamp); gc_free gchar* time_str = g_date_time_format(dt, "%Y-%m-%d %H:%M:%S"); g_date_time_unref(dt); gc_free gchar* size_str = NULL; if (opt_human) size_str = n->size > 0 ? g_format_size_full(n->size, G_FORMAT_SIZE_IEC_UNITS) : g_strdup("-"); else size_str = n->size > 0 ? g_strdup_printf("%" G_GUINT64_FORMAT, n->size) : g_strdup("-"); g_print("%-11s %-11s %d %13s %19s %s\n", n->handle, n->user_handle ? n->user_handle : "", n->type, size_str, n->timestamp > 0 ? time_str : "", opt_names ? n->name : node_path ); } else g_print("%s\n", opt_names ? n->name : node_path); } g_slist_free(l); tool_fini(s); return 0; }
int main(int ac, char* av[]) { tool_init(&ac, &av, "- synchronize local and remote mega.nz directories", entries); if (!opt_local_path || !opt_remote_path) { g_printerr("ERROR: You must specify local and remote paths\n"); return 1; } s = tool_start_session(); if (!s) { tool_fini(NULL); return 1; } mega_session_watch_status(s, status_callback, NULL); // check remote dir existence mega_node* remote_dir = mega_session_stat(s, opt_remote_path); if (!remote_dir) { g_printerr("ERROR: Remote directory not found %s\n", opt_remote_path); goto err0; } else if (!mega_node_is_container(remote_dir)) { g_printerr("ERROR: Remote path must be a folder: %s\n", opt_remote_path); goto err0; } // check local dir existence GFile* local_file = g_file_new_for_path(opt_local_path); if (opt_download) { dl_sync_dir(remote_dir, local_file, opt_remote_path); } else { if (g_file_query_file_type(local_file, 0, NULL) != G_FILE_TYPE_DIRECTORY) { g_printerr("ERROR: Local directory not found %s\n", opt_local_path); goto err1; } up_sync_dir(local_file, local_file, opt_remote_path); mega_session_save(s, NULL); } g_object_unref(local_file); tool_fini(s); return 0; err1: g_object_unref(local_file); err0: tool_fini(s); return 1; }
int main(int ac, char* av[]) { GError *local_err = NULL; GRegex *file_regex, *folder_regex; gchar* key; gchar* handle; gint i; tool_init_bare(&ac, &av, "- download exported files from mega.co.nz", entries); if (!strcmp(opt_path, "-")) opt_noprogress = opt_stream = TRUE; if (ac < 2) { g_printerr("ERROR: No links specified for download!\n"); tool_fini(NULL); return 1; } if (opt_stream && ac != 2) { g_printerr("ERROR: Can't stream from multiple files!\n"); tool_fini(NULL); return 1; } // prepare link parsers file_regex = g_regex_new("^https?://mega.co.nz/#!([a-z0-9_-]{8})!([a-z0-9_-]{43})$", G_REGEX_CASELESS, 0, NULL); g_assert(file_regex != NULL); folder_regex = g_regex_new("^https?://mega.co.nz/#F!([a-z0-9_-]{8})!([a-z0-9_-]{22})$", G_REGEX_CASELESS, 0, NULL); g_assert(folder_regex != NULL); // create session s = mega_session_new(); mega_session_watch_status(s, status_callback, NULL); // process links for (i = 1; i < ac; i++) { GMatchInfo* m1 = NULL; GMatchInfo* m2 = NULL; key = NULL; handle = NULL; if (g_regex_match(file_regex, av[i], 0, &m1)) { handle = g_match_info_fetch(m1, 1); key = g_match_info_fetch(m1, 2); // perform download if (!mega_session_dl(s, handle, key, opt_stream ? NULL : opt_path, &local_err)) { if (!opt_noprogress) g_print("\r" ESC_CLREOL "\n"); g_printerr("ERROR: Download failed for '%s': %s\n", av[i], local_err->message); g_clear_error(&local_err); } else { if (!opt_noprogress) g_print("\r" ESC_CLREOL "Downloaded %s\n", cur_file); } } else if (g_regex_match(folder_regex, av[i], 0, &m2)) { if (opt_stream) { g_printerr("ERROR: Can't stream from a directory!\n"); if (m1) g_match_info_unref(m1); if (m2) g_match_info_unref(m2); tool_fini(s); return 1; } handle = g_match_info_fetch(m2, 1); key = g_match_info_fetch(m2, 2); // perform download if (!mega_session_open_exp_folder(s, handle, key, &local_err)) { g_printerr("ERROR: Can't open folder '%s': %s\n", av[i], local_err->message); g_clear_error(&local_err); } else { GSList* l = mega_session_ls(s, "/", FALSE); if (g_slist_length(l) == 1) { mega_node* root_node = l->data; GFile* local_dir = g_file_new_for_path(opt_path); if (g_file_query_file_type(local_dir, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL) == G_FILE_TYPE_DIRECTORY) { dl_sync_dir(root_node, local_dir, root_node->path); } else { g_printerr("ERROR: %s must be a directory\n", opt_path); } } else { g_printerr("ERROR: EXP folder fs has multiple toplevel nodes? Weird!\n"); } } } else { g_printerr("WARNING: Skipping invalid Mega download link: %s\n", av[i]); } if (m1) g_match_info_unref(m1); if (m2) g_match_info_unref(m2); g_free(handle); g_free(key); } tool_fini(s); return 0; }
int main(int ac, char* av[]) { gc_error_free GError *local_err = NULL; mega_session* s; tool_init(&ac, &av, "- move files on the remote filesystem at mega.nz", entries); if (ac < 3) { g_printerr("ERROR: You must specify both source path(s) and destination path"); return 1; } s = tool_start_session(); if (!s) return 1; gboolean rename = FALSE; gc_free gchar* dest = tool_convert_filename(av[ac - 1], FALSE); // check destination path mega_node* destination = mega_session_stat(s, dest); if (destination) { if (destination->type == MEGA_NODE_FILE) { gc_free gchar* path = mega_node_get_path_dup(destination); g_printerr("Destination file already exists: %s", path); goto err; } if (!mega_node_is_writable(s, destination) || destination->type == MEGA_NODE_NETWORK || destination->type == MEGA_NODE_CONTACT) { gc_free gchar* path = mega_node_get_path_dup(destination); g_printerr("You can't move files into: %s", path); goto err; } } else { rename = TRUE; gc_free gchar* parent_path = g_path_get_dirname(dest); destination = mega_session_stat(s, parent_path); if (!destination) { g_printerr("Destination directory doesn't exist: %s", parent_path); goto err; } if (destination->type == MEGA_NODE_FILE) { gc_free gchar* path = mega_node_get_path_dup(destination); g_printerr("Destination is not directory: %s", path); goto err; } if (!mega_node_is_writable(s, destination) || destination->type == MEGA_NODE_NETWORK || destination->type == MEGA_NODE_CONTACT) { gc_free gchar* path = mega_node_get_path_dup(destination); g_printerr("You can't move files into: %s", path); goto err; } } if (rename && ac > 3) { g_printerr("You can't use multiple source paths when renaming file or directory"); goto err; } // enumerate source paths gint i; for (i = 1; i < ac - 1; i++) { gc_free gchar* path = tool_convert_filename(av[i], FALSE); mega_node* n = mega_session_stat(s, path); if (!n) { g_printerr("Source file doesn't exists: %s", path); goto err; } if (n->type != MEGA_NODE_FILE && n->type != MEGA_NODE_FOLDER) { g_printerr("Source is not movable: %s", path); goto err; } // check destination gc_free gchar* n_path = mega_node_get_path_dup(n); gc_free gchar* destination_path = mega_node_get_path_dup(destination); gc_free gchar* basename = g_path_get_basename(n_path); gc_free gchar* tmp = g_strconcat(destination_path, "/", basename, NULL); // check destination path mega_node* dn = mega_session_stat(s, tmp); if (dn) { gc_free gchar* dn_path = mega_node_get_path_dup(dn); g_printerr("Destination file already exists: %s", dn_path); goto err; } // perform move //if (!mega_session_mkdir(s, path, &local_err)) //{ //g_printerr("ERROR: Can't create directory %s: %s\n", path, local_err->message); //g_clear_error(&local_err); //} g_print("mv %s %s/%s\n", n_path, destination_path, tmp); } mega_session_save(s, NULL); tool_fini(s); return 0; err: tool_fini(s); return 1; }