Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / tests / bigfile.c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is the Netscape Portable Runtime (NSPR).
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998-2000
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include "prio.h"
39 #include "prmem.h"
40 #include "prprf.h"
41 #include "prinit.h"
42 #include "prerror.h"
43 #include "prthread.h"
44
45 #include "plerror.h"
46 #include "plgetopt.h"
47
48 #define DEFAULT_COUNT 10
49 #define DEFAULT_FILESIZE 1
50 #define BUFFER_SIZE 1000000
51
52 typedef enum {v_silent, v_whisper, v_shout} Verbosity;
53 static void Verbose(Verbosity, const char*, const char*, PRIntn);
54
55 #define VERBOSE(_l, _m) Verbose(_l, _m, __FILE__, __LINE__)
56
57 static PRIntn test_result = 2;
58 static PRFileDesc *output = NULL;
59 static PRIntn verbose = v_silent;
60 static PRIntn filesize = DEFAULT_FILESIZE;
61
62 static PRIntn Usage(void)
63 {
64     PR_fprintf(output, "Bigfile test usage:\n");
65     PR_fprintf(output, ">bigfile [-G] [-d] [-v[*v]] [-s <n>] <filename>\n");
66     PR_fprintf(output, "\td\tdebug mode (equivalent to -vvv)\t(false)\n");
67     PR_fprintf(output, "\tv\tAdditional levels of output\t(none)\n");
68     PR_fprintf(output, "\tk\tKeep data file after exit\t(false)\n");
69     PR_fprintf(output, "\ts <n>\tFile size in megabytes\t\t(1 megabyte)\n");
70     PR_fprintf(output, "\t<filename>\tName of test file\t(none)\n");
71     return 2;  /* nothing happened */
72 }  /* Usage */
73
74 static PRStatus DeleteIfFound(const char *filename)
75 {
76     PRStatus rv;
77     VERBOSE(v_shout, "Checking for existing file");
78     rv = PR_Access(filename, PR_ACCESS_WRITE_OK);
79     if (PR_SUCCESS == rv)
80     {
81         VERBOSE(v_shout, "Deleting existing file");
82         rv = PR_Delete(filename);
83         if (PR_FAILURE == rv) VERBOSE(v_shout, "Cannot delete big file");
84     }
85     else if (PR_FILE_NOT_FOUND_ERROR !=  PR_GetError())
86         VERBOSE(v_shout, "Cannot access big file");
87     else rv = PR_SUCCESS;
88     return rv;
89 }  /* DeleteIfFound */
90
91 static PRIntn Error(const char *msg, const char *filename)
92 {
93     PRInt32 error = PR_GetError();
94     if (NULL != msg)
95     {
96         if (0 == error) PR_fprintf(output, msg);
97         else PL_FPrintError(output, msg);
98     }
99     (void)DeleteIfFound(filename);
100     if (v_shout == verbose) PR_Abort();
101     return 1;
102 }  /* Error */
103
104 static void Verbose(
105     Verbosity level, const char *msg, const char *file, PRIntn line)
106 {
107     if (level <= verbose)
108         PR_fprintf(output, "[%s : %d]: %s\n", file, line, msg);
109 }  /* Verbose */
110
111 static void PrintInfo(PRFileInfo64 *info, const char *filename)
112 {
113     PRExplodedTime tm;
114     char ctime[40], mtime[40];
115     static const char *types[] = {"FILE", "DIRECTORY", "OTHER"};
116     PR_fprintf(
117         output, "[%s : %d]: File info for %s\n",
118         __FILE__, __LINE__, filename);
119     PR_fprintf(
120         output, "    type: %s, size: %llu bytes,\n",
121         types[info->type - 1], info->size);
122
123     PR_ExplodeTime(info->creationTime, PR_GMTParameters, &tm);
124     (void)PR_FormatTime(ctime, sizeof(ctime), "%c GMT", &tm);
125     PR_ExplodeTime(info->modifyTime, PR_GMTParameters, &tm);
126     (void)PR_FormatTime(mtime, sizeof(mtime), "%c GMT", &tm);
127
128     PR_fprintf(
129         output, "    creation: %s,\n    modify: %s\n", ctime, mtime);
130 }  /* PrintInfo */
131
132 int main(int argc, char **argv)
133 {
134     PRStatus rv;
135     char *buffer;
136     PLOptStatus os;
137     PRInt32 loop, bytes;
138     PRFileInfo small_info;
139     PRFileInfo64 big_info;
140     PRBool keep = PR_FALSE;
141     PRFileDesc *file = NULL;
142     const char *filename = NULL;
143     PRIntn count = DEFAULT_COUNT;
144     PRInt64 filesize64, big_answer, big_size, one_meg, zero_meg, big_fragment;
145     PRInt64 sevenFox = LL_INIT(0,0x7fffffff);
146
147     PLOptState *opt = PL_CreateOptState(argc, argv, "dtvhs:");
148
149     output = PR_GetSpecialFD(PR_StandardError);
150     PR_STDIO_INIT();
151
152     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
153     {
154         if (PL_OPT_BAD == os) continue;
155         switch (opt->option)
156         {
157         case 0:
158             filename = opt->value;
159             break;
160         case 'd':  /* debug mode */
161             verbose = v_shout;
162             break;
163         case 'k':  /* keep file */
164             keep = PR_TRUE;
165             break;
166         case 'v':  /* verbosity */
167             if (v_shout > verbose) verbose += 1;
168             break;
169         case 'c':  /* loop counter */
170             count = atoi(opt->value);
171             break;
172         case 's':  /* filesize */
173             filesize = atoi(opt->value);
174             break;
175         case 'h':  /* confused */
176         default:
177             return Usage();
178         }
179     }
180     PL_DestroyOptState(opt);
181
182     if (0 == count) count = DEFAULT_COUNT;
183     if (0 == filesize) filesize = DEFAULT_FILESIZE;
184     if (NULL == filename)
185     {
186 #ifdef SYMBIAN
187 #define FILE_NAME "c:\\data\\bigfile.dat"
188 #else
189 #define FILE_NAME "bigfile.dat"
190 #endif
191         if (DEFAULT_FILESIZE != filesize) return Usage();
192         else filename = FILE_NAME;
193     }
194
195     if (PR_FAILURE == DeleteIfFound(filename)) return 1;
196
197     test_result = 0;
198
199     LL_I2L(zero_meg, 0);
200     LL_I2L(one_meg, 1000000);
201     LL_I2L(filesize64, filesize);
202     buffer = (char*)PR_MALLOC(BUFFER_SIZE);
203     LL_I2L(big_fragment, BUFFER_SIZE);
204     LL_MUL(filesize64, filesize64, one_meg); 
205
206     for (loop = 0; loop < BUFFER_SIZE; ++loop) buffer[loop] = (char)loop;
207
208     VERBOSE(v_whisper, "Creating big file");
209     file = PR_Open(filename, PR_CREATE_FILE | PR_WRONLY, 0666);
210     if (NULL == file) return Error("PR_Open()", filename);
211     
212     VERBOSE(v_whisper, "Testing available space in empty file");
213     big_answer = file->methods->available64(file);
214     if (!LL_IS_ZERO(big_answer)) return Error("empty available64()", filename);
215
216         LL_SUB(big_size, filesize64, one_meg);
217     VERBOSE(v_whisper, "Creating sparse big file by seeking to end");
218         big_answer = file->methods->seek64(file, big_size, PR_SEEK_SET);
219     if (!LL_EQ(big_answer, big_size)) return Error("seek", filename);
220
221     VERBOSE(v_whisper, "Writing block at end of sparse file");
222         bytes = file->methods->write(file, buffer, BUFFER_SIZE);
223     if (bytes != BUFFER_SIZE) return Error("write", filename);
224
225     VERBOSE(v_whisper, "Testing available space at end of sparse file");
226     big_answer = file->methods->available64(file);
227     if (!LL_IS_ZERO(big_answer)) return Error("eof available64()", filename);
228
229     VERBOSE(v_whisper, "Getting big info on sparse big file");
230     rv = file->methods->fileInfo64(file, &big_info);
231     if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
232     if (v_shout <= verbose) PrintInfo(&big_info, filename);
233
234     VERBOSE(v_whisper, "Getting small info on sparse big file");
235     rv = file->methods->fileInfo(file, &small_info);
236     if (LL_CMP(sevenFox, <, filesize64) && (PR_SUCCESS == rv))
237     {
238         VERBOSE(v_whisper, "Should have failed and didn't");
239         return Error("fileInfo()", filename);
240     }
241     else if (LL_CMP(sevenFox, >, filesize64) && (PR_FAILURE == rv))
242     {
243         VERBOSE(v_whisper, "Should have succeeded and didn't");
244         return Error("fileInfo()", filename);
245     }
246
247     VERBOSE(v_whisper, "Rewinding big file");
248     big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
249     if (!LL_IS_ZERO(big_answer)) return Error("rewind seek64()", filename);
250
251     VERBOSE(v_whisper, "Establishing available space in rewound file");
252     big_answer = file->methods->available64(file);
253     if (LL_NE(filesize64, big_answer))
254         return Error("bof available64()", filename);
255
256     VERBOSE(v_whisper, "Closing big file");
257     rv = file->methods->close(file);
258     if (PR_FAILURE == rv) return Error("close()", filename);
259
260     VERBOSE(v_whisper, "Reopening big file");
261     file = PR_Open(filename, PR_RDWR, 0666);
262     if (NULL == file) return Error("open failed", filename);
263
264     VERBOSE(v_whisper, "Checking available data in reopened file");
265     big_answer = file->methods->available64(file);
266     if (LL_NE(filesize64, big_answer))
267         return Error("reopened available64()", filename);
268
269     big_answer = zero_meg;
270     VERBOSE(v_whisper, "Rewriting every byte of big file data");
271     do
272     {
273         bytes = file->methods->write(file, buffer, BUFFER_SIZE);
274         if (bytes != BUFFER_SIZE)
275             return Error("write", filename);
276         LL_ADD(big_answer, big_answer, big_fragment);
277     } while (LL_CMP(big_answer, <, filesize64));
278
279     VERBOSE(v_whisper, "Checking position at eof");
280     big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_CUR);
281     if (LL_NE(big_answer, filesize64))
282         return Error("file size error", filename);
283
284     VERBOSE(v_whisper, "Testing available space at eof");
285     big_answer = file->methods->available64(file);
286     if (!LL_IS_ZERO(big_answer))
287         return Error("eof available64()", filename);
288
289     VERBOSE(v_whisper, "Rewinding full file");
290     big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
291     if (!LL_IS_ZERO(big_answer)) return Error("bof seek64()", filename);
292
293     VERBOSE(v_whisper, "Testing available space in rewound file");
294     big_answer = file->methods->available64(file);
295     if (LL_NE(big_answer, filesize64)) return Error("bof available64()", filename);
296
297     VERBOSE(v_whisper, "Seeking to end of big file");
298     big_answer = file->methods->seek64(file, filesize64, PR_SEEK_SET);
299     if (LL_NE(big_answer, filesize64)) return Error("eof seek64()", filename);
300
301     VERBOSE(v_whisper, "Getting info on big file while it's open");
302     rv = file->methods->fileInfo64(file, &big_info);
303     if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
304     if (v_shout <= verbose) PrintInfo(&big_info, filename);
305
306     VERBOSE(v_whisper, "Closing big file");
307     rv = file->methods->close(file);
308     if (PR_FAILURE == rv) return Error("close()", filename);
309
310     VERBOSE(v_whisper, "Getting info on big file after it's closed");
311     rv = PR_GetFileInfo64(filename, &big_info);
312     if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
313     if (v_shout <= verbose) PrintInfo(&big_info, filename);
314
315     VERBOSE(v_whisper, "Deleting big file");
316     rv = PR_Delete(filename);
317     if (PR_FAILURE == rv) return Error("PR_Delete()", filename);
318
319     PR_DELETE(buffer);
320     return test_result;
321 } /* main */
322
323 /* bigfile.c */