1
/* $Id: file.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
#define FILENAME "testfil1.txt"
26
#define NEWNAME "testfil2.txt"
27
#define INCLUDE_FILE_TIME_TEST 0
29
static char buffer[11] = {'H', 'e', 'l', 'l', 'o', ' ',
30
'W', 'o', 'r', 'l', 'd' };
32
static int file_test_internal(void)
34
enum { FILE_MAX_AGE = 1000 };
37
char readbuf[sizeof(buffer)+16];
39
pj_time_val start_time;
43
PJ_LOG(3,("", "..file io test.."));
46
pj_gettimeofday(&start_time);
48
/* Delete original file if exists. */
49
if (pj_file_exists(FILENAME))
50
pj_file_delete(FILENAME);
53
* Write data to the file.
55
status = pj_file_open(NULL, FILENAME, PJ_O_WRONLY, &fd);
56
if (status != PJ_SUCCESS) {
57
app_perror("...file_open() error", status);
61
size = sizeof(buffer);
62
status = pj_file_write(fd, buffer, &size);
63
if (status != PJ_SUCCESS) {
64
app_perror("...file_write() error", status);
68
if (size != sizeof(buffer))
71
status = pj_file_close(fd);
72
if (status != PJ_SUCCESS) {
73
app_perror("...file_close() error", status);
77
/* Check the file existance and size. */
78
if (!pj_file_exists(FILENAME))
81
if (pj_file_size(FILENAME) != sizeof(buffer))
85
status = pj_file_getstat(FILENAME, &stat);
86
if (status != PJ_SUCCESS)
89
/* Check stat size. */
90
if (stat.size != sizeof(buffer))
93
#if INCLUDE_FILE_TIME_TEST
94
/* Check file creation time >= start_time. */
95
if (!PJ_TIME_VAL_GTE(stat.ctime, start_time))
97
/* Check file creation time is not much later. */
98
PJ_TIME_VAL_SUB(stat.ctime, start_time);
99
if (stat.ctime.sec > FILE_MAX_AGE)
102
/* Check file modification time >= start_time. */
103
if (!PJ_TIME_VAL_GTE(stat.mtime, start_time))
105
/* Check file modification time is not much later. */
106
PJ_TIME_VAL_SUB(stat.mtime, start_time);
107
if (stat.mtime.sec > FILE_MAX_AGE)
110
/* Check file access time >= start_time. */
111
if (!PJ_TIME_VAL_GTE(stat.atime, start_time))
113
/* Check file access time is not much later. */
114
PJ_TIME_VAL_SUB(stat.atime, start_time);
115
if (stat.atime.sec > FILE_MAX_AGE)
120
* Re-open the file and read data.
122
status = pj_file_open(NULL, FILENAME, PJ_O_RDONLY, &fd);
123
if (status != PJ_SUCCESS) {
124
app_perror("...file_open() error", status);
129
while (size < (pj_ssize_t)sizeof(readbuf)) {
132
status = pj_file_read(fd, &readbuf[size], &read);
133
if (status != PJ_SUCCESS) {
134
PJ_LOG(3,("", "...error reading file after %d bytes (error follows)",
136
app_perror("...error", status);
146
if (size != sizeof(buffer))
150
if (!pj_file_eof(fd, PJ_O_RDONLY))
154
if (pj_memcmp(readbuf, buffer, size) != 0)
158
status = pj_file_setpos(fd, 4, PJ_SEEK_SET);
159
if (status != PJ_SUCCESS) {
160
app_perror("...file_setpos() error", status);
165
status = pj_file_getpos(fd, &pos);
166
if (status != PJ_SUCCESS) {
167
app_perror("...file_getpos() error", status);
173
status = pj_file_close(fd);
174
if (status != PJ_SUCCESS) {
175
app_perror("...file_close() error", status);
182
status = pj_file_move(FILENAME, NEWNAME);
183
if (status != PJ_SUCCESS) {
184
app_perror("...file_move() error", status);
188
if (pj_file_exists(FILENAME))
190
if (!pj_file_exists(NEWNAME))
193
if (pj_file_size(NEWNAME) != sizeof(buffer))
197
status = pj_file_delete(NEWNAME);
198
if (status != PJ_SUCCESS) {
199
app_perror("...file_delete() error", status);
203
if (pj_file_exists(NEWNAME))
206
PJ_LOG(3,("", "...success"));
213
int rc = file_test_internal();
215
/* Delete test file if exists. */
216
if (pj_file_exists(FILENAME))
217
pj_file_delete(FILENAME);