mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-03-25 13:29:03 +02:00
mkhtemp: unified non-error close handling
Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
@@ -530,6 +530,7 @@ int fsync_on_eintr(int fd);
|
||||
int fs_rename_at(int olddirfd, const char *old,
|
||||
int newdirfd, const char *new);
|
||||
int fs_open(const char *path, int flags);
|
||||
void close_no_err(int *fd);
|
||||
struct filesystem *rootfs(void);
|
||||
int fs_resolve_at(int dirfd, const char *path, int flags);
|
||||
int fs_next_component(const char **p,
|
||||
|
||||
@@ -732,6 +732,22 @@ try_err(int loop_err, int errval)
|
||||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
close_no_err(int *fd)
|
||||
{
|
||||
int saved_errno = errno;
|
||||
|
||||
if (fd == NULL)
|
||||
return;
|
||||
if (*fd < 0)
|
||||
return;
|
||||
|
||||
(void) close_on_eintr(*fd);
|
||||
*fd = -1;
|
||||
|
||||
errno = saved_errno;
|
||||
}
|
||||
|
||||
int
|
||||
close_on_eintr(int fd)
|
||||
{
|
||||
@@ -1134,3 +1150,14 @@ retry:
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -68,7 +68,6 @@ new_tmp_common(int *fd, char **path, int type)
|
||||
char suffix[] = "tmp.XXXXXXXXXX";
|
||||
char *tmpdir = NULL;
|
||||
|
||||
int close_errno;
|
||||
size_t dirlen;
|
||||
size_t destlen;
|
||||
char *dest = NULL; /* final path (will be written into "path") */
|
||||
@@ -155,12 +154,7 @@ new_tmp_common(int *fd, char **path, int type)
|
||||
if (*fd < 0)
|
||||
goto err;
|
||||
|
||||
if (dirfd >= 0) {
|
||||
close_errno = errno;
|
||||
(void) close_on_eintr(dirfd);
|
||||
errno = close_errno;
|
||||
dirfd = -1;
|
||||
}
|
||||
close_no_err(&dirfd);
|
||||
|
||||
errno = saved_errno;
|
||||
*path = dest;
|
||||
@@ -179,19 +173,8 @@ err:
|
||||
dest = NULL;
|
||||
}
|
||||
|
||||
if (dirfd >= 0) {
|
||||
close_errno = errno;
|
||||
(void) close_on_eintr(dirfd);
|
||||
errno = close_errno;
|
||||
dirfd = -1;
|
||||
}
|
||||
|
||||
if (*fd >= 0) {
|
||||
close_errno = errno;
|
||||
(void) close_on_eintr(*fd);
|
||||
errno = close_errno;
|
||||
*fd = -1;
|
||||
}
|
||||
close_no_err(&dirfd);
|
||||
close_no_err(fd);
|
||||
|
||||
/* where a TMPDIR isn't found, and we err,
|
||||
* we pass this back through for the
|
||||
@@ -353,8 +336,8 @@ same_dir(const char *a, const char *b)
|
||||
if (st_a.st_dev == st_b.st_dev &&
|
||||
st_a.st_ino == st_b.st_ino) {
|
||||
|
||||
(void) close_on_eintr(fd_a);
|
||||
(void) close_on_eintr(fd_b);
|
||||
close_no_err(&fd_a);
|
||||
close_no_err(&fd_b);
|
||||
|
||||
success_same_dir:
|
||||
|
||||
@@ -365,8 +348,8 @@ success_same_dir:
|
||||
return 1;
|
||||
}
|
||||
|
||||
(void) close_on_eintr(fd_a);
|
||||
(void) close_on_eintr(fd_b);
|
||||
close_no_err(&fd_a);
|
||||
close_no_err(&fd_b);
|
||||
|
||||
/* FAILURE (logical)
|
||||
*/
|
||||
@@ -379,10 +362,8 @@ err_same_dir:
|
||||
/* FAILURE (probably syscall)
|
||||
*/
|
||||
|
||||
if (fd_a >= 0)
|
||||
(void) close_on_eintr(fd_a);
|
||||
if (fd_b >= 0)
|
||||
(void) close_on_eintr(fd_b);
|
||||
close_no_err(&fd_a);
|
||||
close_no_err(&fd_b);
|
||||
|
||||
if (errno == saved_errno)
|
||||
errno = EIO;
|
||||
@@ -472,9 +453,7 @@ world_writeable_and_sticky(
|
||||
sticky_heaven:
|
||||
/* i like the one in hamburg better */
|
||||
|
||||
if (dirfd >= 0)
|
||||
(void) close_on_eintr(dirfd);
|
||||
|
||||
close_no_err(&dirfd);
|
||||
errno = saved_errno;
|
||||
|
||||
return 1;
|
||||
@@ -486,8 +465,7 @@ sticky_hell:
|
||||
|
||||
saved_errno = errno;
|
||||
|
||||
if (dirfd >= 0)
|
||||
(void) close_on_eintr(dirfd);
|
||||
close_no_err(&dirfd);
|
||||
|
||||
errno = saved_errno;
|
||||
|
||||
@@ -677,12 +655,7 @@ mkhtemp(int *fd,
|
||||
errno = EEXIST;
|
||||
|
||||
err:
|
||||
if (*fd >= 0) {
|
||||
close_errno = errno;
|
||||
(void)close_on_eintr(*fd);
|
||||
errno = close_errno;
|
||||
*fd = -1;
|
||||
}
|
||||
close_no_err(fd);
|
||||
|
||||
success:
|
||||
|
||||
@@ -704,7 +677,6 @@ mkhtemp_try_create(int dirfd,
|
||||
{
|
||||
struct stat st_open;
|
||||
int saved_errno = errno;
|
||||
int close_errno;
|
||||
int rval = -1;
|
||||
|
||||
int file_created = 0;
|
||||
@@ -824,12 +796,7 @@ mkhtemp_try_create(int dirfd,
|
||||
goto out;
|
||||
|
||||
err:
|
||||
close_errno = errno;
|
||||
|
||||
if (fd != NULL && *fd >= 0) {
|
||||
(void) close_on_eintr(*fd);
|
||||
*fd = -1;
|
||||
}
|
||||
close_no_err(fd);
|
||||
|
||||
if (file_created)
|
||||
(void) unlinkat(dirfd, fname_copy, 0);
|
||||
@@ -837,7 +804,6 @@ err:
|
||||
if (dir_created)
|
||||
(void) unlinkat(dirfd, fname_copy, AT_REMOVEDIR);
|
||||
|
||||
errno = close_errno;
|
||||
rval = -1;
|
||||
out:
|
||||
return rval;
|
||||
|
||||
Reference in New Issue
Block a user