OVH Cloud OVH Cloud

mkstemp()

3 réponses
Avatar
Arno
Salut,

Voila j'ai besoin de créer un fichier et ceux de manière unique. J'utilise
donc la fonction mkstemp qui me retourne le file descriptor du fichier ainsi
crée. Je me demandais si il y avais moyen de récupérer le nom du fichier ?
J'avais pensé à la fonction fstat mais je ne récupère que les inodes.

--
Arno - Pour le mail : http://cerbermail.com/?P5oJnDlxNt

3 réponses

Avatar
Laurent Le Boterve
"Arno" a écrit dans le message de
news:bl1n7l$tdk$
Salut,

Voila j'ai besoin de créer un fichier et ceux de manière unique. J'utilise
donc la fonction mkstemp qui me retourne le file descriptor du fichier
ainsi

crée. Je me demandais si il y avais moyen de récupérer le nom du fichier ?
J'avais pensé à la fonction fstat mais je ne récupère que les inodes.

--
Arno - Pour le mail : http://cerbermail.com/?P5oJnDlxNt


Si le nom est important, tu peux utiliser mktemp() qui te retourne un nom

unique pour faire ensuite un creat et un open.

Avatar
Stephane CHAZELAS
Le Fri, 26 Sep 2003 17:50:17 +0200, Arno écrivait :
Voila j'ai besoin de créer un fichier et ceux de manière unique. J'utilise
donc la fonction mkstemp qui me retourne le file descriptor du fichier ainsi
crée. Je me demandais si il y avais moyen de récupérer le nom du fichier ?
J'avais pensé à la fonction fstat mais je ne récupère que les inodes.


Le nom du fichier est dans le template que tu as fourni et que
mkstemp a modifié.

int main() {
char template[] = "/tmp/XXXXXX";
mkstemp(template);
perror("mkstemp");
puts(template);
}

$ ./a.out
mkstemp: Success
/tmp/4q6Bfc

--
Stéphane

Avatar
DINH Viêt Hoà

Si le nom est important, tu peux utiliser mktemp() qui te retourne un nom
unique pour faire ensuite un creat et un open.


Dans tout bon man, il est indiqué la chose suivante pour mktemp :

FreeBSD 4.8 :

BUGS
This family of functions produces filenames which can be guessed, though
the risk is minimized when large numbers of `Xs' are used to increase the
number of possible temporary filenames. This makes the race in mktemp(),
between testing for a file's existence (in the mktemp() function call)
and opening it for use (later in the user application) particularly dan-
gerous from a security perspective. Whenever it is possible, mkstemp()
should be used instead, since it does not have the race condition. If
mkstemp() cannot be used, the filename created by mktemp() should be cre-
ated using the O_EXCL flag to open(2) and the return status of the call
should be tested for failure. This will ensure that the program does not
continue blindly in the event that an attacker has already created the
file with the intention of manipulating or reading its contents.

Linux :

BUGS
Never use mktemp(). Some implementations follow BSD 4.3 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 test-
ing whether the name exists and opening the file, every use of mktemp()
is a security risk. The race is avoided by mkstemp(3).

--
DINH V. Hoa,

"je savais que c'était un fan de tunning et de techno mais quand même" -- sunZ