1
/* $Id: file.c 2394 2008-12-23 17:27:53Z bennylp $ */
3
* Copyright (C) 2008-2009 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
20
* Additional permission under GNU GPL version 3 section 7:
22
* If you modify this program, or any covered work, by linking or
23
* combining it with the OpenSSL project's OpenSSL library (or a
24
* modified version of that library), containing parts covered by the
25
* terms of the OpenSSL or SSLeay licenses, Teluu Inc. (http://www.teluu.com)
26
* grants you additional permission to convey the resulting work.
27
* Corresponding Source for a non-source form of such a combination
28
* shall include the source code for the parts of OpenSSL used as well
29
* as that of the covered work.
36
#define FILENAME "testfil1.txt"
37
#define NEWNAME "testfil2.txt"
38
#define INCLUDE_FILE_TIME_TEST 0
40
static char buffer[11] = {'H', 'e', 'l', 'l', 'o', ' ',
41
'W', 'o', 'r', 'l', 'd' };
43
static int file_test_internal(void)
45
enum { FILE_MAX_AGE = 1000 };
48
char readbuf[sizeof(buffer)+16];
50
pj_time_val start_time;
54
PJ_LOG(3,("", "..file io test.."));
57
pj_gettimeofday(&start_time);
59
/* Delete original file if exists. */
60
if (pj_file_exists(FILENAME))
61
pj_file_delete(FILENAME);
64
* Write data to the file.
66
status = pj_file_open(NULL, FILENAME, PJ_O_WRONLY, &fd);
67
if (status != PJ_SUCCESS) {
68
app_perror("...file_open() error", status);
72
size = sizeof(buffer);
73
status = pj_file_write(fd, buffer, &size);
74
if (status != PJ_SUCCESS) {
75
app_perror("...file_write() error", status);
79
if (size != sizeof(buffer))
82
status = pj_file_close(fd);
83
if (status != PJ_SUCCESS) {
84
app_perror("...file_close() error", status);
88
/* Check the file existance and size. */
89
if (!pj_file_exists(FILENAME))
92
if (pj_file_size(FILENAME) != sizeof(buffer))
96
status = pj_file_getstat(FILENAME, &stat);
97
if (status != PJ_SUCCESS)
100
/* Check stat size. */
101
if (stat.size != sizeof(buffer))
104
#if INCLUDE_FILE_TIME_TEST
105
/* Check file creation time >= start_time. */
106
if (!PJ_TIME_VAL_GTE(stat.ctime, start_time))
108
/* Check file creation time is not much later. */
109
PJ_TIME_VAL_SUB(stat.ctime, start_time);
110
if (stat.ctime.sec > FILE_MAX_AGE)
113
/* Check file modification time >= start_time. */
114
if (!PJ_TIME_VAL_GTE(stat.mtime, start_time))
116
/* Check file modification time is not much later. */
117
PJ_TIME_VAL_SUB(stat.mtime, start_time);
118
if (stat.mtime.sec > FILE_MAX_AGE)
121
/* Check file access time >= start_time. */
122
if (!PJ_TIME_VAL_GTE(stat.atime, start_time))
124
/* Check file access time is not much later. */
125
PJ_TIME_VAL_SUB(stat.atime, start_time);
126
if (stat.atime.sec > FILE_MAX_AGE)
131
* Re-open the file and read data.
133
status = pj_file_open(NULL, FILENAME, PJ_O_RDONLY, &fd);
134
if (status != PJ_SUCCESS) {
135
app_perror("...file_open() error", status);
140
while (size < (pj_ssize_t)sizeof(readbuf)) {
143
status = pj_file_read(fd, &readbuf[size], &read);
144
if (status != PJ_SUCCESS) {
145
PJ_LOG(3,("", "...error reading file after %d bytes (error follows)",
147
app_perror("...error", status);
157
if (size != sizeof(buffer))
161
if (!pj_file_eof(fd, PJ_O_RDONLY))
165
if (pj_memcmp(readbuf, buffer, size) != 0)
169
status = pj_file_setpos(fd, 4, PJ_SEEK_SET);
170
if (status != PJ_SUCCESS) {
171
app_perror("...file_setpos() error", status);
176
status = pj_file_getpos(fd, &pos);
177
if (status != PJ_SUCCESS) {
178
app_perror("...file_getpos() error", status);
184
status = pj_file_close(fd);
185
if (status != PJ_SUCCESS) {
186
app_perror("...file_close() error", status);
193
status = pj_file_move(FILENAME, NEWNAME);
194
if (status != PJ_SUCCESS) {
195
app_perror("...file_move() error", status);
199
if (pj_file_exists(FILENAME))
201
if (!pj_file_exists(NEWNAME))
204
if (pj_file_size(NEWNAME) != sizeof(buffer))
208
status = pj_file_delete(NEWNAME);
209
if (status != PJ_SUCCESS) {
210
app_perror("...file_delete() error", status);
214
if (pj_file_exists(NEWNAME))
217
PJ_LOG(3,("", "...success"));
224
int rc = file_test_internal();
226
/* Delete test file if exists. */
227
if (pj_file_exists(FILENAME))
228
pj_file_delete(FILENAME);