dir.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "fspr_portable.h"
  21. #include <string.h>
  22. static fspr_status_t dir_cleanup(void *thedir)
  23. {
  24. fspr_dir_t *dir = thedir;
  25. return fspr_dir_close(dir);
  26. }
  27. APR_DECLARE(fspr_status_t) fspr_dir_open(fspr_dir_t **new, const char *dirname, fspr_pool_t *pool)
  28. {
  29. fspr_dir_t *thedir = (fspr_dir_t *)fspr_palloc(pool, sizeof(fspr_dir_t));
  30. if (thedir == NULL)
  31. return APR_ENOMEM;
  32. thedir->pool = pool;
  33. thedir->dirname = fspr_pstrdup(pool, dirname);
  34. if (thedir->dirname == NULL)
  35. return APR_ENOMEM;
  36. thedir->handle = 0;
  37. thedir->validentry = FALSE;
  38. *new = thedir;
  39. fspr_pool_cleanup_register(pool, thedir, dir_cleanup, fspr_pool_cleanup_null);
  40. return APR_SUCCESS;
  41. }
  42. APR_DECLARE(fspr_status_t) fspr_dir_close(fspr_dir_t *thedir)
  43. {
  44. int rv = 0;
  45. if (thedir->handle) {
  46. rv = DosFindClose(thedir->handle);
  47. if (rv == 0) {
  48. thedir->handle = 0;
  49. }
  50. }
  51. return APR_FROM_OS_ERROR(rv);
  52. }
  53. APR_DECLARE(fspr_status_t) fspr_dir_read(fspr_finfo_t *finfo, fspr_int32_t wanted,
  54. fspr_dir_t *thedir)
  55. {
  56. int rv;
  57. ULONG entries = 1;
  58. if (thedir->handle == 0) {
  59. thedir->handle = HDIR_CREATE;
  60. rv = DosFindFirst(fspr_pstrcat(thedir->pool, thedir->dirname, "/*", NULL), &thedir->handle,
  61. FILE_ARCHIVED|FILE_DIRECTORY|FILE_SYSTEM|FILE_HIDDEN|FILE_READONLY,
  62. &thedir->entry, sizeof(thedir->entry), &entries, FIL_STANDARD);
  63. } else {
  64. rv = DosFindNext(thedir->handle, &thedir->entry, sizeof(thedir->entry), &entries);
  65. }
  66. finfo->pool = thedir->pool;
  67. finfo->fname = NULL;
  68. finfo->valid = 0;
  69. if (rv == 0 && entries == 1) {
  70. thedir->validentry = TRUE;
  71. /* We passed a name off the stack that has popped */
  72. finfo->fname = NULL;
  73. finfo->size = thedir->entry.cbFile;
  74. finfo->csize = thedir->entry.cbFileAlloc;
  75. /* Only directories & regular files show up in directory listings */
  76. finfo->filetype = (thedir->entry.attrFile & FILE_DIRECTORY) ? APR_DIR : APR_REG;
  77. fspr_os2_time_to_fspr_time(&finfo->mtime, thedir->entry.fdateLastWrite,
  78. thedir->entry.ftimeLastWrite);
  79. fspr_os2_time_to_fspr_time(&finfo->atime, thedir->entry.fdateLastAccess,
  80. thedir->entry.ftimeLastAccess);
  81. fspr_os2_time_to_fspr_time(&finfo->ctime, thedir->entry.fdateCreation,
  82. thedir->entry.ftimeCreation);
  83. finfo->name = thedir->entry.achName;
  84. finfo->valid = APR_FINFO_NAME | APR_FINFO_MTIME | APR_FINFO_ATIME |
  85. APR_FINFO_CTIME | APR_FINFO_TYPE | APR_FINFO_SIZE |
  86. APR_FINFO_CSIZE;
  87. return APR_SUCCESS;
  88. }
  89. thedir->validentry = FALSE;
  90. if (rv)
  91. return APR_FROM_OS_ERROR(rv);
  92. return APR_ENOENT;
  93. }
  94. APR_DECLARE(fspr_status_t) fspr_dir_rewind(fspr_dir_t *thedir)
  95. {
  96. return fspr_dir_close(thedir);
  97. }
  98. APR_DECLARE(fspr_status_t) fspr_dir_make(const char *path, fspr_fileperms_t perm, fspr_pool_t *pool)
  99. {
  100. return APR_FROM_OS_ERROR(DosCreateDir(path, NULL));
  101. }
  102. APR_DECLARE(fspr_status_t) fspr_dir_remove(const char *path, fspr_pool_t *pool)
  103. {
  104. return APR_FROM_OS_ERROR(DosDeleteDir(path));
  105. }
  106. APR_DECLARE(fspr_status_t) fspr_os_dir_get(fspr_os_dir_t **thedir, fspr_dir_t *dir)
  107. {
  108. if (dir == NULL) {
  109. return APR_ENODIR;
  110. }
  111. *thedir = &dir->handle;
  112. return APR_SUCCESS;
  113. }
  114. APR_DECLARE(fspr_status_t) fspr_os_dir_put(fspr_dir_t **dir, fspr_os_dir_t *thedir,
  115. fspr_pool_t *pool)
  116. {
  117. if ((*dir) == NULL) {
  118. (*dir) = (fspr_dir_t *)fspr_pcalloc(pool, sizeof(fspr_dir_t));
  119. (*dir)->pool = pool;
  120. }
  121. (*dir)->handle = *thedir;
  122. return APR_SUCCESS;
  123. }