Esempio n. 1
0
mega_session* tool_start_session(void)
{
  GError *local_err = NULL;
  gchar* sid = NULL;
  gboolean loaded = FALSE;

  mega_session* s = mega_session_new();

  // try to load cached session data (they are valid for 10 minutes since last
  // user_get or refresh)
  if (!mega_session_load(s, opt_username, opt_password, opt_cache_timout, &sid, &local_err))
  {
    g_clear_error(&local_err);

    if (!mega_session_open(s, opt_username, opt_password, sid, &local_err))
    {
      g_printerr("ERROR: Can't login to mega.co.nz: %s\n", local_err->message);
      goto err;
    }

    if (!mega_session_refresh(s, &local_err))
    {
      g_printerr("ERROR: Can't read filesystem info from mega.co.nz: %s\n", local_err->message);
      goto err;
    }

    loaded = TRUE;
    mega_session_save(s, NULL);
  }

  if (opt_reload_files && !loaded)
  {
    if (!mega_session_refresh(s, &local_err))
    {
      g_printerr("ERROR: Can't read filesystem info from mega.co.nz: %s\n", local_err->message);
      goto err;
    }

    mega_session_save(s, NULL);
  }

  mega_session_enable_previews(s, !opt_disable_previews);

  g_free(sid);
  return s;

err:
  mega_session_free(s);
  g_clear_error(&local_err);
  g_free(sid);
  return NULL;
}
Esempio n. 2
0
File: rm.c Progetto: wasas/megatools
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;
}
Esempio n. 3
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;
}
Esempio n. 4
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;
}
Esempio n. 5
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;
}
Esempio n. 6
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;
}