NaviServer - programmable web server
4.99  5.0

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

ns_mktemp(n) 5.0.0a naviserver "NaviServer Built-in Commands"

Name

ns_mktemp - Make a unique temporary filename

Table Of Contents

Synopsis

Description

This command is a wrapper around the mktemp(3) function in the C standard library. This function generates a unique temporary filename from an optional template.

In general, the pattern of creating a temporary filename based on its non-occurrence in a directory followed by creating this file is prone to a race condition and can provide security issues. Therefore, the command is deprecated and will issue an entry to the log file unless ?-nocomplain? is used.

In order to create temp files, the Tcl command file tempfile should be used (introduced in Tcl 8.6), which is safe against race conditions. For creating temporary directories, use ns_mkdtemp (introduced in NaviServer 5) or file tempdir in Tcl9.

There are certain situations, where it is not easy to replace ns_mktemp, e.g. in situations, where unique names are passed to external programs. This situation can often be solved by changing the logic by e.g. writing to a temporary file and then pass the name to the external program, or by using other means for creating unique names, such as ns_uuid.

COMMANDS

ns_mktemp ?-nocomplain? ?template?

This command generates a unique temporary filename using optionally a template as argument, which is a string whose last six characters must be XXXXXX, and returns a new string where those are replaced with random characters such as to make the string a unique filename. It is commonly used to create temporary filenames.

If this command is called without the optional template parameter, it behaves like

 ns_mktemp [ns_config ns/parameters tmpdir]/ns-XXXXXX

EXAMPLES

 % ns_mktemp /tmp/foo-XXXXXX
 /tmp/foo-rhaGGx

See Also

ns_mkdtemp, ns_uuid

Keywords

global built-in, tmp, tmpfile