Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
f850a556
Commit
f850a556
authored
Apr 07, 2014
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix bug when writing an array of buffers and the last buffer has zero size.
parent
24dc6ae7
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
2 deletions
+68
-2
Makefile.am
c++/Makefile.am
+1
-0
io-test.c++
c++/src/kj/io-test.c++
+60
-0
io.c++
c++/src/kj/io.c++
+7
-2
No files found.
c++/Makefile.am
View file @
f850a556
...
@@ -348,6 +348,7 @@ capnp_test_SOURCES = \
...
@@ -348,6 +348,7 @@ capnp_test_SOURCES = \
src/kj/tuple-test.c++
\
src/kj/tuple-test.c++
\
src/kj/one-of-test.c++
\
src/kj/one-of-test.c++
\
src/kj/function-test.c++
\
src/kj/function-test.c++
\
src/kj/io-test.c++
\
src/kj/mutex-test.c++
\
src/kj/mutex-test.c++
\
src/kj/threadlocal-test.c++
\
src/kj/threadlocal-test.c++
\
src/kj/threadlocal-pthread-test.c++
\
src/kj/threadlocal-pthread-test.c++
\
...
...
c++/src/kj/io-test.c++
0 → 100644
View file @
f850a556
// Copyright (c) 2014, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "io.h"
#include "debug.h"
#include <gtest/gtest.h>
#include <unistd.h>
namespace
kj
{
namespace
{
TEST
(
Io
,
WriteVec
)
{
// Check that writing an array of arrays works even when some of the arrays are empty. (This
// used to not work in some cases.)
int
fds
[
2
];
KJ_SYSCALL
(
pipe
(
fds
));
FdInputStream
in
((
AutoCloseFd
(
fds
[
0
])));
FdOutputStream
out
((
AutoCloseFd
(
fds
[
1
])));
ArrayPtr
<
const
byte
>
pieces
[
5
]
=
{
arrayPtr
(
implicitCast
<
const
byte
*>
(
nullptr
),
0
),
arrayPtr
(
reinterpret_cast
<
const
byte
*>
(
"foo"
),
3
),
arrayPtr
(
implicitCast
<
const
byte
*>
(
nullptr
),
0
),
arrayPtr
(
reinterpret_cast
<
const
byte
*>
(
"bar"
),
3
),
arrayPtr
(
implicitCast
<
const
byte
*>
(
nullptr
),
0
)
};
out
.
write
(
pieces
);
char
buf
[
7
];
in
.
read
(
buf
,
6
);
buf
[
6
]
=
'\0'
;
EXPECT_STREQ
(
"foobar"
,
buf
);
}
}
// namespace
}
// namespace kj
c++/src/kj/io.c++
View file @
f850a556
...
@@ -301,21 +301,26 @@ void FdOutputStream::write(ArrayPtr<const ArrayPtr<const byte>> pieces) {
...
@@ -301,21 +301,26 @@ void FdOutputStream::write(ArrayPtr<const ArrayPtr<const byte>> pieces) {
struct
iovec
*
current
=
iov
.
begin
();
struct
iovec
*
current
=
iov
.
begin
();
// Make sure we don't do anything on an empty write.
// Advance past any leading empty buffers so that a write full of only empty buffers does not
// cause a syscall at all.
while
(
current
<
iov
.
end
()
&&
current
->
iov_len
==
0
)
{
while
(
current
<
iov
.
end
()
&&
current
->
iov_len
==
0
)
{
++
current
;
++
current
;
}
}
while
(
current
<
iov
.
end
())
{
while
(
current
<
iov
.
end
())
{
// Issue the write.
ssize_t
n
=
0
;
ssize_t
n
=
0
;
KJ_SYSCALL
(
n
=
::
writev
(
fd
,
current
,
iov
.
end
()
-
current
),
fd
);
KJ_SYSCALL
(
n
=
::
writev
(
fd
,
current
,
iov
.
end
()
-
current
),
fd
);
KJ_ASSERT
(
n
>
0
,
"writev() returned zero."
);
KJ_ASSERT
(
n
>
0
,
"writev() returned zero."
);
while
(
n
>
0
&&
static_cast
<
size_t
>
(
n
)
>=
current
->
iov_len
)
{
// Advance past all buffers that were fully-written.
while
(
current
<
iov
.
end
()
&&
static_cast
<
size_t
>
(
n
)
>=
current
->
iov_len
)
{
n
-=
current
->
iov_len
;
n
-=
current
->
iov_len
;
++
current
;
++
current
;
}
}
// If we only partially-wrote one of the buffers, adjust the pointer and size to include only
// the unwritten part.
if
(
n
>
0
)
{
if
(
n
>
0
)
{
current
->
iov_base
=
reinterpret_cast
<
byte
*>
(
current
->
iov_base
)
+
n
;
current
->
iov_base
=
reinterpret_cast
<
byte
*>
(
current
->
iov_base
)
+
n
;
current
->
iov_len
-=
n
;
current
->
iov_len
-=
n
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment