summaryrefslogtreecommitdiffstats
path: root/src/libs/xpcom18a4/nsprpub/pr/tests/bigfile.c
blob: 19c0a7f9d6bf504332e39e33b0ce7660da915032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Netscape Portable Runtime (NSPR).
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998-2000
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "prio.h"
#include "prmem.h"
#include "prprf.h"
#include "prinit.h"
#include "prerror.h"
#include "prthread.h"

#include "plerror.h"
#include "plgetopt.h"

#define DEFAULT_COUNT 10
#define DEFAULT_FILESIZE 1
#define BUFFER_SIZE 1000000

typedef enum {v_silent, v_whisper, v_shout} Verbosity;
static void Verbose(Verbosity, const char*, const char*, PRIntn);

#define VERBOSE(_l, _m) Verbose(_l, _m, __FILE__, __LINE__)

static PRIntn test_result = 2;
static PRFileDesc *output = NULL;
static PRIntn verbose = v_silent;
static PRIntn filesize = DEFAULT_FILESIZE;

static PRIntn Usage(void)
{
    PR_fprintf(output, "Bigfile test usage:\n");
    PR_fprintf(output, ">bigfile [-G] [-d] [-v[*v]] [-s <n>] <filename>\n");
    PR_fprintf(output, "\td\tdebug mode (equivalent to -vvv)\t(false)\n");
    PR_fprintf(output, "\tv\tAdditional levels of output\t(none)\n");
    PR_fprintf(output, "\tk\tKeep data file after exit\t(false)\n");
    PR_fprintf(output, "\ts <n>\tFile size in megabytes\t\t(1 megabyte)\n");
    PR_fprintf(output, "\t<filename>\tName of test file\t(none)\n");
    return 2;  /* nothing happened */
}  /* Usage */

static PRStatus DeleteIfFound(const char *filename)
{
    PRStatus rv;
    VERBOSE(v_shout, "Checking for existing file");
    rv = PR_Access(filename, PR_ACCESS_WRITE_OK);
    if (PR_SUCCESS == rv)
    {
        VERBOSE(v_shout, "Deleting existing file");
        rv = PR_Delete(filename);
        if (PR_FAILURE == rv) VERBOSE(v_shout, "Cannot delete big file");
    }
    else if (PR_FILE_NOT_FOUND_ERROR !=  PR_GetError())
        VERBOSE(v_shout, "Cannot access big file");
    else rv = PR_SUCCESS;
    return rv;
}  /* DeleteIfFound */

static PRIntn Error(const char *msg, const char *filename)
{
    PRInt32 error = PR_GetError();
    if (NULL != msg)
    {
        if (0 == error) PR_fprintf(output, msg);
        else PL_FPrintError(output, msg);
    }
    (void)DeleteIfFound(filename);
    if (v_shout == verbose) PR_Abort();
    return 1;
}  /* Error */

static void Verbose(
    Verbosity level, const char *msg, const char *file, PRIntn line)
{
    if (level <= verbose)
        PR_fprintf(output, "[%s : %d]: %s\n", file, line, msg);
}  /* Verbose */

static void PrintInfo(PRFileInfo64 *info, const char *filename)
{
    PRExplodedTime tm;
    char ctime[40], mtime[40];
    static const char *types[] = {"FILE", "DIRECTORY", "OTHER"};
    PR_fprintf(
        output, "[%s : %d]: File info for %s\n",
        __FILE__, __LINE__, filename);
    PR_fprintf(
        output, "    type: %s, size: %llu bytes,\n",
        types[info->type - 1], info->size);

    PR_ExplodeTime(info->creationTime, PR_GMTParameters, &tm);
    (void)PR_FormatTime(ctime, sizeof(ctime), "%c GMT", &tm);
    PR_ExplodeTime(info->modifyTime, PR_GMTParameters, &tm);
    (void)PR_FormatTime(mtime, sizeof(mtime), "%c GMT", &tm);

    PR_fprintf(
        output, "    creation: %s,\n    modify: %s\n", ctime, mtime);
}  /* PrintInfo */

