| 1 |
// Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors |
| 2 |
// Licensed under the MIT License: |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to deal |
| 6 |
// in the Software without restriction, including without limitation the rights |
| 7 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
// copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 |
// THE SOFTWARE. |
| 21 |
|
| 22 |
#ifndef _FILE_OFFSET_BITS |
| 23 |
#define _FILE_OFFSET_BITS 64 |
| 24 |
// Request 64-bit off_t and ino_t, otherwise this code will break when either value exceeds 2^32. |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "debug.h" |
| 28 |
#include "filesystem.h" |
| 29 |
#include "string.h" |
| 30 |
#include "test.h" |
| 31 |
#include "encoding.h" |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string> |
| 34 |
#if _WIN32 |
| 35 |
#include <windows.h> |
| 36 |
#include "windows-sanity.h" |
| 37 |
#else |
| 38 |
#include <unistd.h> |
| 39 |
#include <errno.h> |
| 40 |
#include <sys/types.h> |
| 41 |
#include <sys/stat.h> |
| 42 |
#include <fcntl.h> |
| 43 |
#include <dirent.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
namespace kj { |
| 47 |
namespace { |
| 48 |
|
| 49 |
bool isWine() KJ_UNUSED; |
| 50 |
|
| 51 |
#if _WIN32 |
| 52 |
|
| 53 |
bool detectWine() { |
| 54 |
HMODULE hntdll = GetModuleHandle("ntdll.dll"); |
| 55 |
if(hntdll == NULL) return false; |
| 56 |
return GetProcAddress(hntdll, "wine_get_version") != nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
bool isWine() { |
| 60 |
static bool result = detectWine(); |
| 61 |
return result; |
| 62 |
} |
| 63 |
|
| 64 |
template <typename Func> |
| 65 |
static auto newTemp(Func&& create) |
| 66 |
-> Decay<decltype(*kj::_::readMaybe(create(Array<wchar_t>())))> { |
| 67 |
wchar_t wtmpdir[MAX_PATH + 1]; |
| 68 |
DWORD len = GetTempPathW(kj::size(wtmpdir), wtmpdir); |
| 69 |
KJ_ASSERT(len < kj::size(wtmpdir)); |
| 70 |
auto tmpdir = decodeWideString(arrayPtr(wtmpdir, len)); |
| 71 |
|
| 72 |
static uint counter = 0; |
| 73 |
for (;;) { |
| 74 |
auto path = kj::str(tmpdir, "kj-filesystem-test.", GetCurrentProcessId(), ".", counter++); |
| 75 |
KJ_IF_MAYBE(result, create(encodeWideString(path, true))) { |
| 76 |
return kj::mv(*result); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
static Own<File> newTempFile() { |
| 82 |
return newTemp([](Array<wchar_t> candidatePath) -> Maybe<Own<File>> { |
| 83 |
HANDLE handle; |
| 84 |
KJ_WIN32_HANDLE_ERRORS(handle = CreateFileW( |
| 85 |
candidatePath.begin(), |
| 86 |
FILE_GENERIC_READ | FILE_GENERIC_WRITE, |
| 87 |
0, |
| 88 |
NULL, |
| 89 |
CREATE_NEW, |
| 90 |
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, |
| 91 |
NULL)) { |
| 92 |
case ERROR_ALREADY_EXISTS: |
| 93 |
case ERROR_FILE_EXISTS: |
| 94 |
return nullptr; |
| 95 |
default: |
| 96 |
KJ_FAIL_WIN32("CreateFileW", error); |
| 97 |
} |
| 98 |
return newDiskFile(AutoCloseHandle(handle)); |
| 99 |
}); |
| 100 |
} |
| 101 |
|
| 102 |
static Array<wchar_t> join16(ArrayPtr<const wchar_t> path, const wchar_t* file) { |
| 103 |
// Assumes `path` ends with a NUL terminator (and `file` is of course NUL terminated as well). |
| 104 |
|
| 105 |
size_t len = wcslen(file) + 1; |
| 106 |
auto result = kj::heapArray<wchar_t>(path.size() + len); |
| 107 |
memcpy(result.begin(), path.begin(), path.asBytes().size() - sizeof(wchar_t)); |
| 108 |
result[path.size() - 1] = '\\'; |
| 109 |
memcpy(result.begin() + path.size(), file, len * sizeof(wchar_t)); |
| 110 |
return result; |
| 111 |
} |
| 112 |
|
| 113 |
class TempDir { |
| 114 |
public: |
| 115 |
TempDir(): filename(newTemp([](Array<wchar_t> candidatePath) -> Maybe<Array<wchar_t>> { |
| 116 |
KJ_WIN32_HANDLE_ERRORS(CreateDirectoryW(candidatePath.begin(), NULL)) { |
| 117 |
case ERROR_ALREADY_EXISTS: |
| 118 |
case ERROR_FILE_EXISTS: |
| 119 |
return nullptr; |
| 120 |
default: |
| 121 |
KJ_FAIL_WIN32("CreateDirectoryW", error); |
| 122 |
} |
| 123 |
return kj::mv(candidatePath); |
| 124 |
})) {} |
| 125 |
|
| 126 |
Own<Directory> get() { |
| 127 |
HANDLE handle; |
| 128 |
KJ_WIN32(handle = CreateFileW( |
| 129 |
filename.begin(), |
| 130 |
GENERIC_READ, |
| 131 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 132 |
NULL, |
| 133 |
OPEN_EXISTING, |
| 134 |
FILE_FLAG_BACKUP_SEMANTICS, // apparently, this flag is required for directories |
| 135 |
NULL)); |
| 136 |
return newDiskDirectory(AutoCloseHandle(handle)); |
| 137 |
} |
| 138 |
|
| 139 |
~TempDir() noexcept(false) { |
| 140 |
recursiveDelete(filename); |
| 141 |
} |
| 142 |
|
| 143 |
private: |
| 144 |
Array<wchar_t> filename; |
| 145 |
|
| 146 |
static void recursiveDelete(ArrayPtr<const wchar_t> path) { |
| 147 |
// Recursively delete the temp dir, verifying that no .kj-tmp. files were left over. |
| 148 |
// |
| 149 |
// Mostly copied from rmrfChildren() in filesystem-win32.c++. |
| 150 |
|
| 151 |
auto glob = join16(path, L"\\*"); |
| 152 |
|
| 153 |
WIN32_FIND_DATAW data; |
| 154 |
HANDLE handle = FindFirstFileW(glob.begin(), &data); |
| 155 |
if (handle == INVALID_HANDLE_VALUE) { |
| 156 |
auto error = GetLastError(); |
| 157 |
if (error == ERROR_FILE_NOT_FOUND) return; |
| 158 |
KJ_FAIL_WIN32("FindFirstFile", error, path) { return; } |
| 159 |
} |
| 160 |
KJ_DEFER(KJ_WIN32(FindClose(handle)) { break; }); |
| 161 |
|
| 162 |
do { |
| 163 |
// Ignore "." and "..", ugh. |
| 164 |
if (data.cFileName[0] == L'.') { |
| 165 |
if (data.cFileName[1] == L'\0' || |
| 166 |
(data.cFileName[1] == L'.' && data.cFileName[2] == L'\0')) { |
| 167 |
continue; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
String utf8Name = decodeWideString(arrayPtr(data.cFileName, wcslen(data.cFileName))); |
| 172 |
KJ_EXPECT(!utf8Name.startsWith(".kj-tmp."), "temp file not cleaned up", utf8Name); |
| 173 |
|
| 174 |
auto child = join16(path, data.cFileName); |
| 175 |
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && |
| 176 |
!(data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) { |
| 177 |
recursiveDelete(child); |
| 178 |
} else { |
| 179 |
KJ_WIN32(DeleteFileW(child.begin())); |
| 180 |
} |
| 181 |
} while (FindNextFileW(handle, &data)); |
| 182 |
|
| 183 |
auto error = GetLastError(); |
| 184 |
if (error != ERROR_NO_MORE_FILES) { |
| 185 |
KJ_FAIL_WIN32("FindNextFile", error, path) { return; } |
| 186 |
} |
| 187 |
|
| 188 |
uint retryCount = 0; |
| 189 |
retry: |
| 190 |
KJ_WIN32_HANDLE_ERRORS(RemoveDirectoryW(path.begin())) { |
| 191 |
case ERROR_DIR_NOT_EMPTY: |
| 192 |
if (retryCount++ < 10) { |
| 193 |
Sleep(10); |
| 194 |
goto retry; |
| 195 |
} |
| 196 |
KJ_FALLTHROUGH; |
| 197 |
default: |
| 198 |
KJ_FAIL_WIN32("RemoveDirectory", error) { break; } |
| 199 |
} |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
#else |
| 204 |
|
| 205 |
bool isWine() { return false; } |
| 206 |
|
| •207 |
#if __APPLE__ || __CYGWIN__ || defined(__FreeBSD_kernel__) |
| 208 |
#define HOLES_NOT_SUPPORTED 1 |
| 209 |
#endif |
| 210 |
|
| 211 |
#if __ANDROID__ |
| 212 |
#define VAR_TMP "/data/local/tmp" |
| 213 |
#else |
| 214 |
#define VAR_TMP "/var/tmp" |
| 215 |
#endif |
| 216 |
|
| 217 |
static Own<File> newTempFile() { |
| 218 |
const char* tmpDir = getenv("TEST_TMPDIR"); |
| 219 |
auto filename = str(tmpDir != nullptr ? tmpDir : VAR_TMP, "/kj-filesystem-test.XXXXXX"); |
| 220 |
int fd; |
| 221 |
KJ_SYSCALL(fd = mkstemp(filename.begin())); |
| 222 |
KJ_DEFER(KJ_SYSCALL(unlink(filename.cStr()))); |
| 223 |
return newDiskFile(AutoCloseFd(fd)); |
| 224 |
} |
| 225 |
|
| 226 |
class TempDir { |
| 227 |
public: |
| 228 |
TempDir() { |
| 229 |
const char* tmpDir = getenv("TEST_TMPDIR"); |
| 230 |
filename = str(tmpDir != nullptr ? tmpDir : VAR_TMP, "/kj-filesystem-test.XXXXXX"); |
| 231 |
if (mkdtemp(filename.begin()) == nullptr) { |
| 232 |
KJ_FAIL_SYSCALL("mkdtemp", errno, filename); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
Own<Directory> get() { |
| 237 |
int fd; |
| 238 |
KJ_SYSCALL(fd = open(filename.cStr(), O_RDONLY)); |
| 239 |
return newDiskDirectory(AutoCloseFd(fd)); |
| 240 |
} |
| 241 |
|
| 242 |
~TempDir() noexcept(false) { |
| 243 |
recursiveDelete(filename); |
| 244 |
} |
| 245 |
|
| 246 |
private: |
| 247 |
String filename; |
| 248 |
|
| 249 |
static void recursiveDelete(StringPtr path) { |
| 250 |
// Recursively delete the temp dir, verifying that no .kj-tmp. files were left over. |
| 251 |
|
| 252 |
{ |
| 253 |
DIR* dir = opendir(path.cStr()); |
| 254 |
KJ_ASSERT(dir != nullptr); |
| 255 |
KJ_DEFER(closedir(dir)); |
| 256 |
|
| 257 |
for (;;) { |
| 258 |
auto entry = readdir(dir); |
| 259 |
if (entry == nullptr) break; |
| 260 |
|
| 261 |
StringPtr name = entry->d_name; |
| 262 |
if (name == "." || name == "..") continue; |
| 263 |
|
| 264 |
auto subPath = kj::str(path, '/', entry->d_name); |
| 265 |
|
| 266 |
KJ_EXPECT(!name.startsWith(".kj-tmp."), "temp file not cleaned up", subPath); |
| 267 |
|
| 268 |
struct stat stats; |
| 269 |
KJ_SYSCALL(lstat(subPath.cStr(), &stats)); |
| 270 |
|
| 271 |
if (S_ISDIR(stats.st_mode)) { |
| 272 |
recursiveDelete(subPath); |
| 273 |
} else { |
| 274 |
KJ_SYSCALL(unlink(subPath.cStr())); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
KJ_SYSCALL(rmdir(path.cStr())); |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
#endif // _WIN32, else |
| 284 |
|
| 285 |
KJ_TEST("DiskFile") { |
| 286 |
auto file = newTempFile(); |
| 287 |
|
| 288 |
KJ_EXPECT(file->readAllText() == ""); |
| 289 |
|
| 290 |
// mmaping empty file should work |
| 291 |
KJ_EXPECT(file->mmap(0, 0).size() == 0); |
| 292 |
KJ_EXPECT(file->mmapPrivate(0, 0).size() == 0); |
| 293 |
KJ_EXPECT(file->mmapWritable(0, 0)->get().size() == 0); |
| 294 |
|
| 295 |
file->writeAll("foo"); |
| 296 |
KJ_EXPECT(file->readAllText() == "foo"); |
| 297 |
|
| 298 |
file->write(3, StringPtr("bar").asBytes()); |
| 299 |
KJ_EXPECT(file->readAllText() == "foobar"); |
| 300 |
|
| 301 |
file->write(3, StringPtr("baz").asBytes()); |
| 302 |
KJ_EXPECT(file->readAllText() == "foobaz"); |
| 303 |
|
| 304 |
file->write(9, StringPtr("qux").asBytes()); |
| 305 |
KJ_EXPECT(file->readAllText() == kj::StringPtr("foobaz\0\0\0qux", 12)); |
| 306 |
|
| 307 |
file->truncate(6); |
| 308 |
KJ_EXPECT(file->readAllText() == "foobaz"); |
| 309 |
|
| 310 |
file->truncate(18); |
| 311 |
KJ_EXPECT(file->readAllText() == kj::StringPtr("foobaz\0\0\0\0\0\0\0\0\0\0\0\0", 18)); |
| 312 |
|
| 313 |
// empty mappings work, even if useless |
| 314 |
KJ_EXPECT(file->mmap(0, 0).size() == 0); |
| 315 |
KJ_EXPECT(file->mmapPrivate(0, 0).size() == 0); |
| 316 |
KJ_EXPECT(file->mmapWritable(0, 0)->get().size() == 0); |
| 317 |
KJ_EXPECT(file->mmap(2, 0).size() == 0); |
| 318 |
KJ_EXPECT(file->mmapPrivate(2, 0).size() == 0); |
| 319 |
KJ_EXPECT(file->mmapWritable(2, 0)->get().size() == 0); |
| 320 |
|
| 321 |
{ |
| 322 |
auto mapping = file->mmap(0, 18); |
| 323 |
auto privateMapping = file->mmapPrivate(0, 18); |
| 324 |
auto writableMapping = file->mmapWritable(0, 18); |
| 325 |
|
| 326 |
KJ_EXPECT(mapping.size() == 18); |
| 327 |
KJ_EXPECT(privateMapping.size() == 18); |
| 328 |
KJ_EXPECT(writableMapping->get().size() == 18); |
| 329 |
|
| 330 |
KJ_EXPECT(writableMapping->get().begin() != mapping.begin()); |
| 331 |
KJ_EXPECT(privateMapping.begin() != mapping.begin()); |
| 332 |
KJ_EXPECT(writableMapping->get().begin() != privateMapping.begin()); |
| 333 |
|
| 334 |
KJ_EXPECT(kj::str(mapping.slice(0, 6).asChars()) == "foobaz"); |
| 335 |
KJ_EXPECT(kj::str(writableMapping->get().slice(0, 6).asChars()) == "foobaz"); |
| 336 |
KJ_EXPECT(kj::str(privateMapping.slice(0, 6).asChars()) == "foobaz"); |
| 337 |
|
| 338 |
privateMapping[0] = 'F'; |
| 339 |
KJ_EXPECT(kj::str(mapping.slice(0, 6).asChars()) == "foobaz"); |
| 340 |
KJ_EXPECT(kj::str(writableMapping->get().slice(0, 6).asChars()) == "foobaz"); |
| 341 |
KJ_EXPECT(kj::str(privateMapping.slice(0, 6).asChars()) == "Foobaz"); |
| 342 |
|
| 343 |
writableMapping->get()[1] = 'D'; |
| 344 |
writableMapping->changed(writableMapping->get().slice(1, 2)); |
| 345 |
KJ_EXPECT(kj::str(mapping.slice(0, 6).asChars()) == "fDobaz"); |
| 346 |
KJ_EXPECT(kj::str(writableMapping->get().slice(0, 6).asChars()) == "fDobaz"); |
| 347 |
KJ_EXPECT(kj::str(privateMapping.slice(0, 6).asChars()) == "Foobaz"); |
| 348 |
|
| 349 |
file->write(0, StringPtr("qux").asBytes()); |
| 350 |
KJ_EXPECT(kj::str(mapping.slice(0, 6).asChars()) == "quxbaz"); |
| 351 |
KJ_EXPECT(kj::str(writableMapping->get().slice(0, 6).asChars()) == "quxbaz"); |
| 352 |
KJ_EXPECT(kj::str(privateMapping.slice(0, 6).asChars()) == "Foobaz"); |
| 353 |
|
| 354 |
file->write(12, StringPtr("corge").asBytes()); |
| 355 |
KJ_EXPECT(kj::str(mapping.slice(12, 17).asChars()) == "corge"); |
| 356 |
|
| 357 |
#if !_WIN32 && !__CYGWIN__ // Windows doesn't allow the file size to change while mapped. |
| 358 |
// Can shrink. |
| 359 |
file->truncate(6); |
| 360 |
KJ_EXPECT(kj::str(mapping.slice(12, 17).asChars()) == kj::StringPtr("\0\0\0\0\0", 5)); |
| 361 |
|
| 362 |
// Can regrow. |
| 363 |
file->truncate(18); |
| 364 |
KJ_EXPECT(kj::str(mapping.slice(12, 17).asChars()) == kj::StringPtr("\0\0\0\0\0", 5)); |
| 365 |
|
| 366 |
// Can even regrow past previous capacity. |
| 367 |
file->truncate(100); |
| 368 |
#endif |
| 369 |
} |
| 370 |
|
| 371 |
file->truncate(6); |
| 372 |
|
| 373 |
KJ_EXPECT(file->readAllText() == "quxbaz"); |
| 374 |
file->zero(3, 3); |
| 375 |
KJ_EXPECT(file->readAllText() == StringPtr("qux\0\0\0", 6)); |
| 376 |
} |
| 377 |
|
| 378 |
KJ_TEST("DiskFile::copy()") { |
| 379 |
auto source = newTempFile(); |
| 380 |
source->writeAll("foobarbaz"); |
| 381 |
|
| 382 |
auto dest = newTempFile(); |
| 383 |
dest->writeAll("quxcorge"); |
| 384 |
|
| 385 |
KJ_EXPECT(dest->copy(3, *source, 6, kj::maxValue) == 3); |
| 386 |
KJ_EXPECT(dest->readAllText() == "quxbazge"); |
| 387 |
|
| 388 |
KJ_EXPECT(dest->copy(0, *source, 3, 4) == 4); |
| 389 |
KJ_EXPECT(dest->readAllText() == "barbazge"); |
| 390 |
|
| 391 |
KJ_EXPECT(dest->copy(0, *source, 128, kj::maxValue) == 0); |
| 392 |
|
| 393 |
KJ_EXPECT(dest->copy(4, *source, 3, 0) == 0); |
| 394 |
|
| 395 |
String bigString = strArray(repeat("foobar", 10000), ""); |
| 396 |
source->truncate(bigString.size() + 1000); |
| 397 |
source->write(123, bigString.asBytes()); |
| 398 |
|
| 399 |
dest->copy(321, *source, 123, bigString.size()); |
| 400 |
KJ_EXPECT(dest->readAllText().slice(321) == bigString); |
| 401 |
} |
| 402 |
|
| 403 |
KJ_TEST("DiskDirectory") { |
| 404 |
TempDir tempDir; |
| 405 |
auto dir = tempDir.get(); |
| 406 |
|
| 407 |
KJ_EXPECT(dir->listNames() == nullptr); |
| 408 |
KJ_EXPECT(dir->listEntries() == nullptr); |
| 409 |
KJ_EXPECT(!dir->exists(Path("foo"))); |
| 410 |
KJ_EXPECT(dir->tryOpenFile(Path("foo")) == nullptr); |
| 411 |
KJ_EXPECT(dir->tryOpenFile(Path("foo"), WriteMode::MODIFY) == nullptr); |
| 412 |
|
| 413 |
{ |
| 414 |
auto file = dir->openFile(Path("foo"), WriteMode::CREATE); |
| 415 |
file->writeAll("foobar"); |
| 416 |
} |
| 417 |
|
| 418 |
KJ_EXPECT(dir->exists(Path("foo"))); |
| 419 |
|
| 420 |
{ |
| 421 |
auto stats = dir->lstat(Path("foo")); |
| 422 |
KJ_EXPECT(stats.type == FsNode::Type::FILE); |
| 423 |
KJ_EXPECT(stats.size == 6); |
| 424 |
} |
| 425 |
|
| 426 |
{ |
| 427 |
auto list = dir->listNames(); |
| 428 |
KJ_ASSERT(list.size() == 1); |
| 429 |
KJ_EXPECT(list[0] == "foo"); |
| 430 |
} |
| 431 |
|
| 432 |
{ |
| 433 |
auto list = dir->listEntries(); |
| 434 |
KJ_ASSERT(list.size() == 1); |
| 435 |
KJ_EXPECT(list[0].name == "foo"); |
| 436 |
KJ_EXPECT(list[0].type == FsNode::Type::FILE); |
| 437 |
} |
| 438 |
|
| 439 |
KJ_EXPECT(dir->openFile(Path("foo"))->readAllText() == "foobar"); |
| 440 |
|
| 441 |
KJ_EXPECT(dir->tryOpenFile(Path({"foo", "bar"}), WriteMode::MODIFY) == nullptr); |
| 442 |
KJ_EXPECT(dir->tryOpenFile(Path({"bar", "baz"}), WriteMode::MODIFY) == nullptr); |
| 443 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("parent is not a directory", |
| 444 |
dir->tryOpenFile(Path({"bar", "baz"}), WriteMode::CREATE)); |
| 445 |
|
| 446 |
{ |
| 447 |
auto file = dir->openFile(Path({"bar", "baz"}), WriteMode::CREATE | WriteMode::CREATE_PARENT); |
| 448 |
file->writeAll("bazqux"); |
| 449 |
} |
| 450 |
|
| 451 |
KJ_EXPECT(dir->openFile(Path({"bar", "baz"}))->readAllText() == "bazqux"); |
| 452 |
|
| 453 |
{ |
| 454 |
auto stats = dir->lstat(Path("bar")); |
| 455 |
KJ_EXPECT(stats.type == FsNode::Type::DIRECTORY); |
| 456 |
} |
| 457 |
|
| 458 |
{ |
| 459 |
auto list = dir->listNames(); |
| 460 |
KJ_ASSERT(list.size() == 2); |
| 461 |
KJ_EXPECT(list[0] == "bar"); |
| 462 |
KJ_EXPECT(list[1] == "foo"); |
| 463 |
} |
| 464 |
|
| 465 |
{ |
| 466 |
auto list = dir->listEntries(); |
| 467 |
KJ_ASSERT(list.size() == 2); |
| 468 |
KJ_EXPECT(list[0].name == "bar"); |
| 469 |
KJ_EXPECT(list[0].type == FsNode::Type::DIRECTORY); |
| 470 |
KJ_EXPECT(list[1].name == "foo"); |
| 471 |
KJ_EXPECT(list[1].type == FsNode::Type::FILE); |
| 472 |
} |
| 473 |
|
| 474 |
{ |
| 475 |
auto subdir = dir->openSubdir(Path("bar")); |
| 476 |
|
| 477 |
KJ_EXPECT(subdir->openFile(Path("baz"))->readAllText() == "bazqux"); |
| 478 |
} |
| 479 |
|
| 480 |
auto subdir = dir->openSubdir(Path("corge"), WriteMode::CREATE); |
| 481 |
|
| 482 |
subdir->openFile(Path("grault"), WriteMode::CREATE)->writeAll("garply"); |
| 483 |
|
| 484 |
KJ_EXPECT(dir->openFile(Path({"corge", "grault"}))->readAllText() == "garply"); |
| 485 |
|
| 486 |
dir->openFile(Path({"corge", "grault"}), WriteMode::CREATE | WriteMode::MODIFY) |
| 487 |
->write(0, StringPtr("rag").asBytes()); |
| 488 |
KJ_EXPECT(dir->openFile(Path({"corge", "grault"}))->readAllText() == "ragply"); |
| 489 |
|
| 490 |
KJ_EXPECT(dir->openSubdir(Path("corge"))->listNames().size() == 1); |
| 491 |
|
| 492 |
{ |
| 493 |
auto replacer = |
| 494 |
dir->replaceFile(Path({"corge", "grault"}), WriteMode::CREATE | WriteMode::MODIFY); |
| 495 |
replacer->get().writeAll("rag"); |
| 496 |
|
| 497 |
// temp file not in list |
| 498 |
KJ_EXPECT(dir->openSubdir(Path("corge"))->listNames().size() == 1); |
| 499 |
|
| 500 |
// Don't commit. |
| 501 |
} |
| 502 |
KJ_EXPECT(dir->openFile(Path({"corge", "grault"}))->readAllText() == "ragply"); |
| 503 |
|
| 504 |
{ |
| 505 |
auto replacer = |
| 506 |
dir->replaceFile(Path({"corge", "grault"}), WriteMode::CREATE | WriteMode::MODIFY); |
| 507 |
replacer->get().writeAll("rag"); |
| 508 |
|
| 509 |
// temp file not in list |
| 510 |
KJ_EXPECT(dir->openSubdir(Path("corge"))->listNames().size() == 1); |
| 511 |
|
| 512 |
replacer->commit(); |
| 513 |
KJ_EXPECT(dir->openFile(Path({"corge", "grault"}))->readAllText() == "rag"); |
| 514 |
} |
| 515 |
|
| 516 |
KJ_EXPECT(dir->openFile(Path({"corge", "grault"}))->readAllText() == "rag"); |
| 517 |
|
| 518 |
{ |
| 519 |
auto appender = dir->appendFile(Path({"corge", "grault"}), WriteMode::MODIFY); |
| 520 |
appender->write("waldo", 5); |
| 521 |
appender->write("fred", 4); |
| 522 |
} |
| 523 |
|
| 524 |
KJ_EXPECT(dir->openFile(Path({"corge", "grault"}))->readAllText() == "ragwaldofred"); |
| 525 |
|
| 526 |
KJ_EXPECT(dir->exists(Path("foo"))); |
| 527 |
dir->remove(Path("foo")); |
| 528 |
KJ_EXPECT(!dir->exists(Path("foo"))); |
| 529 |
KJ_EXPECT(!dir->tryRemove(Path("foo"))); |
| 530 |
|
| 531 |
KJ_EXPECT(dir->exists(Path({"bar", "baz"}))); |
| 532 |
dir->remove(Path({"bar", "baz"})); |
| 533 |
KJ_EXPECT(!dir->exists(Path({"bar", "baz"}))); |
| 534 |
KJ_EXPECT(dir->exists(Path("bar"))); |
| 535 |
KJ_EXPECT(!dir->tryRemove(Path({"bar", "baz"}))); |
| 536 |
|
| 537 |
#if _WIN32 |
| 538 |
// On Windows, we can't delete a directory while we still have it open. |
| 539 |
subdir = nullptr; |
| 540 |
#endif |
| 541 |
|
| 542 |
KJ_EXPECT(dir->exists(Path("corge"))); |
| 543 |
KJ_EXPECT(dir->exists(Path({"corge", "grault"}))); |
| 544 |
dir->remove(Path("corge")); |
| 545 |
KJ_EXPECT(!dir->exists(Path("corge"))); |
| 546 |
KJ_EXPECT(!dir->exists(Path({"corge", "grault"}))); |
| 547 |
KJ_EXPECT(!dir->tryRemove(Path("corge"))); |
| 548 |
} |
| 549 |
|
| 550 |
#if !_WIN32 // Creating symlinks on Win32 requires admin privileges prior to Windows 10. |
| 551 |
KJ_TEST("DiskDirectory symlinks") { |
| 552 |
TempDir tempDir; |
| 553 |
auto dir = tempDir.get(); |
| 554 |
|
| 555 |
dir->symlink(Path("foo"), "bar/qux/../baz", WriteMode::CREATE); |
| 556 |
|
| 557 |
KJ_EXPECT(!dir->trySymlink(Path("foo"), "bar/qux/../baz", WriteMode::CREATE)); |
| 558 |
|
| 559 |
{ |
| 560 |
auto stats = dir->lstat(Path("foo")); |
| 561 |
KJ_EXPECT(stats.type == FsNode::Type::SYMLINK); |
| 562 |
} |
| 563 |
|
| 564 |
KJ_EXPECT(dir->readlink(Path("foo")) == "bar/qux/../baz"); |
| 565 |
|
| 566 |
// Broken link into non-existing directory cannot be opened in any mode. |
| 567 |
KJ_EXPECT(dir->tryOpenFile(Path("foo")) == nullptr); |
| 568 |
KJ_EXPECT(dir->tryOpenFile(Path("foo"), WriteMode::CREATE) == nullptr); |
| 569 |
KJ_EXPECT(dir->tryOpenFile(Path("foo"), WriteMode::MODIFY) == nullptr); |
| 570 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("parent is not a directory", |
| 571 |
dir->tryOpenFile(Path("foo"), WriteMode::CREATE | WriteMode::MODIFY)); |
| 572 |
KJ_EXPECT_THROW_RECOVERABLE_MESSAGE("parent is not a directory", |
| 573 |
dir->tryOpenFile(Path("foo"), |
| 574 |
WriteMode::CREATE | WriteMode::MODIFY | WriteMode::CREATE_PARENT)); |
| 575 |
|
| 576 |
// Create the directory. |
| 577 |
auto subdir = dir->openSubdir(Path("bar"), WriteMode::CREATE); |
| 578 |
subdir->openSubdir(Path("qux"), WriteMode::CREATE); |
| 579 |
|
| 580 |
// Link still points to non-existing file so cannot be open in most modes. |
| 581 |
KJ_EXPECT(dir->tryOpenFile(Path("foo")) == nullptr); |
| 582 |
KJ_EXPECT(dir->tryOpenFile(Path("foo"), WriteMode::CREATE) == nullptr); |
| 583 |
KJ_EXPECT(dir->tryOpenFile(Path("foo"), WriteMode::MODIFY) == nullptr); |
| 584 |
|
| 585 |
// But... CREATE | MODIFY works. |
| 586 |
dir->openFile(Path("foo"), WriteMode::CREATE | WriteMode::MODIFY) |
| 587 |
->writeAll("foobar"); |
| 588 |
|
| 589 |
KJ_EXPECT(dir->openFile(Path({"bar", "baz"}))->readAllText() == "foobar"); |
| 590 |
KJ_EXPECT(dir->openFile(Path("foo"))->readAllText() == "foobar"); |
| 591 |
KJ_EXPECT(dir->openFile(Path("foo"), WriteMode::MODIFY)->readAllText() == "foobar"); |
| 592 |
|
| 593 |
// operations that modify the symlink |
| 594 |
dir->symlink(Path("foo"), "corge", WriteMode::MODIFY); |
| 595 |
KJ_EXPECT(dir->openFile(Path({"bar", "baz"}))->readAllText() == "foobar"); |
| 596 |
KJ_EXPECT(dir->readlink(Path("foo")) == "corge"); |
| 597 |
KJ_EXPECT(!dir->exists(Path("foo"))); |
| 598 |
KJ_EXPECT(dir->lstat(Path("foo")).type == FsNode::Type::SYMLINK); |
| 599 |
KJ_EXPECT(dir->tryOpenFile(Path("foo")) == nullptr); |
| 600 |
|
| 601 |
dir->remove(Path("foo")); |
| 602 |
KJ_EXPECT(!dir->exists(Path("foo"))); |
| 603 |
KJ_EXPECT(dir->tryOpenFile(Path("foo")) == nullptr); |
| 604 |
} |
| 605 |
#endif |
| 606 |
|
| 607 |
KJ_TEST("DiskDirectory link") { |
| 608 |
TempDir tempDirSrc; |
| 609 |
TempDir tempDirDst; |
| 610 |
|
| 611 |
auto src = tempDirSrc.get(); |
| 612 |
auto dst = tempDirDst.get(); |
| 613 |
|
| 614 |
src->openFile(Path("foo"), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 615 |
->writeAll("foobar"); |
| 616 |
|
| 617 |
dst->transfer(Path("link"), WriteMode::CREATE, *src, Path("foo"), TransferMode::LINK); |
| 618 |
|
| 619 |
KJ_EXPECT(dst->openFile(Path("link"))->readAllText() == "foobar"); |
| 620 |
|
| 621 |
// Writing the old location modifies the new. |
| 622 |
src->openFile(Path("foo"), WriteMode::MODIFY)->writeAll("bazqux"); |
| 623 |
KJ_EXPECT(dst->openFile(Path("link"))->readAllText() == "bazqux"); |
| 624 |
|
| 625 |
// Replacing the old location doesn't modify the new. |
| 626 |
{ |
| 627 |
auto replacer = src->replaceFile(Path("foo"), WriteMode::MODIFY); |
| 628 |
replacer->get().writeAll("corge"); |
| 629 |
replacer->commit(); |
| 630 |
} |
| 631 |
KJ_EXPECT(src->openFile(Path("foo"))->readAllText() == "corge"); |
| 632 |
KJ_EXPECT(dst->openFile(Path("link"))->readAllText() == "bazqux"); |
| 633 |
} |
| 634 |
|
| 635 |
KJ_TEST("DiskDirectory copy") { |
| 636 |
TempDir tempDirSrc; |
| 637 |
TempDir tempDirDst; |
| 638 |
|
| 639 |
auto src = tempDirSrc.get(); |
| 640 |
auto dst = tempDirDst.get(); |
| 641 |
|
| 642 |
src->openFile(Path({"foo", "bar"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 643 |
->writeAll("foobar"); |
| 644 |
src->openFile(Path({"foo", "baz", "qux"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 645 |
->writeAll("bazqux"); |
| 646 |
|
| 647 |
dst->transfer(Path("link"), WriteMode::CREATE, *src, Path("foo"), TransferMode::COPY); |
| 648 |
|
| 649 |
KJ_EXPECT(src->openFile(Path({"foo", "bar"}))->readAllText() == "foobar"); |
| 650 |
KJ_EXPECT(src->openFile(Path({"foo", "baz", "qux"}))->readAllText() == "bazqux"); |
| 651 |
KJ_EXPECT(dst->openFile(Path({"link", "bar"}))->readAllText() == "foobar"); |
| 652 |
KJ_EXPECT(dst->openFile(Path({"link", "baz", "qux"}))->readAllText() == "bazqux"); |
| 653 |
|
| 654 |
KJ_EXPECT(dst->exists(Path({"link", "bar"}))); |
| 655 |
src->remove(Path({"foo", "bar"})); |
| 656 |
KJ_EXPECT(dst->openFile(Path({"link", "bar"}))->readAllText() == "foobar"); |
| 657 |
} |
| 658 |
|
| 659 |
KJ_TEST("DiskDirectory copy-replace") { |
| 660 |
TempDir tempDirSrc; |
| 661 |
TempDir tempDirDst; |
| 662 |
|
| 663 |
auto src = tempDirSrc.get(); |
| 664 |
auto dst = tempDirDst.get(); |
| 665 |
|
| 666 |
src->openFile(Path({"foo", "bar"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 667 |
->writeAll("foobar"); |
| 668 |
src->openFile(Path({"foo", "baz", "qux"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 669 |
->writeAll("bazqux"); |
| 670 |
|
| 671 |
dst->openFile(Path({"link", "corge"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 672 |
->writeAll("abcd"); |
| 673 |
|
| 674 |
// CREATE fails. |
| 675 |
KJ_EXPECT(!dst->tryTransfer(Path("link"), WriteMode::CREATE, |
| 676 |
*src, Path("foo"), TransferMode::COPY)); |
| 677 |
|
| 678 |
// Verify nothing changed. |
| 679 |
KJ_EXPECT(dst->openFile(Path({"link", "corge"}))->readAllText() == "abcd"); |
| 680 |
KJ_EXPECT(!dst->exists(Path({"foo", "bar"}))); |
| 681 |
|
| 682 |
// Now try MODIFY. |
| 683 |
dst->transfer(Path("link"), WriteMode::MODIFY, *src, Path("foo"), TransferMode::COPY); |
| 684 |
|
| 685 |
KJ_EXPECT(src->openFile(Path({"foo", "bar"}))->readAllText() == "foobar"); |
| 686 |
KJ_EXPECT(src->openFile(Path({"foo", "baz", "qux"}))->readAllText() == "bazqux"); |
| 687 |
KJ_EXPECT(dst->openFile(Path({"link", "bar"}))->readAllText() == "foobar"); |
| 688 |
KJ_EXPECT(dst->openFile(Path({"link", "baz", "qux"}))->readAllText() == "bazqux"); |
| 689 |
KJ_EXPECT(!dst->exists(Path({"link", "corge"}))); |
| 690 |
|
| 691 |
KJ_EXPECT(dst->exists(Path({"link", "bar"}))); |
| 692 |
src->remove(Path({"foo", "bar"})); |
| 693 |
KJ_EXPECT(dst->openFile(Path({"link", "bar"}))->readAllText() == "foobar"); |
| 694 |
} |
| 695 |
|
| 696 |
KJ_TEST("DiskDirectory move") { |
| 697 |
TempDir tempDirSrc; |
| 698 |
TempDir tempDirDst; |
| 699 |
|
| 700 |
auto src = tempDirSrc.get(); |
| 701 |
auto dst = tempDirDst.get(); |
| 702 |
|
| 703 |
src->openFile(Path({"foo", "bar"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 704 |
->writeAll("foobar"); |
| 705 |
src->openFile(Path({"foo", "baz", "qux"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 706 |
->writeAll("bazqux"); |
| 707 |
|
| 708 |
dst->transfer(Path("link"), WriteMode::CREATE, *src, Path("foo"), TransferMode::MOVE); |
| 709 |
|
| 710 |
KJ_EXPECT(!src->exists(Path({"foo"}))); |
| 711 |
KJ_EXPECT(dst->openFile(Path({"link", "bar"}))->readAllText() == "foobar"); |
| 712 |
KJ_EXPECT(dst->openFile(Path({"link", "baz", "qux"}))->readAllText() == "bazqux"); |
| 713 |
} |
| 714 |
|
| 715 |
KJ_TEST("DiskDirectory move-replace") { |
| 716 |
TempDir tempDirSrc; |
| 717 |
TempDir tempDirDst; |
| 718 |
|
| 719 |
auto src = tempDirSrc.get(); |
| 720 |
auto dst = tempDirDst.get(); |
| 721 |
|
| 722 |
src->openFile(Path({"foo", "bar"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 723 |
->writeAll("foobar"); |
| 724 |
src->openFile(Path({"foo", "baz", "qux"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 725 |
->writeAll("bazqux"); |
| 726 |
|
| 727 |
dst->openFile(Path({"link", "corge"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 728 |
->writeAll("abcd"); |
| 729 |
|
| 730 |
// CREATE fails. |
| 731 |
KJ_EXPECT(!dst->tryTransfer(Path("link"), WriteMode::CREATE, |
| 732 |
*src, Path("foo"), TransferMode::MOVE)); |
| 733 |
|
| 734 |
// Verify nothing changed. |
| 735 |
KJ_EXPECT(dst->openFile(Path({"link", "corge"}))->readAllText() == "abcd"); |
| 736 |
KJ_EXPECT(!dst->exists(Path({"foo", "bar"}))); |
| 737 |
KJ_EXPECT(src->exists(Path({"foo"}))); |
| 738 |
|
| 739 |
// Now try MODIFY. |
| 740 |
dst->transfer(Path("link"), WriteMode::MODIFY, *src, Path("foo"), TransferMode::MOVE); |
| 741 |
|
| 742 |
KJ_EXPECT(!src->exists(Path({"foo"}))); |
| 743 |
KJ_EXPECT(dst->openFile(Path({"link", "bar"}))->readAllText() == "foobar"); |
| 744 |
KJ_EXPECT(dst->openFile(Path({"link", "baz", "qux"}))->readAllText() == "bazqux"); |
| 745 |
} |
| 746 |
|
| 747 |
KJ_TEST("DiskDirectory createTemporary") { |
| 748 |
TempDir tempDir; |
| 749 |
auto dir = tempDir.get(); |
| 750 |
auto file = dir->createTemporary(); |
| 751 |
file->writeAll("foobar"); |
| 752 |
KJ_EXPECT(file->readAllText() == "foobar"); |
| 753 |
KJ_EXPECT(dir->listNames() == nullptr); |
| 754 |
} |
| 755 |
|
| 756 |
#if !__CYGWIN__ // TODO(someday): Figure out why this doesn't work on Cygwin. |
| 757 |
KJ_TEST("DiskDirectory replaceSubdir()") { |
| 758 |
TempDir tempDir; |
| 759 |
auto dir = tempDir.get(); |
| 760 |
|
| 761 |
{ |
| 762 |
auto replacer = dir->replaceSubdir(Path("foo"), WriteMode::CREATE); |
| 763 |
replacer->get().openFile(Path("bar"), WriteMode::CREATE)->writeAll("original"); |
| 764 |
KJ_EXPECT(replacer->get().openFile(Path("bar"))->readAllText() == "original"); |
| 765 |
KJ_EXPECT(!dir->exists(Path({"foo", "bar"}))); |
| 766 |
|
| 767 |
replacer->commit(); |
| 768 |
KJ_EXPECT(replacer->get().openFile(Path("bar"))->readAllText() == "original"); |
| 769 |
KJ_EXPECT(dir->openFile(Path({"foo", "bar"}))->readAllText() == "original"); |
| 770 |
} |
| 771 |
|
| 772 |
{ |
| 773 |
// CREATE fails -- already exists. |
| 774 |
auto replacer = dir->replaceSubdir(Path("foo"), WriteMode::CREATE); |
| 775 |
replacer->get().openFile(Path("corge"), WriteMode::CREATE)->writeAll("bazqux"); |
| 776 |
KJ_EXPECT(dir->listNames().size() == 1 && dir->listNames()[0] == "foo"); |
| 777 |
KJ_EXPECT(!replacer->tryCommit()); |
| 778 |
} |
| 779 |
|
| 780 |
// Unchanged. |
| 781 |
KJ_EXPECT(dir->openFile(Path({"foo", "bar"}))->readAllText() == "original"); |
| 782 |
KJ_EXPECT(!dir->exists(Path({"foo", "corge"}))); |
| 783 |
|
| 784 |
{ |
| 785 |
// MODIFY succeeds. |
| 786 |
auto replacer = dir->replaceSubdir(Path("foo"), WriteMode::MODIFY); |
| 787 |
replacer->get().openFile(Path("corge"), WriteMode::CREATE)->writeAll("bazqux"); |
| 788 |
KJ_EXPECT(dir->listNames().size() == 1 && dir->listNames()[0] == "foo"); |
| 789 |
replacer->commit(); |
| 790 |
} |
| 791 |
|
| 792 |
// Replaced with new contents. |
| 793 |
KJ_EXPECT(!dir->exists(Path({"foo", "bar"}))); |
| 794 |
KJ_EXPECT(dir->openFile(Path({"foo", "corge"}))->readAllText() == "bazqux"); |
| 795 |
} |
| 796 |
#endif // !__CYGWIN__ |
| 797 |
|
| 798 |
KJ_TEST("DiskDirectory replace directory with file") { |
| 799 |
TempDir tempDir; |
| 800 |
auto dir = tempDir.get(); |
| 801 |
|
| 802 |
dir->openFile(Path({"foo", "bar"}), WriteMode::CREATE | WriteMode::CREATE_PARENT) |
| 803 |
->writeAll("foobar"); |
| 804 |
|
| 805 |
{ |
| 806 |
// CREATE fails -- already exists. |
| 807 |
auto replacer = dir->replaceFile(Path("foo"), WriteMode::CREATE); |
| 808 |
replacer->get().writeAll("bazqux"); |
| 809 |
KJ_EXPECT(!replacer->tryCommit()); |
| 810 |
} |
| 811 |
|
| 812 |
// Still a directory. |
| 813 |
KJ_EXPECT(dir->lstat(Path("foo")).type == FsNode::Type::DIRECTORY); |
| 814 |
|
| 815 |
{ |
| 816 |
// MODIFY succeeds. |
| 817 |
auto replacer = dir->replaceFile(Path("foo"), WriteMode::MODIFY); |
| 818 |
replacer->get().writeAll("bazqux"); |
| 819 |
replacer->commit(); |
| 820 |
} |
| 821 |
|
| 822 |
// Replaced with file. |
| 823 |
KJ_EXPECT(dir->openFile(Path("foo"))->readAllText() == "bazqux"); |
| 824 |
} |
| 825 |
|
| •826 |
#if !defined(__FreeBSD_kernel__) |
| 827 |
KJ_TEST("DiskDirectory replace file with directory") { |
| 828 |
TempDir tempDir; |
| 829 |
auto dir = tempDir.get(); |
| 830 |
|
| 831 |
dir->openFile(Path("foo"), WriteMode::CREATE) |
| 832 |
->writeAll("foobar"); |
| 833 |
|
| 834 |
{ |
| 835 |
// CREATE fails -- already exists. |
| 836 |
auto replacer = dir->replaceSubdir(Path("foo"), WriteMode::CREATE); |
| 837 |
replacer->get().openFile(Path("bar"), WriteMode::CREATE)->writeAll("bazqux"); |
| 838 |
KJ_EXPECT(dir->listNames().size() == 1 && dir->listNames()[0] == "foo"); |
| 839 |
KJ_EXPECT(!replacer->tryCommit()); |
| 840 |
} |
| 841 |
|
| 842 |
// Still a file. |
| 843 |
KJ_EXPECT(dir->openFile(Path("foo"))->readAllText() == "foobar"); |
| 844 |
|
| 845 |
{ |
| 846 |
// MODIFY succeeds. |
| 847 |
auto replacer = dir->replaceSubdir(Path("foo"), WriteMode::MODIFY); |
| 848 |
replacer->get().openFile(Path("bar"), WriteMode::CREATE)->writeAll("bazqux"); |
| 849 |
KJ_EXPECT(dir->listNames().size() == 1 && dir->listNames()[0] == "foo"); |
| 850 |
replacer->commit(); |
| 851 |
} |
| 852 |
|
| 853 |
// Replaced with directory. |
| 854 |
KJ_EXPECT(dir->openFile(Path({"foo", "bar"}))->readAllText() == "bazqux"); |
| 855 |
} |
| •856 |
#endif |
| 857 |
|
| 858 |
#if !defined(HOLES_NOT_SUPPORTED) && (CAPNP_DEBUG_TYPES || CAPNP_EXPENSIVE_TESTS) |
| 859 |
// Not all filesystems support sparse files, and if they do, they don't necessarily support |
| 860 |
// copying them in a way that preserves holes. We don't want the capnp test suite to fail just |
| 861 |
// because it was run on the wrong filesystem. We could design the test to check first if the |
| 862 |
// filesystem supports holes, but the code to do that would be almost the same as the code being |
| 863 |
// tested... Instead, we've marked this test so it only runs when building this library using |
| 864 |
// defines that only the Cap'n Proto maintainers use. So, we run the test ourselves but we don't |
| 865 |
// make other people run it. |
| 866 |
|
| 867 |
KJ_TEST("DiskFile holes") { |
| 868 |
if (isWine()) { |
| 869 |
// WINE doesn't support sparse files. |
| 870 |
return; |
| 871 |
} |
| 872 |
|
| 873 |
TempDir tempDir; |
| 874 |
auto dir = tempDir.get(); |
| 875 |
|
| 876 |
auto file = dir->openFile(Path("holes"), WriteMode::CREATE); |
| 877 |
|
| 878 |
#if _WIN32 |
| 879 |
FILE_SET_SPARSE_BUFFER sparseInfo; |
| 880 |
memset(&sparseInfo, 0, sizeof(sparseInfo)); |
| 881 |
sparseInfo.SetSparse = TRUE; |
| 882 |
DWORD dummy; |
| 883 |
KJ_WIN32(DeviceIoControl( |
| 884 |
KJ_ASSERT_NONNULL(file->getWin32Handle()), |
| 885 |
FSCTL_SET_SPARSE, &sparseInfo, sizeof(sparseInfo), |
| 886 |
NULL, 0, &dummy, NULL)); |
| 887 |
#endif |
| 888 |
|
| 889 |
file->writeAll("foobar"); |
| 890 |
file->write(1 << 20, StringPtr("foobar").asBytes()); |
| 891 |
|
| 892 |
// Some filesystems, like BTRFS, report zero `spaceUsed` until synced. |
| 893 |
file->datasync(); |
| 894 |
|
| 895 |
// Allow for block sizes as low as 512 bytes and as high as 64k. Since we wrote two locations, |
| 896 |
// two blocks should be used. |
| 897 |
auto meta = file->stat(); |
| 898 |
#if __FreeBSD__ |
| 899 |
// On FreeBSD with ZFS it seems to report 512 bytes used even if I write more than 512 random |
| 900 |
// (i.e. non-compressible) bytes. I couldn't figure it out so I'm giving up for now. Maybe it's |
| 901 |
// a bug in the system? |
| 902 |
KJ_EXPECT(meta.spaceUsed >= 512, meta.spaceUsed); |
| 903 |
#else |
| 904 |
KJ_EXPECT(meta.spaceUsed >= 2 * 512, meta.spaceUsed); |
| 905 |
#endif |
| 906 |
KJ_EXPECT(meta.spaceUsed <= 2 * 65536); |
| 907 |
|
| 908 |
byte buf[7]; |
| 909 |
|
| 910 |
#if !_WIN32 // Win32 CopyFile() does NOT preserve sparseness. |
| 911 |
{ |
| 912 |
// Copy doesn't fill in holes. |
| 913 |
dir->transfer(Path("copy"), WriteMode::CREATE, Path("holes"), TransferMode::COPY); |
| 914 |
auto copy = dir->openFile(Path("copy")); |
| 915 |
#ifndef __FreeBSD__ |
| 916 |
// The spaceUsed numbers on FreeBSD don't make any sense, but nobody has the time or interest |
| 917 |
// to figure out why. Oh well. |
| 918 |
KJ_EXPECT(copy->stat().spaceUsed == meta.spaceUsed); |
| 919 |
#endif |
| 920 |
KJ_EXPECT(copy->read(0, buf) == 7); |
| 921 |
KJ_EXPECT(StringPtr(reinterpret_cast<char*>(buf), 6) == "foobar"); |
| 922 |
|
| 923 |
KJ_EXPECT(copy->read(1 << 20, buf) == 6); |
| 924 |
KJ_EXPECT(StringPtr(reinterpret_cast<char*>(buf), 6) == "foobar"); |
| 925 |
|
| 926 |
KJ_EXPECT(copy->read(1 << 19, buf) == 7); |
| 927 |
KJ_EXPECT(StringPtr(reinterpret_cast<char*>(buf), 6) == StringPtr("\0\0\0\0\0\0", 6)); |
| 928 |
} |
| 929 |
#endif |
| 930 |
|
| 931 |
file->truncate(1 << 21); |
| 932 |
file->datasync(); |
| 933 |
#ifndef __FreeBSD__ |
| 934 |
// The spaceUsed numbers on FreeBSD don't make any sense, but nobody has the time or interest |
| 935 |
// to figure out why. Oh well. |
| 936 |
KJ_EXPECT(file->stat().spaceUsed == meta.spaceUsed); |
| 937 |
#endif |
| 938 |
KJ_EXPECT(file->read(1 << 20, buf) == 7); |
| 939 |
KJ_EXPECT(StringPtr(reinterpret_cast<char*>(buf), 6) == "foobar"); |
| 940 |
|
| 941 |
#if !_WIN32 // Win32 CopyFile() does NOT preserve sparseness. |
| 942 |
{ |
| 943 |
dir->transfer(Path("copy"), WriteMode::MODIFY, Path("holes"), TransferMode::COPY); |
| 944 |
auto copy = dir->openFile(Path("copy")); |
| 945 |
#ifndef __FreeBSD__ |
| 946 |
// The spaceUsed numbers on FreeBSD don't make any sense, but nobody has the time or interest |
| 947 |
// to figure out why. Oh well. |
| 948 |
KJ_EXPECT(copy->stat().spaceUsed == meta.spaceUsed); |
| 949 |
#endif |
| 950 |
KJ_EXPECT(copy->read(0, buf) == 7); |
| 951 |
KJ_EXPECT(StringPtr(reinterpret_cast<char*>(buf), 6) == "foobar"); |
| 952 |
|
| 953 |
KJ_EXPECT(copy->read(1 << 20, buf) == 7); |
| 954 |
KJ_EXPECT(StringPtr(reinterpret_cast<char*>(buf), 6) == "foobar"); |
| 955 |
|
| 956 |
KJ_EXPECT(copy->read(1 << 19, buf) == 7); |
| 957 |
KJ_EXPECT(StringPtr(reinterpret_cast<char*>(buf), 6) == StringPtr("\0\0\0\0\0\0", 6)); |
| 958 |
} |
| 959 |
#endif |
| 960 |
|
| 961 |
// Try punching a hole with zero(). |
| 962 |
#if _WIN32 |
| 963 |
uint64_t blockSize = 4096; // TODO(someday): Actually ask the OS. |
| 964 |
#else |
| 965 |
struct stat stats; |
| 966 |
KJ_SYSCALL(fstat(KJ_ASSERT_NONNULL(file->getFd()), &stats)); |
| 967 |
uint64_t blockSize = stats.st_blksize; |
| 968 |
#endif |
| 969 |
file->zero(1 << 20, blockSize); |
| 970 |
file->datasync(); |
| 971 |
#if !_WIN32 && !__FreeBSD__ |
| 972 |
// TODO(someday): This doesn't work on Windows. I don't know why. We're definitely using the |
| 973 |
// proper ioctl. Oh well. It also doesn't work on FreeBSD-ZFS, due to the bug(?) mentioned |
| 974 |
// earlier -- the size is just always reported as 512. |
| 975 |
KJ_EXPECT(file->stat().spaceUsed < meta.spaceUsed); |
| 976 |
#endif |
| 977 |
KJ_EXPECT(file->read(1 << 20, buf) == 7); |
| 978 |
KJ_EXPECT(StringPtr(reinterpret_cast<char*>(buf), 6) == StringPtr("\0\0\0\0\0\0", 6)); |
| 979 |
} |
| 980 |
#endif |
| 981 |
|
| 982 |
#if !_WIN32 // Only applies to Unix. |
| 983 |
// Ensure the current path is correctly computed. |
| 984 |
// |
| 985 |
// See issue #1425. |
| 986 |
KJ_TEST("DiskFilesystem::computeCurrentPath") { |
| 987 |
TempDir tempDir; |
| 988 |
auto dir = tempDir.get(); |
| 989 |
|
| 990 |
// Paths can be PATH_MAX, but the segments which make up that path typically |
| 991 |
// can't exceed 255 bytes. |
| 992 |
auto maxPathSegment = std::string(255, 'a'); |
| 993 |
|
| 994 |
// Create a path which exceeds the 256 byte buffer used in |
| 995 |
// computeCurrentPath. |
| 996 |
auto subdir = dir->openSubdir(Path({ |
| 997 |
maxPathSegment, |
| 998 |
maxPathSegment, |
| 999 |
"some_path_longer_than_256_bytes" |
| 1000 |
}), WriteMode::CREATE | WriteMode::CREATE_PARENT); |
| 1001 |
|
| 1002 |
auto origDir = open(".", O_RDONLY); |
| 1003 |
KJ_SYSCALL(fchdir(KJ_ASSERT_NONNULL(subdir->getFd()))); |
| 1004 |
KJ_DEFER(KJ_SYSCALL(fchdir(origDir))); |
| 1005 |
|
| 1006 |
// Test computeCurrentPath indirectly. |
| 1007 |
newDiskFilesystem(); |
| 1008 |
} |
| 1009 |
#endif |
| 1010 |
|
| 1011 |
} // namespace |
| 1012 |
} // namespace kj |