2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/File.h>
21 #include <boost/thread/locks.hpp>
22 #include <glog/logging.h>
23 #include <gtest/gtest.h>
25 #include <folly/Benchmark.h>
26 #include <folly/String.h>
27 #include <folly/Subprocess.h>
28 #include <folly/experimental/io/FsUtil.h>
29 #include <folly/experimental/TestUtil.h>
31 using namespace folly;
32 using namespace folly::test;
35 void expectWouldBlock(ssize_t r) {
36 int savedErrno = errno;
38 EXPECT_EQ(EAGAIN, savedErrno) << errnoStr(errno);
40 void expectOK(ssize_t r) {
41 int savedErrno = errno;
42 EXPECT_LE(0, r) << ": errno=" << errnoStr(errno);
47 // Open a file, ensure it's indeed open for reading
51 EXPECT_NE(-1, f.fd());
52 EXPECT_EQ(1, ::read(f.fd(), &buf, 1));
54 EXPECT_EQ(-1, f.fd());
59 // Wrap a file descriptor, make sure that ownsFd works
60 // We'll test that the file descriptor is closed by closing the writing
61 // end of a pipe and making sure that a non-blocking read from the reading
67 int flags = ::fcntl(p[0], F_GETFL);
69 expectOK(::fcntl(p[0], F_SETFL, flags | O_NONBLOCK));
70 expectWouldBlock(::read(p[0], &buf, 1));
73 EXPECT_EQ(p[1], f.fd());
75 // Ensure that moving the file doesn't close it
78 EXPECT_EQ(p[1], f.fd());
79 File f1(std::move(f));
80 EXPECT_EQ(-1, f.fd());
81 EXPECT_EQ(p[1], f1.fd());
83 expectWouldBlock(::read(p[0], &buf, 1)); // not closed
86 EXPECT_EQ(p[1], f.fd());
88 ssize_t r = ::read(p[0], &buf, 1); // eof
95 File in(STDOUT_FILENO, false);
96 CHECK_EQ(STDOUT_FILENO, in.release());
97 CHECK_EQ(-1, in.release());
100 #define EXPECT_CONTAINS(haystack, needle) \
101 EXPECT_NE(::std::string::npos, ::folly::StringPiece(haystack).find(needle)) \
102 << "Haystack: '" << haystack << "'\nNeedle: '" << needle << "'";
104 TEST(File, UsefulError) {
106 File("does_not_exist.txt", 0, 0666);
107 } catch (const std::runtime_error& e) {
108 EXPECT_CONTAINS(e.what(), "does_not_exist.txt");
109 EXPECT_CONTAINS(e.what(), "0666");
114 File temp = File::temporary();
116 EXPECT_TRUE(bool(temp));
124 if (File file = File::temporary()) {
130 EXPECT_FALSE(bool(File()));
134 if (File notOpened = File()) {
140 typedef std::unique_lock<File> Lock;
141 typedef boost::shared_lock<File> SharedLock;
143 // Find out where we are.
144 static constexpr size_t pathLength = 2048;
145 char buf[pathLength + 1];
146 int r = readlink("/proc/self/exe", buf, pathLength);
150 // NOTE(agallagher): Our various internal build systems layout built
151 // binaries differently, so the two layouts below.
153 auto helper_basename = "file_test_lock_helper";
155 if (fs::exists(me.parent_path() / helper_basename)) {
156 helper = me.parent_path() / helper_basename;
157 } else if (fs::exists(
158 me.parent_path().parent_path() / helper_basename / helper_basename)) {
159 helper = me.parent_path().parent_path()
160 / helper_basename / helper_basename;
162 throw std::runtime_error(
163 folly::to<std::string>("cannot find helper ", helper_basename));
166 TemporaryFile tempFile;
167 File f(tempFile.fd());
169 enum LockMode { EXCLUSIVE, SHARED };
170 auto testLock = [&] (LockMode mode, bool expectedSuccess) {
172 Subprocess({helper.native(),
173 mode == SHARED ? "-s" : "-x",
174 tempFile.path().native()}).wait();
175 EXPECT_TRUE(ret.exited());
177 EXPECT_EQ(expectedSuccess ? 0 : 42, ret.exitStatus());
181 // Make sure nothing breaks and things compile.
191 Lock lock(f, std::defer_lock);
192 EXPECT_TRUE(lock.try_lock());
196 SharedLock lock(f, boost::defer_lock);
197 EXPECT_TRUE(lock.try_lock());
203 testLock(EXCLUSIVE, false);
209 testLock(SHARED, false);
215 testLock(EXCLUSIVE, false);
218 // S does not block S
221 testLock(SHARED, true);