Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 285 lines (250 sloc) 6.259 kb
3f448ba @luciang added a working initial commit.
luciang authored
1 #ifdef LKL_FILE_APIS
2
3 #include "wapr_fileops.h"
4
5 apr_fileperms_t wapr_unix_mode2perms(mode_t mode)
6 {
7 apr_fileperms_t perms = 0;
8
9 if (mode & S_ISUID)
10 perms |= APR_USETID;
11 if (mode & S_IRUSR)
12 perms |= APR_UREAD;
13 if (mode & S_IWUSR)
14 perms |= APR_UWRITE;
15 if (mode & S_IXUSR)
16 perms |= APR_UEXECUTE;
17 if (mode & S_ISGID)
18 perms |= APR_GSETID;
19 if (mode & S_IRGRP)
20 perms |= APR_GREAD;
21 if (mode & S_IWGRP)
22 perms |= APR_GWRITE;
23 if (mode & S_IXGRP)
24 perms |= APR_GEXECUTE;
25 #ifdef S_ISVTX
26 if (mode & S_ISVTX)
27 perms |= APR_WSTICKY;
28 #endif
29 if (mode & S_IROTH)
30 perms |= APR_WREAD;
31 if (mode & S_IWOTH)
32 perms |= APR_WWRITE;
33 if (mode & S_IXOTH)
34 perms |= APR_WEXECUTE;
35
36 return perms;
37 }
38
39 mode_t wapr_unix_perms2mode(apr_fileperms_t perms)
40 {
41 mode_t mode = 0;
42
43 if (perms & APR_USETID)
44 mode |= S_ISUID;
45 if (perms & APR_UREAD)
46 mode |= S_IRUSR;
47 if (perms & APR_UWRITE)
48 mode |= S_IWUSR;
49 if (perms & APR_UEXECUTE)
50 mode |= S_IXUSR;
51 if (perms & APR_GSETID)
52 mode |= S_ISGID;
53 if (perms & APR_GREAD)
54 mode |= S_IRGRP;
55 if (perms & APR_GWRITE)
56 mode |= S_IWGRP;
57 if (perms & APR_GEXECUTE)
58 mode |= S_IXGRP;
59 #ifdef S_ISVTX
60 if (perms & APR_WSTICKY)
61 mode |= S_ISVTX;
62 #endif
63 if (perms & APR_WREAD)
64 mode |= S_IROTH;
65 if (perms & APR_WWRITE)
66 mode |= S_IWOTH;
67 if (perms & APR_WEXECUTE)
68 mode |= S_IXOTH;
69
70 return mode;
71 }
72
73 static apr_filetype_e filetype_from_mode(mode_t mode)
74 {
75 apr_filetype_e type;
76 switch (mode & S_IFMT)
77 {
78 case S_IFREG:
79 type = APR_REG; break;
80 case S_IFDIR:
81 type = APR_DIR; break;
82 case S_IFLNK:
83 type = APR_LNK; break;
84 case S_IFCHR:
85 type = APR_CHR; break;
86 case S_IFBLK:
87 type = APR_BLK; break;
88 case S_IFSOCK:
89 type = APR_SOCK; break;
90 default:
91 if (S_ISFIFO(mode))
92 {
93 type = APR_PIPE;
94 }
95 else if (S_ISSOCK(mode))
96 {
97 type = APR_SOCK;
98 }
99 else
100 type = APR_UNKFILE;
101 }
102
103 return type;
104 }
105
106
ce8532d Update to latest LKL.
tavi authored
107 static void fill_out_finfo(apr_finfo_t *finfo, struct __kernel_stat *info,apr_int32_t wanted)
3f448ba @luciang added a working initial commit.
luciang authored
108 {
109 finfo->valid = APR_FINFO_MIN | APR_FINFO_IDENT | APR_FINFO_NLINK
110 | APR_FINFO_OWNER | APR_FINFO_PROT;
111 finfo->protection = wapr_unix_mode2perms(info->st_mode);
112 finfo->filetype = filetype_from_mode(info->st_mode);
113 // we don't need this for now
114 //finfo->user = info->st_uid;
115 //finfo->group = info->st_gid;
116 finfo->size = info->st_size;
117 finfo->inode = info->st_ino;
118 finfo->device = info->st_dev;
119 finfo->nlink = info->st_nlink;
120 finfo->atime = (apr_time_t) info->st_atime * APR_USEC_PER_SEC;
121 finfo->mtime = (apr_time_t) info->st_mtime * APR_USEC_PER_SEC;
122 finfo->ctime = (apr_time_t) info->st_ctime * APR_USEC_PER_SEC;
123 }
124
125 apr_status_t wapr_file_info_get_locked(apr_finfo_t *finfo, apr_int32_t wanted,
126 wapr_file_t *thefile)
127 {
ce8532d Update to latest LKL.
tavi authored
128 struct __kernel_stat info;
3f448ba @luciang added a working initial commit.
luciang authored
129 apr_status_t rc;
130
131 if (thefile->buffered)
132 {
133 apr_status_t rv = wapr_file_flush_locked(thefile);
134 if (rv != APR_SUCCESS)
135 return rv;
136 }
ce8532d Update to latest LKL.
tavi authored
137 rc = lkl_sys_newfstat(thefile->filedes, &info);
3f448ba @luciang added a working initial commit.
luciang authored
138 if (!rc)
139 {
140 finfo->pool = thefile->pool;
141 finfo->fname = thefile->fname;
142 fill_out_finfo(finfo, &info, wanted);
143 return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS;
144 }
145 return rc;
146 }
147
148
149 apr_status_t wapr_file_info_get(apr_finfo_t *finfo, apr_int32_t wanted, wapr_file_t *thefile)
150 {
ce8532d Update to latest LKL.
tavi authored
151 struct __kernel_stat info;
3f448ba @luciang added a working initial commit.
luciang authored
152 int rc;
153
154 if (thefile->buffered)
155 {
156 apr_status_t rv = wapr_file_flush(thefile);
157 if (rv != APR_SUCCESS)
158 return rv;
159 }
ce8532d Update to latest LKL.
tavi authored
160 rc = lkl_sys_newfstat(thefile->filedes, &info);
3f448ba @luciang added a working initial commit.
luciang authored
161 if (0 == rc)
162 {
163 finfo->pool = thefile->pool;
164 finfo->fname = thefile->fname;
165 fill_out_finfo(finfo, &info, wanted);
166 return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS;
167 }
168 return -rc;
169 }
170
171 apr_status_t wapr_file_perms_set(const char *fname, apr_fileperms_t perms)
172 {
173 apr_status_t rc;
174 mode_t mode = wapr_unix_perms2mode(perms);
175
ce8532d Update to latest LKL.
tavi authored
176 rc = lkl_sys_chmod(fname, mode);
3f448ba @luciang added a working initial commit.
luciang authored
177 if (rc)
178 return -rc;
179
180 return APR_SUCCESS;
181 }
182
183 apr_status_t wapr_file_attrs_set(const char *fname, apr_fileattrs_t attributes,
184 apr_fileattrs_t attr_mask, apr_pool_t *pool)
185 {
186 apr_status_t status;
187 apr_finfo_t finfo;
188
189 /* Don't do anything if we can't handle the requested attributes */
190 if (!(attr_mask & (APR_FILE_ATTR_READONLY
191 | APR_FILE_ATTR_EXECUTABLE)))
192 return APR_SUCCESS;
193
194 status = wapr_stat(&finfo, fname, APR_FINFO_PROT, pool);
195 if (status)
196 return status;
197
198 /* ### TODO: should added bits be umask'd? */
199 if (attr_mask & APR_FILE_ATTR_READONLY)
200 {
201 if (attributes & APR_FILE_ATTR_READONLY)
202 {
203 finfo.protection &= ~APR_UWRITE;
204 finfo.protection &= ~APR_GWRITE;
205 finfo.protection &= ~APR_WWRITE;
206 }
207 else
208 {
209 /* ### umask this! */
210 finfo.protection |= APR_UWRITE;
211 finfo.protection |= APR_GWRITE;
212 finfo.protection |= APR_WWRITE;
213 }
214 }
215
216 if (attr_mask & APR_FILE_ATTR_EXECUTABLE)
217 {
218 if (attributes & APR_FILE_ATTR_EXECUTABLE)
219 {
220 /* ### umask this! */
221 finfo.protection |= APR_UEXECUTE;
222 finfo.protection |= APR_GEXECUTE;
223 finfo.protection |= APR_WEXECUTE;
224 }
225 else
226 {
227 finfo.protection &= ~APR_UEXECUTE;
228 finfo.protection &= ~APR_GEXECUTE;
229 finfo.protection &= ~APR_WEXECUTE;
230 }
231 }
232
233 return wapr_file_perms_set(fname, finfo.protection);
234 }
235
236 apr_status_t wapr_file_mtime_set(const char *fname, apr_time_t mtime,
237 apr_pool_t *pool)
238 {
239 apr_status_t status;
240 apr_finfo_t finfo;
241
242 status = wapr_stat(&finfo, fname, APR_FINFO_ATIME, pool);
243 if (status)
244 return status;
245
246 {
ce8532d Update to latest LKL.
tavi authored
247 struct __kernel_timeval tvp[2];
3f448ba @luciang added a working initial commit.
luciang authored
248
249 tvp[0].tv_sec = apr_time_sec(finfo.atime);
250 tvp[0].tv_usec = apr_time_usec(finfo.atime);
251 tvp[1].tv_sec = apr_time_sec(mtime);
252 tvp[1].tv_usec = apr_time_usec(mtime);
253
ce8532d Update to latest LKL.
tavi authored
254 status = lkl_sys_utimes(fname, tvp);
3f448ba @luciang added a working initial commit.
luciang authored
255 if (status)
256 return status;
257 }
258 return APR_SUCCESS;
259 }
260
261 apr_status_t wapr_stat(apr_finfo_t *finfo,const char *fname, apr_int32_t wanted, apr_pool_t *pool)
262 {
ce8532d Update to latest LKL.
tavi authored
263 struct __kernel_stat info;
3f448ba @luciang added a working initial commit.
luciang authored
264 int srv =0;
265
ce8532d Update to latest LKL.
tavi authored
266 memset(&info,0,sizeof(struct __kernel_stat));
3f448ba @luciang added a working initial commit.
luciang authored
267 if (wanted & APR_FINFO_LINK)
ce8532d Update to latest LKL.
tavi authored
268 srv = lkl_sys_newlstat((char*)fname, &info);
3f448ba @luciang added a working initial commit.
luciang authored
269 else
ce8532d Update to latest LKL.
tavi authored
270 srv = lkl_sys_newstat((char*)fname, &info);
3f448ba @luciang added a working initial commit.
luciang authored
271
272 if (0 == srv)
273 {
274 finfo->pool = pool;
275 finfo->fname = fname;
276 fill_out_finfo(finfo, &info, wanted);
277 if (wanted & APR_FINFO_LINK)
278 wanted &= ~APR_FINFO_LINK;
279 return (wanted & ~finfo->valid) ? APR_INCOMPLETE : APR_SUCCESS;
280 }
281 return -srv;
282 }
283
284 #endif
Something went wrong with that request. Please try again.