oss_sample_util.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <sys/stat.h>
  2. #include <stdlib.h>
  3. #include "oss_config.h"
  4. #include "oss_api.h"
  5. #include "oss_sample_util.h"
  6. void make_rand_string(aos_pool_t *p, int len, aos_string_t *data)
  7. {
  8. char *str = NULL;
  9. int i = 0;
  10. str = (char *)aos_palloc(p, len + 1);
  11. for ( ; i < len; i++) {
  12. str[i] = rand() % 128;
  13. }
  14. str[len] = '\0';
  15. aos_str_set(data, str);
  16. }
  17. aos_buf_t *make_random_buf(aos_pool_t *p, int len)
  18. {
  19. int bytes;
  20. aos_buf_t *b;
  21. aos_string_t str;
  22. make_rand_string(p, 16, &str);
  23. b = aos_create_buf(p, len);
  24. while (b->last < b->end) {
  25. bytes = b->end - b->last;
  26. bytes = aos_min(bytes, 16);
  27. memcpy(b->last, str.data, bytes);
  28. b->last += bytes;
  29. }
  30. return b;
  31. }
  32. void make_random_body(aos_pool_t *p, int count, aos_list_t *bc)
  33. {
  34. int i = 0;
  35. int len;
  36. aos_buf_t *b;
  37. srand((int)time(0));
  38. for (; i < count; ++i) {
  39. len = 1 + (int)(4096.0*rand() / (RAND_MAX+1.0));
  40. b = make_random_buf(p, len);
  41. aos_list_add_tail(&b->node, bc);
  42. }
  43. }
  44. void init_sample_config(oss_config_t *config, int is_cname)
  45. {
  46. aos_str_set(&config->endpoint, OSS_ENDPOINT);
  47. aos_str_set(&config->access_key_id, ACCESS_KEY_ID);
  48. aos_str_set(&config->access_key_secret, ACCESS_KEY_SECRET);
  49. config->is_cname = is_cname;
  50. }
  51. void init_sample_request_options(oss_request_options_t *options, int is_cname)
  52. {
  53. options->config = oss_config_create(options->pool);
  54. init_sample_config(options->config, is_cname);
  55. options->ctl = aos_http_controller_create(options->pool, 0);
  56. }
  57. int64_t get_file_size(const char *file_path)
  58. {
  59. int64_t filesize = -1;
  60. struct stat statbuff;
  61. if(stat(file_path, &statbuff) < 0){
  62. return filesize;
  63. } else {
  64. filesize = statbuff.st_size;
  65. }
  66. return filesize;
  67. }
  68. void percentage(int64_t consumed_bytes, int64_t total_bytes)
  69. {
  70. assert(total_bytes >= consumed_bytes);
  71. printf("%%%" APR_INT64_T_FMT "\n", consumed_bytes * 100 / total_bytes);
  72. }