Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Mar 29, 2024
1 parent 5873606 commit b93b85b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 43 deletions.
17 changes: 0 additions & 17 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,6 @@
# define unreachable() assert(0 && "unreachable")
#endif

// __builtin_assume() is supported only by clang, and [[assume]] is
// available only in C++23, so we use this macro when giving a hint to
// the compiler's optimizer what's true.
#define ASSUME(x) do { if (!(x)) __builtin_unreachable(); } while (0)

// This is an assert() that is enabled even in the release build.
#define ASSERT(x) \
do { \
if (!(x)) { \
std::cerr << "Assertion failed: (" << #x \
<< "), function " << __FUNCTION__ \
<< ", file " << __FILE__ \
<< ", line " << __LINE__ << ".\n"; \
std::abort(); \
} \
} while (0)

inline uint64_t hash_string(std::string_view str) {
return XXH3_64bits(str.data(), str.size());
}
Expand Down
3 changes: 1 addition & 2 deletions common/glob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,12 @@ bool Glob::do_match(std::string_view str, std::span<Element> elements) {
for (;;) {
size_t pos = str.find(elements[0].str);
if (pos == str.npos)
break;
return false;
if (do_match(str.substr(pos + elements[0].str.size()),
elements.subspan(1)))
return true;
str = str.substr(pos + 1);
}
return false;
}

// Other cases are handled here.
Expand Down
45 changes: 21 additions & 24 deletions common/tar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,6 @@ namespace mold {
//
// For simplicity, we always emit a PAX header even for a short filename.
struct UstarHeader {
UstarHeader() {
memset(this, 0, sizeof(*this));
}

void finalize() {
memset(checksum, ' ', sizeof(checksum));
memcpy(magic, "ustar", 5);
memcpy(version, "00", 2);

// Compute checksum
int sum = 0;
for (i64 i = 0; i < sizeof(*this); i++)
sum += ((u8 *)this)[i];

// We need to convince the compiler that sum isn't too big to silence
// -Werror=format-truncation.
ASSUME(sum < 01'000'000);
snprintf(checksum, sizeof(checksum), "%06o", sum);
}

char name[100];
char mode[8];
char uid[8];
Expand All @@ -52,6 +32,23 @@ struct UstarHeader {

static_assert(sizeof(UstarHeader) == 512);

static void finalize(UstarHeader &hdr) {
memset(hdr.checksum, ' ', sizeof(hdr.checksum));
memcpy(hdr.magic, "ustar", 5);
memcpy(hdr.version, "00", 2);

// Compute checksum
int sum = 0;
for (i64 i = 0; i < sizeof(hdr); i++)
sum += ((u8 *)&hdr)[i];

// We need to convince the compiler that sum isn't too big to silence
// -Werror=format-truncation.
if (sum >= 01'000'000)
unreachable();
snprintf(hdr.checksum, sizeof(hdr.checksum), "%06o", sum);
}

static std::string encode_path(std::string basedir, std::string path) {
path = path_clean(basedir + "/" + path);

Expand Down Expand Up @@ -79,24 +76,24 @@ TarWriter::~TarWriter() {
void TarWriter::append(std::string path, std::string_view data) {
// Write PAX header
static_assert(sizeof(UstarHeader) == BLOCK_SIZE);
UstarHeader pax;
UstarHeader pax = {};

std::string attr = encode_path(basedir, path);
snprintf(pax.size, sizeof(pax.size), "%011zo", attr.size());
pax.name[0] = '/';
pax.typeflag[0] = 'x';
pax.finalize();
finalize(pax);
fwrite(&pax, sizeof(pax), 1, out);

// Write pathname
fwrite(attr.data(), attr.size(), 1, out);
fseek(out, align_to(ftell(out), BLOCK_SIZE), SEEK_SET);

// Write Ustar header
UstarHeader ustar;
UstarHeader ustar = {};
memcpy(ustar.mode, "0000664", 8);
snprintf(ustar.size, sizeof(ustar.size), "%011zo", data.size());
ustar.finalize();
finalize(ustar);
fwrite(&ustar, sizeof(ustar), 1, out);

// Write file contents
Expand Down

0 comments on commit b93b85b

Please sign in to comment.