2
0

open.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_strings.h"
  18. #include "fspr_portable.h"
  19. #include "fspr_thread_mutex.h"
  20. #include "fspr_arch_inherit.h"
  21. #ifdef NETWARE
  22. #include "nks/dirio.h"
  23. #include "fspr_hash.h"
  24. #include "fsio.h"
  25. #endif
  26. fspr_status_t fspr_unix_file_cleanup(void *thefile)
  27. {
  28. fspr_file_t *file = thefile;
  29. fspr_status_t flush_rv = APR_SUCCESS, rv = APR_SUCCESS;
  30. if (file->buffered) {
  31. flush_rv = fspr_file_flush(file);
  32. }
  33. if (close(file->filedes) == 0) {
  34. file->filedes = -1;
  35. if (file->flags & APR_DELONCLOSE) {
  36. unlink(file->fname);
  37. }
  38. #if APR_HAS_THREADS
  39. if (file->thlock) {
  40. rv = fspr_thread_mutex_destroy(file->thlock);
  41. }
  42. #endif
  43. }
  44. else {
  45. /* Are there any error conditions other than EINTR or EBADF? */
  46. rv = errno;
  47. }
  48. #ifndef WAITIO_USES_POLL
  49. if (file->pollset != NULL) {
  50. int pollset_rv = fspr_pollset_destroy(file->pollset);
  51. /* If the file close failed, return its error value,
  52. * not fspr_pollset_destroy()'s.
  53. */
  54. if (rv == APR_SUCCESS) {
  55. rv = pollset_rv;
  56. }
  57. }
  58. #endif /* !WAITIO_USES_POLL */
  59. return rv != APR_SUCCESS ? rv : flush_rv;
  60. }
  61. APR_DECLARE(fspr_status_t) fspr_file_open(fspr_file_t **new,
  62. const char *fname,
  63. fspr_int32_t flag,
  64. fspr_fileperms_t perm,
  65. fspr_pool_t *pool)
  66. {
  67. fspr_os_file_t fd;
  68. int oflags = 0;
  69. #if APR_HAS_THREADS
  70. fspr_thread_mutex_t *thlock;
  71. fspr_status_t rv;
  72. #endif
  73. if ((flag & APR_READ) && (flag & APR_WRITE)) {
  74. oflags = O_RDWR;
  75. }
  76. else if (flag & APR_READ) {
  77. oflags = O_RDONLY;
  78. }
  79. else if (flag & APR_WRITE) {
  80. oflags = O_WRONLY;
  81. }
  82. else {
  83. return APR_EACCES;
  84. }
  85. if (flag & APR_CREATE) {
  86. oflags |= O_CREAT;
  87. if (flag & APR_EXCL) {
  88. oflags |= O_EXCL;
  89. }
  90. }
  91. if ((flag & APR_EXCL) && !(flag & APR_CREATE)) {
  92. return APR_EACCES;
  93. }
  94. if (flag & APR_APPEND) {
  95. oflags |= O_APPEND;
  96. }
  97. if (flag & APR_TRUNCATE) {
  98. oflags |= O_TRUNC;
  99. }
  100. #ifdef O_BINARY
  101. if (flag & APR_BINARY) {
  102. oflags |= O_BINARY;
  103. }
  104. #endif
  105. #if APR_HAS_LARGE_FILES && defined(_LARGEFILE64_SOURCE)
  106. oflags |= O_LARGEFILE;
  107. #elif defined(O_LARGEFILE)
  108. if (flag & APR_LARGEFILE) {
  109. oflags |= O_LARGEFILE;
  110. }
  111. #endif
  112. #if APR_HAS_THREADS
  113. if ((flag & APR_BUFFERED) && (flag & APR_XTHREAD)) {
  114. rv = fspr_thread_mutex_create(&thlock,
  115. APR_THREAD_MUTEX_DEFAULT, pool);
  116. if (rv) {
  117. return rv;
  118. }
  119. }
  120. #endif
  121. if (perm == APR_OS_DEFAULT) {
  122. fd = open(fname, oflags, 0666);
  123. }
  124. else {
  125. fd = open(fname, oflags, fspr_unix_perms2mode(perm));
  126. }
  127. if (fd < 0) {
  128. return errno;
  129. }
  130. (*new) = (fspr_file_t *)fspr_pcalloc(pool, sizeof(fspr_file_t));
  131. (*new)->pool = pool;
  132. (*new)->flags = flag;
  133. (*new)->filedes = fd;
  134. (*new)->fname = fspr_pstrdup(pool, fname);
  135. (*new)->blocking = BLK_ON;
  136. (*new)->buffered = (flag & APR_BUFFERED) > 0;
  137. if ((*new)->buffered) {
  138. (*new)->buffer = fspr_palloc(pool, APR_FILE_BUFSIZE);
  139. #if APR_HAS_THREADS
  140. if ((*new)->flags & APR_XTHREAD) {
  141. (*new)->thlock = thlock;
  142. }
  143. #endif
  144. }
  145. else {
  146. (*new)->buffer = NULL;
  147. }
  148. (*new)->is_pipe = 0;
  149. (*new)->timeout = -1;
  150. (*new)->ungetchar = -1;
  151. (*new)->eof_hit = 0;
  152. (*new)->filePtr = 0;
  153. (*new)->bufpos = 0;
  154. (*new)->dataRead = 0;
  155. (*new)->direction = 0;
  156. #ifndef WAITIO_USES_POLL
  157. /* Start out with no pollset. fspr_wait_for_io_or_timeout() will
  158. * initialize the pollset if needed.
  159. */
  160. (*new)->pollset = NULL;
  161. #endif
  162. if (!(flag & APR_FILE_NOCLEANUP)) {
  163. fspr_pool_cleanup_register((*new)->pool, (void *)(*new),
  164. fspr_unix_file_cleanup,
  165. fspr_unix_file_cleanup);
  166. }
  167. return APR_SUCCESS;
  168. }
  169. APR_DECLARE(fspr_status_t) fspr_file_close(fspr_file_t *file)
  170. {
  171. return fspr_pool_cleanup_run(file->pool, file, fspr_unix_file_cleanup);
  172. }
  173. APR_DECLARE(fspr_status_t) fspr_file_remove(const char *path, fspr_pool_t *pool)
  174. {
  175. if (unlink(path) == 0) {
  176. return APR_SUCCESS;
  177. }
  178. else {
  179. return errno;
  180. }
  181. }
  182. APR_DECLARE(fspr_status_t) fspr_file_rename(const char *from_path,
  183. const char *to_path,
  184. fspr_pool_t *p)
  185. {
  186. if (rename(from_path, to_path) != 0) {
  187. return errno;
  188. }
  189. return APR_SUCCESS;
  190. }
  191. APR_DECLARE(fspr_status_t) fspr_os_file_get(fspr_os_file_t *thefile,
  192. fspr_file_t *file)
  193. {
  194. *thefile = file->filedes;
  195. return APR_SUCCESS;
  196. }
  197. APR_DECLARE(fspr_status_t) fspr_os_file_put(fspr_file_t **file,
  198. fspr_os_file_t *thefile,
  199. fspr_int32_t flags, fspr_pool_t *pool)
  200. {
  201. int *dafile = thefile;
  202. (*file) = fspr_pcalloc(pool, sizeof(fspr_file_t));
  203. (*file)->pool = pool;
  204. (*file)->eof_hit = 0;
  205. (*file)->blocking = BLK_UNKNOWN; /* in case it is a pipe */
  206. (*file)->timeout = -1;
  207. (*file)->ungetchar = -1; /* no char avail */
  208. (*file)->filedes = *dafile;
  209. (*file)->flags = flags | APR_FILE_NOCLEANUP;
  210. (*file)->buffered = (flags & APR_BUFFERED) > 0;
  211. #ifndef WAITIO_USES_POLL
  212. /* Start out with no pollset. fspr_wait_for_io_or_timeout() will
  213. * initialize the pollset if needed.
  214. */
  215. (*file)->pollset = NULL;
  216. #endif
  217. if ((*file)->buffered) {
  218. (*file)->buffer = fspr_palloc(pool, APR_FILE_BUFSIZE);
  219. #if APR_HAS_THREADS
  220. if ((*file)->flags & APR_XTHREAD) {
  221. fspr_status_t rv;
  222. rv = fspr_thread_mutex_create(&((*file)->thlock),
  223. APR_THREAD_MUTEX_DEFAULT, pool);
  224. if (rv) {
  225. return rv;
  226. }
  227. }
  228. #endif
  229. }
  230. return APR_SUCCESS;
  231. }
  232. APR_DECLARE(fspr_status_t) fspr_file_eof(fspr_file_t *fptr)
  233. {
  234. if (fptr->eof_hit == 1) {
  235. return APR_EOF;
  236. }
  237. return APR_SUCCESS;
  238. }
  239. APR_DECLARE(fspr_status_t) fspr_file_open_stderr(fspr_file_t **thefile,
  240. fspr_pool_t *pool)
  241. {
  242. int fd = STDERR_FILENO;
  243. return fspr_os_file_put(thefile, &fd, 0, pool);
  244. }
  245. APR_DECLARE(fspr_status_t) fspr_file_open_stdout(fspr_file_t **thefile,
  246. fspr_pool_t *pool)
  247. {
  248. int fd = STDOUT_FILENO;
  249. return fspr_os_file_put(thefile, &fd, 0, pool);
  250. }
  251. APR_DECLARE(fspr_status_t) fspr_file_open_stdin(fspr_file_t **thefile,
  252. fspr_pool_t *pool)
  253. {
  254. int fd = STDIN_FILENO;
  255. return fspr_os_file_put(thefile, &fd, 0, pool);
  256. }
  257. APR_IMPLEMENT_INHERIT_SET(file, flags, pool, fspr_unix_file_cleanup)
  258. APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, fspr_unix_file_cleanup)
  259. APR_POOL_IMPLEMENT_ACCESSOR(file)