Skip to content

Commit

Permalink
lib/fs/mkstemp/: mkostemp(): Split API from fmkomstemp()
Browse files Browse the repository at this point in the history
This reduces the complexity of fmkomstemp().

Signed-off-by: Alejandro Colomar <alx@kernel.org>
  • Loading branch information
alejandro-colomar committed Dec 6, 2024
1 parent 3dfeb6f commit f6224cb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ libshadow_la_SOURCES = \
fputsx.c \
fs/mkstemp/fmkomstemp.c \
fs/mkstemp/fmkomstemp.h \
fs/mkstemp/mkomstemp.c \
fs/mkstemp/mkomstemp.h \
fs/readlink/areadlink.c \
fs/readlink/areadlink.h \
fs/readlink/readlinknul.c \
Expand Down
9 changes: 3 additions & 6 deletions lib/fs/mkstemp/fmkomstemp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "fs/mkstemp/mkomstemp.h"


inline FILE *fmkomstemp(char *template, unsigned int flags, mode_t m);

Expand All @@ -24,13 +24,10 @@ fmkomstemp(char *template, unsigned int flags, mode_t m)
int fd;
FILE *fp;

fd = mkostemp(template, flags);
fd = mkomstemp(template, flags, m);
if (fd == -1)
return NULL;

if (fchmod(fd, m) == -1)
goto fail;

fp = fdopen(fd, "w");
if (fp == NULL)
goto fail;
Expand Down
12 changes: 12 additions & 0 deletions lib/fs/mkstemp/mkomstemp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include "fs/mkstemp/mkomstemp.h"

#include <sys/types.h>


extern inline int mkomstemp(char *template, unsigned int flags, mode_t m);
40 changes: 40 additions & 0 deletions lib/fs/mkstemp/mkomstemp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_
#define SHADOW_INCLUDE_LIB_FS_MKSTEMP_MKOMSTEMP_H_


#include <config.h>

#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>


inline int mkomstemp(char *template, unsigned int flags, mode_t m);


inline int
mkomstemp(char *template, unsigned int flags, mode_t m)
{
int fd;

fd = mkostemp(template, flags);
if (fd == -1)
return -1;

if (fchmod(fd, m) == -1)
goto fail;

return fd;
fail:
close(fd);
unlink(template);
return -1;
}


#endif // include guard

0 comments on commit f6224cb

Please sign in to comment.