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

允我心安 提交于 2019-12-30 01:26:06

问题


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


回答1:


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); 



回答2:


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.



来源:https://stackoverflow.com/questions/1082590/how-to-create-temporary-file-0x100-to-accelerate-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!