1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
//===- unittests/Basic/FileSystemTest.cpp ---------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "../BuildSystem/TempDir.h"
#include "llbuild/Basic/FileSystem.h"
#include "llbuild/Basic/LLVM.h"
#include "llbuild/Basic/PlatformUtility.h"
#include "llbuild/Basic/Stat.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
using namespace llbuild;
using namespace llbuild::basic;
namespace {
TEST(FileSystemTest, basic) {
// Check basic sanity of the local filesystem object.
auto fs = createLocalFileSystem();
// Write a temp file.
SmallString<256> tempPath;
llvm::sys::fs::createTemporaryFile("FileSystemTests", "txt", tempPath);
{
std::error_code ec;
llvm::raw_fd_ostream os(tempPath.str(), ec, llvm::sys::fs::F_Text);
EXPECT_FALSE(ec);
os << "Hello, world!";
os.close();
}
auto missingFileInfo = fs->getFileInfo("/does/not/exists");
EXPECT_TRUE(missingFileInfo.isMissing());
auto ourFileInfo = fs->getFileInfo(tempPath.str());
EXPECT_FALSE(ourFileInfo.isMissing());
auto missingFileContents = fs->getFileContents("/does/not/exist");
EXPECT_EQ(missingFileContents.get(), nullptr);
auto ourFileContents = fs->getFileContents(tempPath.str());
EXPECT_EQ(ourFileContents->getBuffer().str(), "Hello, world!");
// Remote the temporary file.
auto ec = llvm::sys::fs::remove(tempPath.str());
EXPECT_FALSE(ec);
}
TEST(FileSystemTest, testRecursiveRemoval) {
TmpDir rootTempDir(__func__);
SmallString<256> tempDir { rootTempDir.str() };
llvm::sys::path::append(tempDir, "root");
sys::mkdir(tempDir.c_str());
SmallString<256> file{ tempDir.str() };
llvm::sys::path::append(file, "test.txt");
{
std::error_code ec;
llvm::raw_fd_ostream os(file.str(), ec, llvm::sys::fs::F_Text);
EXPECT_FALSE(ec);
os << "Hello, world!";
os.close();
}
SmallString<256> dir{ tempDir.str() };
llvm::sys::path::append(dir, "subdir");
sys::mkdir(dir.c_str());
llvm::sys::path::append(dir, "file_in_subdir.txt");
{
std::error_code ec;
llvm::raw_fd_ostream os(dir.str(), ec, llvm::sys::fs::F_Text);
EXPECT_FALSE(ec);
os << "Hello, world!";
os.close();
}
auto fs = createLocalFileSystem();
bool result = fs->remove(tempDir.c_str());
EXPECT_TRUE(result);
sys::StatStruct statbuf;
EXPECT_EQ(-1, sys::stat(tempDir.c_str(), &statbuf));
EXPECT_EQ(ENOENT, errno);
}
TEST(FileSystemTest, testRecursiveRemovalDoesNotFollowSymlinks) {
TmpDir rootTempDir(__func__);
SmallString<256> file{ rootTempDir.str() };
llvm::sys::path::append(file, "test.txt");
{
std::error_code ec;
llvm::raw_fd_ostream os(file.str(), ec, llvm::sys::fs::F_Text);
EXPECT_FALSE(ec);
os << "Hello, world!";
os.close();
}
SmallString<256> otherDir{ rootTempDir.str() };
llvm::sys::path::append(otherDir, "other_dir");
sys::mkdir(otherDir.c_str());
SmallString<256> otherFile{ otherDir.str() };
llvm::sys::path::append(otherFile, "test.txt");
{
std::error_code ec;
llvm::raw_fd_ostream os(otherFile.str(), ec, llvm::sys::fs::F_Text);
EXPECT_FALSE(ec);
os << "Hello, world!";
os.close();
}
SmallString<256> tempDir { rootTempDir.str() };
llvm::sys::path::append(tempDir, "root");
sys::mkdir(tempDir.c_str());
SmallString<256> linkPath { tempDir.str() };
llvm::sys::path::append(linkPath, "link.txt");
int res = sys::symlink(file.c_str(), linkPath.c_str());
EXPECT_EQ(res, 0);
SmallString<256> directoryLinkPath { tempDir.str() };
llvm::sys::path::append(directoryLinkPath, "link_to_other_dir");
res = sys::symlink(otherDir.c_str(), directoryLinkPath.c_str());
EXPECT_EQ(res, 0);
auto fs = createLocalFileSystem();
bool result = fs->remove(tempDir.c_str());
EXPECT_TRUE(result);
sys::StatStruct statbuf;
EXPECT_EQ(-1, sys::stat(tempDir.c_str(), &statbuf));
EXPECT_EQ(ENOENT, errno);
// Verify that the symlink target still exists.
EXPECT_EQ(0, sys::stat(file.c_str(), &statbuf));
// Verify that we did not delete the symlinked directories contents.
EXPECT_EQ(0, sys::stat(otherFile.c_str(), &statbuf));
}
TEST(DeviceAgnosticFileSystemTest, basic) {
// Check basic sanity of the local filesystem object.
auto fs = DeviceAgnosticFileSystem::from(createLocalFileSystem());
// Write a temp file.
SmallString<256> tempPath;
llvm::sys::fs::createTemporaryFile("FileSystemTests", "txt", tempPath);
{
std::error_code ec;
llvm::raw_fd_ostream os(tempPath.str(), ec, llvm::sys::fs::F_Text);
EXPECT_FALSE(ec);
os << "Hello, world!";
os.close();
}
auto missingFileInfo = fs->getFileInfo("/does/not/exists");
EXPECT_TRUE(missingFileInfo.isMissing());
auto ourFileInfo = fs->getFileInfo(tempPath.str());
EXPECT_FALSE(ourFileInfo.isMissing());
EXPECT_EQ(ourFileInfo.device, 0ull);
EXPECT_EQ(ourFileInfo.inode, 0ull);
auto missingFileContents = fs->getFileContents("/does/not/exist");
EXPECT_EQ(missingFileContents.get(), nullptr);
auto ourFileContents = fs->getFileContents(tempPath.str());
EXPECT_EQ(ourFileContents->getBuffer().str(), "Hello, world!");
// Remove the temporary file.
auto ec = llvm::sys::fs::remove(tempPath.str());
EXPECT_FALSE(ec);
}
TEST(ChecksumOnlyFileSystem, basic) {
// Check basic sanity of the local filesystem object.
auto fs = ChecksumOnlyFileSystem::from(createLocalFileSystem());
// Write a temp file.
SmallString<256> tempPath;
llvm::sys::fs::createTemporaryFile("FileSystemTests", "txt", tempPath);
{
std::error_code ec;
llvm::raw_fd_ostream os(tempPath.str(), ec, llvm::sys::fs::F_Text);
EXPECT_FALSE(ec);
os << "Hello, world!";
os.close();
}
auto missingFileInfo = fs->getFileInfo("/does/not/exists");
EXPECT_TRUE(missingFileInfo.isMissing());
auto ourFileInfo = fs->getFileInfo(tempPath.str());
EXPECT_FALSE(ourFileInfo.isMissing());
EXPECT_EQ(ourFileInfo.device, 0ull);
EXPECT_EQ(ourFileInfo.inode, 0ull);
auto missingFileContents = fs->getFileContents("/does/not/exist");
EXPECT_EQ(missingFileContents.get(), nullptr);
auto ourFileContents = fs->getFileContents(tempPath.str());
EXPECT_EQ(ourFileContents->getBuffer().str(), "Hello, world!");
// Remove the temporary file.
auto ec = llvm::sys::fs::remove(tempPath.str());
EXPECT_FALSE(ec);
}
}
|