Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libexpr/eval-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ struct AttrDb
{
auto state(_state->lock());

Path cacheDir = getCacheDir() + "/eval-cache-v5";
auto cacheDir = std::filesystem::path(getCacheDir()) / "eval-cache-v5";
createDirs(cacheDir);

Path dbPath = cacheDir + "/" + fingerprint.to_string(HashFormat::Base16, false) + ".sqlite";
auto dbPath = cacheDir / (fingerprint.to_string(HashFormat::Base16, false) + ".sqlite");

state->db = SQLite(dbPath);
state->db.isCache();
Expand Down
3 changes: 2 additions & 1 deletion src/libstore/include/nix/store/sqlite.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
///@file

#include <filesystem>
#include <functional>
#include <string>

Expand Down Expand Up @@ -41,7 +42,7 @@ struct SQLite

SQLite() {}

SQLite(const Path & path, SQLiteOpenMode mode = SQLiteOpenMode::Normal);
SQLite(const std::filesystem::path & path, SQLiteOpenMode mode = SQLiteOpenMode::Normal);
SQLite(const SQLite & from) = delete;
SQLite & operator=(const SQLite & from) = delete;

Expand Down
3 changes: 1 addition & 2 deletions src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,11 @@ void LocalStore::openDB(State & state, bool create)
throw SysError("Nix database directory '%1%' is not writable", dbDir);

/* Open the Nix database. */
std::string dbPath = dbDir + "/db.sqlite";
auto & db(state.db);
auto openMode = config->readOnly ? SQLiteOpenMode::Immutable
: create ? SQLiteOpenMode::Normal
: SQLiteOpenMode::NoCreate;
state.db = SQLite(dbPath, openMode);
state.db = SQLite(std::filesystem::path(dbDir) / "db.sqlite", openMode);

#ifdef __CYGWIN__
/* The cygwin version of sqlite3 has a patch which calls
Expand Down
28 changes: 26 additions & 2 deletions src/libstore/sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "nix/util/url.hh"
#include "nix/util/signals.hh"

#ifdef __linux__
# include <sys/vfs.h>
#endif

#include <sqlite3.h>

#include <atomic>
Expand Down Expand Up @@ -58,8 +62,28 @@ static void traceSQL(void * x, const char * sql)
notice("SQL<[%1%]>", sql);
};

SQLite::SQLite(const Path & path, SQLiteOpenMode mode)
SQLite::SQLite(const std::filesystem::path & path, SQLiteOpenMode mode)
{
#ifdef __linux__
// Work around a ZFS issue where SQLite's truncate() call on
// db.sqlite-shm can randomly take up to a few seconds. See
// https://github.com/openzfs/zfs/issues/14290#issuecomment-3074672917.
try {
auto shmFile = path;
shmFile += "-shm";
AutoCloseFD fd = open(shmFile.string().c_str(), O_RDWR | O_CLOEXEC);
if (fd) {
struct statfs fs;
if (fstatfs(fd.get(), &fs))
throw SysError("statfs() on '%s'", shmFile);
if (fs.f_type == /* ZFS_SUPER_MAGIC */ 801189825 && fdatasync(fd.get()) != 0)
throw SysError("fsync() on '%s'", shmFile);
Comment thread
edolstra marked this conversation as resolved.
Outdated
}
} catch (...) {
throw;
}
#endif

// useSQLiteWAL also indicates what virtual file system we need. Using
// `unix-dotfile` is needed on NFS file systems and on Windows' Subsystem
// for Linux (WSL) where useSQLiteWAL should be false by default.
Expand All @@ -68,7 +92,7 @@ SQLite::SQLite(const Path & path, SQLiteOpenMode mode)
int flags = immutable ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
if (mode == SQLiteOpenMode::Normal)
flags |= SQLITE_OPEN_CREATE;
auto uri = "file:" + percentEncode(path) + "?immutable=" + (immutable ? "1" : "0");
auto uri = "file:" + percentEncode(path.string()) + "?immutable=" + (immutable ? "1" : "0");
int ret = sqlite3_open_v2(uri.c_str(), &db, SQLITE_OPEN_URI | flags, vfs);
if (ret != SQLITE_OK) {
const char * err = sqlite3_errstr(ret);
Expand Down
Loading