aos_buf.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "aos_buf.h"
  2. #include "aos_log.h"
  3. #include <apr_file_io.h>
  4. aos_buf_t *aos_create_buf(aos_pool_t *p, int size)
  5. {
  6. aos_buf_t* b;
  7. b = aos_palloc(p, sizeof(aos_buf_t) + size);
  8. if (b == NULL) {
  9. return NULL;
  10. }
  11. b->pos = (uint8_t *)b + sizeof(aos_buf_t);
  12. b->start = b->pos;
  13. b->last = b->start;
  14. b->end = b->last + size;
  15. aos_list_init(&b->node);
  16. return b;
  17. }
  18. aos_buf_t *aos_buf_pack(aos_pool_t *p, const void *data, int size)
  19. {
  20. aos_buf_t* b;
  21. b = aos_palloc(p, sizeof(aos_buf_t));
  22. if (b == NULL) {
  23. return NULL;
  24. }
  25. b->pos = (uint8_t *)data;
  26. b->start = b->pos;
  27. b->last = b->start + size;
  28. b->end = b->last;
  29. aos_list_init(&b->node);
  30. return b;
  31. }
  32. int64_t aos_buf_list_len(aos_list_t *list)
  33. {
  34. aos_buf_t *b;
  35. int64_t len = 0;
  36. aos_list_for_each_entry(aos_buf_t, b, list, node) {
  37. len += aos_buf_size(b);
  38. }
  39. return len;
  40. }
  41. char *aos_buf_list_content(aos_pool_t *p, aos_list_t *list)
  42. {
  43. int64_t body_len;
  44. char *buf;
  45. int64_t pos = 0;
  46. int64_t size = 0;
  47. aos_buf_t *content;
  48. body_len = aos_buf_list_len(list);
  49. buf = aos_pcalloc(p, (size_t)(body_len + 1));
  50. buf[body_len] = '\0';
  51. aos_list_for_each_entry(aos_buf_t, content, list, node) {
  52. size = aos_buf_size(content);
  53. memcpy(buf + pos, content->pos, (size_t)(size));
  54. pos += size;
  55. }
  56. return buf;
  57. }
  58. aos_file_buf_t *aos_create_file_buf(aos_pool_t *p)
  59. {
  60. return (aos_file_buf_t*)aos_pcalloc(p, sizeof(aos_file_buf_t));
  61. }
  62. int aos_open_file_for_read(aos_pool_t *p, const char *path, aos_file_buf_t *fb)
  63. {
  64. int s;
  65. char buf[256];
  66. apr_finfo_t finfo;
  67. if ((s = apr_file_open(&fb->file, path, APR_READ, APR_UREAD | APR_GREAD, p)) != APR_SUCCESS) {
  68. aos_error_log("apr_file_open failure, code:%d %s.", s, apr_strerror(s, buf, sizeof(buf)));
  69. assert(fb->file == NULL);
  70. return AOSE_OPEN_FILE_ERROR;
  71. }
  72. if ((s = apr_file_info_get(&finfo, APR_FINFO_NORM, fb->file)) != APR_SUCCESS) {
  73. apr_file_close(fb->file);
  74. aos_error_log("apr_file_open failure, code:%d %s.", s, apr_strerror(s, buf, sizeof(buf)));
  75. return AOSE_FILE_INFO_ERROR;
  76. }
  77. fb->file_pos = 0;
  78. fb->file_last = finfo.size;
  79. fb->owner = 1;
  80. return AOSE_OK;
  81. }
  82. int aos_open_file_for_all_read(aos_pool_t *p, const char *path, aos_file_buf_t *fb)
  83. {
  84. return aos_open_file_for_read(p, path, fb);
  85. }
  86. int aos_open_file_for_range_read(aos_pool_t *p, const char *path,
  87. int64_t file_pos, int64_t file_last, aos_file_buf_t *fb)
  88. {
  89. int s;
  90. s = aos_open_file_for_read(p, path, fb);
  91. if (s == AOSE_OK) {
  92. if (file_pos > fb->file_pos) {
  93. if (file_pos > fb->file_last) {
  94. aos_warn_log("read range beyond file size, read start:%" APR_INT64_T_FMT ", file size:%" APR_INT64_T_FMT "\n",
  95. file_pos, fb->file_last);
  96. file_pos = fb->file_last;
  97. }
  98. fb->file_pos = file_pos;
  99. }
  100. if (file_last < fb->file_last) {
  101. fb->file_last = file_last;
  102. }
  103. apr_file_seek(fb->file, APR_SET, (apr_off_t *)&fb->file_pos);
  104. }
  105. return s;
  106. }
  107. int aos_open_file_for_write(aos_pool_t *p, const char *path, aos_file_buf_t *fb)
  108. {
  109. int s;
  110. char buf[256];
  111. if ((s = apr_file_open(&fb->file, path, APR_CREATE | APR_WRITE | APR_TRUNCATE,
  112. APR_UREAD | APR_UWRITE | APR_GREAD, p)) != APR_SUCCESS) {
  113. aos_error_log("apr_file_open failure, code:%d %s.", s, apr_strerror(s, buf, sizeof(buf)));
  114. assert(fb->file == NULL);
  115. return AOSE_OPEN_FILE_ERROR;
  116. }
  117. fb->owner = 1;
  118. return AOSE_OK;
  119. }
  120. void aos_buf_append_string(aos_pool_t *p, aos_buf_t *b, const char *str, int len)
  121. {
  122. int size;
  123. int nsize;
  124. int remain;
  125. char *buf;
  126. if (len <= 0) return;
  127. remain = b->end - b->last;
  128. if (remain > len + 128) {
  129. memcpy(b->last, str, len);
  130. b->last += len;
  131. } else {
  132. size = aos_buf_size(b);
  133. nsize = (size + len) * 2;
  134. buf = aos_palloc(p, nsize);
  135. memcpy(buf, b->pos, size);
  136. memcpy(buf+size, str, len);
  137. b->start = (uint8_t *)buf;
  138. b->end = (uint8_t *)buf + nsize;
  139. b->pos = (uint8_t *)buf;
  140. b->last = (uint8_t *)buf + size + len;
  141. }
  142. }