summaryrefslogtreecommitdiff
path: root/indra/llvfs/lllfsthread.cpp
blob: 6af638fd12a59ac0a050d92b5037d25fb2baec3b (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
/** 
 * @file lllfsthread.cpp
 * @brief LLLFSThread base class
 *
 * Copyright (c) 2001-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#include "linden_common.h"
#include "llmath.h"
#include "lllfsthread.h"
#include "llstl.h"
#include "llapr.h"

//============================================================================

/*static*/ LLLFSThread* LLLFSThread::sLocal = NULL;

//============================================================================
// Run on MAIN thread
//static
void LLLFSThread::initClass(bool local_is_threaded, bool local_run_always)
{
	llassert(sLocal == NULL);
	sLocal = new LLLFSThread(local_is_threaded, local_run_always);
}

//static
S32 LLLFSThread::updateClass(U32 ms_elapsed)
{
	sLocal->update(ms_elapsed);
	return sLocal->getPending();
}

//static
void LLLFSThread::cleanupClass()
{
	sLocal->setQuitting();
	while (sLocal->getPending())
	{
		sLocal->update(0);
	}
	delete sLocal;
	sLocal = 0;
}

//----------------------------------------------------------------------------

LLLFSThread::LLLFSThread(bool threaded, bool runalways) :
	LLQueuedThread("LFS", threaded, runalways)
{
}

LLLFSThread::~LLLFSThread()
{
	// ~LLQueuedThread() will be called here
}

//----------------------------------------------------------------------------

LLLFSThread::handle_t LLLFSThread::read(const LLString& filename,	/* Flawfinder: ignore */ 
										U8* buffer, S32 offset, S32 numbytes, U32 priority, U32 flags)
{
	handle_t handle = generateHandle();

	priority = llmax(priority, (U32)PRIORITY_LOW); // All reads are at least PRIORITY_LOW
	Request* req = new Request(handle, priority, flags,
							   FILE_READ, filename,
							   buffer, offset, numbytes);

	bool res = addRequest(req);
	if (!res)
	{
		llerrs << "LLLFSThread::read called after LLLFSThread::cleanupClass()" << llendl;
		req->deleteRequest();
		handle = nullHandle();
	}

	return handle;
}

S32 LLLFSThread::readImmediate(const LLString& filename,
							   U8* buffer, S32 offset, S32 numbytes)
{
	handle_t handle = generateHandle();

	Request* req = new Request(handle, PRIORITY_IMMEDIATE, 0,
							   FILE_READ, filename,
							   buffer, offset, numbytes);
	
	S32 res = addRequest(req) ? 1 : 0;
	if (res == 0)
	{
		llerrs << "LLLFSThread::read called after LLLFSThread::cleanupClass()" << llendl;
		req->deleteRequest();
	}
	else
	{
		llverify(waitForResult(handle, false) == true);
		res = req->getBytesRead();
		completeRequest(handle);
	}
	return res;
}

LLLFSThread::handle_t LLLFSThread::write(const LLString& filename,
										 U8* buffer, S32 offset, S32 numbytes, U32 flags)
{
	handle_t handle = generateHandle();

	Request* req = new Request(handle, 0, flags,
							   FILE_WRITE, filename,
							   buffer, offset, numbytes);

	bool res = addRequest(req);
	if (!res)
	{
		llerrs << "LLLFSThread::read called after LLLFSThread::cleanupClass()" << llendl;
		req->deleteRequest();
		handle = nullHandle();
	}
	
	return handle;
}

S32 LLLFSThread::writeImmediate(const LLString& filename,
								 U8* buffer, S32 offset, S32 numbytes)
{
	handle_t handle = generateHandle();

	Request* req = new Request(handle, PRIORITY_IMMEDIATE, 0,
							   FILE_WRITE, filename,
							   buffer, offset, numbytes);

	S32 res = addRequest(req) ? 1 : 0;
	if (res == 0)
	{
		llerrs << "LLLFSThread::write called after LLLFSThread::cleanupClass()" << llendl;
		req->deleteRequest();
	}
	else
	{
		llverify(waitForResult(handle, false) == true);
		res = req->getBytesRead();
		completeRequest(handle);
	}
	return res;
}


LLLFSThread::handle_t LLLFSThread::rename(const LLString& filename, const LLString& newname, U32 flags)
{
	handle_t handle = generateHandle();

	LLString* new_name_str = new LLString(newname); // deleted with Request
	Request* req = new Request(handle, 0, flags,
							   FILE_RENAME, filename,
							   (U8*)new_name_str, 0, 0);

	bool res = addRequest(req);
	if (!res)
	{
		llerrs << "LLLFSThread::rename called after LLLFSThread::cleanupClass()" << llendl;
		req->deleteRequest();
		handle = nullHandle();
	}
	
	return handle;
}

LLLFSThread::handle_t LLLFSThread::remove(const LLString& filename, U32 flags)
{
	handle_t handle = generateHandle();

	Request* req = new Request(handle, 0, flags,
							   FILE_RENAME, filename,
							   NULL, 0, 0);

	bool res = addRequest(req);
	if (!res)
	{
		llerrs << "LLLFSThread::remove called after LLLFSThread::cleanupClass()" << llendl;
		req->deleteRequest();
		handle = nullHandle();
	}
	
	return handle;
}

//============================================================================
// Runs on its OWN thread

bool LLLFSThread::processRequest(QueuedRequest* qreq)
{
	Request *req = (Request*)qreq;

	bool complete = req->processIO();

	return complete;
}

//============================================================================

LLLFSThread::Request::Request(handle_t handle, U32 priority, U32 flags,
							  operation_t op, const LLString& filename,
							  U8* buffer, S32 offset, S32 numbytes) :
	QueuedRequest(handle, priority, flags),
	mOperation(op),
	mFileName(filename),
	mBuffer(buffer),
	mOffset(offset),
	mBytes(numbytes),
	mBytesRead(0)
{
	llassert(mBuffer);

	if (numbytes <= 0 && mOperation != FILE_RENAME && mOperation != FILE_REMOVE)
	{
		llwarns << "LLLFSThread: Request with numbytes = " << numbytes << llendl;
	}
}

void LLLFSThread::Request::finishRequest()
{
}

void LLLFSThread::Request::deleteRequest()
{
	if (getStatus() == STATUS_QUEUED || getStatus() == STATUS_ABORT)
	{
		llerrs << "Attempt to delete a queued LLLFSThread::Request!" << llendl;
	}	
	if (mOperation == FILE_WRITE)
	{
		if (mFlags & AUTO_DELETE)
		{
			delete mBuffer;
		}
	}
	else if (mOperation == FILE_RENAME)
	{
		LLString* new_name = (LLString*)mBuffer;
		delete new_name;
	}
	LLQueuedThread::QueuedRequest::deleteRequest();
}

bool LLLFSThread::Request::processIO()
{
	bool complete = false;
	if (mOperation ==  FILE_READ)
	{
		llassert(mOffset >= 0);
		apr_file_t* filep = ll_apr_file_open(mFileName, LL_APR_RB);
		if (!filep)
		{
			llwarns << "LLLFS: Unable to read file: " << mFileName << llendl;
			mBytesRead = 0; // fail
			return true;
		}
		if (mOffset < 0)
			ll_apr_file_seek(filep, APR_END, 0);
		else
			ll_apr_file_seek(filep, APR_SET, mOffset);
		mBytesRead = ll_apr_file_read(filep, mBuffer, mBytes );
		apr_file_close(filep);
		complete = true;
		//llinfos << llformat("LLLFSThread::READ '%s': %d bytes",mFileName.c_str(),mBytesRead) << llendl;
	}
	else if (mOperation ==  FILE_WRITE)
	{
		apr_file_t* filep = ll_apr_file_open(mFileName, LL_APR_WB);
		if (!filep)
		{
			llwarns << "LLLFS: Unable to write file: " << mFileName << llendl;
			mBytesRead = 0; // fail
			return true;
		}
		if (mOffset < 0)
			ll_apr_file_seek(filep, APR_END, 0);
		else
			ll_apr_file_seek(filep, APR_SET, mOffset);
		mBytesRead = ll_apr_file_write(filep, mBuffer, mBytes );
		complete = true;
		apr_file_close(filep);
		//llinfos << llformat("LLLFSThread::WRITE '%s': %d bytes",mFileName.c_str(),mBytesRead) << llendl;
	}
	else if (mOperation ==  FILE_RENAME)
	{
		LLString* new_name = (LLString*)mBuffer;
		ll_apr_file_rename(mFileName, *new_name);
		complete = true;
		//llinfos << llformat("LLLFSThread::RENAME '%s': '%s'",mFileName.c_str(),new_name->c_str()) << llendl;
	}
	else if (mOperation ==  FILE_REMOVE)
	{
		ll_apr_file_remove(mFileName);
		complete = true;
		//llinfos << llformat("LLLFSThread::REMOVE '%s'",mFileName.c_str()) << llendl;
	}
	else
	{
		llerrs << llformat("LLLFSThread::unknown operation: %d", mOperation) << llendl;
	}
	return complete;
}

//============================================================================