filedup.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "fspr_arch_file_io.h"
  17. #include "fspr_file_io.h"
  18. #include "fspr_lib.h"
  19. #include "fspr_strings.h"
  20. #include <string.h>
  21. #include "fspr_arch_inherit.h"
  22. static fspr_status_t file_dup(fspr_file_t **new_file, fspr_file_t *old_file, fspr_pool_t *p)
  23. {
  24. int rv;
  25. fspr_file_t *dup_file;
  26. if (*new_file == NULL) {
  27. dup_file = (fspr_file_t *)fspr_palloc(p, sizeof(fspr_file_t));
  28. if (dup_file == NULL) {
  29. return APR_ENOMEM;
  30. }
  31. dup_file->filedes = -1;
  32. } else {
  33. dup_file = *new_file;
  34. }
  35. dup_file->pool = p;
  36. rv = DosDupHandle(old_file->filedes, &dup_file->filedes);
  37. if (rv) {
  38. return APR_FROM_OS_ERROR(rv);
  39. }
  40. dup_file->fname = fspr_pstrdup(dup_file->pool, old_file->fname);
  41. dup_file->buffered = old_file->buffered;
  42. dup_file->isopen = old_file->isopen;
  43. dup_file->flags = old_file->flags & ~APR_INHERIT;
  44. /* TODO - dup pipes correctly */
  45. dup_file->pipe = old_file->pipe;
  46. if (*new_file == NULL) {
  47. fspr_pool_cleanup_register(dup_file->pool, dup_file, fspr_file_cleanup,
  48. fspr_pool_cleanup_null);
  49. *new_file = dup_file;
  50. }
  51. return APR_SUCCESS;
  52. }
  53. APR_DECLARE(fspr_status_t) fspr_file_dup(fspr_file_t **new_file, fspr_file_t *old_file, fspr_pool_t *p)
  54. {
  55. if (*new_file) {
  56. fspr_file_close(*new_file);
  57. (*new_file)->filedes = -1;
  58. }
  59. return file_dup(new_file, old_file, p);
  60. }
  61. APR_DECLARE(fspr_status_t) fspr_file_dup2(fspr_file_t *new_file, fspr_file_t *old_file, fspr_pool_t *p)
  62. {
  63. return file_dup(&new_file, old_file, p);
  64. }
  65. APR_DECLARE(fspr_status_t) fspr_file_setaside(fspr_file_t **new_file,
  66. fspr_file_t *old_file,
  67. fspr_pool_t *p)
  68. {
  69. *new_file = (fspr_file_t *)fspr_palloc(p, sizeof(fspr_file_t));
  70. memcpy(*new_file, old_file, sizeof(fspr_file_t));
  71. (*new_file)->pool = p;
  72. if (old_file->buffered) {
  73. (*new_file)->buffer = fspr_palloc(p, APR_FILE_BUFSIZE);
  74. if (old_file->direction == 1) {
  75. memcpy((*new_file)->buffer, old_file->buffer, old_file->bufpos);
  76. }
  77. else {
  78. memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
  79. }
  80. if (old_file->mutex) {
  81. fspr_thread_mutex_create(&((*new_file)->mutex),
  82. APR_THREAD_MUTEX_DEFAULT, p);
  83. fspr_thread_mutex_destroy(old_file->mutex);
  84. }
  85. }
  86. if (old_file->fname) {
  87. (*new_file)->fname = fspr_pstrdup(p, old_file->fname);
  88. }
  89. if (!(old_file->flags & APR_FILE_NOCLEANUP)) {
  90. fspr_pool_cleanup_register(p, (void *)(*new_file),
  91. fspr_file_cleanup,
  92. fspr_file_cleanup);
  93. }
  94. old_file->filedes = -1;
  95. fspr_pool_cleanup_kill(old_file->pool, (void *)old_file,
  96. fspr_file_cleanup);
  97. return APR_SUCCESS;
  98. }