filedup.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "win32/fspr_arch_file_io.h"
  17. #include "fspr_file_io.h"
  18. #include "fspr_general.h"
  19. #include "fspr_strings.h"
  20. #include <string.h>
  21. #include "fspr_arch_inherit.h"
  22. APR_DECLARE(fspr_status_t) fspr_file_dup(fspr_file_t **new_file,
  23. fspr_file_t *old_file, fspr_pool_t *p)
  24. {
  25. #ifdef _WIN32_WCE
  26. return APR_ENOTIMPL;
  27. #else
  28. HANDLE hproc = GetCurrentProcess();
  29. HANDLE newhand = NULL;
  30. if (!DuplicateHandle(hproc, old_file->filehand,
  31. hproc, &newhand, 0, FALSE,
  32. DUPLICATE_SAME_ACCESS)) {
  33. return fspr_get_os_error();
  34. }
  35. (*new_file) = (fspr_file_t *) fspr_pcalloc(p, sizeof(fspr_file_t));
  36. (*new_file)->filehand = newhand;
  37. (*new_file)->flags = old_file->flags & ~APR_INHERIT;
  38. (*new_file)->pool = p;
  39. (*new_file)->fname = fspr_pstrdup(p, old_file->fname);
  40. (*new_file)->append = old_file->append;
  41. (*new_file)->buffered = FALSE;
  42. (*new_file)->ungetchar = old_file->ungetchar;
  43. #if APR_HAS_THREADS
  44. if (old_file->mutex) {
  45. fspr_thread_mutex_create(&((*new_file)->mutex),
  46. APR_THREAD_MUTEX_DEFAULT, p);
  47. }
  48. #endif
  49. fspr_pool_cleanup_register((*new_file)->pool, (void *)(*new_file), file_cleanup,
  50. fspr_pool_cleanup_null);
  51. /* Create a pollset with room for one descriptor. */
  52. /* ### check return codes */
  53. (void) fspr_pollset_create(&(*new_file)->pollset, 1, p, 0);
  54. return APR_SUCCESS;
  55. #endif /* !defined(_WIN32_WCE) */
  56. }
  57. #define stdin_handle 0x01
  58. #define stdout_handle 0x02
  59. #define stderr_handle 0x04
  60. APR_DECLARE(fspr_status_t) fspr_file_dup2(fspr_file_t *new_file,
  61. fspr_file_t *old_file, fspr_pool_t *p)
  62. {
  63. #ifdef _WIN32_WCE
  64. return APR_ENOTIMPL;
  65. #else
  66. DWORD stdhandle = 0;
  67. HANDLE hproc = GetCurrentProcess();
  68. HANDLE newhand = NULL;
  69. fspr_int32_t newflags;
  70. /* dup2 is not supported literaly with native Windows handles.
  71. * We can, however, emulate dup2 for the standard i/o handles,
  72. * and close and replace other handles with duped handles.
  73. * The os_handle will change, however.
  74. */
  75. if (new_file->filehand == GetStdHandle(STD_ERROR_HANDLE)) {
  76. stdhandle |= stderr_handle;
  77. }
  78. if (new_file->filehand == GetStdHandle(STD_OUTPUT_HANDLE)) {
  79. stdhandle |= stdout_handle;
  80. }
  81. if (new_file->filehand == GetStdHandle(STD_INPUT_HANDLE)) {
  82. stdhandle |= stdin_handle;
  83. }
  84. if (stdhandle) {
  85. if (!DuplicateHandle(hproc, old_file->filehand,
  86. hproc, &newhand, 0,
  87. TRUE, DUPLICATE_SAME_ACCESS)) {
  88. return fspr_get_os_error();
  89. }
  90. if (((stdhandle & stderr_handle) && !SetStdHandle(STD_ERROR_HANDLE, newhand)) ||
  91. ((stdhandle & stdout_handle) && !SetStdHandle(STD_OUTPUT_HANDLE, newhand)) ||
  92. ((stdhandle & stdin_handle) && !SetStdHandle(STD_INPUT_HANDLE, newhand))) {
  93. return fspr_get_os_error();
  94. }
  95. newflags = old_file->flags | APR_INHERIT;
  96. }
  97. else {
  98. if (!DuplicateHandle(hproc, old_file->filehand,
  99. hproc, &newhand, 0,
  100. FALSE, DUPLICATE_SAME_ACCESS)) {
  101. return fspr_get_os_error();
  102. }
  103. newflags = old_file->flags & ~APR_INHERIT;
  104. }
  105. if (new_file->filehand && (new_file->filehand != INVALID_HANDLE_VALUE)) {
  106. CloseHandle(new_file->filehand);
  107. }
  108. new_file->flags = newflags;
  109. new_file->filehand = newhand;
  110. new_file->fname = fspr_pstrdup(new_file->pool, old_file->fname);
  111. new_file->append = old_file->append;
  112. new_file->buffered = FALSE;
  113. new_file->ungetchar = old_file->ungetchar;
  114. #if APR_HAS_THREADS
  115. if (old_file->mutex) {
  116. fspr_thread_mutex_create(&(new_file->mutex),
  117. APR_THREAD_MUTEX_DEFAULT, p);
  118. }
  119. #endif
  120. return APR_SUCCESS;
  121. #endif /* !defined(_WIN32_WCE) */
  122. }
  123. APR_DECLARE(fspr_status_t) fspr_file_setaside(fspr_file_t **new_file,
  124. fspr_file_t *old_file,
  125. fspr_pool_t *p)
  126. {
  127. *new_file = (fspr_file_t *)fspr_palloc(p, sizeof(fspr_file_t));
  128. memcpy(*new_file, old_file, sizeof(fspr_file_t));
  129. (*new_file)->pool = p;
  130. if (old_file->buffered) {
  131. (*new_file)->buffer = fspr_palloc(p, APR_FILE_BUFSIZE);
  132. if (old_file->direction == 1) {
  133. memcpy((*new_file)->buffer, old_file->buffer, old_file->bufpos);
  134. }
  135. else {
  136. memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
  137. }
  138. }
  139. if (old_file->mutex) {
  140. fspr_thread_mutex_create(&((*new_file)->mutex),
  141. APR_THREAD_MUTEX_DEFAULT, p);
  142. fspr_thread_mutex_destroy(old_file->mutex);
  143. }
  144. if (old_file->fname) {
  145. (*new_file)->fname = fspr_pstrdup(p, old_file->fname);
  146. }
  147. if (!(old_file->flags & APR_FILE_NOCLEANUP)) {
  148. fspr_pool_cleanup_register(p, (void *)(*new_file),
  149. file_cleanup,
  150. file_cleanup);
  151. }
  152. old_file->filehand = INVALID_HANDLE_VALUE;
  153. fspr_pool_cleanup_kill(old_file->pool, (void *)old_file,
  154. file_cleanup);
  155. /* Create a pollset with room for one descriptor. */
  156. /* ### check return codes */
  157. (void) fspr_pollset_create(&(*new_file)->pollset, 1, p, 0);
  158. return APR_SUCCESS;
  159. }