/* 显示云盘信息 */ int command_info(int argc, char **argv) { //{{{ if (!init_api()) return 1; int ret = 0; const char *error = NULL; char buf[1024]; BaiduPCSInfo info; BaiduPCS_Info(api, &info); error = BaiduPCS_GetError(api); if (error != NULL) { color_log(COLOR_LOG_ERROR, "获取信息失败%s\n", error); ret = 1; goto free; } readable_size(info.quota, buf); printf("总容量:%s\n", buf); readable_size(info.used, buf); printf("已使用:%s\n", buf); free: return ret; }
void print_file(PCSFile *file, int show_detail) { //{{{ char buf2[100]; char buf3[100]; if (show_detail) { readable_timestamp(file->mtime, buf2); if (file->is_dir) { printf("目录 - %s %s\n", buf2, file->path); } else { readable_size(file->size, buf3); printf("文件 %11s %s %s\n", buf3, buf2, file->path); } } else { printf("%s\n", file->path); } }
/* 上传文件或目录 */ 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; }
/* * UI print function */ void tui_print() { sorted_list_node* nn = NULL; char host1[HOSTNAME_LENGTH], host2[HOSTNAME_LENGTH]; char buf0_10[10], buf1_10[10], buf2_10[10]; int j; static char *label; static char *labellong; int l = 0; if (!label) { xfree(label); label = (char *)calloc(PRINT_WIDTH + 1, 1); } if (!labellong) { xfree(labellong); labellong = (char *)calloc(PRINT_WIDTH + 1 + 9, 1); } if (options.paused ) { return; } /* Headings */ snprintf(label, PRINT_WIDTH, "%-*s", PRINT_WIDTH, "Host name (port/service if enabled)"); printf("%s %s %10s %10s %10s %10s\n", " #", label, "last 2s", "last 10s", "last 40s", "cumulative"); /* Divider line */ for (j = 0; j < PRINT_WIDTH + 52; j++) { printf("-"); } printf("\n"); /* Traverse the list of all connections */ while((nn = sorted_list_next_item(&screen_list, nn)) != NULL && l < options.num_lines) { /* Increment the line counter */ l++; /* Get the connection information */ host_pair_line* screen_line = (host_pair_line*)nn->data; /* Assemble host information */ sprint_host(host1, screen_line->ap.af, &(screen_line->ap.src6), screen_line->ap.src_port, screen_line->ap.protocol, PRINT_WIDTH, options.aggregate_src); sprint_host(host2, screen_line->ap.af, &(screen_line->ap.dst6), screen_line->ap.dst_port, screen_line->ap.protocol, PRINT_WIDTH, options.aggregate_dest); /* Send rate per connection */ printf("%4d %s%s", l, host1, " =>"); for(j = 0; j < HISTORY_DIVISIONS; j++) { readable_size(screen_line->sent[j], buf0_10, 10, 1024, options.bandwidth_in_bytes); printf(" %10s", buf0_10); } /* Cumulative sent data per connection */ readable_size(screen_line->total_sent, buf0_10, 10, 1024, 1); printf(" %10s\n", buf0_10); /* Receive rate per connection */ printf(" %s%s", host2, " <="); for(j = 0; j < HISTORY_DIVISIONS; j++) { readable_size(screen_line->recv[j], buf0_10, 10, 1024, options.bandwidth_in_bytes); printf(" %10s", buf0_10); } /* Cumulative received data per connection */ readable_size(screen_line->total_recv, buf0_10, 10, 1024, 1); printf(" %10s\n", buf0_10); } /* Divider line */ for (j = 0; j < PRINT_WIDTH + 52; j++) { printf("-"); } printf("\n"); /* Rate totals */ snprintf(labellong, PRINT_WIDTH + 9, "%-*s", PRINT_WIDTH + 9, "Total send rate:"); printf("%s ", labellong); for(j = 0; j < HISTORY_DIVISIONS; j++) { readable_size((((host_pair_line *)&totals)->sent[j]) , buf0_10, 10, 1024, options.bandwidth_in_bytes); printf("%10s%c", buf0_10, j == HISTORY_DIVISIONS - 1 ? '\n' : ' '); } snprintf(labellong, PRINT_WIDTH + 9, "%-*s", PRINT_WIDTH + 9, "Total receive rate:"); printf("%s ", labellong); for(j = 0; j < HISTORY_DIVISIONS; j++) { readable_size((((host_pair_line *)&totals)->recv[j]) , buf0_10, 10, 1024, options.bandwidth_in_bytes); printf("%10s%c", buf0_10, j == HISTORY_DIVISIONS - 1 ? '\n' : ' '); } snprintf(labellong, PRINT_WIDTH + 9, "%-*s", PRINT_WIDTH + 9, "Total send and receive rate:"); printf("%s ", labellong); for(j = 0; j < HISTORY_DIVISIONS; j++) { readable_size((((host_pair_line *)&totals)->sent[j] + ((host_pair_line *)&totals)->recv[j]) , buf0_10, 10, 1024, options.bandwidth_in_bytes); printf("%10s%c", buf0_10, j == HISTORY_DIVISIONS - 1 ? '\n' : ' '); } /* Divider line */ for (j = 0; j < PRINT_WIDTH + 52; j++) { printf("-"); } printf("\n"); /* Peak traffic */ snprintf(labellong, PRINT_WIDTH + 9, "%-*s", PRINT_WIDTH + 9, "Peak rate (sent/received/total):"); readable_size(peaksent / RESOLUTION, buf0_10, 10, 1024, options.bandwidth_in_bytes); readable_size(peakrecv / RESOLUTION, buf1_10, 10, 1024, options.bandwidth_in_bytes); readable_size(peaktotal / RESOLUTION, buf2_10, 10, 1024, options.bandwidth_in_bytes); printf("%s %10s %10s %10s\n", labellong, buf0_10, buf1_10, buf2_10); /* Cumulative totals */ snprintf(labellong, PRINT_WIDTH + 9, "%-*s", PRINT_WIDTH + 9, "Cumulative (sent/received/total):"); readable_size(history_totals.total_sent, buf0_10, 10, 1024, 1); readable_size(history_totals.total_recv, buf1_10, 10, 1024, 1); readable_size(history_totals.total_recv + history_totals.total_sent, buf2_10, 10, 1024, 1); printf("%s %10s %10s %10s\n", labellong, buf0_10, buf1_10, buf2_10); /* Double divider line */ for (j = 0; j < PRINT_WIDTH + 52; j++) { printf("="); } printf("\n\n"); }