11#include " swift/extractor/infra/file/TargetFile.h"
2+ #include " swift/extractor/infra/file/FsLogger.h"
3+ #include " swift/extractor/infra/log/SwiftLogging.h"
4+ #include " swift/extractor/infra/log/SwiftAssert.h"
25
3- #include < iostream>
46#include < cassert>
57#include < cstdio>
68#include < cerrno>
1012namespace fs = std::filesystem;
1113
1214namespace codeql {
13- namespace {
14- [[noreturn]] void error (const char * action, const fs::path& arg, std::error_code ec) {
15- std::cerr << " Unable to " << action << " : " << arg << " (" << ec.message () << " )\n " ;
16- std::abort ();
17- }
1815
19- [[noreturn]] void error (const char * action, const fs::path& arg) {
20- error (action, arg, {errno, std::system_category ()});
21- }
16+ using namespace fs_logger ;
2217
23- void check (const char * action, const fs::path& arg, std::error_code ec) {
24- if (ec) {
25- error (action, arg, ec);
26- }
18+ namespace {
19+ std::error_code currentErrorCode () {
20+ return {errno, std::system_category ()};
2721}
2822
2923void ensureParentDir (const fs::path& path) {
3024 auto parent = path.parent_path ();
3125 std::error_code ec;
3226 fs::create_directories (parent, ec);
33- check ( " create directory" , parent, ec);
27+ CODEQL_ASSERT (!ec, " Unable to create directory {} ({}) " , parent, ec);
3428}
3529
3630fs::path initPath (const std::filesystem::path& target, const std::filesystem::path& dir) {
3731 fs::path ret{dir};
38- assert (!target.empty () && " target must be a non-empty path " );
32+ CODEQL_ASSERT (!target.empty ());
3933 ret /= target.relative_path ();
4034 ensureParentDir (ret);
4135 return ret;
@@ -53,13 +47,12 @@ bool TargetFile::init() {
5347 if (auto f = std::fopen (targetPath.c_str (), " wx" )) {
5448 std::fclose (f);
5549 out.open (workingPath);
56- checkOutput (" open file for writing " );
50+ checkOutput (" open" );
5751 return true ;
5852 }
59- if (errno != EEXIST) {
60- error (" open file for writing" , targetPath);
61- }
62- // else we just lost the race
53+ CODEQL_ASSERT (errno == EEXIST, " Unable to open {} for writing ({})" , targetPath,
54+ currentErrorCode ());
55+ // else the file already exists and we just lost the race
6356 return false ;
6457}
6558
@@ -76,13 +69,11 @@ void TargetFile::commit() {
7669 out.close ();
7770 std::error_code ec;
7871 fs::rename (workingPath, targetPath, ec);
79- check ( " rename file " , targetPath, ec);
72+ CODEQL_ASSERT (!ec, " Unable to rename {} -> {} ({}) " , workingPath , targetPath, ec);
8073 }
8174}
8275
8376void TargetFile::checkOutput (const char * action) {
84- if (!out) {
85- error (action, workingPath);
86- }
77+ CODEQL_ASSERT (out, " Unable to {} {} ({})" , action, workingPath, currentErrorCode ());
8778}
8879} // namespace codeql
0 commit comments