Skip to content

Commit e8b7dee

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 ed84902 commit e8b7dee

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

src/tools/master_functions.cc

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,44 +61,39 @@ 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];
73+
std::memcpy(blob, kBlobStr, sizeof(blob));
74+
75+
serializeLegacyPacket(request, CLTOMA_FUSE_REGISTER, blob, regTools, cuid, majorVer,
76+
minorVer, microVer);
77+
78+
response = ServerConnection::sendAndReceive(rfd, request, MATOCL_FUSE_REGISTER);
79+
80+
if (response.size() != sizeof(uint8_t)) {
81+
printf("register to master: wrong answer (length)\n");
82+
return -1;
83+
}
84+
85+
uint8_t status = response[0];
86+
87+
if (status != SAUNAFS_STATUS_OK) {
88+
printf("register to master: %s\n", saunafs_error_string(status));
89+
return -1;
90+
}
91+
92+
return 0;
93+
} catch (Exception &e) {
94+
fprintf(stderr, "register to master: %s\n", e.what());
9995
return -1;
10096
}
101-
return 0;
10297
}
10398

10499
static int master_connect(const master_info_t *info) {

0 commit comments

Comments
 (0)