Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions src/build/adapter_filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ auto BuildAdapterFilesystem::write_dependencies(
this->refresh(path);
const auto dependencies_path{this->dependencies_path(path)};
std::filesystem::create_directories(dependencies_path.parent_path());
// To reset the inode and correctly handle hard links
std::filesystem::remove(dependencies_path);
std::ofstream dependencies_stream{dependencies_path};
assert(!dependencies_stream.fail());
for (const auto &dependency : dependencies) {
Expand Down
2 changes: 0 additions & 2 deletions src/index/generators.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,6 @@ struct GENERATE_URITEMPLATE_ROUTES {
const sourcemeta::one::BuildDynamicCallback<std::filesystem::path> &,
const Context &router) -> void {
std::filesystem::create_directories(destination.parent_path());
// To reset the inode and correctly handle hard links
std::filesystem::remove(destination);
sourcemeta::core::URITemplateRouterView::save(router, destination);
}
};
Expand Down
66 changes: 8 additions & 58 deletions src/index/index.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <sourcemeta/blaze/linter.h>

#include <sourcemeta/core/io.h>
#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonschema.h>
#include <sourcemeta/core/options.h>
Expand All @@ -24,7 +23,6 @@
#include <cstdlib> // EXIT_FAILURE, EXIT_SUCCESS
#include <exception> // std::exception
#include <filesystem> // std::filesystem
#include <fstream> // std::ofstream
#include <iomanip> // std::setw, std::setfill
#include <iostream> // std::cerr, std::cout
#include <string> // std::string
Expand Down Expand Up @@ -52,9 +50,6 @@
// the resolver will not unescape it back when computing the relative path to an
// entry
constexpr auto SENTINEL{"%"};
constexpr std::string_view STAGING_DIRECTORY{".sourcemeta-one-staging"};
constexpr std::string_view STAGING_COMMITTED{
".sourcemeta-one-staging-committed"};

static auto attribute_not_disabled(
const sourcemeta::one::Configuration::Collection &collection,
Expand Down Expand Up @@ -116,17 +111,17 @@ static auto index_main(const std::string_view &program,
// (1) Parse the output directory
/////////////////////////////////////////////////////////////////////////////

const auto final_output_path{
const auto output_path{
std::filesystem::weakly_canonical(app.positional().at(1))};

if (std::filesystem::exists(final_output_path) &&
!std::filesystem::is_directory(final_output_path)) {
if (std::filesystem::exists(output_path) &&
!std::filesystem::is_directory(output_path)) {
throw std::filesystem::filesystem_error{
"file already exists", final_output_path,
"file already exists", output_path,
std::make_error_code(std::errc::file_exists)};
}

std::cerr << "Writing output to: " << final_output_path.string() << "\n";
std::cerr << "Writing output to: " << output_path.string() << "\n";

/////////////////////////////////////////////////////////////////////////////
// (2) Process the configuration file
Expand Down Expand Up @@ -183,42 +178,10 @@ static auto index_main(const std::string_view &program,
}

/////////////////////////////////////////////////////////////////////////////
// (5) Prepare the output directory via staging for atomic commits
/////////////////////////////////////////////////////////////////////////////

// Place the staging directory as a sibling of the final output path to
// guarantee both reside on the same filesystem volume, which is required
// for the atomic rename to succeed
const auto staging_path{final_output_path.parent_path() /
std::string{STAGING_DIRECTORY}};
// After an atomic swap, the staging directory already contains the previous
// committed output, so we can reuse it directly. However, if the process
// crashed mid-build, staging may be in a dirty state. We use a sentinel
// file to distinguish clean staging (from a completed swap) from dirty
// staging (from a crash). If dirty, we discard and re-bootstrap.
const auto staging_sentinel{final_output_path.parent_path() /
std::string{STAGING_COMMITTED}};
if (std::filesystem::exists(staging_path) &&
!std::filesystem::exists(staging_sentinel)) {
std::cerr << "Discarding dirty staging directory\n";
std::filesystem::remove_all(staging_path);
}

if (!std::filesystem::exists(staging_path)) {
if (std::filesystem::exists(final_output_path)) {
std::cerr << "Hardlinking: " << final_output_path.string() << " => "
<< staging_path.string() << "\n";
sourcemeta::core::hardlink_directory(final_output_path, staging_path);
} else {
std::filesystem::create_directories(staging_path);
}
}

// Remove the sentinel before building so that a crash leaves staging
// recognizable as dirty
std::filesystem::remove(staging_sentinel);
// (5) Prepare the output directory
/////////////////////////////////////////////////////////////////////////////

sourcemeta::one::Output output{staging_path};
sourcemeta::one::Output output{output_path};

/////////////////////////////////////////////////////////////////////////////
// (6) Store a mark of the One version for target dependencies
Expand Down Expand Up @@ -790,19 +753,6 @@ static auto index_main(const std::string_view &program,

PROFILE_END(profiling, "Profile");

std::cerr << "Committing: " << staging_path.string() << " => "
<< final_output_path.string() << "\n";
sourcemeta::core::atomic_directory_swap(final_output_path, staging_path);

// Mark the staging directory as clean so the next run knows it can be
// safely reused. On the very first run the swap is a plain rename and
// staging no longer exists, but writing the sentinel is still harmless
// because the next run will see no staging directory and bootstrap fresh.
std::ofstream sentinel_stream(staging_sentinel);
sentinel_stream.close();

PROFILE_END(profiling, "Commit");

if (app.contains("time")) {
for (const auto &entry : profiling.first) {
std::cout << entry.second.count() << "ms " << entry.first << "\n";
Expand Down
3 changes: 1 addition & 2 deletions src/index/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <cassert> // assert
#include <filesystem> // std::filesystem
#include <fstream> // std::ofstream
#include <shared_mutex> // std::shared_mutex, std::shared_lock
#include <unordered_map> // std::unordered_map

Expand Down Expand Up @@ -85,8 +86,6 @@ class Output {
-> const std::filesystem::path & {
assert(path.is_absolute());
std::filesystem::create_directories(path.parent_path());
// To reset the inode and correctly handle hard links
std::filesystem::remove(path);
std::ofstream stream{path};
assert(!stream.fail());
sourcemeta::core::stringify(document, stream);
Expand Down
2 changes: 0 additions & 2 deletions src/shared/metapack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ auto write_stream(const std::filesystem::path &path,
metadata.assign("extension", extension);
}

// To reset the inode and correctly handle hard links
std::filesystem::remove(path);
std::ofstream output{path};
assert(!output.fail());
sourcemeta::core::stringify(metadata, output);
Expand Down
4 changes: 0 additions & 4 deletions test/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ if(ONE_INDEX)
sourcemeta_one_test_cli(common index non-existent-collection-directory)
sourcemeta_one_test_cli(common index collection-path-is-file)
sourcemeta_one_test_cli(common index deps-contents)
sourcemeta_one_test_cli(common index inode-reset-on-rebuild)
sourcemeta_one_test_cli(common index atomic-failure)
sourcemeta_one_test_cli(common index stale-staging-cleanup)
sourcemeta_one_test_cli(common index configuration-no-stale-cleanup)
sourcemeta_one_test_cli(common index rebuild-zero-to-one)
sourcemeta_one_test_cli(common index rebuild-one-to-zero)
sourcemeta_one_test_cli(common index resolve-schema)
Expand Down
62 changes: 0 additions & 62 deletions test/cli/index/common/atomic-failure.sh

This file was deleted.

3 changes: 0 additions & 3 deletions test/cli/index/common/comment-removed-on-rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Using configuration: $(realpath "$TMP")/one.json
( 0%) Producing: explorer
(100%) Producing: .
(100%) Rendering: .
Committing: $(realpath "$TMP")/.sourcemeta-one-staging => $(realpath "$TMP")/output
EOF

diff "$TMP/output.txt" "$TMP/expected.txt"
Expand All @@ -51,7 +50,6 @@ remove_threads_information "$TMP/output.txt"
cat << EOF > "$TMP/expected.txt"
Writing output to: $(realpath "$TMP")/output
Using configuration: $(realpath "$TMP")/one.json
Hardlinking: $(realpath "$TMP")/output => $(realpath "$TMP")/.sourcemeta-one-staging
( 50%) Reviewing: schemas
(100%) Reviewing: schemas
( 0%) Producing: explorer
Expand All @@ -61,7 +59,6 @@ Hardlinking: $(realpath "$TMP")/output => $(realpath "$TMP")/.sourcemeta-one-sta
(skip) Rendering: . [index]
(skip) Rendering: . [not-found]
(skip) Producing: routes.bin [routes]
Committing: $(realpath "$TMP")/.sourcemeta-one-staging => $(realpath "$TMP")/output
EOF

diff "$TMP/output.txt" "$TMP/expected.txt"
Expand Down
3 changes: 0 additions & 3 deletions test/cli/index/common/comment-updated-on-rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Using configuration: $(realpath "$TMP")/one.json
( 0%) Producing: explorer
(100%) Producing: .
(100%) Rendering: .
Committing: $(realpath "$TMP")/.sourcemeta-one-staging => $(realpath "$TMP")/output
EOF

diff "$TMP/output.txt" "$TMP/expected.txt"
Expand All @@ -51,7 +50,6 @@ remove_threads_information "$TMP/output.txt"
cat << EOF > "$TMP/expected.txt"
Writing output to: $(realpath "$TMP")/output
Using configuration: $(realpath "$TMP")/one.json
Hardlinking: $(realpath "$TMP")/output => $(realpath "$TMP")/.sourcemeta-one-staging
( 50%) Reviewing: schemas
(100%) Reviewing: schemas
( 0%) Producing: explorer
Expand All @@ -61,7 +59,6 @@ Hardlinking: $(realpath "$TMP")/output => $(realpath "$TMP")/.sourcemeta-one-sta
(skip) Rendering: . [index]
(skip) Rendering: . [not-found]
(skip) Producing: routes.bin [routes]
Committing: $(realpath "$TMP")/.sourcemeta-one-staging => $(realpath "$TMP")/output
EOF

diff "$TMP/output.txt" "$TMP/expected.txt"
Expand Down
51 changes: 0 additions & 51 deletions test/cli/index/common/configuration-no-stale-cleanup.sh

This file was deleted.

Loading