ファイルに書き込み、定期的にシーケンス名に名前を変更して、新しい名前を作成するDelphi2006アプリがあります。
open file.dat
write a record to file.dat
close file.dat
...
open file.dat
write a record to file.dat
close file.dat
rename file.dat to file20110818123456.dat
create file.dat
open file.dat
write a record to file.dat
close file.dat
...
問題は、名前の変更が。で失敗することがあることError 32 - The process cannot access the file because it is being used by another process
です。名前の変更先が存在しないことを確認しています。これは、レコードを書き込んだ後のファイルのクローズがすぐには発生しないかのようです。名前の変更手順にループを追加して、少しスリープし、最大10回再試行しましたが、それは役に立ちません。
function RenameFileAtAllCosts (const OldFileID : TFilename ;
const NewFileID : TFilename ;
out ErrorCode : Integer) : boolean ;
const
MaxRenameAttempts = 10 ;
var
RenameAttempts : integer ;
begin
Result := FileExists (OldFileID) ;
if (Result = true) then
begin
if FileExists (NewFileID) then
begin
Result := DeleteFile (PChar (NewFileID)) ;
end ;
if (Result = true) then
begin
Result := (not FileExists (NewFileID)) ;
end ;
if (Result = true) then
begin
RenameAttempts := 0 ;
repeat
SysUtils.Sleep (50) ;
Result := RenameFile (OldFileID, NewFileID) ;
inc (RenameAttempts)
until (Result or (RenameAttempts >= MaxRenameAttempts)) ;
end ;
end ;
if (not Result) then
begin
ErrorCode := GetLastError ;
end ;
end ;
* 追加情報 *
これは、関連するディスクI / OのProcMonログであり、各ブロックの前に関連するPascalソースまたは疑似コードがあります
open file.dat
write a record to file.dat
close file.dat
5:45:27.1718325 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Desired Access: Generic Read/Write, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: n/a, OpenResult: Opened
5:45:27.1719739 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,208, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:27.1720475 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,208, Length: 28, Priority: Normal
5:45:27.1721403 PM APP.exe 1276 CloseFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS
if FileExists (NewFileID) then // before call to RenameFileAtAllCosts
begin
5:45:27.8630005 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP20110819054527.DAT NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
if DeleteFile (NewFileID) then // before call to RenameFileAtAllCosts
begin
5:45:27.8634050 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP20110819054527.DAT NAME NOT FOUND Desired Access: Read Attributes, Delete, Disposition: Open, Options: Non-Directory File, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
Result := FileExists (OldFileID) ;
5:45:27.8640878 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
5:45:27.8641684 PM APP.exe 1276 QueryBasicInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS CreationTime: 2/08/2011 7:45:59 PM, LastAccessTime: 19/08/2011 4:25:33 PM, LastWriteTime: 19/08/2011 5:45:27 PM, ChangeTime: 19/08/2011 5:45:27 PM, FileAttributes: A
5:45:27.8641902 PM APP.exe 1276 CloseFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS
if FileExists (NewFileID) then
begin
5:45:27.8648698 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP20110819054527.DAT NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
if (Result = true) then
begin
Result := (not FileExists (NewFileID)) ;
end ;
5:45:27.8656780 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP20110819054527.DAT NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
RenameAttempts := 0 ;
repeat
SysUtils.Sleep (50) ;
Result := RenameFile (OldFileID, NewFileID) ;
inc (RenameAttempts)
until (Result or (RenameAttempts >= MaxRenameAttempts)) ;
5:45:27.9211195 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:27.9834427 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.0459285 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.1084086 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.1710646 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.2335139 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.2959037 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.3584062 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.4209304 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
5:45:28.4834629 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SHARING VIOLATION Desired Access: Read Attributes, Delete, Synchronize, Disposition: Open, Options: Synchronous IO Non-Alert, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
open file.dat
write several records to file.dat
close file.dat
5:45:28.4899722 PM APP.exe 1276 CreateFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Desired Access: Generic Read/Write, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: n/a, OpenResult: Opened
5:45:28.4901002 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,236, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4901636 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,236, Length: 28, Priority: Normal
5:45:28.4902365 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,264, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4903031 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,264, Length: 28, Priority: Normal
5:45:28.4903517 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,292, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4905200 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,292, Length: 28, Priority: Normal
5:45:28.4905917 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,320, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4906633 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,320, Length: 28, Priority: Normal
5:45:28.4907120 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,348, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4907747 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,348, Length: 28, Priority: Normal
5:45:28.4908214 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,376, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4908841 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,376, Length: 28, Priority: Normal
5:45:28.4909308 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,404, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4909929 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,404, Length: 28, Priority: Normal
5:45:28.4910396 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,432, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4911023 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,432, Length: 28, Priority: Normal
5:45:28.4911491 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,460, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4912118 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,460, Length: 28, Priority: Normal
5:45:28.4912578 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,488, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4913206 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,488, Length: 28, Priority: Normal
5:45:28.4913673 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,516, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4914300 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,516, Length: 28, Priority: Normal
5:45:28.4914761 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,544, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4915388 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,544, Length: 28, Priority: Normal
5:45:28.4915855 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,572, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4916482 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,572, Length: 28, Priority: Normal
5:45:28.4916936 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,600, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4917570 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,600, Length: 28, Priority: Normal
5:45:28.4918043 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,628, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4919003 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,628, Length: 28, Priority: Normal
5:45:28.4919483 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,656, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4920110 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,656, Length: 28, Priority: Normal
5:45:28.4920577 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,684, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4921205 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,684, Length: 28, Priority: Normal
5:45:28.4921672 PM APP.exe 1276 QueryStandardInformationFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS AllocationSize: 57,344, EndOfFile: 54,712, NumberOfLinks: 1, DeletePending: False, Directory: False
5:45:28.4922299 PM APP.exe 1276 WriteFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS Offset: 54,712, Length: 28, Priority: Normal
5:45:28.4922843 PM APP.exe 1276 CloseFile C:\Users\Public\Documents\APP\Datafile\APP.DAT SUCCESS
* アップデート *
私はそれを釘付けにしたと思います。handle.exeをダウンロードして実行しました。プロセスエクスプローラーは常にAPP.DATに一致するハンドルを表示しませんが、handle.exeユーティリティは、ファイル名としてAPP.DATを使用してPID 4(システム)が所有するハンドルのリストを表示しました。OPで言及していなかったのは、同じアプリが別のネットワークマシンで、APP * .DATファイルに書き込まず、それらから読み取るだけのセカンダリ「モニター」モードで実行されていることです。「あはは!」モニターアプリも実行しているときにのみ問題が発生し、ハンドルのリークが「システム」プロセスに属していることを示したのはその瞬間でした。おそらく、ネットワークマシンからファイルを開いたときにハンドルが作成されました。
時折、モニターアプリがファイルへのアクセスを拒否され(常にエラー998-メモリ位置への無効なアクセス-何らかの理由で)、エラーパスがハンドルを閉じませんでした。その時点から、アプリのプライマリインスタンスはファイルAPP.DATの読み取りと書き込みを行うことができましたが、名前の変更に失敗しました。エラー時にファイルが閉じられたことを確認すると、修正されたようです。
「handle.exe」へのポインタが重要であり、別の開いたハンドルを使用してファイルに書き込むことはできますが、名前の変更は失敗します。皆さんの貢献に感謝します。