Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-10

Temporary Filenames in GO32

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jason P Sage

I read an old thread about how to make temporary filenames (unique filenames is what the topic was about) and I have written a simple unit that sort of does this without needing a second to elapse (or millisecond for that matter) to pass in order to get the next unique name. I have heard of a possible unit or routine that MAY be available to us FPC coders that does some sort of GUID algorithm. this would interest me.

My algorithm is platform independent but I know my technique may be frowned upon by many but it works.

I create a file called something intuitive: counterlockfile.lock

Each call to my unit TRIES to open the file for REWRITE - if it can't (because another process has it - then it retries a few times with a sleep/yield call and both the repeat attempts and sleep duration are configurable.

When the file CAN be opened, this means ownership of a second file called something intuitive: mycounter.txt

This file is simply one line of text that is a zero padded string e.g.: 00000001

I'm assuming readers understand there is logic in the code to create the files if they don't already exist and there is a way for all programs wishing to use this little system to know how to locate the same directory that was set up for this purpose.

Each time a program calls the function that reads the counter, it works like an autonumber in a database application, returning the current value and then adding 1 to it and saving the new value out there for the next time.

The cleanup is merely closing the file that is/was named counterlockfile.lock that has been opened for rewrite during the counter read, add one, save, close process.

A bit nasty. but it works - and it works EVERY TIME.

So this message is to one: maybe help a GO32 user who needs unique filenames, and anyone else on (I would assume) any platform.

Again - if there is anyone who knows of a more elegant way to do this reliably - in an environment that may have a ton of different EXE's all trying to do the same thing at the same time - without choking - please let me know!

Jason P Sage

Reply by email

Michael Van Canneyt
to Jason P Sage
Jason P Sage wrote 6 lines
I read an old thread about how to make temporary filenames (unique filenames is what the topic was about) and I have written a simple unit that sort of does this without needing a second to elapse (or millisecond for that matter) to pass in order to get the next unique name. I have heard of a possible unit or routine that MAY be available to us FPC coders that does some sort of GUID algorithm. this would interest me.

What is the problem with the systutils version of gettempfilename ? On windows, this reverts to the OS call. On other platforms, an own implementation is used.

Michael.

Reply by email

Tomas Hajny
to Jason P Sage: I read an old thread about how to make temporary filenames (unique filenames is what the…
Jason P Sage wrote
I read an old thread about how to make temporary filenames (unique filenames

.
.

Show quoted text 10 lines
My algorithm is platform independent but I know my technique may be frowned upon by many but it works. I create a file called something intuitive: counterlockfile.lock Each call to my unit TRIES to open the file for REWRITE - if it can't (because another process has it - then it retries a few times with a sleep/yield call and both the repeat attempts and sleep duration are configurable.

.
.

Actually, I don't think your solution really is platform independent. The main reason for that is that file locking doesn't work the same way on different platforms, so in fact you can have 2 processes having the same file open for writing on some platforms.

In addition to that, your approach doesn't seem to be very efficient from my point of view, because it implies additional disk accesses. Furthermore, it only works across programs created with the same unit, whereas other programs not using your unit (and possibly not created with FPC at all) could easily create files with the same name, thus uniqueness is not really guaranteed.

SysUtils.GetTempFileName is supposed to provide you with such filename, but the appropriate platform specific implementation for GO32v2 is currently missing (i.e. only general version not necessarily guaranteeing uniqueness is in place). I guess I could fix that, because the appropriate API is provided by DOS, but I'd need to check the real implementation to make sure that it really works properly for multiple emulated DOS virtual DOS machines/sessions running under a different operating system.

Tomas

Reply by email

Jason P Sage
Jason P Sage wrote 7 lines
> I read an old thread about how to make temporary filenames (unique > filenames is what the topic was about) and I have written a simple > unit that sort of does this without needing a second to elapse (or > millisecond for that > matter) to pass in order to get the next unique name. I have heard of a > possible unit or routine that MAY be available to us FPC coders that does > some sort of GUID algorithm. this would interest me.
Then Michael wrote 6 lines
What is the problem with the systutils version of gettempfilename ? On windows, this reverts to the OS call. On other platforms, an own implementation is used. Michael.

Hello Michael.

This may work even better! If this works faster than the method I implemented, I'd much rather use it in place of my own. I will have to do some experimenting with it, but if it generates unique filenames under load (multiple programs all using the same call simultaneously) without flaw, than I will definitely adopt the FPC method sysutils.GetTempFilename - for code management, portability reasons, and the likelihood of a performance boost to what I have devised and am using now.

Perhaps the original author who was trying to do this for GO32 (whose email request was a few years old now Nov, 2001 I think) was unaware of this GetTempFileName call, it didn't exist then, or it did not work for him in GO32, at least there are "home made" alternatives one could devise with a little creativity that can accomplish the same thing as I have done.

Thank You,
Jason P Sage

Reply by email