Raised This Month: $ Target: $400
 0% 

Reserve a file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sn4k3
Senior Member
Join Date: Nov 2005
Old 07-20-2008 , 19:14   Reserve a file
Reply With Quote #1

there are any way using/without using SDK (filesystem) functions to reserve a file for writing?

like, reserve file: "cstrike/myfile.txt" for users/plugins/server/etc.. not write on that file

I thinked on fopen() on write mod, but if server crash when file is reserved will create a empty file, any other way?

Other OffTopic question:
Is safe fire events in a threaded function?

thanks.

Last edited by sn4k3; 07-20-2008 at 21:51. Reason: reverve thing :P
sn4k3 is offline
Send a message via MSN to sn4k3
BAILOPAN
Join Date: Jan 2004
Old 07-20-2008 , 21:46   Re: Reverve a file
Reply With Quote #2

I hate to tell you but there is no such word as "reverved." You used it like three times. Did you mean reserve? Or reverse?

HL2 is generally not thread safe.
__________________
egg
BAILOPAN is offline
sn4k3
Senior Member
Join Date: Nov 2005
Old 07-20-2008 , 21:57   Re: Reverve a file
Reply With Quote #3

Quote:
Originally Posted by BAILOPAN View Post
I hate to tell you but there is no such word as "reverved." You used it like three times. Did you mean reserve? Or reverse?

HL2 is generally not thread safe.
lol yes i mistake the word is reserve sorry

about events things:
i think im using the best way to safe fire events out of thread functions:

Code:
void fireQueuedEvents()
{
	for(int i = 0; i < queueEvent.Count(); i++)
	{
		IGameEvent *event = gameeventmanager2->CreateEvent("event_name");
		if(event)
		{
			event->SetString("source", queueEvent[i].srcFile);
			// ...
			// ...
			if(!gameeventmanager2->FireEvent(event))
				Warning(""PLUGIN_TAG_LONG" Problems on fire event 'event_name', skipping that!\n");
		}
		else
			Warning(""PLUGIN_TAG_LONG" Couldn't initalize event 'event_name', skipping that!\n");
		queueEvent.Remove(i);
	}
}
Code:
void CServerToolsPlugin::GameFrame( bool simulating )
{
	//Msg("Tick: %f - %f\n", gpGlobals->curtime, engine->Time());
	fireQueuedEvents();
	float curtime = engine->Time();
	if (fLastCurtime == 0)
	{
		fLastCurtime = curtime;
		return;
	}
	if ((curtime - fLastCurtime) >= st_gameframe.GetFloat())
	{
		fLastCurtime = curtime;
		//Msg("In gameframe after 2s\n");
	}
}
After inside thread function i AddTo tail

Code:
event_struct fakeEvent;
fakeEvent.srcFile = srcFile;
...
...
queueEvent.AddToTail(fakeEvent);
is that correct? or should i fire event directly from threaded func?

--

and about reserve a file?

Last edited by sn4k3; 07-20-2008 at 22:03.
sn4k3 is offline
Send a message via MSN to sn4k3
BAILOPAN
Join Date: Jan 2004
Old 07-20-2008 , 22:52   Re: Reserve a file
Reply With Quote #4

You should never call HL2 functions from another thread unless they explicitly say you can do so.

I do not understand your use case for "reserving files."
__________________
egg
BAILOPAN is offline
sn4k3
Senior Member
Join Date: Nov 2005
Old 07-21-2008 , 07:57   Re: Reserve a file
Reply With Quote #5

Quote:
Originally Posted by BAILOPAN View Post
You should never call HL2 functions from another thread unless they explicitly say you can do so.

I do not understand your use case for "reserving files."
because my thread can long for some time like 15s - 30s, just prevent server or client write a new file in that place, that cant be deleted by system

for now i just use:
in thread function @ START:

Code:
if(g_pFullFileSystem->FileExists(destFile, "MOD"))
{
	if(!replaceDest)
		return false;
	else
		fakeEvent.replaceDest = true; // Event stuff

	if(!g_pFullFileSystem->IsFileWritable(destFile, "MOD"))
	{
		if(!g_pFullFileSystem->SetFileWritable(destFile, true, "MOD"))
		{
			Warning(""PLUGIN_TAG_LONG" Can't write/set writeble file: %s\n", destFile);
			return false;
		}
	}
}
in thread func @ END

Code:
if(!g_pFullFileSystem->RenameFile(tempFile, destFile, "MOD"))
{
	Warning(""PLUGIN_TAG_LONG" Can't rename temp file to original destination [%s]! deleting temp file\n", destFile);
	g_pFullFileSystem->RemoveFile(tempFile, "MOD");
	return false;
}
sn4k3 is offline
Send a message via MSN to sn4k3
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:40.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode