xref: /aoo42x/main/store/workben/t_file.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_store.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "sal/types.h"
32*cdf0e10cSrcweir #include "osl/thread.h"
33*cdf0e10cSrcweir #include "rtl/ustring.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "lockbyte.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #ifndef INCLUDED_STDIO_H
38*cdf0e10cSrcweir #include <stdio.h>
39*cdf0e10cSrcweir #define INCLUDED_STDIO_H
40*cdf0e10cSrcweir #endif
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include "osl/file.h"
43*cdf0e10cSrcweir #include "osl/process.h"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace store;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #define TEST_PAGESIZE 16384
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir /*========================================================================
50*cdf0e10cSrcweir  *
51*cdf0e10cSrcweir  * main.
52*cdf0e10cSrcweir  *
53*cdf0e10cSrcweir  *======================================================================*/
54*cdf0e10cSrcweir int SAL_CALL main (int argc, char **argv)
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir 	storeError eErrCode = store_E_None;
57*cdf0e10cSrcweir 	rtl::Reference<ILockBytes> xLockBytes;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     if (argc > 1)
60*cdf0e10cSrcweir     {
61*cdf0e10cSrcweir         rtl::OUString aFilename (
62*cdf0e10cSrcweir             argv[1], rtl_str_getLength(argv[1]),
63*cdf0e10cSrcweir             osl_getThreadTextEncoding());
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir #if 0   /* EXP */
66*cdf0e10cSrcweir 		oslFileError result;
67*cdf0e10cSrcweir 		rtl::OUString aPath;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 		result = osl_getFileURLFromSystemPath(aFilename.pData, &(aPath.pData));
70*cdf0e10cSrcweir 		if (result != osl_File_E_None)
71*cdf0e10cSrcweir 		{
72*cdf0e10cSrcweir 			// not SystemPath, assume FileUrl.
73*cdf0e10cSrcweir 			aPath = aFilename;
74*cdf0e10cSrcweir 		}
75*cdf0e10cSrcweir 		if (rtl_ustr_ascii_shortenedCompare_WithLength(aPath.pData->buffer, aPath.pData->length, "file://", 7) != 0)
76*cdf0e10cSrcweir 		{
77*cdf0e10cSrcweir 			// not FileUrl, assume relative path.
78*cdf0e10cSrcweir 			rtl::OUString aBase;
79*cdf0e10cSrcweir 			(void) osl_getProcessWorkingDir (&(aBase.pData));
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 			// absolute FileUrl.
82*cdf0e10cSrcweir 			(void) osl_getAbsoluteFileURL(aBase.pData, aPath.pData, &(aPath.pData));
83*cdf0e10cSrcweir 		}
84*cdf0e10cSrcweir 		aFilename = aPath;
85*cdf0e10cSrcweir #endif  /* EXP */
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         eErrCode = FileLockBytes_createInstance (
88*cdf0e10cSrcweir             xLockBytes, aFilename.pData, store_AccessReadWrite);
89*cdf0e10cSrcweir         if (eErrCode != store_E_None)
90*cdf0e10cSrcweir         {
91*cdf0e10cSrcweir             // Check reason.
92*cdf0e10cSrcweir             if (eErrCode != store_E_NotExists)
93*cdf0e10cSrcweir             {
94*cdf0e10cSrcweir                 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
95*cdf0e10cSrcweir                 return eErrCode;
96*cdf0e10cSrcweir             }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir             // Create.
99*cdf0e10cSrcweir             eErrCode = FileLockBytes_createInstance (
100*cdf0e10cSrcweir                 xLockBytes, aFilename.pData, store_AccessReadCreate);
101*cdf0e10cSrcweir             if (eErrCode != store_E_None)
102*cdf0e10cSrcweir             {
103*cdf0e10cSrcweir                 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
104*cdf0e10cSrcweir                 return eErrCode;
105*cdf0e10cSrcweir             }
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir         fprintf (stdout, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv[1]);
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir     else
110*cdf0e10cSrcweir     {
111*cdf0e10cSrcweir         eErrCode = MemoryLockBytes_createInstance (xLockBytes);
112*cdf0e10cSrcweir         if (eErrCode != store_E_None)
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
115*cdf0e10cSrcweir             return eErrCode;
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir         fprintf (stdout, "t_file: using MemoryLockBytes implementation.\n");
118*cdf0e10cSrcweir     }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     rtl::Reference< PageData::Allocator > xAllocator;
121*cdf0e10cSrcweir     eErrCode = xLockBytes->initialize (xAllocator, TEST_PAGESIZE);
122*cdf0e10cSrcweir     if (eErrCode != store_E_None)
123*cdf0e10cSrcweir     {
124*cdf0e10cSrcweir         fprintf (stderr, "t_file: initialize() error: %d\n", eErrCode);
125*cdf0e10cSrcweir         return eErrCode;
126*cdf0e10cSrcweir     }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	sal_Char buffer[TEST_PAGESIZE];
129*cdf0e10cSrcweir 	rtl_fillMemory (buffer, sizeof(buffer), sal_uInt8('B'));
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	sal_uInt32 i, k;
132*cdf0e10cSrcweir 	for (k = 0; k < 4; k++)
133*cdf0e10cSrcweir 	{
134*cdf0e10cSrcweir 		sal_uInt32 index = k * TEST_PAGESIZE / 4;
135*cdf0e10cSrcweir 		buffer[index] = 'A';
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	for (i = 0; i < 256; i++)
139*cdf0e10cSrcweir 	{
140*cdf0e10cSrcweir 		sal_uInt32 offset = i * TEST_PAGESIZE;
141*cdf0e10cSrcweir 		eErrCode = xLockBytes->setSize (offset + TEST_PAGESIZE);
142*cdf0e10cSrcweir 		if (eErrCode != store_E_None)
143*cdf0e10cSrcweir 		{
144*cdf0e10cSrcweir 			fprintf (stderr, "t_file: setSize() error: %d\n", eErrCode);
145*cdf0e10cSrcweir 			return eErrCode;
146*cdf0e10cSrcweir 		}
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 		for (k = 0; k < 4; k++)
149*cdf0e10cSrcweir 		{
150*cdf0e10cSrcweir 			sal_uInt32 magic = i * 4 + k;
151*cdf0e10cSrcweir 			if (magic)
152*cdf0e10cSrcweir 			{
153*cdf0e10cSrcweir 				sal_uInt32 verify = 0;
154*cdf0e10cSrcweir 				eErrCode = xLockBytes->readAt (
155*cdf0e10cSrcweir 					0, &verify, sizeof(verify));
156*cdf0e10cSrcweir 				if (eErrCode != store_E_None)
157*cdf0e10cSrcweir 				{
158*cdf0e10cSrcweir 					fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
159*cdf0e10cSrcweir 					return eErrCode;
160*cdf0e10cSrcweir 				}
161*cdf0e10cSrcweir 				if (verify != magic)
162*cdf0e10cSrcweir 				{
163*cdf0e10cSrcweir 					// Failure.
164*cdf0e10cSrcweir 					fprintf (stderr, "Expected %ld read %ld\n", (unsigned long)(magic), (unsigned long)(verify));
165*cdf0e10cSrcweir 				}
166*cdf0e10cSrcweir 			}
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 			sal_uInt32 index = k * TEST_PAGESIZE / 4;
169*cdf0e10cSrcweir 			eErrCode = xLockBytes->writeAt (
170*cdf0e10cSrcweir 				offset + index, &(buffer[index]), TEST_PAGESIZE / 4);
171*cdf0e10cSrcweir 			if (eErrCode != store_E_None)
172*cdf0e10cSrcweir 			{
173*cdf0e10cSrcweir 				fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
174*cdf0e10cSrcweir 				return eErrCode;
175*cdf0e10cSrcweir 			}
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 			magic += 1;
178*cdf0e10cSrcweir 			eErrCode = xLockBytes->writeAt (
179*cdf0e10cSrcweir 				0, &magic, sizeof(magic));
180*cdf0e10cSrcweir 			if (eErrCode != store_E_None)
181*cdf0e10cSrcweir 			{
182*cdf0e10cSrcweir 				fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
183*cdf0e10cSrcweir 				return eErrCode;
184*cdf0e10cSrcweir 			}
185*cdf0e10cSrcweir 		}
186*cdf0e10cSrcweir 	}
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 	eErrCode = xLockBytes->flush();
189*cdf0e10cSrcweir 	if (eErrCode != store_E_None)
190*cdf0e10cSrcweir 	{
191*cdf0e10cSrcweir 		fprintf (stderr, "t_file: flush() error: %d\n", eErrCode);
192*cdf0e10cSrcweir 		return eErrCode;
193*cdf0e10cSrcweir 	}
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 	sal_Char verify[TEST_PAGESIZE];
196*cdf0e10cSrcweir 	for (i = 0; i < 256; i++)
197*cdf0e10cSrcweir 	{
198*cdf0e10cSrcweir 		sal_uInt32 offset = i * TEST_PAGESIZE;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 		eErrCode = xLockBytes->readAt (offset, verify, TEST_PAGESIZE);
201*cdf0e10cSrcweir 		if (eErrCode != store_E_None)
202*cdf0e10cSrcweir 		{
203*cdf0e10cSrcweir 			fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
204*cdf0e10cSrcweir 			return eErrCode;
205*cdf0e10cSrcweir 		}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 		sal_uInt32 index = 0;
208*cdf0e10cSrcweir 		if (offset == 0)
209*cdf0e10cSrcweir 		{
210*cdf0e10cSrcweir 			sal_uInt32 magic = 256 * 4;
211*cdf0e10cSrcweir 			if (rtl_compareMemory (&verify[index], &magic, sizeof(magic)))
212*cdf0e10cSrcweir 			{
213*cdf0e10cSrcweir 				// Failure.
214*cdf0e10cSrcweir 				fprintf (stderr, "t_file: Unexpected value at 0x00000000\n");
215*cdf0e10cSrcweir 			}
216*cdf0e10cSrcweir 			index += 4;
217*cdf0e10cSrcweir 		}
218*cdf0e10cSrcweir 		if (rtl_compareMemory (
219*cdf0e10cSrcweir 			&verify[index], &buffer[index], TEST_PAGESIZE - index))
220*cdf0e10cSrcweir 		{
221*cdf0e10cSrcweir 			// Failure.
222*cdf0e10cSrcweir 			fprintf (stderr, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset));
223*cdf0e10cSrcweir 		}
224*cdf0e10cSrcweir 	}
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	for (i = 0; i < 256; i++)
227*cdf0e10cSrcweir 	{
228*cdf0e10cSrcweir         PageHolder xPage;
229*cdf0e10cSrcweir 		sal_uInt32 offset = i * TEST_PAGESIZE;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 		eErrCode = xLockBytes->readPageAt (xPage, offset);
232*cdf0e10cSrcweir 		if (eErrCode != store_E_None)
233*cdf0e10cSrcweir 		{
234*cdf0e10cSrcweir 			fprintf (stderr, "t_file: readPageAt() error: %d\n", eErrCode);
235*cdf0e10cSrcweir 			return eErrCode;
236*cdf0e10cSrcweir 		}
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir         PageData * page = xPage.get();
239*cdf0e10cSrcweir         (void)page; // UNUSED
240*cdf0e10cSrcweir     }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 	xLockBytes.clear();
243*cdf0e10cSrcweir 	return 0;
244*cdf0e10cSrcweir }
245