filestat.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "apr_arch_file_io.h"
  19. #include "apr_file_io.h"
  20. #include "apr_lib.h"
  21. #include <sys/time.h>
  22. #include "apr_strings.h"
  23. static void FS3_to_finfo(apr_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. apr_os2_time_to_apr_time(&finfo->atime, fstatus->fdateLastAccess,
  38. fstatus->ftimeLastAccess );
  39. apr_os2_time_to_apr_time(&finfo->mtime, fstatus->fdateLastWrite,
  40. fstatus->ftimeLastWrite );
  41. apr_os2_time_to_apr_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 apr_status_t handle_type(apr_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(apr_status_t) apr_file_info_get(apr_finfo_t *finfo, apr_int32_t wanted,
  73. apr_file_t *thefile)
  74. {
  75. ULONG rc;
  76. FILESTATUS3 fstatus;
  77. if (thefile->isopen) {
  78. if (thefile->buffered) {
  79. /* XXX: flush here is not mutex protected */
  80. apr_status_t rv = apr_file_flush(thefile);
  81. if (rv != APR_SUCCESS) {
  82. return rv;
  83. }
  84. }
  85. rc = DosQueryFileInfo(thefile->filedes, FIL_STANDARD, &fstatus, sizeof(fstatus));
  86. }
  87. else
  88. rc = DosQueryPathInfo(thefile->fname, FIL_STANDARD, &fstatus, sizeof(fstatus));
  89. if (rc == 0) {
  90. FS3_to_finfo(finfo, &fstatus);
  91. finfo->fname = thefile->fname;
  92. if (finfo->filetype == APR_REG) {
  93. if (thefile->isopen) {
  94. return handle_type(&finfo->filetype, thefile->filedes);
  95. }
  96. } else {
  97. return APR_SUCCESS;
  98. }
  99. }
  100. finfo->protection = 0;
  101. finfo->filetype = APR_NOFILE;
  102. return APR_FROM_OS_ERROR(rc);
  103. }
  104. APR_DECLARE(apr_status_t) apr_file_perms_set(const char *fname, apr_fileperms_t perms)
  105. {
  106. return APR_ENOTIMPL;
  107. }
  108. APR_DECLARE(apr_status_t) apr_stat(apr_finfo_t *finfo, const char *fname,
  109. apr_int32_t wanted, apr_pool_t *cont)
  110. {
  111. ULONG rc;
  112. FILESTATUS3 fstatus;
  113. finfo->protection = 0;
  114. finfo->filetype = APR_NOFILE;
  115. finfo->name = NULL;
  116. rc = DosQueryPathInfo(fname, FIL_STANDARD, &fstatus, sizeof(fstatus));
  117. if (rc == 0) {
  118. FS3_to_finfo(finfo, &fstatus);
  119. finfo->fname = fname;
  120. if (wanted & APR_FINFO_NAME) {
  121. ULONG count = 1;
  122. HDIR hDir = HDIR_SYSTEM;
  123. FILEFINDBUF3 ffb;
  124. rc = DosFindFirst(fname, &hDir,
  125. FILE_DIRECTORY|FILE_HIDDEN|FILE_SYSTEM|FILE_ARCHIVED,
  126. &ffb, sizeof(ffb), &count, FIL_STANDARD);
  127. if (rc == 0 && count == 1) {
  128. finfo->name = apr_pstrdup(cont, ffb.achName);
  129. finfo->valid |= APR_FINFO_NAME;
  130. }
  131. }
  132. } else if (rc == ERROR_INVALID_ACCESS) {
  133. memset(finfo, 0, sizeof(apr_finfo_t));
  134. finfo->valid = APR_FINFO_TYPE | APR_FINFO_PROT;
  135. finfo->protection = 0666;
  136. finfo->filetype = APR_CHR;
  137. if (wanted & APR_FINFO_NAME) {
  138. finfo->name = apr_pstrdup(cont, fname);
  139. finfo->valid |= APR_FINFO_NAME;
  140. }
  141. } else {
  142. return APR_FROM_OS_ERROR(rc);
  143. }
  144. return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS;
  145. }
  146. APR_DECLARE(apr_status_t) apr_file_attrs_set(const char *fname,
  147. apr_fileattrs_t attributes,
  148. apr_fileattrs_t attr_mask,
  149. apr_pool_t *cont)
  150. {
  151. FILESTATUS3 fs3;
  152. ULONG rc;
  153. /* Don't do anything if we can't handle the requested attributes */
  154. if (!(attr_mask & (APR_FILE_ATTR_READONLY
  155. | APR_FILE_ATTR_HIDDEN)))
  156. return APR_SUCCESS;
  157. rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3));
  158. if (rc == 0) {
  159. ULONG old_attr = fs3.attrFile;
  160. if (attr_mask & APR_FILE_ATTR_READONLY)
  161. {
  162. if (attributes & APR_FILE_ATTR_READONLY) {
  163. fs3.attrFile |= FILE_READONLY;
  164. } else {
  165. fs3.attrFile &= ~FILE_READONLY;
  166. }
  167. }
  168. if (attr_mask & APR_FILE_ATTR_HIDDEN)
  169. {
  170. if (attributes & APR_FILE_ATTR_HIDDEN) {
  171. fs3.attrFile |= FILE_HIDDEN;
  172. } else {
  173. fs3.attrFile &= ~FILE_HIDDEN;
  174. }
  175. }
  176. if (fs3.attrFile != old_attr) {
  177. rc = DosSetPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3), 0);
  178. }
  179. }
  180. return APR_FROM_OS_ERROR(rc);
  181. }
  182. /* ### Somebody please write this! */
  183. APR_DECLARE(apr_status_t) apr_file_mtime_set(const char *fname,
  184. apr_time_t mtime,
  185. apr_pool_t *pool)
  186. {
  187. FILESTATUS3 fs3;
  188. ULONG rc;
  189. rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3));
  190. if (rc) {
  191. return APR_FROM_OS_ERROR(rc);
  192. }
  193. apr_apr_time_to_os2_time(&fs3.fdateLastWrite, &fs3.ftimeLastWrite, mtime);
  194. rc = DosSetPathInfo(fname, FIL_STANDARD, &fs3, sizeof(fs3), 0);
  195. return APR_FROM_OS_ERROR(rc);
  196. }