void HttpLoader::do_item(const std::shared_ptr<WorkItem> &item,
                         const std::shared_ptr<ICurlWrapper> &curl_wrapper)
{
    auto download_item = std::dynamic_pointer_cast<DownloadItem>(item);
    if (nullptr != download_item) {
        do_download(download_item, curl_wrapper);
        return;
    }

    auto upload_item = std::dynamic_pointer_cast<UploadItem>(item);
    if (nullptr != upload_item) {
        do_upload(upload_item, curl_wrapper);
        return;
    }
}
/*------------------------------------------------------------------------*/
int main(int argc, char **argv)
{
	int ret=0;
	
	while(1) {
		int ret = getopt(argc,argv, "U:X:Di:AlLI:E:Z:d:F:P:u:p:rRqK");
		if (ret==-1)
			break;
			
		char c=(char)ret;

		switch(c) {
		case 'F':
			strncpy(ftp_cmd,optarg,512);
			ftp_cmd[511]=0;
			break;
		case 'X':
			ret=do_update(uuids, num_uuids, device, optarg);
			if (ret==-2)
				exit(ret);
			break;
		case 'U':
			ret=do_upload(uuids, num_uuids, device, optarg);
			if (ret==-2)
				exit(ret);
			break;
		case 'D':
			ret|=do_download(uuids, num_uuids, device, NC_CONFPATH, NC_CONFFILE);
			break;
		case 'i':
			uuids[num_uuids]=strdup(optarg);
			num_uuids++;
			break;
		case 'A':
			num_uuids=get_uuids(uuids,255);
			break;
		case 'l':
			show_uuids();
			break;
		case 'd':
			strncpy(device,optarg,255);
			device[255]=0;
			break;
		case 'P':
			strncpy(socket_path,optarg,255);
			socket_path[255]=0;
			break;
		case 'p':
			strncpy(password,optarg,255);
			password[255]=0;
			break;
		case 'u':
			strncpy(username,optarg,255);
			username[255]=0;
			break;
		case 'r':
			no_reboot=1;
			break;
		case 'K':
			ret|=do_all_kill(uuids,num_uuids,device);
			break;	
		case 'R':
			ret|=do_all_reboot(uuids, num_uuids, device);
			break;
		case 'L':
			show_all_firmwares(uuids, num_uuids);
			break;
		case 'I':
			do_fw_actions(uuids, num_uuids, 0, optarg);
			break;
		case 'E':
			do_fw_actions(uuids, num_uuids, 1, optarg);
			break;
		case 'Z':
			do_fw_actions(uuids, num_uuids, 2, optarg);
			break;			
		case 'q':
			verbose=0;
			break;
		default:
			usage();
			break;
		}
	}
	exit(ret);
}
/* 上传文件或目录 */
int command_upload(int argc, char **argv) {
//{{{
    int option_overwrite        = 0; /* 覆盖同名文件        */
    int option_new              = 0; /* 创建新文件          */
    int option_follow_link      = 0; /* 复制链接源文件      */
    size_t option_split_size    = 0; /* 分片大小            */

    int ret = 0;
    int i   = 0;
    char c;

    char *split_size    = NULL;
    char *remote_path   = NULL;
    char *local_path    = NULL;

    /* 最大分片 2G  */
    size_t max_split_size = 2 * 1024 * 1024 * (size_t)1024;
    /* 最小分片 10M */
    size_t min_split_size = 10 * 1024 * 1024;


    opterr = 0;
    while ((c = getopt(argc, argv, "onlp:")) != -1) {
        switch (c) {
            case 'o':
                option_overwrite = 1;
                break;
            case 'n':
                option_new = 1;
                break;
            case 'l':
                option_follow_link = 1;
                break;
            case 'p':
                split_size = optarg;
                break;
            case '?':
                if (optopt == 'p') {
                    color_log(COLOR_LOG_ERROR, "-p 请指定分片大小\n");
                    ret = 1;
                    goto free;
                }
                break;
        }
    }
 
    if (option_overwrite && option_new) {
        color_log(COLOR_LOG_ERROR, "请不要同时指定-n -o\n");
        ret = 1;
        goto free;
    }

    if (optind < argc - 2) {
        color_log(COLOR_LOG_ERROR, "请指定路径\n");
        usage();
        ret = 1;
        goto free;
    }

    local_path = argv[argc - 2];
    if (local_path[strlen(local_path) - 1] == '/') {
        local_path[strlen(local_path) - 1] = '\0';
    }

    remote_path = argv[argc - 1];
    if (remote_path[strlen(remote_path) - 1] == '/') {
        remote_path[strlen(remote_path) - 1] = '\0';
    }

    /* 默认50M分片大小 */
    if (split_size == NULL) {
        option_split_size = 50 * 1024 * 1024;
    } else {
        i = strlen(split_size) - 1;
        if (i > 0) {
            if (split_size[i] == 'M' || split_size[i] == 'm') {
                split_size[i] = '\0';
                option_split_size = atof(split_size) * 1024 * 1024;
            } else if (split_size[i] == 'G' || split_size[i] == 'g') {
                split_size[i] = '\0';
                option_split_size = atof(split_size) * 1024 * 1024 * 1024;
            }
        } else {
            option_split_size = atol(split_size);
        }

        if (option_split_size < min_split_size) {
            ret = 1;
            color_log(COLOR_LOG_ERROR, "分片尺寸不能小于10M\n");
            goto free;
        } else if (option_split_size > max_split_size) {
            ret = 1;
            color_log(COLOR_LOG_ERROR, "分片尺寸不能大于2G\n");
            goto free;
        }
    }

    if (!init_api()) {
        ret = 1;
        goto free;
    }

#ifdef DEBUG
    fprintf(stderr, "Upload %s to %s\n", local_path, remote_path);
    readable_size(option_split_size, api->util_buffer0);
    fprintf(stderr, "分片尺寸:%s\n", api->util_buffer0);
#endif

   
    if (stat(local_path, &(api->file_st)) == -1) {
        color_log(COLOR_LOG_ERROR, "%s 不存在\n", local_path);
        ret = 1;
        goto free;
    }

    ret = do_upload(local_path,
                    remote_path,
                    option_overwrite, 
                    option_new, 
                    option_follow_link,
                    option_split_size);
free:

    return ret;
} 
bool HttpLoader::upload_sync(const std::string &target_url, const std::string &local_path)
{
    auto work_item = std::make_shared<UploadItem>(target_url, local_path, nullptr);
    bool success = do_upload(work_item, _curl_wrapper);
    return success;
}
Exemple #5
0
int main(int argc, char **argv)
{

  static const char *global_opt_string = "GPu:p:l:r:C:h?";

  static const struct option global_long_opts[] = {
    { "user", required_argument, NULL, 'u' }, // user name
    { "password", required_argument, NULL, 'p' }, // password
    { "local-file", required_argument, NULL, 'l' }, // local file name
    { "remote-file", required_argument, NULL, 'r' }, // remote file name
    { "url", required_argument, NULL, 'U'}, // remote url
    { "get", no_argument, NULL, 'G' }, // use get method
    { "put", no_argument, NULL, 'P' }, // use put method
    { "help", no_argument, NULL, 'h'}, // help
    { NULL, no_argument, NULL, 0 },
  };

  /* parse the arguments */
  int opt = 0;
  int long_index = 0;
  global_arg.usr = NULL;
  global_arg.pwd = NULL;
  global_arg.url = NULL;
  global_arg.local_file = NULL;
  global_arg.remote_file = NULL;
  global_arg.method = METHOD_GET;

  opt = getopt_long(argc, argv, global_opt_string, global_long_opts, &long_index);

  while(opt != -1){
    switch(opt){
    case 'u':
      global_arg.usr = optarg;
      break;
    case 'p':
      global_arg.pwd = optarg;
      break;
    case 'l':
      global_arg.local_file = optarg;
      break;
    case 'r':
      global_arg.remote_file = optarg;
      break;
    case 'U':
      global_arg.url = optarg;
      break;
    case 'G':
      global_arg.method = METHOD_GET;
      break;
    case 'P':
      global_arg.method = METHOD_PUT;
      break;
    case 'h':
    case '?':
      display_usage();
      break;
    default:
      printf("wrong option:%c\n",opt);
      break;
    }
    opt = getopt_long(argc, argv, global_opt_string, global_long_opts, &long_index);
  }

#ifdef _DEBUG
  printf("arguments:\n");
  printf("user name:%s\n", global_arg.usr);
  printf("password:%s\n", global_arg.pwd);
  printf("locale file:%s\n", global_arg.local_file);
  printf("remote file:%s\n", global_arg.remote_file);
  printf("remote url:%s\n", global_arg.url);
  printf("method:%s\n", global_arg.method == METHOD_GET?"get":"put");
#endif

  // check the arguments
  if(!global_arg.url || strlen(global_arg.url) == 0){
    printf("error:not remote url or bad url!!!\n");
    display_usage();
  }

  if(global_arg.usr && !global_arg.pwd){
    printf("error:not password provide!!!\n");
    display_usage();
  }

  if(!global_arg.usr && global_arg.pwd){
    printf("warning:no user, ignore password!!!\n");
  }
  
  if(global_arg.method == METHOD_PUT){
    if(!global_arg.local_file || strlen(global_arg.local_file) == 0){
      printf("error:put method must have local file name!!!\n");
      display_usage();
    }
    if(!global_arg.remote_file || strlen(global_arg.remote_file) == 0){
      global_arg.remote_file = global_arg.local_file;
      //printf("warning:no argument remote file!!!\n");
    }
  }else{
    if(!global_arg.remote_file || strlen(global_arg.remote_file) == 0){
      printf("error:get method must have remote file name!!!\n");
      display_usage();
    }
    if(!global_arg.local_file || strlen(global_arg.local_file) == 0){
      global_arg.local_file = global_arg.remote_file;
      //printf("warning:no argument local file!!!\n");
    }
  }

  if(global_arg.method == METHOD_GET){
    do_download(global_arg.url, 
		global_arg.usr,
		global_arg.pwd,
		global_arg.local_file,
		global_arg.remote_file);
  }else{
    // call the upload function
    do_upload(global_arg.url, 
	      global_arg.usr, 
	      global_arg.pwd, 
	      global_arg.local_file, 
	      global_arg.remote_file);
  }
	
  printf("\n");
  return SUCCESS;
}
Exemple #6
0
int create_playlist_rio (rios_t *rio, char *name, int songs[], int memory_units[], int nsongs) {
  info_page_t info;
  int error, addpipe, i, tmpi;
  char filename[PATH_MAX];
  char tmpc;
  file_list *tmp;
  FILE *fh;

  if (!rio)
    return -EINVAL;
  
  /* Current implementation only works for S-Series and newer. For
     older, upload a .lst file. */
  if (return_generation_rio (rio) < 4)
    return -EPERM;
  
  if (try_lock_rio (rio) != 0)
    return -EBUSY;

  rio_log (rio, 0, "create_playlist_rio: creating a new playlist %s.\n", name);

  /* Create a temporary file to store the new playlist */
  snprintf (filename, PATH_MAX, "/tmp/rioutil_%s.%08x.lst", name, time (NULL));
  fh = fopen (filename, "w");
  if (fh == 0)
    UNLOCK(-errno);
  fprintf (fh, "FIDLST%c%c%c", 1, 0, 0);
  tmpi = arch32_2_little32(nsongs);
  fwrite (&tmpi, 1, 3, fh);

  for (i = 0 ; i < nsongs ; i++) {
    rio_log (rio, 0, "Adding for song %i to playlist %s...\n", songs[i], name);
    for (tmp = rio->info.memory[memory_units[i]].files ; tmp ; tmp = tmp->next)
      if (tmp->num == songs[i])
        break;

    if (tmp == NULL)
      continue;

    tmpi = arch32_2_little32(tmp->rio_num);
    fwrite (&tmpi, 1, 3, fh);
    fwrite (tmp->sflags, 3, 1, fh);
  }

  fclose (fh);

  new_playlist_info (&info, filename, name);
  
  if ((addpipe = open(filename, O_RDONLY)) == -1)
    return -1;
  
  /* i moved the major functionality of both add_file and add_song down a layer */
  if ((error = do_upload (rio, 0, addpipe, info, 0)) != URIO_SUCCESS) {
    free (info.data);
    
    close (addpipe);

    /* make sure no malicious user has messed with this variable */
    if (strstr (filename, "/tmp/rioutil_") == filename)
      unlink (filename);
    
    UNLOCK(error);
  }
  
  close (addpipe);
  
  if (strstr (filename, "/tmp/rioutil_") == filename)
    unlink (filename);
    
  free (info.data);
  
  rio_log (rio, 0, "add_file_rio: copy complete.\n");
  
  UNLOCK(URIO_SUCCESS);
}
int sprd_start_upload(void)
{
	g_bd.sys_bootm |= SYS_BOOTM_UP;
	
	return do_upload();
}
Exemple #8
0
static int
process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
{
	char *tmp_dst = NULL;
	char *abs_dst = NULL;
	char *tmp;
	glob_t g;
	int err = 0;
	int i;

	if (dst) {
		tmp_dst = xstrdup(dst);
		tmp_dst = make_absolute(tmp_dst, pwd);
	}

	memset(&g, 0, sizeof(g));
	debug3("Looking up %s", src);
	if (glob(src, 0, NULL, &g)) {
		error("File \"%s\" not found.", src);
		err = -1;
		goto out;
	}

	/* If multiple matches, dst may be directory or unspecified */
	if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
		error("Multiple files match, but \"%s\" is not a directory",
		    tmp_dst);
		err = -1;
		goto out;
	}

	for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
		if (!is_reg(g.gl_pathv[i])) {
			error("skipping non-regular file %s",
			    g.gl_pathv[i]);
			continue;
		}
		if (infer_path(g.gl_pathv[i], &tmp)) {
			err = -1;
			goto out;
		}

		if (g.gl_matchc == 1 && tmp_dst) {
			/* If directory specified, append filename */
			if (remote_is_dir(conn, tmp_dst)) {
				if (infer_path(g.gl_pathv[0], &tmp)) {
					err = 1;
					goto out;
				}
				abs_dst = path_append(tmp_dst, tmp);
				xfree(tmp);
			} else
				abs_dst = xstrdup(tmp_dst);

		} else if (tmp_dst) {
			abs_dst = path_append(tmp_dst, tmp);
			xfree(tmp);
		} else
			abs_dst = make_absolute(tmp, pwd);

		printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
		if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
			err = -1;
	}

out:
	if (abs_dst)
		xfree(abs_dst);
	if (tmp_dst)
		xfree(tmp_dst);
	globfree(&g);
	return(err);
}