Secure temporary file: Difference between revisions

From Rosetta Code
Content added Content deleted
(New page: {{Task}} Create a temporary file, '''securely and exclusively''' (opening it such that there are no possible race conditions). It's fine assuming local filesystem semantics (NFS or other...)
 
No edit summary
Line 2: Line 2:


Create a temporary file, '''securely and exclusively''' (opening it such that there are no possible race conditions). It's fine assuming local filesystem semantics (NFS or other networking filesystems can have signficantly more complicated semantics for satisfying the "no race conditions" criteria). The function should automatically resolve name collisions and should only fail in cases where permission is denied, the filesystem is read-only or full, or similar conditions exist (returning an error or raising an exception as appropriate to the language/environment).
Create a temporary file, '''securely and exclusively''' (opening it such that there are no possible race conditions). It's fine assuming local filesystem semantics (NFS or other networking filesystems can have signficantly more complicated semantics for satisfying the "no race conditions" criteria). The function should automatically resolve name collisions and should only fail in cases where permission is denied, the filesystem is read-only or full, or similar conditions exist (returning an error or raising an exception as appropriate to the language/environment).

=={{header|Ada}}==
Ada creates a temporary file whenever the create procedure is called with file name. That temporary file is automatically deleted at the end of the program creating the file.

This example creates a temporary file, writes to the file, then reads from the file.

with Ada.Text_Io; use Ada.Text_Io;
procedure Temp_File is
Temp : File_Type;
Contents : String(1..80);
Length : Natural;
begin
-- Create a temporary file
Create(File => Temp);
Put_Line(File => Temp, Item => "Hello World");
Reset(File => Temp, Mode => In_File);
Get_Line(File => Temp, Item => Contents, Last => Length);
Put_Line(Contents(1..Length));
end Temp_File;





Revision as of 02:08, 23 October 2007

Task
Secure temporary file
You are encouraged to solve this task according to the task description, using any language you may know.

Create a temporary file, securely and exclusively (opening it such that there are no possible race conditions). It's fine assuming local filesystem semantics (NFS or other networking filesystems can have signficantly more complicated semantics for satisfying the "no race conditions" criteria). The function should automatically resolve name collisions and should only fail in cases where permission is denied, the filesystem is read-only or full, or similar conditions exist (returning an error or raising an exception as appropriate to the language/environment).

Ada

Ada creates a temporary file whenever the create procedure is called with file name. That temporary file is automatically deleted at the end of the program creating the file.

This example creates a temporary file, writes to the file, then reads from the file.

with Ada.Text_Io; use Ada.Text_Io;

procedure Temp_File is
   Temp : File_Type;
   Contents : String(1..80);
   Length : Natural;
begin
   -- Create a temporary file
   Create(File => Temp);
   Put_Line(File => Temp, Item => "Hello World");
   Reset(File => Temp, Mode => In_File);
   Get_Line(File => Temp, Item => Contents, Last => Length);
   Put_Line(Contents(1..Length));  
end Temp_File;


Python

Interpreter: Python 2.3 and later

import tempfile
tfile = tempfile.TemporaryFile()
   # Will be "anonymous" under UNIX-like systems
   # Implicitly uses $TMPDIR from UNIX/Linux or %TEMP for MS Windows, etc.
tfilen = tempfile.NamedTemporaryFile()
   # Will be automatically unlinked (removed) on tfilen.close()
   # (use os.link() to preserve temporary file contents after close())
print tfile.name   # Somthing like: /tmp/tmpXXXXXX --- with random alphanumerics for XXXXXX
mytf = tempfile.NamedTemporaryFile(dir="/some/special/directory", prefix="myTempFile", suffix="TMP")
   # "keyword arguments" can over-ride default location and other aspects of temporary file creation

Note: prior to version 2.3 programmers had to use their own calls to tempfile.mkstemp() and os.open() (with special flags and other options) in order to correctly avoid race conditions. The programmer was also responsible for removing the file (possibly in a try: .... finally block or using the atexit module to register the actions to occur as part of the normal interpreter shutdown.