未验证 提交 adb47574 编写于 作者: O openharmony_ci 提交者: Gitee

!932 fix wirte optimization which fail to write pipe

Merge pull request !932 from Wang Yaofeng/write_optimization_come_back
......@@ -14,6 +14,9 @@
*/
#include "functionalext.h"
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
const char *path = "/data/test.txt";
......@@ -71,10 +74,42 @@ void fwrite_0300(void)
remove(path);
}
void fwrite_0400(void)
{
pid_t childPid = 0;
int fds[2] = {0};
pipe(fds);
int pipeRead = 0;
int pipeWrite = 1;
char buf[1024] = {0};
childPid = fork();
EXPECT_NE("fwrite_0400", childPid, -1);
if (childPid == 0) {
// childr
dup2(fds[pipeWrite], STDOUT_FILENO);
dup2(fds[pipeRead], STDIN_FILENO);
close(fds[pipeWrite]);
close(fds[pipeRead]);
// exec
execl("/bin/sh", "/bin/sh", "-c", "/system/bin/bm get -u", NULL);
exit(0);
} else {
// parent
close(fds[pipeWrite]);
fcntl(fds[pipeRead], F_SETFD, F_DUPFD_CLOEXEC);
int cn = read(fds[pipeRead], buf, sizeof(buf));
EXPECT_MT("fwrite_0400", cn, 0);
}
}
int main(int argc, char *argv[])
{
fwrite_0100();
fwrite_0200();
fwrite_0300();
return t_status;
}
\ No newline at end of file
}
......@@ -2167,6 +2167,8 @@ musl_src_porting_file = [
"src/stdio/__fdopen.c",
"src/stdio/vfprintf.c",
"src/stdio/__stdio_read.c",
"src/stdio/__stdio_write.c",
"src/stdio/__stdout_write.c",
"src/stdio/fread.c",
"src/stdio/fmemopen.c",
"src/stdio/freopen.c",
......
......@@ -63,6 +63,7 @@ hidden off_t __stdio_seek(FILE *, off_t, int);
hidden int __stdio_close(FILE *);
hidden int __fill_buffer(FILE *f);
hidden ssize_t __flush_buffer(FILE *f);
hidden int __toread(FILE *);
hidden int __towrite(FILE *);
......
#include "stdio_impl.h"
#include <sys/uio.h>
#include <string.h>
ssize_t __flush_buffer(FILE *f)
{
ssize_t cnt = 0;
char *wbase = (char *)f->wbase;
size_t rem = f->wpos - f->wbase;
while (rem > 0) {
cnt = syscall(SYS_write, f->fd, wbase, rem);
if (cnt < 0) {
f->wpos = f->wbase = f->wend = 0;
f->flags |= F_ERR;
return cnt;
}
wbase += cnt;
rem -= cnt;
}
/* reset file buffer */
f->wend = f->buf + f->buf_size;
f->wpos = f->wbase = f->buf;
return cnt;
}
size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
{
size_t rem = len;
unsigned char *wbuf = (unsigned char *)buf;
/* flush buffer first */
ssize_t cnt = __flush_buffer(f);
if (cnt < 0) {
return 0;
}
for (;;) {
if (f->lbf < 0 && rem <= f->wend - f->wpos) {
memcpy(f->wpos, wbuf, rem);
f->wpos += rem;
return len;
}
/* write directly if
* 1. file buffer < rem
* 2. line buffer mode
*/
cnt = syscall(SYS_write, f->fd, wbuf, rem);
if (cnt < 0) {
f->wpos = f->wbase = f->wend = 0;
f->flags |= F_ERR;
return len - rem;
}
rem -= cnt;
wbuf += cnt;
if (rem == 0) {
break;
}
}
return len;
}
#include "stdio_impl.h"
#include <sys/ioctl.h>
size_t __stdout_write(FILE *f, const unsigned char *buf, size_t len)
{
struct winsize wsz;
f->write = __stdio_write;
/*
* write directly at first time.
* check f->flags & tty, and take effect later.
*/
size_t cnt = __stdio_write(f, buf, len);
if (!(f->flags & F_SVB) && __syscall(SYS_ioctl, f->fd, TIOCGWINSZ, &wsz))
f->lbf = -1;
return cnt;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册