2
0

filestat.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #define INCL_DOS
  17. #define INCL_DOSERRORS
  18. #include "fspr_arch_file_io.h"
  19. #include "fspr_file_io.h"
  20. #include "fspr_lib.h"
  21. #include <sys/time.h>
  22. #include "fspr_strings.h"
  23. static void FS3_to_finfo(fspr_finfo_t *finfo, FILESTATUS3 *fstatus)
  24. {
  25. finfo->protection = (fstatus->attrFile & FILE_READONLY) ? 0x555 : 0x777;
  26. if (fstatus->attrFile & FILE_DIRECTORY)
  27. finfo->filetype = APR_DIR;
  28. else
  29. finfo->filetype = APR_REG;
  30. /* XXX: No other possible types from FS3? */
  31. finfo->user = 0;
  32. finfo->group = 0;
  33. finfo->inode = 0;
  34. finfo->device = 0;
  35. finfo->size = fstatus->cbFile;
  36. finfo->csize = fstatus->cbFileAlloc;
  37. fspr_os2_time_to_fspr_time(&finfo->atime, fstatus->fdateLastAccess,
  38. fstatus->ftimeLastAccess );
  39. fspr_os2_time_to_fspr_time(&finfo->mtime, fstatus->fdateLastWrite,
  40. fstatus->ftimeLastWrite );
  41. fspr_os2_time_to_fspr_time(&finfo->ctime, fstatus->fdateCreation,
  42. fstatus->ftimeCreation );
  43. finfo->valid = APR_FINFO_TYPE | APR_FINFO_PROT | APR_FINFO_SIZE
  44. | APR_FINFO_CSIZE | APR_FINFO_MTIME
  45. | APR_FINFO_CTIME | APR_FINFO_ATIME | APR_FINFO_LINK;
  46. }
  47. static fspr_status_t handle_type(fspr_filetype_e *ftype, HFILE file)
  48. {
  49. ULONG filetype, fileattr, rc;
  50. rc = DosQueryHType(file, &filetype, &fileattr);
  51. if (rc == 0) {
  52. switch (filetype & 0xff) {
  53. case 0:
  54. *ftype = APR_REG;
  55. break;
  56. case 1:
  57. *ftype = APR_CHR;
  58. break;
  59. case 2:
  60. *ftype = APR_PIPE;
  61. break;
  62. default:
  63. /* Brian, is this correct???
  64. */
  65. *ftype = APR_UNKFILE;
  66. break;
  67. }
  68. return APR_SUCCESS;
  69. }
  70. return APR_FROM_OS_ERROR(rc);
  71. }
  72. APR_DECLARE(fspr_status_t) fspr_file_info_get(fspr_finfo_t *finfo, fspr_int32_t wanted,
  73. fspr_file_t *thefile)
  74. {
  75. ULONG rc;
  76. FILESTATUS3 fstatus;
  77. if (thefile->isopen) {
  78. if (thefile->buffered) {
  79. fspr_status_t rv = fspr_file_flush(thefile);
  80. if (rv != APR_SUCCESS) {
  81. return rv;
  82. }
  83. }
  84. rc = DosQueryFileInfo(thefile->filedes, FIL_STANDARD, &fstatus, sizeof(fstatus));
  85. }
  86. else
  87. rc = DosQueryPathInfo(thefile->fname, FIL_STANDARD, &fstatus, sizeof(fstatus));
  88. if (rc == 0) {
  89. FS3_to_finfo(finfo, &fstatus);
  90. finfo->fname = thefile->fname;
  91. if (finfo->filetype == APR_REG) {
  92. if (thefile->isopen) {
  93. return handle_type(&finfo->filetype, thefile->filedes);
  94. }
  95. } else {
  96. return APR_SUCCESS;
  97. }
  98. }
  99. finfo->protection = 0;
  100. finfo->filetype = APR_NOFILE;
  101. return APR_FROM_OS_ERROR(rc);
  102. }
  103. APR_DECLARE(fspr_status_t) fspr_file_perms_set(const char *fname, fspr_fileperms_t perms)
  104. {
  105. return APR_ENOTIMPL;
  106. }
  107. APR_DECLARE(fspr_status_t) fspr_stat(fspr_finfo_t *finfo, const char *fname,
  108. fspr_int32_t wanted, fspr_pool_t *cont)
  109. {
  110. ULONG rc;
  111. FILESTATUS3 fstatus;
  112. finfo->protection = 0;
  113. finfo->filetype = APR_NOFILE;
  114. finfo->name = NULL;
  115. rc = DosQueryPathInfo(fname, FIL_STANDARD, &fstatus, sizeof(fstatus));
  116. if (rc == 0) {
  117. FS3_to_finfo(finfo, &fstatus);
  118. finfo->fname = fname;
  119. if (wanted & APR_FINFO_NAME) {
  120. ULONG count = 1;
  121. HDIR hDir = HDIR_SYSTEM;
  122. FILEFINDBUF3 ffb;
  123. rc = DosFindFirst(fname, &hDir,
  124. FILE_DIRECTORY|FILE_HIDDEN|FILE_SYSTEM|FILE_ARCHIVED,
  125. &ffb, sizeof(ffb), &count, FIL_STANDARD);
  126. if (rc == 0 && count == 1) {
  127. finfo->name = fspr_pstrdup(cont, ffb.achName);
  128. finfo->valid |= APR_FINFO_NAME;
  129. }
  130. }
  131. } else if (rc == ERROR_INVALID_ACCESS) {
  132. memset(finfo, 0, sizeof(fspr_finfo_t));
  133. finfo->valid = APR_FINFO_TYPE | APR_FINFO_PROT;
  134. finfo->protection = 0666;
  135. finfo->filetype = APR_CHR;
  136. if (wanted & APR_FINFO_NAME) {
  137. finfo->name = fspr_pstrdup(cont, fname);
  138. finfo->valid |= APR_FINFO_NAME;
  139. }
  140. } else {
  141. return APR_FROM_OS_ERROR(rc);
  142. }
  143. return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS;
  144. }
  145. APR_DECLARE(fspr_status_t) fspr_file_attrs_set(const char *fname,
  146. fspr_fileattrs_t attributes,
  147. fspr_fileattrs_t attr_mask,
  148. fspr_pool_t *cont)
  149. {
  150. FILESTATUS3 fs3;
  151. ULONG rc;
  152. /* Don't do anything if we can't handle the requested attributes */
  153. if (!(attr_mask & (APR_FILE_ATTR_READONLY
  154. | APR_FILE_ATTR_HIDDEN)))
  155. return APR_SUCCESS;
  156. rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3));
  157. if (rc == 0) {
  158. ULONG old_attr = fs3.attrFile;
  159. if (attr_mask & APR_FILE_ATTR_READONLY)
  160. {
  161. if (attributes & APR_FILE_ATTR_READONLY) {
  162. fs3.attrFile |= FILE_READONLY;
  163. } else {
  164. fs3.attrFile &= ~FILE_READONLY;
  165. }
  166. }
  167. if (attr_mask & APR_FILE_ATTR_HIDDEN)
  168. {
  169. if (attributes & APR_FILE_ATTR_HIDDEN) {
  170. fs3.attrFile |= FILE_HIDDEN;
  171. } else {
  172. fs3.attrFile &= ~FILE_HIDDEN;
  173. }
  174. }
  175. if (fs3.attrFile != old_attr) {
  176. rc = DosSetPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3), 0);
  177. }
  178. }
  179. return APR_FROM_OS_ERROR(rc);
  180. }
  181. /* ### Somebody please write this! */
  182. APR_DECLARE(fspr_status_t) fspr_file_mtime_set(const char *fname,
  183. fspr_time_t mtime,
  184. fspr_pool_t *pool)
  185. {
  186. FILESTATUS3 fs3;
  187. ULONG rc;
  188. rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3));
  189. if (rc) {
  190. return APR_FROM_OS_ERROR(rc);
  191. }
  192. fspr_fspr_time_to_os2_time(&fs3.fdateLastWrite, &fs3.ftimeLastWrite, mtime);
  193. rc = DosSetPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3), 0);
  194. return APR_FROM_OS_ERROR(rc);
  195. }