Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
folly
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
folly
Commits
c7796707
Commit
c7796707
authored
Nov 13, 2012
by
Tudor Bosman
Committed by
Jordan DeLong
Dec 16, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix ~File crash in debug mode
Test Plan: test added Reviewed By: tjackson@fb.com FB internal diff: D630163
parent
e4d6eb2a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
93 additions
and
7 deletions
+93
-7
folly/experimental/File.cpp
folly/experimental/File.cpp
+0
-1
folly/experimental/FileGen-inl.h
folly/experimental/FileGen-inl.h
+7
-6
folly/experimental/test/FileTest.cpp
folly/experimental/test/FileTest.cpp
+86
-0
No files found.
folly/experimental/File.cpp
View file @
c7796707
...
@@ -37,7 +37,6 @@ void File::close() {
...
@@ -37,7 +37,6 @@ void File::close() {
}
}
bool
File
::
closeNoThrow
()
{
bool
File
::
closeNoThrow
()
{
DCHECK
(
fd_
!=
-
1
);
int
r
=
ownsFd_
?
::
close
(
fd_
)
:
0
;
int
r
=
ownsFd_
?
::
close
(
fd_
)
:
0
;
release
();
release
();
return
r
==
0
;
return
r
==
0
;
...
...
folly/experimental/FileGen-inl.h
View file @
c7796707
...
@@ -37,8 +37,10 @@ class FileReader : public GenImpl<ByteRange, FileReader> {
...
@@ -37,8 +37,10 @@ class FileReader : public GenImpl<ByteRange, FileReader> {
template
<
class
Body
>
template
<
class
Body
>
bool
apply
(
Body
&&
body
)
const
{
bool
apply
(
Body
&&
body
)
const
{
for
(;;)
{
for
(;;)
{
ssize_t
n
=
::
read
(
file_
.
fd
(),
buffer_
->
writableTail
(),
ssize_t
n
;
buffer_
->
capacity
());
do
{
n
=
::
read
(
file_
.
fd
(),
buffer_
->
writableTail
(),
buffer_
->
capacity
());
}
while
(
n
==
-
1
&&
errno
==
EINTR
);
if
(
n
==
-
1
)
{
if
(
n
==
-
1
)
{
throw
std
::
system_error
(
errno
,
std
::
system_category
(),
"read failed"
);
throw
std
::
system_error
(
errno
,
std
::
system_category
(),
"read failed"
);
}
}
...
@@ -91,11 +93,10 @@ class FileWriter : public Operator<FileWriter> {
...
@@ -91,11 +93,10 @@ class FileWriter : public Operator<FileWriter> {
void
write
(
ByteRange
v
)
const
{
void
write
(
ByteRange
v
)
const
{
ssize_t
n
;
ssize_t
n
;
while
(
!
v
.
empty
())
{
while
(
!
v
.
empty
())
{
n
=
::
write
(
file_
.
fd
(),
v
.
data
(),
v
.
size
());
do
{
n
=
::
write
(
file_
.
fd
(),
v
.
data
(),
v
.
size
());
}
while
(
n
==
-
1
&&
errno
==
EINTR
);
if
(
n
==
-
1
)
{
if
(
n
==
-
1
)
{
if
(
errno
==
EINTR
)
{
continue
;
}
throw
std
::
system_error
(
errno
,
std
::
system_category
(),
throw
std
::
system_error
(
errno
,
std
::
system_category
(),
"write() failed"
);
"write() failed"
);
}
}
...
...
folly/experimental/test/FileTest.cpp
0 → 100644
View file @
c7796707
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "folly/experimental/File.h"
#include <glog/logging.h>
#include <gtest/gtest.h>
#include "folly/Benchmark.h"
#include "folly/String.h"
using
namespace
folly
;
namespace
{
void
expectWouldBlock
(
ssize_t
r
)
{
int
savedErrno
=
errno
;
EXPECT_EQ
(
-
1
,
r
);
EXPECT_EQ
(
EAGAIN
,
savedErrno
)
<<
errnoStr
(
errno
);
}
void
expectOK
(
ssize_t
r
)
{
int
savedErrno
=
errno
;
EXPECT_LE
(
0
,
r
)
<<
": errno="
<<
errnoStr
(
errno
);
}
}
// namespace
TEST
(
File
,
Simple
)
{
// Open a file, ensure it's indeed open for reading
char
buf
=
'x'
;
{
File
f
(
"/etc/hosts"
);
EXPECT_NE
(
-
1
,
f
.
fd
());
EXPECT_EQ
(
1
,
::
read
(
f
.
fd
(),
&
buf
,
1
));
f
.
close
();
EXPECT_EQ
(
-
1
,
f
.
fd
());
}
}
TEST
(
File
,
OwnsFd
)
{
// Wrap a file descriptor, make sure that ownsFd works
// We'll test that the file descriptor is closed by closing the writing
// end of a pipe and making sure that a non-blocking read from the reading
// end returns 0.
char
buf
=
'x'
;
int
p
[
2
];
expectOK
(
::
pipe
(
p
));
int
flags
=
::
fcntl
(
p
[
0
],
F_GETFL
);
expectOK
(
flags
);
expectOK
(
::
fcntl
(
p
[
0
],
F_SETFL
,
flags
|
O_NONBLOCK
));
expectWouldBlock
(
::
read
(
p
[
0
],
&
buf
,
1
));
{
File
f
(
p
[
1
]);
EXPECT_EQ
(
p
[
1
],
f
.
fd
());
}
// Ensure that moving the file doesn't close it
{
File
f
(
p
[
1
]);
EXPECT_EQ
(
p
[
1
],
f
.
fd
());
File
f1
(
std
::
move
(
f
));
EXPECT_EQ
(
-
1
,
f
.
fd
());
EXPECT_EQ
(
p
[
1
],
f1
.
fd
());
}
expectWouldBlock
(
::
read
(
p
[
0
],
&
buf
,
1
));
// not closed
{
File
f
(
p
[
1
],
true
);
EXPECT_EQ
(
p
[
1
],
f
.
fd
());
}
ssize_t
r
=
::
read
(
p
[
0
],
&
buf
,
1
);
// eof
expectOK
(
r
);
EXPECT_EQ
(
0
,
r
);
::
close
(
p
[
0
]);
}
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