mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-09 23:00:07 +00:00
support: Close fds in copy_func
copy_func may leave file descriptors open on error, so close them on function exit.
This commit is contained in:
parent
7eeb05a395
commit
75fe6d1a16
@ -93,8 +93,9 @@ copy_func (char **argv)
|
|||||||
{
|
{
|
||||||
char *sname = argv[0];
|
char *sname = argv[0];
|
||||||
char *dname = argv[1];
|
char *dname = argv[1];
|
||||||
int sfd, dfd;
|
int sfd = -1, dfd = -1;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
int ret = 1;
|
||||||
|
|
||||||
sfd = open (sname, O_RDONLY);
|
sfd = open (sname, O_RDONLY);
|
||||||
if (sfd < 0)
|
if (sfd < 0)
|
||||||
@ -108,7 +109,7 @@ copy_func (char **argv)
|
|||||||
{
|
{
|
||||||
fprintf (stderr, "cp: unable to fstat %s: %s\n",
|
fprintf (stderr, "cp: unable to fstat %s: %s\n",
|
||||||
sname, strerror (errno));
|
sname, strerror (errno));
|
||||||
return 1;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
dfd = open (dname, O_WRONLY | O_TRUNC | O_CREAT, 0600);
|
dfd = open (dname, O_WRONLY | O_TRUNC | O_CREAT, 0600);
|
||||||
@ -116,22 +117,26 @@ copy_func (char **argv)
|
|||||||
{
|
{
|
||||||
fprintf (stderr, "cp: unable to open %s for writing: %s\n",
|
fprintf (stderr, "cp: unable to open %s for writing: %s\n",
|
||||||
dname, strerror (errno));
|
dname, strerror (errno));
|
||||||
return 1;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (support_copy_file_range (sfd, 0, dfd, 0, st.st_size, 0) != st.st_size)
|
if (support_copy_file_range (sfd, 0, dfd, 0, st.st_size, 0) != st.st_size)
|
||||||
{
|
{
|
||||||
fprintf (stderr, "cp: cannot copy file %s to %s: %s\n",
|
fprintf (stderr, "cp: cannot copy file %s to %s: %s\n",
|
||||||
sname, dname, strerror (errno));
|
sname, dname, strerror (errno));
|
||||||
return 1;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
close (sfd);
|
ret = 0;
|
||||||
close (dfd);
|
|
||||||
|
|
||||||
chmod (dname, st.st_mode & 0777);
|
chmod (dname, st.st_mode & 0777);
|
||||||
|
|
||||||
return 0;
|
out:
|
||||||
|
if (sfd >= 0)
|
||||||
|
close (sfd);
|
||||||
|
if (dfd >= 0)
|
||||||
|
close (dfd);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user