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 | /***********************************************************************
|
---|
39 | **
|
---|
40 | ** Name: fileio.c
|
---|
41 | **
|
---|
42 | ** Description: Program to copy one file to another.
|
---|
43 | **
|
---|
44 | ** Modification History:
|
---|
45 | ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
|
---|
46 | ** The debug mode will print all of the printfs associated with this test.
|
---|
47 | ** The regress mode will be the default mode. Since the regress tool limits
|
---|
48 | ** the output to a one line status:PASS or FAIL,all of the printf statements
|
---|
49 | ** have been handled with an if (debug_mode) statement.
|
---|
50 | ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
|
---|
51 | ** recognize the return code from tha main program.
|
---|
52 | ** 12-June-97 Revert to return code 0 and 1, remove debug option (obsolete).
|
---|
53 | ***********************************************************************/
|
---|
54 |
|
---|
55 | /***********************************************************************
|
---|
56 | ** Includes
|
---|
57 | ***********************************************************************/
|
---|
58 | #include "prinit.h"
|
---|
59 | #include "prthread.h"
|
---|
60 | #include "prlock.h"
|
---|
61 | #include "prcvar.h"
|
---|
62 | #include "prmon.h"
|
---|
63 | #include "prmem.h"
|
---|
64 | #include "prio.h"
|
---|
65 | #include "prlog.h"
|
---|
66 |
|
---|
67 | #include <stdio.h>
|
---|
68 |
|
---|
69 | #ifdef XP_MAC
|
---|
70 | #include "prsem.h"
|
---|
71 | #include "prlog.h"
|
---|
72 | #define printf PR_LogPrint
|
---|
73 | extern void SetupMacPrintfLog(char *logFile);
|
---|
74 | #else
|
---|
75 | #include "obsolete/prsem.h"
|
---|
76 | #endif
|
---|
77 |
|
---|
78 |
|
---|
79 | #define TBSIZE 1024
|
---|
80 |
|
---|
81 | static PRUint8 tbuf[TBSIZE];
|
---|
82 |
|
---|
83 | static PRFileDesc *t1, *t2;
|
---|
84 |
|
---|
85 | PRIntn failed_already=0;
|
---|
86 | PRIntn debug_mode;
|
---|
87 | static void InitialSetup(void)
|
---|
88 | {
|
---|
89 | PRUintn i;
|
---|
90 | PRInt32 nWritten, rv;
|
---|
91 |
|
---|
92 | t1 = PR_Open("t1.tmp", PR_CREATE_FILE | PR_RDWR, 0);
|
---|
93 | PR_ASSERT(t1 != NULL);
|
---|
94 |
|
---|
95 | for (i=0; i<TBSIZE; i++)
|
---|
96 | tbuf[i] = i;
|
---|
97 |
|
---|
98 | nWritten = PR_Write((PRFileDesc*)t1, tbuf, TBSIZE);
|
---|
99 | PR_ASSERT(nWritten == TBSIZE);
|
---|
100 |
|
---|
101 | rv = PR_Seek(t1,0,PR_SEEK_SET);
|
---|
102 | PR_ASSERT(rv == 0);
|
---|
103 |
|
---|
104 | t2 = PR_Open("t2.tmp", PR_CREATE_FILE | PR_RDWR, 0);
|
---|
105 | PR_ASSERT(t2 != NULL);
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | static void VerifyAndCleanup(void)
|
---|
110 | {
|
---|
111 | PRUintn i;
|
---|
112 | PRInt32 nRead, rv;
|
---|
113 |
|
---|
114 | for (i=0; i<TBSIZE; i++)
|
---|
115 | tbuf[i] = 0;
|
---|
116 |
|
---|
117 | rv = PR_Seek(t2,0,PR_SEEK_SET);
|
---|
118 | PR_ASSERT(rv == 0);
|
---|
119 |
|
---|
120 | nRead = PR_Read((PRFileDesc*)t2, tbuf, TBSIZE);
|
---|
121 | PR_ASSERT(nRead == TBSIZE);
|
---|
122 |
|
---|
123 | for (i=0; i<TBSIZE; i++)
|
---|
124 | if (tbuf[i] != (PRUint8)i) {
|
---|
125 | if (debug_mode) printf("data mismatch for index= %d \n", i);
|
---|
126 | else failed_already=1;
|
---|
127 | }
|
---|
128 | PR_Close(t1);
|
---|
129 | PR_Close(t2);
|
---|
130 |
|
---|
131 | PR_Delete("t1.tmp");
|
---|
132 | PR_Delete("t2.tmp");
|
---|
133 |
|
---|
134 | if (debug_mode) printf("fileio test passed\n");
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /*------------------ Following is the real test program ---------*/
|
---|
139 | /*
|
---|
140 | Program to copy one file to another. Two temporary files get
|
---|
141 | created. First one gets written in one write call. Then,
|
---|
142 | a reader thread reads from this file into a double buffer.
|
---|
143 | The writer thread writes from double buffer into the other
|
---|
144 | temporary file. The second temporary file gets verified
|
---|
145 | for accurate data.
|
---|
146 | */
|
---|
147 |
|
---|
148 | PRSemaphore *emptyBufs; /* number of empty buffers */
|
---|
149 | PRSemaphore *fullBufs; /* number of buffers that are full */
|
---|
150 |
|
---|
151 | #define BSIZE 100
|
---|
152 |
|
---|
153 | struct {
|
---|
154 | char data[BSIZE];
|
---|
155 | PRUintn nbytes; /* number of bytes in this buffer */
|
---|
156 | } buf[2];
|
---|
157 |
|
---|
158 | static void PR_CALLBACK reader(void *arg)
|
---|
159 | {
|
---|
160 | PRUintn i = 0;
|
---|
161 | PRInt32 nbytes;
|
---|
162 |
|
---|
163 | do {
|
---|
164 | (void) PR_WaitSem(emptyBufs);
|
---|
165 | nbytes = PR_Read((PRFileDesc*)arg, buf[i].data, BSIZE);
|
---|
166 | if (nbytes >= 0) {
|
---|
167 | buf[i].nbytes = nbytes;
|
---|
168 | PR_PostSem(fullBufs);
|
---|
169 | i = (i + 1) % 2;
|
---|
170 | }
|
---|
171 | } while (nbytes > 0);
|
---|
172 | }
|
---|
173 |
|
---|
174 | static void PR_CALLBACK writer(void *arg)
|
---|
175 | {
|
---|
176 | PRUintn i = 0;
|
---|
177 | PRInt32 nbytes;
|
---|
178 |
|
---|
179 | do {
|
---|
180 | (void) PR_WaitSem(fullBufs);
|
---|
181 | nbytes = buf[i].nbytes;
|
---|
182 | if (nbytes > 0) {
|
---|
183 | nbytes = PR_Write((PRFileDesc*)arg, buf[i].data, nbytes);
|
---|
184 | PR_PostSem(emptyBufs);
|
---|
185 | i = (i + 1) % 2;
|
---|
186 | }
|
---|
187 | } while (nbytes > 0);
|
---|
188 | }
|
---|
189 |
|
---|
190 | int main(int argc, char **argv)
|
---|
191 | {
|
---|
192 | PRThread *r, *w;
|
---|
193 |
|
---|
194 |
|
---|
195 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
|
---|
196 | PR_STDIO_INIT();
|
---|
197 |
|
---|
198 | #ifdef XP_MAC
|
---|
199 | SetupMacPrintfLog("fileio.log");
|
---|
200 | debug_mode = 1;
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | emptyBufs = PR_NewSem(2); /* two empty buffers */
|
---|
204 |
|
---|
205 | fullBufs = PR_NewSem(0); /* zero full buffers */
|
---|
206 |
|
---|
207 | /* Create initial temp file setup */
|
---|
208 | InitialSetup();
|
---|
209 |
|
---|
210 | /* create the reader thread */
|
---|
211 |
|
---|
212 | r = PR_CreateThread(PR_USER_THREAD,
|
---|
213 | reader, t1,
|
---|
214 | PR_PRIORITY_NORMAL,
|
---|
215 | PR_LOCAL_THREAD,
|
---|
216 | PR_JOINABLE_THREAD,
|
---|
217 | 0);
|
---|
218 |
|
---|
219 | w = PR_CreateThread(PR_USER_THREAD,
|
---|
220 | writer, t2,
|
---|
221 | PR_PRIORITY_NORMAL,
|
---|
222 | PR_LOCAL_THREAD,
|
---|
223 | PR_JOINABLE_THREAD,
|
---|
224 | 0);
|
---|
225 |
|
---|
226 | /* Do the joining for both threads */
|
---|
227 | (void) PR_JoinThread(r);
|
---|
228 | (void) PR_JoinThread(w);
|
---|
229 |
|
---|
230 | /* Do the verification and clean up */
|
---|
231 | VerifyAndCleanup();
|
---|
232 |
|
---|
233 | PR_DestroySem(emptyBufs);
|
---|
234 | PR_DestroySem(fullBufs);
|
---|
235 |
|
---|
236 | PR_Cleanup();
|
---|
237 |
|
---|
238 | if(failed_already)
|
---|
239 | {
|
---|
240 | printf("Fail\n");
|
---|
241 | return 1;
|
---|
242 | }
|
---|
243 | else
|
---|
244 | {
|
---|
245 | printf("PASS\n");
|
---|
246 | return 0;
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | }
|
---|