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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_arch_file_io.h"
18 #include "apr_file_io.h"
19 #include "apr_general.h"
20 #include "apr_strings.h"
21 #include "apr_errno.h"
27 static apr_filetype_e filetype_from_mode(mode_t mode)
31 switch (mode & S_IFMT) {
33 type = APR_REG; break;
35 type = APR_DIR; break;
37 type = APR_LNK; break;
39 type = APR_CHR; break;
41 type = APR_BLK; break;
44 type = APR_PIPE; break;
46 #if !defined(BEOS) && defined(S_IFSOCK)
48 type = APR_SOCK; break;
52 /* Work around missing S_IFxxx values above
55 #if !defined(S_IFFIFO) && defined(S_ISFIFO)
60 #if !defined(BEOS) && !defined(S_IFSOCK) && defined(S_ISSOCK)
70 static void fill_out_finfo(apr_finfo_t *finfo, struct_stat *info,
73 finfo->valid = APR_FINFO_MIN | APR_FINFO_IDENT | APR_FINFO_NLINK
74 | APR_FINFO_OWNER | APR_FINFO_PROT;
75 finfo->protection = apr_unix_mode2perms(info->st_mode);
76 finfo->filetype = filetype_from_mode(info->st_mode);
77 finfo->user = info->st_uid;
78 finfo->group = info->st_gid;
79 finfo->size = info->st_size;
80 finfo->device = info->st_dev;
81 finfo->nlink = info->st_nlink;
83 /* Check for overflow if storing a 64-bit st_ino in a 32-bit
84 * apr_ino_t for LFS builds: */
85 if (sizeof(apr_ino_t) >= sizeof(info->st_ino)
86 || (apr_ino_t)info->st_ino == info->st_ino) {
87 finfo->inode = info->st_ino;
89 finfo->valid &= ~APR_FINFO_INODE;
92 apr_time_ansi_put(&finfo->atime, info->st_atime);
93 #ifdef HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
94 finfo->atime += info->st_atim.tv_nsec / APR_TIME_C(1000);
95 #elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
96 finfo->atime += info->st_atimensec / APR_TIME_C(1000);
97 #elif defined(HAVE_STRUCT_STAT_ST_ATIME_N)
98 finfo->atime += info->st_atime_n / APR_TIME_C(1000);
101 apr_time_ansi_put(&finfo->mtime, info->st_mtime);
102 #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
103 finfo->mtime += info->st_mtim.tv_nsec / APR_TIME_C(1000);
104 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
105 finfo->mtime += info->st_mtimensec / APR_TIME_C(1000);
106 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
107 finfo->mtime += info->st_mtime_n / APR_TIME_C(1000);
110 apr_time_ansi_put(&finfo->ctime, info->st_ctime);
111 #ifdef HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC
112 finfo->ctime += info->st_ctim.tv_nsec / APR_TIME_C(1000);
113 #elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
114 finfo->ctime += info->st_ctimensec / APR_TIME_C(1000);
115 #elif defined(HAVE_STRUCT_STAT_ST_CTIME_N)
116 finfo->ctime += info->st_ctime_n / APR_TIME_C(1000);
119 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
121 finfo->csize = (apr_off_t)info->st_blocks * (apr_off_t)DEV_BSIZE;
123 finfo->csize = (apr_off_t)info->st_blocks * (apr_off_t)512;
125 finfo->valid |= APR_FINFO_CSIZE;
129 apr_status_t apr_file_info_get_locked(apr_finfo_t *finfo, apr_int32_t wanted,
134 if (thefile->buffered) {
135 apr_status_t rv = apr_file_flush_locked(thefile);
136 if (rv != APR_SUCCESS)
140 if (fstat(thefile->filedes, &info) == 0) {
141 finfo->pool = thefile->pool;
142 finfo->fname = thefile->fname;
143 fill_out_finfo(finfo, &info, wanted);
144 return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS;
151 APR_DECLARE(apr_status_t) apr_file_info_get(apr_finfo_t *finfo,
157 if (thefile->buffered) {
158 apr_status_t rv = apr_file_flush(thefile);
159 if (rv != APR_SUCCESS)
163 if (fstat(thefile->filedes, &info) == 0) {
164 finfo->pool = thefile->pool;
165 finfo->fname = thefile->fname;
166 fill_out_finfo(finfo, &info, wanted);
167 return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS;
174 APR_DECLARE(apr_status_t) apr_file_perms_set(const char *fname,
175 apr_fileperms_t perms)
177 mode_t mode = apr_unix_perms2mode(perms);
179 if (chmod(fname, mode) == -1)
184 APR_DECLARE(apr_status_t) apr_file_attrs_set(const char *fname,
185 apr_fileattrs_t attributes,
186 apr_fileattrs_t attr_mask,
192 /* Don't do anything if we can't handle the requested attributes */
193 if (!(attr_mask & (APR_FILE_ATTR_READONLY
194 | APR_FILE_ATTR_EXECUTABLE)))
197 status = apr_stat(&finfo, fname, APR_FINFO_PROT, pool);
201 /* ### TODO: should added bits be umask'd? */
202 if (attr_mask & APR_FILE_ATTR_READONLY)
204 if (attributes & APR_FILE_ATTR_READONLY)
206 finfo.protection &= ~APR_UWRITE;
207 finfo.protection &= ~APR_GWRITE;
208 finfo.protection &= ~APR_WWRITE;
212 /* ### umask this! */
213 finfo.protection |= APR_UWRITE;
214 finfo.protection |= APR_GWRITE;
215 finfo.protection |= APR_WWRITE;
219 if (attr_mask & APR_FILE_ATTR_EXECUTABLE)
221 if (attributes & APR_FILE_ATTR_EXECUTABLE)
223 /* ### umask this! */
224 finfo.protection |= APR_UEXECUTE;
225 finfo.protection |= APR_GEXECUTE;
226 finfo.protection |= APR_WEXECUTE;
230 finfo.protection &= ~APR_UEXECUTE;
231 finfo.protection &= ~APR_GEXECUTE;
232 finfo.protection &= ~APR_WEXECUTE;
236 return apr_file_perms_set(fname, finfo.protection);
240 APR_DECLARE(apr_status_t) apr_file_mtime_set(const char *fname,
247 status = apr_stat(&finfo, fname, APR_FINFO_ATIME, pool);
254 struct timeval tvp[2];
256 tvp[0].tv_sec = apr_time_sec(finfo.atime);
257 tvp[0].tv_usec = apr_time_usec(finfo.atime);
258 tvp[1].tv_sec = apr_time_sec(mtime);
259 tvp[1].tv_usec = apr_time_usec(mtime);
261 if (utimes(fname, tvp) == -1) {
265 #elif defined(HAVE_UTIME)
269 buf.actime = (time_t) (finfo.atime / APR_USEC_PER_SEC);
270 buf.modtime = (time_t) (mtime / APR_USEC_PER_SEC);
272 if (utime(fname, &buf) == -1) {
284 APR_DECLARE(apr_status_t) apr_stat(apr_finfo_t *finfo,
286 apr_int32_t wanted, apr_pool_t *pool)
291 if (wanted & APR_FINFO_LINK)
292 srv = lstat(fname, &info);
294 srv = stat(fname, &info);
298 finfo->fname = fname;
299 fill_out_finfo(finfo, &info, wanted);
300 if (wanted & APR_FINFO_LINK)
301 wanted &= ~APR_FINFO_LINK;
302 return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS;
305 #if !defined(ENOENT) || !defined(ENOTDIR)
306 #error ENOENT || ENOTDIR not defined; please see the
307 #error comments at this line in the source for a workaround.
309 * If ENOENT || ENOTDIR is not defined in one of the your OS's
310 * include files, APR cannot report a good reason why the stat()
311 * of the file failed; there are cases where it can fail even though
312 * the file exists. This opens holes in Apache, for example, because
313 * it becomes possible for someone to get a directory listing of a
314 * directory even though there is an index (eg. index.html) file in
315 * it. If you do not have a problem with this, delete the above
316 * #error lines and start the compile again. If you need to do this,
317 * please submit a bug report to http://www.apache.org/bug_report.html
318 * letting us know that you needed to do this. Please be sure to
319 * include the operating system you are using.
321 /* WARNING: All errors will be handled as not found
326 /* WARNING: All errors but not found will be handled as not directory
333 #else /* All was defined well, report the usual: */