PRIntn main(PRIntn argc, char **argv)
{
    PRStatus rv;
    char *buffer;
    PLOptStatus os;
    PRInt32 loop, bytes;
    PRFileInfo small_info;
    PRFileInfo64 big_info;
    PRBool keep = PR_FALSE;
    PRFileDesc *file = NULL;
    const char *filename = NULL;
    PRIntn count = DEFAULT_COUNT;
    PRInt64 filesize64, big_answer, big_size, one_meg, zero_meg, big_fragment;
    PRInt64 sevenFox = LL_INIT(0,0x7fffffff);

    PLOptState *opt = PL_CreateOptState(argc, argv, "dtvhs:");

    output = PR_GetSpecialFD(PR_StandardError);
    PR_STDIO_INIT();

    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    {
        if (PL_OPT_BAD == os) continue;
        switch (opt->option)
        {
        case 0:
            filename = opt->value;
            break;
        case 'd':  /* debug mode */
            verbose = v_shout;
            break;
        case 'k':  /* keep file */
            keep = PR_TRUE;
            break;
        case 'v':  /* verbosity */
            if (v_shout > verbose) verbose += 1;
            break;
        case 'c':  /* loop counter */
            count = atoi(opt->value);
            break;
        case 's':  /* filesize */
            filesize = atoi(opt->value);
            break;
        case 'h':  /* confused */
        default:
            return Usage();
        }
    }
    PL_DestroyOptState(opt);

    if (0 == count) count = DEFAULT_COUNT;
    if (0 == filesize) filesize = DEFAULT_FILESIZE;
    if (NULL == filename)
    {
        if (DEFAULT_FILESIZE != filesize) return Usage();
        else filename = "bigfile.dat";
    }

    if (PR_FAILURE == DeleteIfFound(filename)) return 1;

    test_result = 0;

    LL_I2L(zero_meg, 0);
    LL_I2L(one_meg, 1000000);
    LL_I2L(filesize64, filesize);
    buffer = (char*)PR_MALLOC(BUFFER_SIZE);
    LL_I2L(big_fragment, BUFFER_SIZE);
    LL_MUL(filesize64, filesize64, one_meg); 

    for (loop = 0; loop < BUFFER_SIZE; ++loop) buffer[loop] = (char)loop;

    VERBOSE(v_whisper, "Creating big file");
    file = PR_Open(filename, PR_CREATE_FILE | PR_WRONLY, 0666);
    if (NULL == file) return Error("PR_Open()", filename);
    
    VERBOSE(v_whisper, "Testing available space in empty file");
    big_answer = file->methods->available64(file);
    if (!LL_IS_ZERO(big_answer)) return Error("empty available64()", filename);

	LL_SUB(big_size, filesize64, one_meg);
    VERBOSE(v_whisper, "Creating sparse big file by seeking to end");
	big_answer = file->methods->seek64(file, big_size, PR_SEEK_SET);
    if (!LL_EQ(big_answer, big_size)) return Error("seek", filename);

    VERBOSE(v_whisper, "Writing block at end of sparse file");
	bytes = file->methods->write(file, buffer, BUFFER_SIZE);
    if (bytes != BUFFER_SIZE) return Error("write", filename);

    VERBOSE(v_whisper, "Testing available space at end of sparse file");
    big_answer = file->methods->available64(file);
    if (!LL_IS_ZERO(big_answer)) return Error("eof available64()", filename);

    VERBOSE(v_whisper, "Getting big info on sparse big file");
    rv = file->methods->fileInfo64(file, &big_info);
    if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
    if (v_shout <= verbose) PrintInfo(&big_info, filename);

    VERBOSE(v_whisper, "Getting small info on sparse big file");
    rv = file->methods->fileInfo(file, &small_info);
    if (LL_CMP(sevenFox, <, filesize64) && (PR_SUCCESS == rv))
    {
        VERBOSE(v_whisper, "Should have failed and didn't");
        return Error("fileInfo()", filename);
    }
    else if (LL_CMP(sevenFox, >, filesize64) && (PR_FAILURE == rv))
    {
        VERBOSE(v_whisper, "Should have succeeded and didn't");
        return Error("fileInfo()", filename);
    }

    VERBOSE(v_whisper, "Rewinding big file");
    big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
    if (!LL_IS_ZERO(big_answer)) return Error("rewind seek64()", filename);

    VERBOSE(v_whisper, "Establishing available space in rewound file");
    big_answer = file->methods->available64(file);
    if (LL_NE(filesize64, big_answer))
        return Error("bof available64()", filename);

    VERBOSE(v_whisper, "Closing big file");
    rv = file->methods->close(file);
    if (PR_FAILURE == rv) return Error("close()", filename);

    VERBOSE(v_whisper, "Reopening big file");
    file = PR_Open(filename, PR_RDWR, 0666);
    if (NULL == file) return Error("open failed", filename);

    VERBOSE(v_whisper, "Checking available data in reopened file");
    big_answer = file->methods->available64(file);
    if (LL_NE(filesize64, big_answer))
        return Error("reopened available64()", filename);

    big_answer = zero_meg;
    VERBOSE(v_whisper, "Rewriting every byte of big file data");
    do
    {
        bytes = file->methods->write(file, buffer, BUFFER_SIZE);
        if (bytes != BUFFER_SIZE)
            return Error("write", filename);
        LL_ADD(big_answer, big_answer, big_fragment);
    } while (LL_CMP(big_answer, <, filesize64));

    VERBOSE(v_whisper, "Checking position at eof");
    big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_CUR);
    if (LL_NE(big_answer, filesize64))
        return Error("file size error", filename);

    VERBOSE(v_whisper, "Testing available space at eof");
    big_answer = file->methods->available64(file);
    if (!LL_IS_ZERO(big_answer))
        return Error("eof available64()", filename);

    VERBOSE(v_whisper, "Rewinding full file");
    big_answer = file->methods->seek64(file, zero_meg, PR_SEEK_SET);
    if (!LL_IS_ZERO(big_answer)) return Error("bof seek64()", filename);

    VERBOSE(v_whisper, "Testing available space in rewound file");
    big_answer = file->methods->available64(file);
    if (LL_NE(big_answer, filesize64)) return Error("bof available64()", filename);

    VERBOSE(v_whisper, "Seeking to end of big file");
    big_answer = file->methods->seek64(file, filesize64, PR_SEEK_SET);
    if (LL_NE(big_answer, filesize64)) return Error("eof seek64()", filename);

    VERBOSE(v_whisper, "Getting info on big file while it's open");
    rv = file->methods->fileInfo64(file, &big_info);
    if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
    if (v_shout <= verbose) PrintInfo(&big_info, filename);

    VERBOSE(v_whisper, "Closing big file");
    rv = file->methods->close(file);
    if (PR_FAILURE == rv) return Error("close()", filename);

    VERBOSE(v_whisper, "Getting info on big file after it's closed");
    rv = PR_GetFileInfo64(filename, &big_info);
    if (PR_FAILURE == rv) return Error("fileInfo64()", filename);
    if (v_shout <= verbose) PrintInfo(&big_info, filename);

    VERBOSE(v_whisper, "Deleting big file");
    rv = PR_Delete(filename);
    if (PR_FAILURE == rv) return Error("PR_Delete()", filename);

    PR_DELETE(buffer);
    return test_result;
} /* main */

/* bigfile.c */