Differences

This shows you the differences between two versions of the page.

Link to this comparison view

file_exists_considered_harmful [2013/01/14 13:41]
sparre created - with open-or-create example
file_exists_considered_harmful [2013/01/16 08:24] (current)
sparre Read to end of file example
Line 23: Line 23:
               Mode => Append_File);​               Mode => Append_File);​
 end Open_Or_Create;</​code>​ end Open_Or_Create;</​code>​
 +
 +====== Similar file system race conditions ======
 +
 +Not only "​Ada.Directories.Exists"​ is dangerous and a possible source of race conditions. ​ Other functions checking the state of the file system should also be used with care.  Below are a few (so far only one) examples.
 +
 +===== Read to end of file =====
 +
 +This procedure will read every line in a text file and send it to processing until it reaches the end of the file.  Then it will close the file.  Even if our loop checked ''​End_Of_File (File)''​ before calling "​Get_Line (File)",​ we would still have to handle exception End_Error, as another process/​task might change the contents/​size of the file in parallel with this procedure.
 +
 +<code Ada>type Processor is access procedure (Item : in String);
 +
 +procedure Process (File         : in out File_Type;
 +                   ​Line_Handler : in     ​Processor) is
 +begin
 +   loop
 +      Line_Handler (Get_Line (File));
 +   end loop;
 +exception
 +   when End_Error =>
 +      Close (File);
 +   when others =>
 +      raise;
 +end Process;</​code>​
  

Navigation