buffer.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_arch_file_io.h"
  17. #include "apr_thread_mutex.h"
  18. APR_DECLARE(apr_status_t) apr_file_buffer_set(apr_file_t *file,
  19. char * buffer,
  20. apr_size_t bufsize)
  21. {
  22. apr_status_t rv;
  23. apr_thread_mutex_lock(file->mutex);
  24. if(file->buffered) {
  25. /* Flush the existing buffer */
  26. rv = apr_file_flush(file);
  27. if (rv != APR_SUCCESS) {
  28. apr_thread_mutex_unlock(file->mutex);
  29. return rv;
  30. }
  31. }
  32. file->buffer = buffer;
  33. file->bufsize = bufsize;
  34. file->buffered = 1;
  35. file->bufpos = 0;
  36. file->direction = 0;
  37. file->dataRead = 0;
  38. if (file->bufsize == 0) {
  39. /* Setting the buffer size to zero is equivalent to turning
  40. * buffering off.
  41. */
  42. file->buffered = 0;
  43. }
  44. apr_thread_mutex_unlock(file->mutex);
  45. return APR_SUCCESS;
  46. }
  47. APR_DECLARE(apr_size_t) apr_file_buffer_size_get(apr_file_t *file)
  48. {
  49. return file->bufsize;
  50. }