| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pj/file_io.h> |
| 20 |
#include <pj/assert.h> |
| 21 |
#include <pj/errno.h> |
| 22 |
#include <pj/limits.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <errno.h> |
| 25 |
#if defined(PJ_HAS_FCNTL_H) && PJ_HAS_FCNTL_H != 0 |
| 26 |
#include <fcntl.h> |
| 27 |
#endif |
| 28 |
#if defined(PJ_HAS_SYS_TYPES_H) && PJ_HAS_SYS_TYPES_H != 0 |
| 29 |
#include <sys/types.h> |
| 30 |
#endif |
| 31 |
#if defined(PJ_HAS_SYS_STAT_H) && PJ_HAS_SYS_STAT_H != 0 |
| 32 |
#include <sys/stat.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#if PJ_FILE_IO == PJ_FILE_IO_ANSI |
| 36 |
|
| 37 |
PJ_DEF(pj_status_t) pj_file_open( pj_pool_t *pool, |
| 38 |
const char *pathname, |
| 39 |
unsigned flags, |
| 40 |
pj_oshandle_t *fd) |
| 41 |
{ |
| 42 |
char mode[10]; |
| 43 |
char *p = mode; |
| 44 |
|
| 45 |
PJ_ASSERT_RETURN(pathname && fd, PJ_EINVAL); |
| 46 |
PJ_UNUSED_ARG(pool); |
| 47 |
|
| 48 |
if ((flags & PJ_O_APPEND) == PJ_O_APPEND) { |
| 49 |
if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) { |
| 50 |
*p++ = 'a'; |
| 51 |
if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) |
| 52 |
*p++ = '+'; |
| 53 |
} else { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
} |
| 58 |
} else { |
| 59 |
if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) { |
| 60 |
*p++ = 'r'; |
| 61 |
if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) |
| 62 |
*p++ = '+'; |
| 63 |
} else { |
| 64 |
*p++ = 'w'; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#if defined(O_CLOEXEC) |
| 69 |
if ((flags & PJ_O_CLOEXEC) == PJ_O_CLOEXEC) |
| 70 |
*p++ = 'e'; |
| 71 |
#endif |
| 72 |
|
| 73 |
if (p==mode) |
| 74 |
return PJ_EINVAL; |
| 75 |
|
| 76 |
*p++ = 'b'; |
| 77 |
|
| 78 |
#if defined(_O_SEQUENTIAL) |
| 79 |
|
| 80 |
if ((flags & PJ_O_SEQUENTIAL) == PJ_O_SEQUENTIAL) |
| 81 |
*p++ = 'S'; |
| 82 |
#endif |
| 83 |
|
| 84 |
#if defined(_O_RANDOM) |
| 85 |
|
| 86 |
if ((flags & PJ_O_RANDOM) == PJ_O_RANDOM) |
| 87 |
*p++ = 'R'; |
| 88 |
#endif |
| 89 |
|
| 90 |
*p++ = '\0'; |
| 91 |
|
| 92 |
*fd = fopen(pathname, mode); |
| 93 |
if (*fd == NULL) |
| 94 |
return PJ_RETURN_OS_ERROR(errno); |
| 95 |
|
| 96 |
return PJ_SUCCESS; |
| 97 |
} |
| 98 |
|
| 99 |
PJ_DEF(pj_status_t) pj_file_close(pj_oshandle_t fd) |
| 100 |
{ |
| 101 |
PJ_ASSERT_RETURN(fd, PJ_EINVAL); |
| 102 |
if (fclose((FILE*)fd) != 0) |
| 103 |
return PJ_RETURN_OS_ERROR(errno); |
| 104 |
|
| 105 |
return PJ_SUCCESS; |
| 106 |
} |
| 107 |
|
| 108 |
PJ_DEF(pj_status_t) pj_file_write( pj_oshandle_t fd, |
| 109 |
const void *data, |
| 110 |
pj_ssize_t *size) |
| 111 |
{ |
| 112 |
size_t written; |
| 113 |
|
| 114 |
clearerr((FILE*)fd); |
| 115 |
written = fwrite(data, 1, *size, (FILE*)fd); |
| 116 |
if (ferror((FILE*)fd)) { |
| 117 |
*size = -1; |
| 118 |
return PJ_RETURN_OS_ERROR(errno); |
| 119 |
} |
| 120 |
|
| 121 |
*size = written; |
| 122 |
return PJ_SUCCESS; |
| 123 |
} |
| 124 |
|
| 125 |
PJ_DEF(pj_status_t) pj_file_read( pj_oshandle_t fd, |
| 126 |
void *data, |
| 127 |
pj_ssize_t *size) |
| 128 |
{ |
| 129 |
size_t bytes; |
| 130 |
|
| 131 |
clearerr((FILE*)fd); |
| 132 |
bytes = fread(data, 1, *size, (FILE*)fd); |
| 133 |
if (ferror((FILE*)fd)) { |
| 134 |
*size = -1; |
| 135 |
return PJ_RETURN_OS_ERROR(errno); |
| 136 |
} |
| 137 |
|
| 138 |
*size = bytes; |
| 139 |
return PJ_SUCCESS; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
PJ_DEF(pj_status_t) pj_file_setpos( pj_oshandle_t fd, |
| 151 |
pj_off_t offset, |
| 152 |
enum pj_file_seek_type whence) |
| 153 |
{ |
| 154 |
int mode; |
| 155 |
|
| 156 |
if ((sizeof(pj_off_t) > sizeof(long)) && |
| 157 |
(offset > PJ_MAXLONG || offset < PJ_MINLONG)) |
| 158 |
{ |
| 159 |
return PJ_ENOTSUP; |
| 160 |
} |
| 161 |
|
| 162 |
switch (whence) { |
| 163 |
case PJ_SEEK_SET: |
| 164 |
mode = SEEK_SET; break; |
| 165 |
case PJ_SEEK_CUR: |
| 166 |
mode = SEEK_CUR; break; |
| 167 |
case PJ_SEEK_END: |
| 168 |
mode = SEEK_END; break; |
| 169 |
default: |
| 170 |
pj_assert(!"Invalid whence in file_setpos"); |
| 171 |
return PJ_EINVAL; |
| 172 |
} |
| 173 |
|
| 174 |
if (fseek((FILE*)fd, (long)offset, mode) != 0) |
| 175 |
return PJ_RETURN_OS_ERROR(errno); |
| 176 |
|
| 177 |
return PJ_SUCCESS; |
| 178 |
} |
| 179 |
|
| 180 |
PJ_DEF(pj_status_t) pj_file_getpos( pj_oshandle_t fd, |
| 181 |
pj_off_t *pos) |
| 182 |
{ |
| 183 |
long offset; |
| 184 |
|
| 185 |
offset = ftell((FILE*)fd); |
| 186 |
if (offset == -1) { |
| 187 |
*pos = -1; |
| 188 |
return PJ_RETURN_OS_ERROR(errno); |
| 189 |
} |
| 190 |
|
| 191 |
*pos = offset; |
| 192 |
return PJ_SUCCESS; |
| 193 |
} |
| 194 |
|
| 195 |
PJ_DEF(pj_status_t) pj_file_flush(pj_oshandle_t fd) |
| 196 |
{ |
| 197 |
int rc; |
| 198 |
|
| 199 |
rc = fflush((FILE*)fd); |
| 200 |
if (rc == EOF) { |
| 201 |
return PJ_RETURN_OS_ERROR(errno); |
| 202 |
} |
| 203 |
|
| 204 |
return PJ_SUCCESS; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
PJ_DECL(pj_off_t) pj_file_size_by_handle(pj_oshandle_t fh) |
| 211 |
{ |
| 212 |
#if !defined(PJ_HAS_SYS_STAT_H) && PJ_HAS_SYS_STAT_H == 0 |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
pj_off_t size = -1, pos; |
| 220 |
|
| 221 |
PJ_ASSERT_RETURN(fh, -1); |
| 222 |
|
| 223 |
|
| 224 |
if (pj_file_getpos(fh, &pos) != PJ_SUCCESS) |
| 225 |
return -1; |
| 226 |
|
| 227 |
if (pj_file_setpos(fh, 0, PJ_SEEK_END) != PJ_SUCCESS || |
| 228 |
pj_file_getpos(fh, &size) != PJ_SUCCESS) |
| 229 |
{ |
| 230 |
size = -1; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
if (pj_file_setpos(fh, pos, PJ_SEEK_SET) != PJ_SUCCESS) |
| 235 |
return -1; |
| 236 |
|
| 237 |
return size; |
| 238 |
|
| 239 |
#else |
| 240 |
|
| 241 |
#if (defined(PJ_HAS_UNISTD_H) && PJ_HAS_UNISTD_H != 0) |
| 242 |
# define _fileno(f) fileno(f) |
| 243 |
#endif |
| 244 |
struct stat buf; |
| 245 |
int fd, result; |
| 246 |
|
| 247 |
fd = _fileno((FILE*)fh); |
| 248 |
if (fd == -1) |
| 249 |
return -1; |
| 250 |
|
| 251 |
result = fstat(fd, &buf); |
| 252 |
if (result != 0) |
| 253 |
return -1; |
| 254 |
|
| 255 |
return buf.st_size; |
| 256 |
#endif |
| 257 |
} |
| 258 |
|
| 259 |
#endif |