Skip to content

Commit ec792f1

Browse files
committed
refactor(tools): Modernize register to master
The previous register to master implementation in the saunafs command used an outdated, error-prone manual approach for building, sending, and receiving packets to and from the master. This made the code difficult to read and unnecessarily verbose, especially since a cleaner implementation for handling legacy packets already existed in the codebase. This commit replaces the old approach with the existing, cleaner solution. Signed-off-by: Rolando Sánchez Ramos <rolysr@leil.io>
1 parent 177b23b commit ec792f1

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

src/tools/master_functions.cc

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,44 +61,40 @@ static int kMaxMasterRetries = 5;
6161
#endif
6262

6363
static int master_register(int rfd, uint32_t cuid) {
64-
uint32_t i;
65-
const uint8_t *rptr;
66-
uint8_t *wptr, regbuff[8 + 73];
67-
68-
wptr = regbuff;
69-
put32bit(&wptr, CLTOMA_FUSE_REGISTER);
70-
put32bit(&wptr, 73);
71-
memcpy(wptr, FUSE_REGISTER_BLOB_ACL, 64);
72-
wptr += 64;
73-
put8bit(&wptr, REGISTER_TOOLS);
74-
put32bit(&wptr, cuid);
75-
put16bit(&wptr, SAUNAFS_PACKAGE_VERSION_MAJOR);
76-
put8bit(&wptr, SAUNAFS_PACKAGE_VERSION_MINOR);
77-
put8bit(&wptr, SAUNAFS_PACKAGE_VERSION_MICRO);
78-
if (tcpwrite(rfd, regbuff, 8 + 73) != 8 + 73) {
79-
printf("register to master: send error\n");
80-
return -1;
81-
}
82-
if (tcpread(rfd, regbuff, 9) != 9) {
83-
printf("register to master: receive error\n");
84-
return -1;
85-
}
86-
rptr = regbuff;
87-
get32bit(&rptr, i);
88-
if (i != MATOCL_FUSE_REGISTER) {
89-
printf("register to master: wrong answer (type)\n");
90-
return -1;
91-
}
92-
get32bit(&rptr, i);
93-
if (i != 1) {
94-
printf("register to master: wrong answer (length)\n");
95-
return -1;
96-
}
97-
if (*rptr) {
98-
printf("register to master: %s\n", saunafs_error_string(*rptr));
64+
MessageBuffer request, response;
65+
66+
try {
67+
uint8_t regTools = static_cast<uint8_t>(REGISTER_TOOLS);
68+
uint16_t majorVer = static_cast<uint16_t>(SAUNAFS_PACKAGE_VERSION_MAJOR);
69+
uint8_t minorVer = static_cast<uint8_t>(SAUNAFS_PACKAGE_VERSION_MINOR);
70+
uint8_t microVer = static_cast<uint8_t>(SAUNAFS_PACKAGE_VERSION_MICRO);
71+
constexpr char kBlobStr[] = FUSE_REGISTER_BLOB_ACL;
72+
uint8_t blob[REGISTER_BLOB_SIZE] = {0};
73+
std::memcpy(blob, kBlobStr,
74+
(sizeof(kBlobStr) < sizeof(blob)) ? sizeof(kBlobStr) : sizeof(blob));
75+
76+
serializeLegacyPacket(request, CLTOMA_FUSE_REGISTER, blob, regTools, cuid, majorVer,
77+
minorVer, microVer);
78+
79+
response = ServerConnection::sendAndReceive(rfd, request, MATOCL_FUSE_REGISTER);
80+
81+
if (response.size() != sizeof(uint8_t)) {
82+
printf("register to master: wrong answer (length)\n");
83+
return -1;
84+
}
85+
86+
uint8_t status = response[0];
87+
88+
if (status != SAUNAFS_STATUS_OK) {
89+
printf("register to master: %s\n", saunafs_error_string(status));
90+
return -1;
91+
}
92+
93+
return 0;
94+
} catch (const Exception &e) {
95+
fprintf(stderr, "register to master: %s\n", e.what());
9996
return -1;
10097
}
101-
return 0;
10298
}
10399

104100
static int master_connect(const master_info_t *info) {

0 commit comments

Comments
 (0)