|
25 | 25 | #include <stdlib.h> |
26 | 26 |
|
27 | 27 | #include "common/datapack.h" |
| 28 | +#include "common/server_connection.h" |
28 | 29 | #include "errors/saunafs_error_codes.h" |
29 | 30 | #include "errors/sfserr.h" |
30 | 31 | #include "tools/tools_commands.h" |
@@ -53,110 +54,75 @@ static void set_eattr_usage() { |
53 | 54 | } |
54 | 55 |
|
55 | 56 | static int set_eattr(const char *fname, uint8_t eattr, uint8_t mode) { |
56 | | - uint32_t cmd, leng, uid; |
57 | | - inode_t inode, changed, notchanged, notpermitted; |
58 | 57 | uint32_t msgid{0}; |
| 58 | + inode_t inode; |
59 | 59 |
|
60 | | - int fd; |
61 | | - fd = open_master_conn(fname, &inode, nullptr, true); |
62 | | - if (fd < 0) { |
63 | | - return -1; |
64 | | - } |
| 60 | + MessageBuffer request, response; |
65 | 61 |
|
66 | | - uid = getUId(); |
67 | | - |
68 | | - constexpr uint32_t kSetEAttrPayload = |
69 | | - sizeof(msgid) + sizeof(inode) + sizeof(uid) + sizeof(eattr) + sizeof(mode); |
70 | | - constexpr uint32_t kSetEAttrFullSize = |
71 | | - sizeof(cmd) + sizeof(kSetEAttrPayload) + kSetEAttrPayload; |
72 | | - uint8_t reqbuff[kSetEAttrFullSize], *wptr, *buff; |
73 | | - const uint8_t *rptr; |
74 | | - |
75 | | - wptr = reqbuff; |
76 | | - put32bit(&wptr, CLTOMA_FUSE_SETEATTR); |
77 | | - put32bit(&wptr, kSetEAttrPayload); |
78 | | - put32bit(&wptr, msgid); |
79 | | - putINode(&wptr, inode); |
80 | | - put32bit(&wptr, uid); |
81 | | - put8bit(&wptr, eattr); |
82 | | - put8bit(&wptr, mode); |
83 | | - |
84 | | - // send request to master |
85 | | - if (tcpwrite(fd, reqbuff, kSetEAttrFullSize) != kSetEAttrFullSize) { |
86 | | - printf("%s: master query: send error\n", fname); |
87 | | - close_master_conn(1); |
88 | | - return -1; |
89 | | - } |
| 62 | + int fd = open_master_conn(fname, &inode, nullptr, true); |
| 63 | + if (fd < 0) { return -1; } |
90 | 64 |
|
91 | | - // read the first part of the answer |
92 | | - if (tcpread(fd, reqbuff, sizeof(cmd) + sizeof(leng)) != sizeof(cmd) + sizeof(leng)) { |
93 | | - printf("%s: master query: receive error\n", fname); |
94 | | - close_master_conn(1); |
95 | | - return -1; |
96 | | - } |
| 65 | + try { |
| 66 | + uint32_t uid = getUId(); |
97 | 67 |
|
98 | | - rptr = reqbuff; |
99 | | - get32bit(&rptr, cmd); |
100 | | - get32bit(&rptr, leng); |
| 68 | + serializeLegacyPacket(request, CLTOMA_FUSE_SETEATTR, msgid, inode, uid, eattr, mode); |
101 | 69 |
|
102 | | - if (cmd != MATOCL_FUSE_SETEATTR) { |
103 | | - printf("%s: master query: wrong answer (type)\n", fname); |
104 | | - close_master_conn(1); |
105 | | - return -1; |
106 | | - } |
| 70 | + response = ServerConnection::sendAndReceive(fd, request, MATOCL_FUSE_SETEATTR); |
107 | 71 |
|
108 | | - buff = (uint8_t *)malloc(leng); |
| 72 | + close_master_conn(0); |
109 | 73 |
|
110 | | - if (tcpread(fd, buff, leng) != (int32_t)leng) { |
111 | | - printf("%s: master query: receive error\n", fname); |
112 | | - free(buff); |
113 | | - close_master_conn(1); |
114 | | - return -1; |
115 | | - } |
| 74 | + const uint8_t *rptr = response.data(); |
116 | 75 |
|
117 | | - close_master_conn(0); // not needed anymore |
| 76 | + get32bit(&rptr, msgid); |
| 77 | + if (msgid != 0) { |
| 78 | + printf("%s: master query: wrong answer (queryid)\n", fname); |
| 79 | + return -1; |
| 80 | + } |
118 | 81 |
|
119 | | - rptr = buff; |
120 | | - get32bit(&rptr, msgid); // queryid |
| 82 | + uint32_t bodySize = static_cast<uint32_t>(response.size() - sizeof(msgid)); |
121 | 83 |
|
122 | | - if (msgid != 0) { |
123 | | - printf("%s: master query: wrong answer (queryid)\n", fname); |
124 | | - free(buff); |
125 | | - return -1; |
126 | | - } |
| 84 | + if (bodySize == sizeof(uint8_t)) { |
| 85 | + uint8_t status = *rptr; |
| 86 | + printf("%s: %s\n", fname, saunafs_error_string(status)); |
| 87 | + return -1; |
| 88 | + } |
127 | 89 |
|
128 | | - if (leng - sizeof(msgid) == 1) { // an error code was returned |
129 | | - printf("%s: %s\n", fname, saunafs_error_string(*rptr)); |
130 | | - free(buff); |
131 | | - return -1; |
132 | | - } |
| 90 | + inode_t changed; |
| 91 | + inode_t notchanged; |
| 92 | + inode_t notpermitted; |
| 93 | + constexpr uint32_t kExpectedSize = |
| 94 | + sizeof(changed) + sizeof(notchanged) + sizeof(notpermitted); |
133 | 95 |
|
134 | | - if (leng != sizeof(msgid) + 3 * sizeof(inode_t)) { |
135 | | - printf("%s: master query: wrong answer (leng)\n", fname); |
136 | | - free(buff); |
137 | | - return -1; |
138 | | - } |
| 96 | + if (bodySize != kExpectedSize) { |
| 97 | + printf("%s: master query: wrong answer (leng)\n", fname); |
| 98 | + return -1; |
| 99 | + } |
139 | 100 |
|
140 | | - getINode(&rptr, changed); |
141 | | - getINode(&rptr, notchanged); |
142 | | - getINode(&rptr, notpermitted); |
| 101 | + getINode(&rptr, changed); |
| 102 | + getINode(&rptr, notchanged); |
| 103 | + getINode(&rptr, notpermitted); |
143 | 104 |
|
144 | | - if ((mode & SMODE_RMASK) == 0) { |
145 | | - if (changed) { |
146 | | - printf("%s: attribute(s) changed\n", fname); |
| 105 | + if ((mode & SMODE_RMASK) == 0) { |
| 106 | + if (changed) { |
| 107 | + printf("%s: attribute(s) changed\n", fname); |
| 108 | + } else { |
| 109 | + printf("%s: attribute(s) not changed\n", fname); |
| 110 | + } |
147 | 111 | } else { |
148 | | - printf("%s: attribute(s) not changed\n", fname); |
| 112 | + printf("%s:\n", fname); |
| 113 | + print_number(" inodes with attributes changed: ", "\n", changed, kMode32, 0, 1); |
| 114 | + print_number(" inodes with attributes not changed: ", "\n", notchanged, kMode32, 0, 1); |
| 115 | + print_number(" inodes with permission denied: ", "\n", notpermitted, kMode32, 0, |
| 116 | + 1); |
149 | 117 | } |
150 | | - } else { |
151 | | - printf("%s:\n", fname); |
152 | | - print_number(" inodes with attributes changed: ", "\n", changed, kMode32, 0, 1); |
153 | | - print_number(" inodes with attributes not changed: ", "\n", notchanged, kMode32, 0, 1); |
154 | | - print_number(" inodes with permission denied: ", "\n", notpermitted, kMode32, 0, 1); |
155 | | - } |
156 | 118 |
|
157 | | - free(buff); |
| 119 | + return 0; |
158 | 120 |
|
159 | | - return 0; |
| 121 | + } catch (const Exception &e) { |
| 122 | + fprintf(stderr, "%s\n", e.what()); |
| 123 | + close_master_conn(1); |
| 124 | + return -1; |
| 125 | + } |
160 | 126 | } |
161 | 127 |
|
162 | 128 | static int gene_eattr_run(int argc, char **argv, uint8_t mode, void (*usage_func)(void)) { |
|
0 commit comments