2
0

open.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_portable.h"
  20. #include "fspr_strings.h"
  21. #include "fspr_arch_inherit.h"
  22. #include <string.h>
  23. fspr_status_t fspr_file_cleanup(void *thefile)
  24. {
  25. fspr_file_t *file = thefile;
  26. return fspr_file_close(file);
  27. }
  28. APR_DECLARE(fspr_status_t) fspr_file_open(fspr_file_t **new, const char *fname, fspr_int32_t flag, fspr_fileperms_t perm, fspr_pool_t *pool)
  29. {
  30. int oflags = 0;
  31. int mflags = OPEN_FLAGS_FAIL_ON_ERROR|OPEN_SHARE_DENYNONE;
  32. int rv;
  33. ULONG action;
  34. fspr_file_t *dafile = (fspr_file_t *)fspr_palloc(pool, sizeof(fspr_file_t));
  35. dafile->pool = pool;
  36. dafile->isopen = FALSE;
  37. dafile->eof_hit = FALSE;
  38. dafile->buffer = NULL;
  39. dafile->flags = flag;
  40. dafile->blocking = BLK_ON;
  41. if ((flag & APR_READ) && (flag & APR_WRITE)) {
  42. mflags |= OPEN_ACCESS_READWRITE;
  43. } else if (flag & APR_READ) {
  44. mflags |= OPEN_ACCESS_READONLY;
  45. } else if (flag & APR_WRITE) {
  46. mflags |= OPEN_ACCESS_WRITEONLY;
  47. } else {
  48. dafile->filedes = -1;
  49. return APR_EACCES;
  50. }
  51. dafile->buffered = (flag & APR_BUFFERED) > 0;
  52. if (dafile->buffered) {
  53. dafile->buffer = fspr_palloc(pool, APR_FILE_BUFSIZE);
  54. rv = fspr_thread_mutex_create(&dafile->mutex, 0, pool);
  55. if (rv)
  56. return rv;
  57. }
  58. if (flag & APR_CREATE) {
  59. oflags |= OPEN_ACTION_CREATE_IF_NEW;
  60. if (!(flag & APR_EXCL) && !(flag & APR_TRUNCATE)) {
  61. oflags |= OPEN_ACTION_OPEN_IF_EXISTS;
  62. }
  63. }
  64. if ((flag & APR_EXCL) && !(flag & APR_CREATE))
  65. return APR_EACCES;
  66. if (flag & APR_TRUNCATE) {
  67. oflags |= OPEN_ACTION_REPLACE_IF_EXISTS;
  68. } else if ((oflags & 0xFF) == 0) {
  69. oflags |= OPEN_ACTION_OPEN_IF_EXISTS;
  70. }
  71. rv = DosOpen(fname, &(dafile->filedes), &action, 0, 0, oflags, mflags, NULL);
  72. if (rv == 0 && (flag & APR_APPEND)) {
  73. ULONG newptr;
  74. rv = DosSetFilePtr(dafile->filedes, 0, FILE_END, &newptr );
  75. if (rv)
  76. DosClose(dafile->filedes);
  77. }
  78. if (rv != 0)
  79. return APR_FROM_OS_ERROR(rv);
  80. dafile->isopen = TRUE;
  81. dafile->fname = fspr_pstrdup(pool, fname);
  82. dafile->filePtr = 0;
  83. dafile->bufpos = 0;
  84. dafile->dataRead = 0;
  85. dafile->direction = 0;
  86. dafile->pipe = FALSE;
  87. if (!(flag & APR_FILE_NOCLEANUP)) {
  88. fspr_pool_cleanup_register(dafile->pool, dafile, fspr_file_cleanup, fspr_file_cleanup);
  89. }
  90. *new = dafile;
  91. return APR_SUCCESS;
  92. }
  93. APR_DECLARE(fspr_status_t) fspr_file_close(fspr_file_t *file)
  94. {
  95. ULONG rc;
  96. fspr_status_t status;
  97. if (file && file->isopen) {
  98. fspr_file_flush(file);
  99. rc = DosClose(file->filedes);
  100. if (rc == 0) {
  101. file->isopen = FALSE;
  102. status = APR_SUCCESS;
  103. if (file->flags & APR_DELONCLOSE) {
  104. status = APR_FROM_OS_ERROR(DosDelete(file->fname));
  105. }
  106. } else {
  107. return APR_FROM_OS_ERROR(rc);
  108. }
  109. }
  110. if (file->buffered)
  111. fspr_thread_mutex_destroy(file->mutex);
  112. return APR_SUCCESS;
  113. }
  114. APR_DECLARE(fspr_status_t) fspr_file_remove(const char *path, fspr_pool_t *pool)
  115. {
  116. ULONG rc = DosDelete(path);
  117. return APR_FROM_OS_ERROR(rc);
  118. }
  119. APR_DECLARE(fspr_status_t) fspr_file_rename(const char *from_path, const char *to_path,
  120. fspr_pool_t *p)
  121. {
  122. ULONG rc = DosMove(from_path, to_path);
  123. if (rc == ERROR_ACCESS_DENIED || rc == ERROR_ALREADY_EXISTS) {
  124. rc = DosDelete(to_path);
  125. if (rc == 0 || rc == ERROR_FILE_NOT_FOUND) {
  126. rc = DosMove(from_path, to_path);
  127. }
  128. }
  129. return APR_FROM_OS_ERROR(rc);
  130. }
  131. APR_DECLARE(fspr_status_t) fspr_os_file_get(fspr_os_file_t *thefile, fspr_file_t *file)
  132. {
  133. *thefile = file->filedes;
  134. return APR_SUCCESS;
  135. }
  136. APR_DECLARE(fspr_status_t) fspr_os_file_put(fspr_file_t **file, fspr_os_file_t *thefile, fspr_int32_t flags, fspr_pool_t *pool)
  137. {
  138. fspr_os_file_t *dafile = thefile;
  139. (*file) = fspr_palloc(pool, sizeof(fspr_file_t));
  140. (*file)->pool = pool;
  141. (*file)->filedes = *dafile;
  142. (*file)->isopen = TRUE;
  143. (*file)->eof_hit = FALSE;
  144. (*file)->flags = flags;
  145. (*file)->pipe = FALSE;
  146. (*file)->buffered = (flags & APR_BUFFERED) > 0;
  147. if ((*file)->buffered) {
  148. fspr_status_t rv;
  149. (*file)->buffer = fspr_palloc(pool, APR_FILE_BUFSIZE);
  150. rv = fspr_thread_mutex_create(&(*file)->mutex, 0, pool);
  151. if (rv)
  152. return rv;
  153. }
  154. return APR_SUCCESS;
  155. }
  156. APR_DECLARE(fspr_status_t) fspr_file_eof(fspr_file_t *fptr)
  157. {
  158. if (!fptr->isopen || fptr->eof_hit == 1) {
  159. return APR_EOF;
  160. }
  161. return APR_SUCCESS;
  162. }
  163. APR_DECLARE(fspr_status_t) fspr_file_open_stderr(fspr_file_t **thefile, fspr_pool_t *pool)
  164. {
  165. fspr_os_file_t fd = 2;
  166. return fspr_os_file_put(thefile, &fd, 0, pool);
  167. }
  168. APR_DECLARE(fspr_status_t) fspr_file_open_stdout(fspr_file_t **thefile, fspr_pool_t *pool)
  169. {
  170. fspr_os_file_t fd = 1;
  171. return fspr_os_file_put(thefile, &fd, 0, pool);
  172. }
  173. APR_DECLARE(fspr_status_t) fspr_file_open_stdin(fspr_file_t **thefile, fspr_pool_t *pool)
  174. {
  175. fspr_os_file_t fd = 0;
  176. return fspr_os_file_put(thefile, &fd, 0, pool);
  177. }
  178. APR_POOL_IMPLEMENT_ACCESSOR(file);
  179. APR_IMPLEMENT_INHERIT_SET(file, flags, pool, fspr_file_cleanup)
  180. APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, fspr_file_cleanup)