Linux temporary file name

Mktemp Manual

In this mode, the template (if specified) should be a directory component (as opposed to a full path) and thus should not contain any forward slashes.

-u Operate in “unsafe” mode. The temp file will be unlinked before mktemp exits. This is slightly better than mktemp(3) but still introduces a race condition. Use of this option is not encouraged.

The mktemp utility exits with a value of 0 on success or 1 on failure.

ENVIRONMENT

EXAMPLES

The following sh(1) fragment illustrates a simple use of mktemp where the script should quit if it cannot get a safe temporary file.

TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1 echo "program output" >> $TMPFILE

The same fragment with support for a user’s TMPDIR environment variable can be written as follows.

TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1 echo "program output" >> $TMPFILE

This can be further simplified if we don’t care about the actual name of the temporary file. In this case the -t flag is implied.

TMPFILE=`mktemp` || exit 1 echo "program output" >> $TMPFILE

In some cases, it may be desirable to use a default temporary directory other than /tmp . In this example the temporary file will be created in /extra/tmp unless the user’s TMPDIR environment variable specifies otherwise.

TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1 echo "program output" >> $TMPFILE

In other cases, we want the script to catch the error. For instance, if we attempt to create two temporary files and the second one fails we need to remove the first before exiting.

TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1 TMP2=`mktemp -t example.2.XXXXXXXXXX` if [ $? -ne 0 ]; then rm -f $TMP1 exit 1 fi

Or perhaps you don’t want to exit if mktemp is unable to create the file. In this case you can protect that part of the script thusly.

TMPFILE=`mktemp -q -t example.XXXXXXXXXX` && < # Safe to use $TMPFILE in this block echo data >$TMPFILE . rm -f $TMPFILE >

DIAGNOSTICS

One of the following error messages may be displayed if mktemp does not succeed and the -q option was not specified:

Читайте также:  Kaspersky endpoint security 11 linux проверка

insufficient number of Xs in template The specified template contained fewer than six ‘ Xs ’ at the end. template must not contain directory separators in -t mode The template contained one or more directory components and the -t option was specified. cannot make temp dir mktemp was unable to create the temporary directory for any of the reasons specified by mkdir(2). cannot make temp file mktemp was unable to create the temporary file for any of the reasons specified by open(2). cannot allocate memory mktemp was unable to allocate memory for any of the reasons specified by malloc(3).

SEE ALSO

HISTORY

The mktemp utility appeared in OpenBSD 2.1.

Источник

mktemp(3) — Linux man page

Feature Test Macro Requirements for glibc (see feature_test_macros(7)): mktemp(): Since glibc 2.12: _BSD_SOURCE || _SVID_SOURCE || (_XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) Before glibc 2.12: _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED

Description

The mktemp() function generates a unique temporary filename from template. The last six characters of template must be XXXXXX and these are replaced with a string that makes the filename unique. Since it will be modified, template must not be a string constant, but should be declared as a character array.

Return Value

The mktemp() function always returns template. If a unique name was created, the last six bytes of template will have been modified in such a way that the resulting name is unique (i.e., does not exist already) If a unique name could not be created, template is made an empty string.

Errors

The last six characters of template were not XXXXXX.

Conforming To

4.3BSD, POSIX.1-2001. POSIX.1-2008 removes the specification of mktemp().

Notes

The prototype is in for libc4, libc5, glibc1; glibc2 follows the Single UNIX Specification and has the prototype in .

Bugs

Never use mktemp(). Some implementations follow 4.3BSD and replace XXXXXX by the current process ID and a single letter, so that at most 26 different names can be returned. Since on the one hand the names are easy to guess, and on the other hand there is a race between testing whether the name exists and opening the file, every use of mktemp() is a security risk. The race is avoided by mkstemp(3).

Читайте также:  Криптопро jcp linux установка

Источник

Linux temporary file name

mktemp manages the creation of temporary files and directories. Synopsis:

Safely create a temporary file or directory based on template , and print its name. If given, template must include at least three consecutive ‘ X ’s in the last component. If omitted, the template ‘ tmp.XXXXXXXXXX ’ is used, and option —tmpdir is implied. The final run of ‘ X ’s in the template will be replaced by alpha-numeric characters; thus, on a case-sensitive file system, and with a template including a run of n instances of ‘ X ’, there are ‘ 62** n ’ potential file names.

Older scripts used to create temporary files by simply joining the name of the program with the process id (‘ $$ ’) as a suffix. However, that naming scheme is easily predictable, and suffers from a race condition where the attacker can create an appropriately named symbolic link, such that when the script then opens a handle to what it thought was an unused file, it is instead modifying an existing file. Using the same scheme to create a directory is slightly safer, since the mkdir will fail if the target already exists, but it is still inferior because it allows for denial of service attacks. Therefore, modern scripts should use the mktemp command to guarantee that the generated name will be unpredictable, and that knowledge of the temporary file name implies that the file was created by the current script and cannot be modified by other users.

When creating a file, the resulting file has read and write permissions for the current user, but no permissions for the group or others; these permissions are reduced if the current umask is more restrictive.

Here are some examples (although note that if you repeat them, you will most likely get different file names):

    Create a temporary file in the current directory.

$ mktemp --suffix=.txt file-XXXX file-H08W.txt $ mktemp file-XXXX-XXXX.txt file-XXXX-eI9L.txt

$ dir=$(mktemp -p «$» -d dir-XXXX) || exit 1 $ fifo=$dir/fifo $ mkfifo «$fifo» ||

$ file=$(mktemp -q) && < ># Safe to use $file only within this block. Use quotes, > # since $TMPDIR, and thus $file, may contain whitespace. > echo . > "$file" > rm "$file" > >
$ mktemp -u XXX Gb9 $ mktemp -u XXX nzC

The program accepts the following options. Also see Common options.

Читайте также:  Альт линукс пакетный менеджер

Create a directory rather than a file. The directory will have read, write, and search permissions for the current user, but no permissions for the group or others; these permissions are reduced if the current umask is more restrictive.

Suppress diagnostics about failure to create a file or directory. The exit status will still reflect whether a file was created.

Generate a temporary name that does not name an existing file, without changing the file system contents. Using the output of this command to create a new file is inherently unsafe, as there is a window of time between generating the name and using it where another process can create an object by the same name.

Treat template relative to the directory dir . If dir is not specified (only possible with the long option —tmpdir ) or is the empty string, use the value of TMPDIR if available, otherwise use ‘ /tmp ’. If this is specified, template must not be absolute. However, template can still contain slashes, although intermediate directories must already exist.

Append suffix to the template . suffix must not contain slash. If —suffix is specified, template must end in ‘ X ’; if it is not specified, then an appropriate —suffix is inferred by finding the last ‘ X ’ in template . This option exists for use with the default template and for the creation of a suffix that starts with ‘ X ’.

Treat template as a single file relative to the value of TMPDIR if available, or to the directory specified by -p , otherwise to ‘ /tmp ’. template must not contain slashes. This option is deprecated; the use of -p without -t offers better defaults (by favoring the command line over TMPDIR ) and more flexibility (by allowing intermediate directories).

0 if the file was created, 1 otherwise.

Источник

Оцените статью
Adblock
detector