POSIXにmktempコマンドがないのはなぜですか?

POSIXにmktempコマンドがないのはなぜですか?

シェルスクリプトが実行する最も一般的な作業の1つは、一時ファイルを作成して操作することです。名前の競合を避け、競合状態を避け、ファイルに正しい権限があることを確認する必要があるため、安全に実行することは困難です。 (よりGNU Coreutils マニュアルそしてこのマイナーな兆候のブログ投稿ほとんどのUnixシリーズオペレーティングシステムは、これらのmktemp問題をすべて解決する単一のコマンドを提供することでこの問題を解決します。しかし、これらのコマンドの構文と意味はmktemp標準化されていません。。本当に安全に一時ファイルを作成したい場合そしてポータブル、あなたは頼る必要があります醜いパッチワークたとえば、次のようになります。

tmpfile=$(
  echo 'mkstemp(template)' |
    m4 -D template="${TMPDIR:-/tmp}/baseXXXXXX"
) || exit

m4(この回避策は、マクロプロセッサがPOSIXの一部であり、m4POSIXで定義されたC標準ライブラリ関数を公開するという事実を利用します。)mkstemp()

これらすべてを考えると、POSIXがmktempコマンドの存在と少なくともいくつかの動作の側面を保証するコマンドを標準化しないのはなぜですか?これはPOSIX委員会の明白な監督ですか、それともmktemp委員会が実際に議論した標準化のアイデアが技術的な理由や他の理由で拒否されたのでしょうか。

ベストアンサー1

これはAustin Groupのメーリングリストに頻繁に登場し、Open Groupがそれを指定することに反対するとは思わない。アドバイスをしてくれる人が必要なだけです。たとえば、Eric Blake(毎週のPOSIX会議に出席するRed Hat)の2011年のメッセージを参照してください(ここではgmaneからコピー)。

Date: Tue, 10 May 2011 07:13:32 -0600
From: Eric Blake <[email protected]>
To: Nico Schottelius <[email protected]>
Cc: austin-group-l-7882/[email protected]
Newsgroups: gmane.comp.standards.posix.austin.general
Subject: Re: No mktemp in posix?
Organization: Red Hat
Message-ID: <[email protected]>
References: <[email protected]>
Xref: news.gmane.org gmane.comp.standards.posix.austin.general:4151

On 05/10/2011 04:50 AM, Nico Schottelius wrote:
> Good morning,
>
> digging through Issue 7, I haven't found any utility that gives
> the ability to create a secure, temporary file that is usually
> implemented in mktemp.

echo 'mkstemp(fileXXXXXX)' | m4

will output the name of a just-created temporary file.  However, I agree
that there does not seem to be any standardized utility that gives
mkdtemp functionality, which is often more useful than mkstemp (after
all, once you have a secure temporary directory, then you can create
secure fifos within that directory, rather than having to wish for a
counterpart 'mkftemp' function).

> Is there no mktemp utility by intent or can we add it in the
> next issue?

I know both BSD and GNU have a mktemp(1) that wraps mktemp(), mkstemp(),
and mkdtemp().  In my inbox, I have record of some off-list email in
February of this year regarding some work between those teams to try and
converge on some common functionality and to word that in a manner
appropriate for the standard, although I can't find any publicly
archived messages to that effect.  But yes, I think adding mktemp(1) to
the next revision of the standard would be worthwhile.  I'll try to
revive those efforts and actually post some proposed wording.

--
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

(読む価値がある)最近の投稿では、Geoff Clare(Open Groupに所属)は次のように語っています。

Date: Wed, 2 Nov 2016 15:13:46 +0000
From: Geoff Clare <gwc-7882/[email protected]>
To: austin-group-l-7882/[email protected]
Newsgroups: gmane.comp.standards.posix.austin.general
Subject: Re: [1003.1(2013)/Issue7+TC1 0001016]: race condition with set -C
Message-ID: <[email protected]>
Xref: news.gmane.org gmane.comp.standards.posix.austin.general:13408

Stephane Chazelas <[email protected]> wrote, on 02 Nov 2016:
>
> At the moment, there's no way (that I know) to create a temp file
> reliably with POSIX utilities

Given an m4 utility that conforms to the 2008 standard, there is:

tmpfile=$(echo 'mkstemp(/tmp/fooXXXXXX)' | m4)

However, I don't know how widespread support for the new mkstemp()
macro is.

--
Geoff Clare <g.clare-7882/[email protected]>
The Open Group, Apex Plaza, Forbury Road, Reading, RG1 1AX, England

(あなたの質問で言及したトリックをここで学びました。)

おすすめ記事