/[pearpc]/src/system/osapi/posix/sysfile.cc
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /src/system/osapi/posix/sysfile.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Wed Sep 5 17:11:21 2007 UTC (12 years, 8 months ago) by dpavlin
File size: 6350 byte(s)
import upstream CVS
1 /*
2 * HT Editor
3 * sysfile.cc - file system functions for POSIX
4 *
5 * Copyright (C) 1999-2002 Stefan Weyergraf
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <cerrno>
22 #include <fcntl.h>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstddef>
26 #include <cstring>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <sys/mman.h>
32
33 #include <limits.h> /* for PAGESIZE */
34 #ifndef PAGESIZE
35 #define PAGESIZE 4096
36 #endif
37
38 #include "system/file.h"
39
40 #include <dirent.h>
41
42 struct posixfindstate {
43 DIR *fhandle;
44 };
45
46 inline bool sys_filename_is_absolute(const char *filename)
47 {
48 return sys_is_path_delim(filename[0]);
49 }
50
51 int sys_file_mode(int mode)
52 {
53 int m = 0;
54 if (S_ISREG(mode)) {
55 m |= HT_S_IFREG;
56 } else if (S_ISBLK(mode)) {
57 m |= HT_S_IFBLK;
58 } else if (S_ISCHR(mode)) {
59 m |= HT_S_IFCHR;
60 } else if (S_ISDIR(mode)) {
61 m |= HT_S_IFDIR;
62 } else if (S_ISFIFO(mode)) {
63 m |= HT_S_IFFIFO;
64 } else if (S_ISLNK(mode)) {
65 m |= HT_S_IFLNK;
66 } else if (S_ISSOCK(mode)) {
67 m |= HT_S_IFSOCK;
68 }
69 if (mode & S_IRUSR) m |= HT_S_IRUSR;
70 if (mode & S_IRGRP) m |= HT_S_IRGRP;
71 if (mode & S_IROTH) m |= HT_S_IROTH;
72
73 if (mode & S_IWUSR) m |= HT_S_IWUSR;
74 if (mode & S_IWGRP) m |= HT_S_IWGRP;
75 if (mode & S_IWOTH) m |= HT_S_IWOTH;
76
77 if (mode & S_IXUSR) m |= HT_S_IXUSR;
78 if (mode & S_IXGRP) m |= HT_S_IXGRP;
79 if (mode & S_IXOTH) m |= HT_S_IXOTH;
80 return m;
81 }
82
83 bool sys_is_path_delim(char c)
84 {
85 return c == '/';
86 }
87
88 int sys_filename_cmp(const char *a, const char *b)
89 {
90 while (*a && *b) {
91 if (sys_is_path_delim(*a) && sys_is_path_delim(*b)) {
92 } else if (*a != *b) {
93 break;
94 }
95 a++;
96 b++;
97 }
98 return *a - *b;
99 }
100
101 int sys_canonicalize(char *result, const char *filename)
102 {
103 if (!sys_filename_is_absolute(filename)) return ENOENT;
104 return (realpath(filename, result)==result) ? 0 : ENOENT;
105 }
106
107 static char sys_find_dirname[HT_NAME_MAX];
108
109 int sys_findclose(pfind_t &pfind)
110 {
111 int r = closedir(((posixfindstate*)pfind.findstate)->fhandle);
112 free(pfind.findstate);
113 return r;
114 }
115
116 int sys_findfirst(pfind_t &pfind, const char *dirname)
117 {
118 if (!sys_filename_is_absolute(dirname)) return ENOENT;
119 int r;
120 pfind.findstate = malloc(sizeof (posixfindstate));
121 posixfindstate *pfs = (posixfindstate*)pfind.findstate;
122 if ((pfs->fhandle = opendir(dirname))) {
123 strcpy(sys_find_dirname, dirname);
124 char *s = sys_find_dirname+strlen(sys_find_dirname);
125 if ((s > sys_find_dirname) && (*(s-1) != '/')) {
126 *(s++) = '/';
127 *s = 0;
128 }
129 r = sys_findnext(pfind);
130 } else r = errno ? errno : ENOENT;
131 if (r) free(pfind.findstate);
132 return r;
133 }
134
135 int sys_findnext(pfind_t &pfind)
136 {
137 posixfindstate *pfs = (posixfindstate*)pfind.findstate;
138 struct dirent *d;
139 if ((d = readdir(pfs->fhandle))) {
140 pfind.name = d->d_name;
141 char *s = sys_find_dirname+strlen(sys_find_dirname);
142 strcpy(s, d->d_name);
143 sys_pstat(pfind.stat, sys_find_dirname);
144 *s = 0;
145 return 0;
146 }
147 return ENOENT;
148 }
149
150 static void stat_to_pstat_t(const struct stat &st, pstat_t &s)
151 {
152 s.caps = pstat_ctime|pstat_mtime|pstat_atime|pstat_uid|pstat_gid|pstat_mode_all|pstat_size|pstat_inode;
153 s.ctime = st.st_ctime;
154 s.mtime = st.st_mtime;
155 s.atime = st.st_atime;
156 s.gid = st.st_uid;
157 s.uid = st.st_gid;
158 s.mode = sys_file_mode(st.st_mode);
159 s.size = st.st_size;
160 s.fsid = st.st_ino;
161 }
162
163 int sys_pstat(pstat_t &s, const char *filename)
164 {
165 if (!sys_filename_is_absolute(filename)) return ENOENT;
166 struct stat st;
167 errno = 0;
168 int e = lstat(filename, &st);
169 if (e) return errno ? errno : ENOENT;
170 stat_to_pstat_t(st, s);
171 return 0;
172 }
173
174 int sys_pstat_fd(pstat_t &s, int fd)
175 {
176 struct stat st;
177 errno = 0;
178 int e = fstat(fd, &st);
179 if (e) return errno ? errno : ENOENT;
180 stat_to_pstat_t(st, s);
181 return 0;
182 }
183
184 int sys_truncate(const char *filename, FileOfs ofs)
185 {
186 if (!sys_filename_is_absolute(filename)) return ENOENT;
187 int fd = open(filename, O_RDWR, 0);
188 if (fd < 0) return errno;
189 if (ftruncate(fd, ofs) != 0) return errno;
190 return close(fd);
191 }
192
193 int sys_deletefile(const char *filename)
194 {
195 if (!sys_filename_is_absolute(filename)) return ENOENT;
196 return remove(filename);
197 }
198
199 int sys_truncate_fd(int fd, FileOfs ofs)
200 {
201 if (ftruncate(fd, ofs) != 0) return errno;
202 return 0;
203 }
204
205 void sys_suspend()
206 {
207 timeval tm;
208 fd_set zerofds;
209 FD_ZERO(&zerofds);
210
211 tm.tv_sec = 0;
212 tm.tv_usec = 100;
213 select(0, &zerofds, &zerofds, &zerofds, &tm);
214 }
215
216 int sys_get_free_mem()
217 {
218 return 0;
219 }
220
221 SYS_FILE *sys_fopen(const char *filename, int openmode)
222 {
223 if (openmode & SYS_OPEN_CREATE) {
224 return (SYS_FILE *)fopen(filename, "w+");
225 } else {
226 if (openmode & SYS_OPEN_WRITE) {
227 return (SYS_FILE *)fopen(filename, "r+");
228 } else {
229 return (SYS_FILE *)fopen(filename, "r");
230 }
231 }
232 }
233
234 void sys_fclose(SYS_FILE *file)
235 {
236 fclose((FILE *)file);
237 }
238
239 int sys_fread(SYS_FILE *file, byte *buf, int size)
240 {
241 return fread(buf, 1, size, (FILE *)file);
242 }
243
244 int sys_fwrite(SYS_FILE *file, byte *buf, int size)
245 {
246 return fwrite(buf, 1, size, (FILE *)file);
247 }
248
249 int sys_fseek(SYS_FILE *file, FileOfs newofs, int seekmode)
250 {
251 int r;
252 switch (seekmode) {
253 case SYS_SEEK_SET: r = fseeko((FILE *)file, newofs, SEEK_SET); break;
254 case SYS_SEEK_REL: r = fseeko((FILE *)file, newofs, SEEK_CUR); break;
255 case SYS_SEEK_END: r = fseeko((FILE *)file, newofs, SEEK_END); break;
256 default: return EINVAL;
257 }
258 return r ? errno : 0;
259 }
260
261 void sys_flush(SYS_FILE *file)
262 {
263 fflush((FILE *)file);
264 }
265
266
267 FileOfs sys_ftell(SYS_FILE *file)
268 {
269 return ftello((FILE *)file);
270 }
271
272 void *sys_alloc_read_write_execute(int size)
273 {
274 void *p = malloc(size+PAGESIZE-1);
275 if (!p) return NULL;
276
277 void *ret = (void *)(((ptrdiff_t)p + PAGESIZE-1) & ~(PAGESIZE-1));
278
279 if (mprotect(ret, size, PROT_READ | PROT_WRITE | PROT_EXEC)) {
280 free(p);
281 return NULL;
282 }
283 return ret;
284 }
285
286 void sys_free_read_write_execute(void *p)
287 {
288 // do nothing :(
289 }

  ViewVC Help
Powered by ViewVC 1.1.26