Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create temporary file (0x100) to accelerate application

I have seen that Windows system use temporary files to increase the performance of some tasks. Those files are marked with the 0x100 attribute when i look at them. I have got the following text from Microsoft: "

By using CreateFile() with the FILE_ATTRIBUTE_TEMPORARY flag, you let the system know that the file is likely to be short lived. The temporary file is created as a normal file. The system needs to do a minimal amount of lazy writes to the file system to keep the disk structures (directories and so forth) consistent. This gives the appearance that the file has been written to the disk ."

Any example of creating such temporary file using Delphi?

Thanks.

[EDIT]

Complementary question: what could be the context of using such file, example, could it be used for a log system. the log being this file with the temp attribute? Would it be faster and less memory prone when log get very large?

[EDIT]

Ok i have create file using solution given by schnaader below with the FILE_ATTRIBUTE_TEMPORARY:

hMyFile := CreateFile(FileName,
                      GENERIC_WRITE,
                      0,
                      nil,
                      CREATE_ALWAYS,
                      FILE_ATTRIBUTE_TEMPORARY,
                      0); 

Such file get the 0x120 attribute when created. Thus a temporary file according to system.

I have also create a file with the FILE_FLAG_DELETE_ON_CLOSE flag (see this article by L. Osterman).

So:

hMyFile := CreateFile(FileName,
                      GENERIC_WRITE,
                      0,
                      nil,
                      CREATE_ALWAYS,
                      FILE_FLAG_DELETE_ON_CLOSE,
                      0);

This file gets no attribute and the file is automatically deleted when the application is closed or destroyed.

I did not find how to combine the attribute and the flag. Any idea?

Thanks

like image 391
volvox Avatar asked Jul 04 '09 17:07

volvox


2 Answers

Well, how about using the CreateFile() method?

var
  FileName : PChar;
  hMyFile : THandle;

...

hMyFile := CreateFile(FileName,
                      GENERIC_WRITE,
                      0,
                      nil,
                      CREATE_ALWAYS,
                      FILE_ATTRIBUTE_TEMPORARY,
                      0); 

if (hMyFile = INVALID_HANDLE_VALUE) then begin
  // Error
end;

...

CloseHandle(hMyFile);

To combine the flag with FILE_FLAG_DELETE_ON_CLOSE, use or:

hMyFile := CreateFile(FileName,
                      GENERIC_WRITE,
                      0,
                      nil,
                      CREATE_ALWAYS,
                      FILE_ATTRIBUTE_TEMPORARY or FILE_FLAG_DELETE_ON_CLOSE,
                      0); 
like image 71
schnaader Avatar answered Sep 28 '22 08:09

schnaader


The temporary flag just indicates that you'd like the write to the disk to be as late as possible. This is quite the contrary you'd want for a log file, as you'd like to get the content of the file written safly to disk so you can access it even in case of a system failure.

To quote from the MSDN page fro CreateFile():

Specifying the FILE_ATTRIBUTE_TEMPORARY attribute causes file systems to avoid writing data back to mass storage if sufficient cache memory is available, because an application deletes a temporary file after a handle is closed. In that case, the system can entirely avoid writing the data. Although it doesn't directly control data caching in the same way as the previously mentioned flags, the FILE_ATTRIBUTE_TEMPORARY attribute does tell the system to hold as much as possible in the system cache without writing and therefore may be of concern for certain applications.

In my opinion, you'd only use it, if it really is temporary data which you are going to delete anyways after you are done with it, e.g. things you'd write to %TEMP% directory.

like image 38
Martin C. Avatar answered Sep 28 '22 08:09

Martin C.