fullrw.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr_file_io.h"
  17. APR_DECLARE(apr_status_t) apr_file_read_full(apr_file_t *thefile, void *buf,
  18. apr_size_t nbytes,
  19. apr_size_t *bytes_read)
  20. {
  21. apr_status_t status;
  22. apr_size_t total_read = 0;
  23. do {
  24. apr_size_t amt = nbytes;
  25. status = apr_file_read(thefile, buf, &amt);
  26. buf = (char *)buf + amt;
  27. nbytes -= amt;
  28. total_read += amt;
  29. } while (status == APR_SUCCESS && nbytes > 0);
  30. if (bytes_read != NULL)
  31. *bytes_read = total_read;
  32. return status;
  33. }
  34. APR_DECLARE(apr_status_t) apr_file_write_full(apr_file_t *thefile,
  35. const void *buf,
  36. apr_size_t nbytes,
  37. apr_size_t *bytes_written)
  38. {
  39. apr_status_t status;
  40. apr_size_t total_written = 0;
  41. do {
  42. apr_size_t amt = nbytes;
  43. status = apr_file_write(thefile, buf, &amt);
  44. buf = (char *)buf + amt;
  45. nbytes -= amt;
  46. total_written += amt;
  47. } while (status == APR_SUCCESS && nbytes > 0);
  48. if (bytes_written != NULL)
  49. *bytes_written = total_written;
  50. return status;
  51. }
  52. APR_DECLARE(apr_status_t) apr_file_writev_full(apr_file_t *thefile,
  53. const struct iovec *vec,
  54. apr_size_t nvec,
  55. apr_size_t *bytes_written)
  56. {
  57. apr_status_t rv = APR_SUCCESS;
  58. apr_size_t i;
  59. apr_size_t amt = 0;
  60. apr_size_t total = 0;
  61. for (i = 0; i < nvec; i++) {
  62. total += vec[i].iov_len;
  63. }
  64. rv = apr_file_writev(thefile, vec, nvec, &amt);
  65. if (bytes_written != NULL)
  66. *bytes_written = amt;
  67. if (rv != APR_SUCCESS || (amt == total)) {
  68. return rv;
  69. }
  70. for (i = 0; i < nvec && amt; i++) {
  71. if (amt >= vec[i].iov_len) {
  72. amt -= vec[i].iov_len;
  73. }
  74. else {
  75. break;
  76. }
  77. }
  78. if (amt) {
  79. rv = apr_file_write_full(thefile, (const char *)vec[i].iov_base + amt,
  80. vec[i].iov_len - amt, NULL);
  81. }
  82. for (; i < nvec && rv == APR_SUCCESS; i++) {
  83. rv = apr_file_write_full(thefile, vec[i].iov_base,
  84. vec[i].iov_len, &amt);
  85. }
  86. if (bytes_written != NULL)
  87. *bytes_written = total;
  88. return rv;
  89. }